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" |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclTemplate.h" |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceLocation.h" |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 17 | #include "clang/Index/USRGeneration.h" |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Casting.h" |
| 19 | #include "llvm/Support/ScopedPrinter.h" |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 20 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 22 | namespace clang { |
| 23 | namespace clangd { |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 24 | |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 25 | // Returns true if the complete name of decl \p D is spelled in the source code. |
Kadir Cetinkaya | 6f9b204 | 2018-12-05 11:57:15 +0000 | [diff] [blame^] | 26 | // This is not the case for: |
| 27 | // * symbols formed via macro concatenation, the spelling location will |
| 28 | // be "<scratch space>" |
| 29 | // * symbols controlled and defined by a compile command-line option |
| 30 | // `-DName=foo`, the spelling location will be "<command line>". |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 31 | bool isSpelledInSourceCode(const Decl *D) { |
| 32 | const auto &SM = D->getASTContext().getSourceManager(); |
| 33 | auto Loc = D->getLocation(); |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 34 | // FIXME: Revisit the strategy, the heuristic is limitted when handling |
| 35 | // macros, we should use the location where the whole definition occurs. |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 36 | if (Loc.isMacroID()) { |
| 37 | std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM); |
Kadir Cetinkaya | 6f9b204 | 2018-12-05 11:57:15 +0000 | [diff] [blame^] | 38 | if (StringRef(PrintLoc).startswith("<scratch") || |
| 39 | StringRef(PrintLoc).startswith("<command line>")) |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 40 | return false; |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 41 | } |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool isImplementationDetail(const Decl *D) { return !isSpelledInSourceCode(D); } |
| 46 | |
| 47 | SourceLocation findNameLoc(const clang::Decl* D) { |
| 48 | const auto &SM = D->getASTContext().getSourceManager(); |
| 49 | if (!isSpelledInSourceCode(D)) |
| 50 | // Use the expansion location as spelling location is not interesting. |
| 51 | return SM.getExpansionRange(D->getLocation()).getBegin(); |
| 52 | return SM.getSpellingLoc(D->getLocation()); |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 55 | std::string printQualifiedName(const NamedDecl &ND) { |
| 56 | std::string QName; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 57 | raw_string_ostream OS(QName); |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 58 | PrintingPolicy Policy(ND.getASTContext().getLangOpts()); |
| 59 | // Note that inline namespaces are treated as transparent scopes. This |
| 60 | // reflects the way they're most commonly used for lookup. Ideally we'd |
| 61 | // include them, but at query time it's hard to find all the inline |
| 62 | // namespaces to query: the preamble doesn't have a dedicated list. |
| 63 | Policy.SuppressUnwrittenScope = true; |
| 64 | ND.printQualifiedName(OS, Policy); |
| 65 | OS.flush(); |
| 66 | assert(!StringRef(QName).startswith("::")); |
| 67 | return QName; |
| 68 | } |
| 69 | |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 70 | static const TemplateArgumentList * |
| 71 | getTemplateSpecializationArgs(const NamedDecl &ND) { |
| 72 | if (auto *Func = llvm::dyn_cast<FunctionDecl>(&ND)) |
| 73 | return Func->getTemplateSpecializationArgs(); |
| 74 | if (auto *Cls = llvm::dyn_cast<ClassTemplateSpecializationDecl>(&ND)) |
| 75 | return &Cls->getTemplateInstantiationArgs(); |
| 76 | if (auto *Var = llvm::dyn_cast<VarTemplateSpecializationDecl>(&ND)) |
| 77 | return &Var->getTemplateInstantiationArgs(); |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
| 81 | std::string printName(const ASTContext &Ctx, const NamedDecl &ND) { |
| 82 | std::string Name; |
| 83 | llvm::raw_string_ostream Out(Name); |
| 84 | PrintingPolicy PP(Ctx.getLangOpts()); |
| 85 | // Handle 'using namespace'. They all have the same name - <using-directive>. |
| 86 | if (auto *UD = llvm::dyn_cast<UsingDirectiveDecl>(&ND)) { |
| 87 | Out << "using namespace "; |
| 88 | if (auto *Qual = UD->getQualifier()) |
| 89 | Qual->print(Out, PP); |
| 90 | UD->getNominatedNamespaceAsWritten()->printName(Out); |
| 91 | return Out.str(); |
| 92 | } |
| 93 | ND.getDeclName().print(Out, PP); |
| 94 | if (!Out.str().empty()) { |
| 95 | // FIXME(ibiryukov): do not show args not explicitly written by the user. |
| 96 | if (auto *ArgList = getTemplateSpecializationArgs(ND)) |
| 97 | printTemplateArgumentList(Out, ArgList->asArray(), PP); |
| 98 | return Out.str(); |
| 99 | } |
| 100 | // The name was empty, so present an anonymous entity. |
Henry Wong | 1a1fbdc | 2018-11-27 04:27:00 +0000 | [diff] [blame] | 101 | if (isa<NamespaceDecl>(ND)) |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 102 | return "(anonymous namespace)"; |
| 103 | if (auto *Cls = llvm::dyn_cast<RecordDecl>(&ND)) |
| 104 | return ("(anonymous " + Cls->getKindName() + ")").str(); |
Henry Wong | 1a1fbdc | 2018-11-27 04:27:00 +0000 | [diff] [blame] | 105 | if (isa<EnumDecl>(ND)) |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 106 | return "(anonymous enum)"; |
| 107 | return "(anonymous)"; |
| 108 | } |
| 109 | |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 110 | std::string printNamespaceScope(const DeclContext &DC) { |
| 111 | for (const auto *Ctx = &DC; Ctx != nullptr; Ctx = Ctx->getParent()) |
| 112 | if (const auto *NS = dyn_cast<NamespaceDecl>(Ctx)) |
| 113 | if (!NS->isAnonymousNamespace() && !NS->isInlineNamespace()) |
| 114 | return printQualifiedName(*NS) + "::"; |
| 115 | return ""; |
| 116 | } |
| 117 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 118 | Optional<SymbolID> getSymbolID(const Decl *D) { |
| 119 | SmallString<128> USR; |
Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 120 | if (index::generateUSRForDecl(D, USR)) |
| 121 | return None; |
| 122 | return SymbolID(USR); |
| 123 | } |
| 124 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 125 | Optional<SymbolID> getSymbolID(const IdentifierInfo &II, const MacroInfo *MI, |
| 126 | const SourceManager &SM) { |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 127 | if (MI == nullptr) |
| 128 | return None; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 129 | SmallString<128> USR; |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 130 | if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR)) |
| 131 | return None; |
| 132 | return SymbolID(USR); |
| 133 | } |
| 134 | |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 135 | } // namespace clangd |
| 136 | } // namespace clang |