[clang-tools-extra] r370869 - [clangd] Remove macro-expansion-location from getBeginningOfIdentifier. Inline into relevant callsites. NFC

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 4 03:15:27 PDT 2019


Author: sammccall
Date: Wed Sep  4 03:15:27 2019
New Revision: 370869

URL: http://llvm.org/viewvc/llvm-project?rev=370869&view=rev
Log:
[clangd] Remove macro-expansion-location from getBeginningOfIdentifier. Inline into relevant callsites. NFC

Modified:
    clang-tools-extra/trunk/clangd/SourceCode.cpp
    clang-tools-extra/trunk/clangd/SourceCode.h
    clang-tools-extra/trunk/clangd/XRefs.cpp
    clang-tools-extra/trunk/clangd/refactor/Rename.cpp

Modified: clang-tools-extra/trunk/clangd/SourceCode.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SourceCode.cpp?rev=370869&r1=370868&r2=370869&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SourceCode.cpp (original)
+++ clang-tools-extra/trunk/clangd/SourceCode.cpp Wed Sep  4 03:15:27 2019
@@ -250,7 +250,7 @@ SourceLocation getBeginningOfIdentifier(
   // location is correct for both!
   SourceLocation InputLoc = SM.getComposedLoc(FID, *Offset);
   if (*Offset == 0) // Case 1 or 3.
-    return SM.getMacroArgExpandedLocation(InputLoc);
+    return InputLoc;
   SourceLocation Before = SM.getComposedLoc(FID, *Offset - 1);
 
   Before = Lexer::GetBeginningOfToken(Before, SM, LangOpts);
@@ -258,8 +258,8 @@ SourceLocation getBeginningOfIdentifier(
   if (Before.isValid() &&
       !Lexer::getRawToken(Before, Tok, SM, LangOpts, false) &&
       Tok.is(tok::raw_identifier))
-    return SM.getMacroArgExpandedLocation(Before); // Case 2.
-  return SM.getMacroArgExpandedLocation(InputLoc); // Case 1 or 3.
+    return Before; // Case 2.
+  return InputLoc; // Case 1 or 3.
 }
 
 bool isValidFileRange(const SourceManager &Mgr, SourceRange R) {

Modified: clang-tools-extra/trunk/clangd/SourceCode.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SourceCode.h?rev=370869&r1=370868&r2=370869&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SourceCode.h (original)
+++ clang-tools-extra/trunk/clangd/SourceCode.h Wed Sep  4 03:15:27 2019
@@ -77,7 +77,8 @@ llvm::Expected<SourceLocation> sourceLoc
 
 /// Get the beginning SourceLocation at a specified \p Pos in the main file.
 /// May be invalid if Pos is, or if there's no identifier.
-/// FIXME: this returns the macro-expansion location, but it shouldn't.
+/// The returned position is in the main file, callers may prefer to
+/// obtain the macro expansion location.
 SourceLocation getBeginningOfIdentifier(const Position &Pos,
                                         const SourceManager &SM,
                                         const LangOptions &LangOpts);

Modified: clang-tools-extra/trunk/clangd/XRefs.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/XRefs.cpp?rev=370869&r1=370868&r2=370869&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/XRefs.cpp (original)
+++ clang-tools-extra/trunk/clangd/XRefs.cpp Wed Sep  4 03:15:27 2019
@@ -255,8 +255,9 @@ std::vector<LocatedSymbol> locateSymbolA
     }
   }
 
-  SourceLocation SourceLocationBeg = getBeginningOfIdentifier(
-      Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts());
+  SourceLocation SourceLocationBeg =
+      SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
+          Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts()));
 
   // Macros are simple: there's no declaration/definition distinction.
   // As a consequence, there's no need to look them up in the index either.
@@ -409,10 +410,11 @@ std::vector<DocumentHighlight> findDocum
                                                       Position Pos) {
   const SourceManager &SM = AST.getSourceManager();
   // FIXME: show references to macro within file?
-  auto References = findRefs(
-      getDeclAtPosition(AST, getBeginningOfIdentifier(
-                                 Pos, SM, AST.getASTContext().getLangOpts())),
-      AST);
+  auto References =
+      findRefs(getDeclAtPosition(
+                   AST, SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
+                            Pos, SM, AST.getASTContext().getLangOpts()))),
+               AST);
 
   // FIXME: we may get multiple DocumentHighlights with the same location and
   // different kinds, deduplicate them.
@@ -875,9 +877,10 @@ bool hasDeducedType(ParsedAST &AST, Sour
 llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
                                    format::FormatStyle Style,
                                    const SymbolIndex *Index) {
+  const SourceManager &SM = AST.getSourceManager();
   llvm::Optional<HoverInfo> HI;
-  SourceLocation SourceLocationBeg = getBeginningOfIdentifier(
-      Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts());
+  SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
+      getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
 
   if (auto M = locateMacroAt(SourceLocationBeg, AST.getPreprocessor())) {
     HI = getHoverContents(*M, AST);
@@ -919,8 +922,8 @@ std::vector<Location> findReferences(Par
     elog("Failed to get a path for the main file, so no references");
     return Results;
   }
-  auto Loc =
-      getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts());
+  auto Loc = SM.getMacroArgExpandedLocation(
+      getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
   // TODO: should we handle macros, too?
   auto Decls = getDeclAtPosition(AST, Loc);
 
@@ -976,8 +979,8 @@ std::vector<Location> findReferences(Par
 
 std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
   const SourceManager &SM = AST.getSourceManager();
-  auto Loc =
-      getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts());
+  auto Loc = SM.getMacroArgExpandedLocation(
+      getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
 
   std::vector<SymbolDetails> Results;
 
@@ -1147,8 +1150,9 @@ static void fillSuperTypes(const CXXReco
 }
 
 const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
-  SourceLocation SourceLocationBeg = getBeginningOfIdentifier(
-      Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts());
+  const SourceManager &SM = AST.getSourceManager();
+  SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
+      getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
   auto Decls = getDeclAtPosition(AST, SourceLocationBeg);
   if (Decls.empty())
     return nullptr;

Modified: clang-tools-extra/trunk/clangd/refactor/Rename.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/Rename.cpp?rev=370869&r1=370868&r2=370869&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/refactor/Rename.cpp (original)
+++ clang-tools-extra/trunk/clangd/refactor/Rename.cpp Wed Sep  4 03:15:27 2019
@@ -153,8 +153,9 @@ findOccurrencesWithinFile(ParsedAST &AST
 llvm::Expected<tooling::Replacements>
 renameWithinFile(ParsedAST &AST, llvm::StringRef File, Position Pos,
                  llvm::StringRef NewName, const SymbolIndex *Index) {
-  SourceLocation SourceLocationBeg = getBeginningOfIdentifier(
-      Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts());
+  const SourceManager &SM = AST.getSourceManager();
+  SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
+      getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
   // FIXME: renaming macros is not supported yet, the macro-handling code should
   // be moved to rename tooling library.
   if (locateMacroAt(SourceLocationBeg, AST.getPreprocessor()))




More information about the cfe-commits mailing list