[clangd] Unfold SourceLocation flattening from findNameLoc in preparation for adding more overloads. NFC

llvm-svn: 368083
diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index f8057be..7a2f7c7 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -200,6 +200,24 @@
   return P;
 }
 
+bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM) {
+  if (Loc.isMacroID()) {
+    std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM);
+    if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
+        llvm::StringRef(PrintLoc).startswith("<command line>"))
+      return false;
+  }
+  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) {