Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 1 | //===--- AST.cpp - Utility AST functions -----------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "AST.h" |
| 10 | |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/AST/Decl.h" |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclTemplate.h" |
Kadir Cetinkaya | a80a522 | 2019-04-12 10:09:14 +0000 | [diff] [blame] | 14 | #include "clang/AST/TemplateBase.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" |
Kadir Cetinkaya | a80a522 | 2019-04-12 10:09:14 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Optional.h" |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Casting.h" |
| 20 | #include "llvm/Support/ScopedPrinter.h" |
Kadir Cetinkaya | a80a522 | 2019-04-12 10:09:14 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
| 24 | namespace clangd { |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 25 | |
Kadir Cetinkaya | a80a522 | 2019-04-12 10:09:14 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | llvm::Optional<llvm::ArrayRef<TemplateArgumentLoc>> |
| 28 | getTemplateSpecializationArgLocs(const NamedDecl &ND) { |
| 29 | if (auto *Func = llvm::dyn_cast<FunctionDecl>(&ND)) { |
| 30 | if (const ASTTemplateArgumentListInfo *Args = |
| 31 | Func->getTemplateSpecializationArgsAsWritten()) |
| 32 | return Args->arguments(); |
| 33 | } else if (auto *Cls = |
| 34 | llvm::dyn_cast<ClassTemplatePartialSpecializationDecl>(&ND)) { |
| 35 | if (auto *Args = Cls->getTemplateArgsAsWritten()) |
| 36 | return Args->arguments(); |
| 37 | } else if (auto *Var = llvm::dyn_cast<VarTemplateSpecializationDecl>(&ND)) |
| 38 | return Var->getTemplateArgsInfo().arguments(); |
| 39 | // We return None for ClassTemplateSpecializationDecls because it does not |
| 40 | // contain TemplateArgumentLoc information. |
| 41 | return llvm::None; |
| 42 | } |
| 43 | } // namespace |
| 44 | |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 45 | // 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] | 46 | // This is not the case for: |
| 47 | // * symbols formed via macro concatenation, the spelling location will |
| 48 | // be "<scratch space>" |
| 49 | // * symbols controlled and defined by a compile command-line option |
| 50 | // `-DName=foo`, the spelling location will be "<command line>". |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 51 | bool isSpelledInSourceCode(const Decl *D) { |
| 52 | const auto &SM = D->getASTContext().getSourceManager(); |
| 53 | auto Loc = D->getLocation(); |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 54 | // FIXME: Revisit the strategy, the heuristic is limitted when handling |
| 55 | // macros, we should use the location where the whole definition occurs. |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 56 | if (Loc.isMacroID()) { |
| 57 | std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 58 | if (llvm::StringRef(PrintLoc).startswith("<scratch") || |
| 59 | llvm::StringRef(PrintLoc).startswith("<command line>")) |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 60 | return false; |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 61 | } |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | |
| 65 | bool isImplementationDetail(const Decl *D) { return !isSpelledInSourceCode(D); } |
| 66 | |
Ilya Biryukov | 22fa465 | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 67 | SourceLocation findNameLoc(const clang::Decl *D) { |
Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 68 | const auto &SM = D->getASTContext().getSourceManager(); |
| 69 | if (!isSpelledInSourceCode(D)) |
| 70 | // Use the expansion location as spelling location is not interesting. |
| 71 | return SM.getExpansionRange(D->getLocation()).getBegin(); |
| 72 | return SM.getSpellingLoc(D->getLocation()); |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 75 | std::string printQualifiedName(const NamedDecl &ND) { |
| 76 | std::string QName; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 77 | llvm::raw_string_ostream OS(QName); |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 78 | PrintingPolicy Policy(ND.getASTContext().getLangOpts()); |
| 79 | // Note that inline namespaces are treated as transparent scopes. This |
| 80 | // reflects the way they're most commonly used for lookup. Ideally we'd |
| 81 | // include them, but at query time it's hard to find all the inline |
| 82 | // namespaces to query: the preamble doesn't have a dedicated list. |
| 83 | Policy.SuppressUnwrittenScope = true; |
| 84 | ND.printQualifiedName(OS, Policy); |
| 85 | OS.flush(); |
| 86 | assert(!StringRef(QName).startswith("::")); |
| 87 | return QName; |
| 88 | } |
| 89 | |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 90 | std::string printName(const ASTContext &Ctx, const NamedDecl &ND) { |
| 91 | std::string Name; |
| 92 | llvm::raw_string_ostream Out(Name); |
| 93 | PrintingPolicy PP(Ctx.getLangOpts()); |
| 94 | // Handle 'using namespace'. They all have the same name - <using-directive>. |
| 95 | if (auto *UD = llvm::dyn_cast<UsingDirectiveDecl>(&ND)) { |
| 96 | Out << "using namespace "; |
| 97 | if (auto *Qual = UD->getQualifier()) |
| 98 | Qual->print(Out, PP); |
| 99 | UD->getNominatedNamespaceAsWritten()->printName(Out); |
| 100 | return Out.str(); |
| 101 | } |
| 102 | ND.getDeclName().print(Out, PP); |
| 103 | if (!Out.str().empty()) { |
Kadir Cetinkaya | a80a522 | 2019-04-12 10:09:14 +0000 | [diff] [blame] | 104 | Out << printTemplateSpecializationArgs(ND); |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 105 | return Out.str(); |
| 106 | } |
| 107 | // The name was empty, so present an anonymous entity. |
Henry Wong | 1a1fbdc | 2018-11-27 04:27:00 +0000 | [diff] [blame] | 108 | if (isa<NamespaceDecl>(ND)) |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 109 | return "(anonymous namespace)"; |
| 110 | if (auto *Cls = llvm::dyn_cast<RecordDecl>(&ND)) |
| 111 | return ("(anonymous " + Cls->getKindName() + ")").str(); |
Henry Wong | 1a1fbdc | 2018-11-27 04:27:00 +0000 | [diff] [blame] | 112 | if (isa<EnumDecl>(ND)) |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 113 | return "(anonymous enum)"; |
| 114 | return "(anonymous)"; |
| 115 | } |
| 116 | |
Kadir Cetinkaya | a80a522 | 2019-04-12 10:09:14 +0000 | [diff] [blame] | 117 | std::string printTemplateSpecializationArgs(const NamedDecl &ND) { |
| 118 | std::string TemplateArgs; |
| 119 | llvm::raw_string_ostream OS(TemplateArgs); |
| 120 | PrintingPolicy Policy(ND.getASTContext().getLangOpts()); |
| 121 | if (llvm::Optional<llvm::ArrayRef<TemplateArgumentLoc>> Args = |
| 122 | getTemplateSpecializationArgLocs(ND)) { |
| 123 | printTemplateArgumentList(OS, *Args, Policy); |
| 124 | } else if (auto *Cls = llvm::dyn_cast<ClassTemplateSpecializationDecl>(&ND)) { |
| 125 | if (const TypeSourceInfo *TSI = Cls->getTypeAsWritten()) { |
| 126 | // ClassTemplateSpecializationDecls do not contain |
| 127 | // TemplateArgumentTypeLocs, they only have TemplateArgumentTypes. So we |
| 128 | // create a new argument location list from TypeSourceInfo. |
| 129 | auto STL = TSI->getTypeLoc().getAs<TemplateSpecializationTypeLoc>(); |
| 130 | llvm::SmallVector<TemplateArgumentLoc, 8> ArgLocs; |
| 131 | ArgLocs.reserve(STL.getNumArgs()); |
| 132 | for (unsigned I = 0; I < STL.getNumArgs(); ++I) |
| 133 | ArgLocs.push_back(STL.getArgLoc(I)); |
| 134 | printTemplateArgumentList(OS, ArgLocs, Policy); |
| 135 | } else { |
| 136 | // FIXME: Fix cases when getTypeAsWritten returns null inside clang AST, |
| 137 | // e.g. friend decls. Currently we fallback to Template Arguments without |
| 138 | // location information. |
| 139 | printTemplateArgumentList(OS, Cls->getTemplateArgs().asArray(), Policy); |
| 140 | } |
| 141 | } |
| 142 | OS.flush(); |
| 143 | return TemplateArgs; |
| 144 | } |
| 145 | |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 146 | std::string printNamespaceScope(const DeclContext &DC) { |
| 147 | for (const auto *Ctx = &DC; Ctx != nullptr; Ctx = Ctx->getParent()) |
| 148 | if (const auto *NS = dyn_cast<NamespaceDecl>(Ctx)) |
| 149 | if (!NS->isAnonymousNamespace() && !NS->isInlineNamespace()) |
| 150 | return printQualifiedName(*NS) + "::"; |
| 151 | return ""; |
| 152 | } |
| 153 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 154 | llvm::Optional<SymbolID> getSymbolID(const Decl *D) { |
| 155 | llvm::SmallString<128> USR; |
Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 156 | if (index::generateUSRForDecl(D, USR)) |
| 157 | return None; |
| 158 | return SymbolID(USR); |
| 159 | } |
| 160 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 161 | llvm::Optional<SymbolID> getSymbolID(const IdentifierInfo &II, |
| 162 | const MacroInfo *MI, |
| 163 | const SourceManager &SM) { |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 164 | if (MI == nullptr) |
| 165 | return None; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 166 | llvm::SmallString<128> USR; |
Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 167 | if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR)) |
| 168 | return None; |
| 169 | return SymbolID(USR); |
| 170 | } |
| 171 | |
Sam McCall | 9470142 | 2019-07-11 16:04:18 +0000 | [diff] [blame] | 172 | std::string shortenNamespace(const llvm::StringRef OriginalName, |
| 173 | const llvm::StringRef CurrentNamespace) { |
| 174 | llvm::SmallVector<llvm::StringRef, 8> OriginalParts; |
| 175 | llvm::SmallVector<llvm::StringRef, 8> CurrentParts; |
| 176 | llvm::SmallVector<llvm::StringRef, 8> Result; |
| 177 | OriginalName.split(OriginalParts, "::"); |
| 178 | CurrentNamespace.split(CurrentParts, "::"); |
| 179 | auto MinLength = std::min(CurrentParts.size(), OriginalParts.size()); |
| 180 | |
| 181 | unsigned DifferentAt = 0; |
| 182 | while (DifferentAt < MinLength && |
| 183 | CurrentParts[DifferentAt] == OriginalParts[DifferentAt]) { |
| 184 | DifferentAt++; |
| 185 | } |
| 186 | |
Paul Robinson | 2cb5c46 | 2019-07-11 23:48:06 +0000 | [diff] [blame^] | 187 | for (unsigned i = DifferentAt; i < OriginalParts.size(); ++i) { |
Sam McCall | 9470142 | 2019-07-11 16:04:18 +0000 | [diff] [blame] | 188 | Result.push_back(OriginalParts[i]); |
| 189 | } |
| 190 | return join(Result, "::"); |
| 191 | } |
| 192 | |
| 193 | std::string printType(const QualType QT, const DeclContext & Context){ |
| 194 | PrintingPolicy PP(Context.getParentASTContext().getPrintingPolicy()); |
| 195 | PP.SuppressTagKeyword = 1; |
| 196 | return shortenNamespace( |
| 197 | QT.getAsString(PP), |
| 198 | printNamespaceScope(Context) ); |
| 199 | } |
| 200 | |
| 201 | |
Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 202 | } // namespace clangd |
| 203 | } // namespace clang |