Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 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" |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 14 | #include "clang/Basic/SourceLocation.h" |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 16 | #include "clang/Index/USRGeneration.h" |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 17 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 18 | using namespace llvm; |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 19 | namespace clang { |
| 20 | namespace clangd { |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 21 | |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 22 | // Returns true if the complete name of decl \p D is spelled in the source code. |
| 23 | // This is not the case for |
| 24 | // * symbols formed via macro concatenation, the spelling location will |
| 25 | // be "<scratch space>" |
| 26 | // * symbols controlled and defined by a compile command-line option |
| 27 | // `-DName=foo`, the spelling location will be "<command line>". |
| 28 | bool isSpelledInSourceCode(const Decl *D) { |
| 29 | const auto &SM = D->getASTContext().getSourceManager(); |
| 30 | auto Loc = D->getLocation(); |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 31 | // FIXME: Revisit the strategy, the heuristic is limitted when handling |
| 32 | // macros, we should use the location where the whole definition occurs. |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 33 | if (Loc.isMacroID()) { |
| 34 | std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 35 | if (StringRef(PrintLoc).startswith("<scratch") || |
| 36 | StringRef(PrintLoc).startswith("<command line>")) |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 37 | return false; |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 38 | } |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 39 | return true; |
| 40 | } |
| 41 | |
| 42 | bool isImplementationDetail(const Decl *D) { return !isSpelledInSourceCode(D); } |
| 43 | |
| 44 | SourceLocation findNameLoc(const clang::Decl* D) { |
| 45 | const auto &SM = D->getASTContext().getSourceManager(); |
| 46 | if (!isSpelledInSourceCode(D)) |
| 47 | // Use the expansion location as spelling location is not interesting. |
| 48 | return SM.getExpansionRange(D->getLocation()).getBegin(); |
| 49 | return SM.getSpellingLoc(D->getLocation()); |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 52 | std::string printQualifiedName(const NamedDecl &ND) { |
| 53 | std::string QName; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 54 | raw_string_ostream OS(QName); |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 55 | PrintingPolicy Policy(ND.getASTContext().getLangOpts()); |
| 56 | // Note that inline namespaces are treated as transparent scopes. This |
| 57 | // reflects the way they're most commonly used for lookup. Ideally we'd |
| 58 | // include them, but at query time it's hard to find all the inline |
| 59 | // namespaces to query: the preamble doesn't have a dedicated list. |
| 60 | Policy.SuppressUnwrittenScope = true; |
| 61 | ND.printQualifiedName(OS, Policy); |
| 62 | OS.flush(); |
| 63 | assert(!StringRef(QName).startswith("::")); |
| 64 | return QName; |
| 65 | } |
| 66 | |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 67 | std::string printNamespaceScope(const DeclContext &DC) { |
| 68 | for (const auto *Ctx = &DC; Ctx != nullptr; Ctx = Ctx->getParent()) |
| 69 | if (const auto *NS = dyn_cast<NamespaceDecl>(Ctx)) |
| 70 | if (!NS->isAnonymousNamespace() && !NS->isInlineNamespace()) |
| 71 | return printQualifiedName(*NS) + "::"; |
| 72 | return ""; |
| 73 | } |
| 74 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 75 | Optional<SymbolID> getSymbolID(const Decl *D) { |
| 76 | SmallString<128> USR; |
Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 77 | if (index::generateUSRForDecl(D, USR)) |
| 78 | return None; |
| 79 | return SymbolID(USR); |
| 80 | } |
| 81 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 82 | Optional<SymbolID> getSymbolID(const IdentifierInfo &II, const MacroInfo *MI, |
| 83 | const SourceManager &SM) { |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 84 | if (MI == nullptr) |
| 85 | return None; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 86 | SmallString<128> USR; |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 87 | if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR)) |
| 88 | return None; |
| 89 | return SymbolID(USR); |
| 90 | } |
| 91 | |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 92 | } // namespace clangd |
| 93 | } // namespace clang |