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. |
Sam McCall | 72ccf00 | 2018-11-09 12:56:49 +0000 | [diff] [blame] | 26 | // This is not the case for symbols formed via macro concatenation. |
| 27 | // (We used to attempt to treat names spelled on the command-line this way too, |
| 28 | // but the preamble doesn't preserve the required information). |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 29 | bool isSpelledInSourceCode(const Decl *D) { |
| 30 | const auto &SM = D->getASTContext().getSourceManager(); |
| 31 | auto Loc = D->getLocation(); |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 32 | // FIXME: Revisit the strategy, the heuristic is limitted when handling |
| 33 | // macros, we should use the location where the whole definition occurs. |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 34 | if (Loc.isMacroID()) { |
| 35 | std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM); |
Sam McCall | 72ccf00 | 2018-11-09 12:56:49 +0000 | [diff] [blame] | 36 | if (StringRef(PrintLoc).startswith("<scratch")) |
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 | |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 67 | static const TemplateArgumentList * |
| 68 | getTemplateSpecializationArgs(const NamedDecl &ND) { |
| 69 | if (auto *Func = llvm::dyn_cast<FunctionDecl>(&ND)) |
| 70 | return Func->getTemplateSpecializationArgs(); |
| 71 | if (auto *Cls = llvm::dyn_cast<ClassTemplateSpecializationDecl>(&ND)) |
| 72 | return &Cls->getTemplateInstantiationArgs(); |
| 73 | if (auto *Var = llvm::dyn_cast<VarTemplateSpecializationDecl>(&ND)) |
| 74 | return &Var->getTemplateInstantiationArgs(); |
| 75 | return nullptr; |
| 76 | } |
| 77 | |
| 78 | std::string printName(const ASTContext &Ctx, const NamedDecl &ND) { |
| 79 | std::string Name; |
| 80 | llvm::raw_string_ostream Out(Name); |
| 81 | PrintingPolicy PP(Ctx.getLangOpts()); |
| 82 | // Handle 'using namespace'. They all have the same name - <using-directive>. |
| 83 | if (auto *UD = llvm::dyn_cast<UsingDirectiveDecl>(&ND)) { |
| 84 | Out << "using namespace "; |
| 85 | if (auto *Qual = UD->getQualifier()) |
| 86 | Qual->print(Out, PP); |
| 87 | UD->getNominatedNamespaceAsWritten()->printName(Out); |
| 88 | return Out.str(); |
| 89 | } |
| 90 | ND.getDeclName().print(Out, PP); |
| 91 | if (!Out.str().empty()) { |
| 92 | // FIXME(ibiryukov): do not show args not explicitly written by the user. |
| 93 | if (auto *ArgList = getTemplateSpecializationArgs(ND)) |
| 94 | printTemplateArgumentList(Out, ArgList->asArray(), PP); |
| 95 | return Out.str(); |
| 96 | } |
| 97 | // The name was empty, so present an anonymous entity. |
Ilya Biryukov | 4174d09 | 2018-11-26 09:57:41 +0000 | [diff] [blame] | 98 | if (llvm::dyn_cast<NamespaceDecl>(&ND)) |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 99 | return "(anonymous namespace)"; |
| 100 | if (auto *Cls = llvm::dyn_cast<RecordDecl>(&ND)) |
| 101 | return ("(anonymous " + Cls->getKindName() + ")").str(); |
Ilya Biryukov | 4174d09 | 2018-11-26 09:57:41 +0000 | [diff] [blame] | 102 | if (llvm::dyn_cast<EnumDecl>(&ND)) |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 103 | return "(anonymous enum)"; |
| 104 | return "(anonymous)"; |
| 105 | } |
| 106 | |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 107 | std::string printNamespaceScope(const DeclContext &DC) { |
| 108 | for (const auto *Ctx = &DC; Ctx != nullptr; Ctx = Ctx->getParent()) |
| 109 | if (const auto *NS = dyn_cast<NamespaceDecl>(Ctx)) |
| 110 | if (!NS->isAnonymousNamespace() && !NS->isInlineNamespace()) |
| 111 | return printQualifiedName(*NS) + "::"; |
| 112 | return ""; |
| 113 | } |
| 114 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 115 | Optional<SymbolID> getSymbolID(const Decl *D) { |
| 116 | SmallString<128> USR; |
Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 117 | if (index::generateUSRForDecl(D, USR)) |
| 118 | return None; |
| 119 | return SymbolID(USR); |
| 120 | } |
| 121 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 122 | Optional<SymbolID> getSymbolID(const IdentifierInfo &II, const MacroInfo *MI, |
| 123 | const SourceManager &SM) { |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 124 | if (MI == nullptr) |
| 125 | return None; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 126 | SmallString<128> USR; |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 127 | if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR)) |
| 128 | return None; |
| 129 | return SymbolID(USR); |
| 130 | } |
| 131 | |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 132 | } // namespace clangd |
| 133 | } // namespace clang |