[clang-tools-extra] b63c35e - [clangd] Simplify code using findName. NFC

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 10 01:23:01 PST 2019


Author: Ilya Biryukov
Date: 2019-12-10T10:22:43+01:00
New Revision: b63c35ebf76ca0ac89405aeadee2b98a0e91e05e

URL: https://github.com/llvm/llvm-project/commit/b63c35ebf76ca0ac89405aeadee2b98a0e91e05e
DIFF: https://github.com/llvm/llvm-project/commit/b63c35ebf76ca0ac89405aeadee2b98a0e91e05e.diff

LOG: [clangd] Simplify code using findName. NFC

`findName` was always used in conjuction with `spellingLocIfSpelled`.
This patch replaces patterns of the form:
  spellingLocIfSpelled(findName(&ND), SM)

With a new helper function:
  nameLocation(ND, SM)

And removes `spellingLocIfSpelled` and `findName`. Both are never used
anywhere else and the latter is an equivalent of `Decl::getLocation` if
we ever need it again.

Added: 
    

Modified: 
    clang-tools-extra/clangd/AST.cpp
    clang-tools-extra/clangd/AST.h
    clang-tools-extra/clangd/FindSymbols.cpp
    clang-tools-extra/clangd/SourceCode.cpp
    clang-tools-extra/clangd/SourceCode.h
    clang-tools-extra/clangd/XRefs.cpp
    clang-tools-extra/clangd/index/SymbolCollector.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp
index e4d808921c01..65fdecc9e1ae 100644
--- a/clang-tools-extra/clangd/AST.cpp
+++ b/clang-tools-extra/clangd/AST.cpp
@@ -156,7 +156,12 @@ bool isImplementationDetail(const Decl *D) {
                             D->getASTContext().getSourceManager());
 }
 
-SourceLocation findName(const clang::Decl *D) { return D->getLocation(); }
+SourceLocation nameLocation(const clang::Decl &D, const SourceManager &SM) {
+  auto L = D.getLocation();
+  if (isSpelledInSource(L, SM))
+    return SM.getSpellingLoc(L);
+  return SM.getExpansionLoc(L);
+}
 
 std::string printQualifiedName(const NamedDecl &ND) {
   std::string QName;

diff  --git a/clang-tools-extra/clangd/AST.h b/clang-tools-extra/clangd/AST.h
index bc38f19c6553..a823365e7053 100644
--- a/clang-tools-extra/clangd/AST.h
+++ b/clang-tools-extra/clangd/AST.h
@@ -34,11 +34,11 @@ namespace clangd {
 /// in code is considered implementation detail.
 bool isImplementationDetail(const Decl *D);
 
-/// Find the identifier source location of the given D.
-///
-/// The returned location is usually the spelling location where the name of the
-/// decl occurs in the code.
-SourceLocation findName(const clang::Decl *D);
+/// Find the source location of the identifier for \p D.
+/// Transforms macro locations to locations spelled inside files. All code
+/// that needs locations of declaration names (e.g. the index) should go through
+/// this function.
+SourceLocation nameLocation(const clang::Decl &D, const SourceManager &SM);
 
 /// Returns the qualified name of ND. The scope doesn't contain unwritten scopes
 /// like inline namespaces.

diff  --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp
index 619f1a5cfdbc..4c92c8896b9d 100644
--- a/clang-tools-extra/clangd/FindSymbols.cpp
+++ b/clang-tools-extra/clangd/FindSymbols.cpp
@@ -131,7 +131,7 @@ namespace {
 llvm::Optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) {
   auto &SM = Ctx.getSourceManager();
 
-  SourceLocation NameLoc = spellingLocIfSpelled(findName(&ND), SM);
+  SourceLocation NameLoc = nameLocation(ND, SM);
   // getFileLoc is a good choice for us, but we also need to make sure
   // sourceLocToPosition won't switch files, so we call getSpellingLoc on top of
   // that to make sure it does not switch files.

diff  --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index b36d11d64a3e..15403d215fc6 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -224,14 +224,6 @@ bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM) {
   return true;
 }
 
-SourceLocation spellingLocIfSpelled(SourceLocation Loc,
-                                    const SourceManager &SM) {
-  if (!isSpelledInSource(Loc, SM))
-    // Use the expansion location as spelling location is not interesting.
-    return SM.getExpansionRange(Loc).getBegin();
-  return SM.getSpellingLoc(Loc);
-}
-
 llvm::Optional<Range> getTokenRange(const SourceManager &SM,
                                     const LangOptions &LangOpts,
                                     SourceLocation TokLoc) {

diff  --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h
index f75be998dc2d..47fde505c252 100644
--- a/clang-tools-extra/clangd/SourceCode.h
+++ b/clang-tools-extra/clangd/SourceCode.h
@@ -107,12 +107,6 @@ SourceLocation includeHashLoc(FileID IncludedFile, const SourceManager &SM);
 ///     `-DName=foo`, the spelling location will be "<command line>".
 bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM);
 
-/// Returns the spelling location of the token at Loc if isSpelledInSource,
-/// otherwise its expansion location.
-/// FIXME: Most callers likely want some variant of "file location" instead.
-SourceLocation spellingLocIfSpelled(SourceLocation Loc,
-                                    const SourceManager &SM);
-
 /// Turns a token range into a half-open range and checks its correctness.
 /// The resulting range will have only valid source location on both sides, both
 /// of which are file locations.

diff  --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index cf5cc4eb273e..3ad20522226b 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -246,8 +246,7 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
       }
     }
 
-    auto Loc = makeLocation(AST.getASTContext(),
-                            spellingLocIfSpelled(findName(Preferred), SM),
+    auto Loc = makeLocation(AST.getASTContext(), nameLocation(*Preferred, SM),
                             *MainFilePath);
     if (!Loc)
       continue;
@@ -535,8 +534,7 @@ static llvm::Optional<TypeHierarchyItem>
 declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND) {
   auto &SM = Ctx.getSourceManager();
 
-  SourceLocation NameLoc =
-      spellingLocIfSpelled(findName(&ND), Ctx.getSourceManager());
+  SourceLocation NameLoc = nameLocation(ND, Ctx.getSourceManager());
   // getFileLoc is a good choice for us, but we also need to make sure
   // sourceLocToPosition won't switch files, so we call getSpellingLoc on top of
   // that to make sure it does not switch files.

diff  --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 6775ccc5bc16..ac782c07c7aa 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -82,7 +82,7 @@ static const char *PROTO_HEADER_COMMENT =
 // filters.
 bool isPrivateProtoDecl(const NamedDecl &ND) {
   const auto &SM = ND.getASTContext().getSourceManager();
-  auto Loc = spellingLocIfSpelled(findName(&ND), SM);
+  auto Loc = nameLocation(ND, SM);
   auto FileName = SM.getFilename(Loc);
   if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
     return false;
@@ -595,7 +595,7 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
     S.Flags |= Symbol::VisibleOutsideFile;
   S.SymInfo = index::getSymbolInfo(&ND);
   std::string FileURI;
-  auto Loc = spellingLocIfSpelled(findName(&ND), SM);
+  auto Loc = nameLocation(ND, SM);
   assert(Loc.isValid() && "Invalid source location for NamedDecl");
   // FIXME: use the result to filter out symbols.
   shouldIndexFile(SM.getFileID(Loc));
@@ -656,7 +656,7 @@ void SymbolCollector::addDefinition(const NamedDecl &ND,
   Symbol S = DeclSym;
   std::string FileURI;
   const auto &SM = ND.getASTContext().getSourceManager();
-  auto Loc = spellingLocIfSpelled(findName(&ND), SM);
+  auto Loc = nameLocation(ND, SM);
   // FIXME: use the result to filter out symbols.
   shouldIndexFile(SM.getFileID(Loc));
   if (auto DefLoc =


        


More information about the cfe-commits mailing list