1 //===--- AST.cpp - Utility AST functions  -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "AST.h"
11 
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/Basic/SourceManager.h"
15 
16 namespace clang {
17 namespace clangd {
18 using namespace llvm;
19 
20 SourceLocation findNameLoc(const clang::Decl* D) {
21   const auto& SM = D->getASTContext().getSourceManager();
22   // FIXME: Revisit the strategy, the heuristic is limitted when handling
23   // macros, we should use the location where the whole definition occurs.
24   SourceLocation SpellingLoc = SM.getSpellingLoc(D->getLocation());
25   if (D->getLocation().isMacroID()) {
26     std::string PrintLoc = SpellingLoc.printToString(SM);
27     if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
28         llvm::StringRef(PrintLoc).startswith("<command line>")) {
29       // We use the expansion location for the following symbols, as spelling
30       // locations of these symbols are not interesting to us:
31       //   * symbols formed via macro concatenation, the spelling location will
32       //     be "<scratch space>"
33       //   * symbols controlled and defined by a compile command-line option
34       //     `-DName=foo`, the spelling location will be "<command line>".
35       SpellingLoc = SM.getExpansionRange(D->getLocation()).first;
36     }
37   }
38   return SpellingLoc;
39 }
40 
41 } // namespace clangd
42 } // namespace clang
43