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()).getBegin(); 36 } 37 } 38 return SpellingLoc; 39 } 40 41 std::string printQualifiedName(const NamedDecl &ND) { 42 std::string QName; 43 llvm::raw_string_ostream OS(QName); 44 PrintingPolicy Policy(ND.getASTContext().getLangOpts()); 45 // Note that inline namespaces are treated as transparent scopes. This 46 // reflects the way they're most commonly used for lookup. Ideally we'd 47 // include them, but at query time it's hard to find all the inline 48 // namespaces to query: the preamble doesn't have a dedicated list. 49 Policy.SuppressUnwrittenScope = true; 50 ND.printQualifiedName(OS, Policy); 51 OS.flush(); 52 assert(!StringRef(QName).startswith("::")); 53 return QName; 54 } 55 56 } // namespace clangd 57 } // namespace clang 58