From 51eedf9aaa88f8e786dca2298afeea6bd12d718c Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Mon, 13 Oct 2025 11:01:12 -0300 Subject: [PATCH] [clang] NFC: NNS getLocalSourceRange user cleanup This adds a note to the documentation of getLocalSourceRange, and cleans up one of the users of this function which was changed in https://github.com/llvm/llvm-project/pull/147835 This also removes `TypeLoc::getNonPrefixBeginLoc`, which was recently introduced in the above patch, as that became unused. --- clang-tools-extra/clangd/FindTarget.cpp | 13 +++----- .../clang/AST/NestedNameSpecifierBase.h | 3 ++ clang/include/clang/AST/TypeLoc.h | 5 --- clang/lib/AST/TypeLoc.cpp | 33 ------------------- 4 files changed, 7 insertions(+), 47 deletions(-) diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp index 8aae41420b83e..799c64b8dab4d 100644 --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -1040,16 +1040,11 @@ class ExplicitReferenceCollector if (auto *S = N.get()) return refInStmt(S, Resolver); if (auto *NNSL = N.get()) { + if (auto TL = NNSL->getAsTypeLoc()) + return refInTypeLoc(NNSL->getAsTypeLoc(), Resolver); // (!) 'DeclRelation::Alias' ensures we do not lose namespace aliases. - NestedNameSpecifierLoc Qualifier; - SourceLocation NameLoc; - if (auto TL = NNSL->getAsTypeLoc()) { - Qualifier = TL.getPrefix(); - NameLoc = TL.getNonPrefixBeginLoc(); - } else { - Qualifier = NNSL->getAsNamespaceAndPrefix().Prefix; - NameLoc = NNSL->getLocalBeginLoc(); - } + NestedNameSpecifierLoc Qualifier = NNSL->getAsNamespaceAndPrefix().Prefix; + SourceLocation NameLoc = NNSL->getLocalBeginLoc(); return { ReferenceLoc{Qualifier, NameLoc, false, explicitReferenceTargets( diff --git a/clang/include/clang/AST/NestedNameSpecifierBase.h b/clang/include/clang/AST/NestedNameSpecifierBase.h index 73c60ba695419..8f4bbe97f6e9a 100644 --- a/clang/include/clang/AST/NestedNameSpecifierBase.h +++ b/clang/include/clang/AST/NestedNameSpecifierBase.h @@ -361,6 +361,9 @@ class NestedNameSpecifierLoc { /// Retrieve the source range covering just the last part of /// this nested-name-specifier, not including the prefix. /// + /// Note that this is the source range of this NestedNameSpecifier chunk, + /// and for a type this includes the prefix of that type. + /// /// For example, if this instance refers to a nested-name-specifier /// \c \::std::vector::, the returned source range would cover /// from "vector" to the last '::'. diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h index 38e8fba569396..3f14ee86d55b1 100644 --- a/clang/include/clang/AST/TypeLoc.h +++ b/clang/include/clang/AST/TypeLoc.h @@ -198,11 +198,6 @@ class TypeLoc { /// "foo::bar". Returns null if this type represents an unqualified-id. NestedNameSpecifierLoc getPrefix() const; - /// This returns the position of the type after any elaboration, such as the - /// 'struct' keyword, and name qualifiers. This will the 'template' keyword if - /// present, or the name location otherwise. - SourceLocation getNonPrefixBeginLoc() const; - /// This returns the position of the type after any elaboration, such as the /// 'struct' keyword. This may be the position of the name qualifiers, /// 'template' keyword, or the name location otherwise. diff --git a/clang/lib/AST/TypeLoc.cpp b/clang/lib/AST/TypeLoc.cpp index 55476e2175a1f..e952e82031976 100644 --- a/clang/lib/AST/TypeLoc.cpp +++ b/clang/lib/AST/TypeLoc.cpp @@ -494,39 +494,6 @@ NestedNameSpecifierLoc TypeLoc::getPrefix() const { } } -SourceLocation TypeLoc::getNonPrefixBeginLoc() const { - switch (getTypeLocClass()) { - case TypeLoc::TemplateSpecialization: { - auto TL = castAs(); - SourceLocation Loc = TL.getTemplateKeywordLoc(); - if (!Loc.isValid()) - Loc = TL.getTemplateNameLoc(); - return Loc; - } - case TypeLoc::DeducedTemplateSpecialization: { - auto TL = castAs(); - SourceLocation Loc = TL.getTemplateKeywordLoc(); - if (!Loc.isValid()) - Loc = TL.getTemplateNameLoc(); - return Loc; - } - case TypeLoc::DependentName: - return castAs().getNameLoc(); - case TypeLoc::Enum: - case TypeLoc::Record: - case TypeLoc::InjectedClassName: - return castAs().getNameLoc(); - case TypeLoc::Typedef: - return castAs().getNameLoc(); - case TypeLoc::UnresolvedUsing: - return castAs().getNameLoc(); - case TypeLoc::Using: - return castAs().getNameLoc(); - default: - return getBeginLoc(); - } -} - SourceLocation TypeLoc::getNonElaboratedBeginLoc() const { // For elaborated types (e.g. `struct a::A`) we want the portion after the // `struct` but including the namespace qualifier, `a::`.