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

git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@370869 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/clangd/SourceCode.cpp b/clangd/SourceCode.cpp
index 4291016..812e42a 100644
--- a/clangd/SourceCode.cpp
+++ b/clangd/SourceCode.cpp
@@ -250,7 +250,7 @@
   // 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 @@
   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) {
diff --git a/clangd/SourceCode.h b/clangd/SourceCode.h
index 5f27ee2..81b4672 100644
--- a/clangd/SourceCode.h
+++ b/clangd/SourceCode.h
@@ -77,7 +77,8 @@
 
 /// 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);
diff --git a/clangd/XRefs.cpp b/clangd/XRefs.cpp
index b21987e..bc2c222 100644
--- a/clangd/XRefs.cpp
+++ b/clangd/XRefs.cpp
@@ -255,8 +255,9 @@
     }
   }
 
-  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 @@
                                                       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 @@
 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 @@
     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<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 @@
 }
 
 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;
diff --git a/clangd/refactor/Rename.cpp b/clangd/refactor/Rename.cpp
index 306c804..770064f 100644
--- a/clangd/refactor/Rename.cpp
+++ b/clangd/refactor/Rename.cpp
@@ -153,8 +153,9 @@
 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()))