blob: dea45671e5584109dd36b94da03df087f6be541b [file] [log] [blame]
Haojian Wu5f100262018-03-09 14:00:34 +00001//===--- 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 Biryukov19d75602018-11-23 15:21:19 +000014#include "clang/AST/DeclTemplate.h"
Eric Liu48597382018-10-18 12:23:05 +000015#include "clang/Basic/SourceLocation.h"
Haojian Wu5f100262018-03-09 14:00:34 +000016#include "clang/Basic/SourceManager.h"
Haojian Wuc6ddb462018-08-07 08:57:52 +000017#include "clang/Index/USRGeneration.h"
Ilya Biryukov19d75602018-11-23 15:21:19 +000018#include "llvm/Support/Casting.h"
19#include "llvm/Support/ScopedPrinter.h"
Haojian Wu5f100262018-03-09 14:00:34 +000020
Sam McCallc008af62018-10-20 15:30:37 +000021using namespace llvm;
Haojian Wu5f100262018-03-09 14:00:34 +000022namespace clang {
23namespace clangd {
Haojian Wu5f100262018-03-09 14:00:34 +000024
Eric Liu48597382018-10-18 12:23:05 +000025// Returns true if the complete name of decl \p D is spelled in the source code.
Kadir Cetinkaya6f9b2042018-12-05 11:57:15 +000026// 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 Liu48597382018-10-18 12:23:05 +000031bool isSpelledInSourceCode(const Decl *D) {
32 const auto &SM = D->getASTContext().getSourceManager();
33 auto Loc = D->getLocation();
Haojian Wu5f100262018-03-09 14:00:34 +000034 // FIXME: Revisit the strategy, the heuristic is limitted when handling
35 // macros, we should use the location where the whole definition occurs.
Eric Liu48597382018-10-18 12:23:05 +000036 if (Loc.isMacroID()) {
37 std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM);
Kadir Cetinkaya6f9b2042018-12-05 11:57:15 +000038 if (StringRef(PrintLoc).startswith("<scratch") ||
39 StringRef(PrintLoc).startswith("<command line>"))
Eric Liu48597382018-10-18 12:23:05 +000040 return false;
Haojian Wu5f100262018-03-09 14:00:34 +000041 }
Eric Liu48597382018-10-18 12:23:05 +000042 return true;
43}
44
45bool isImplementationDetail(const Decl *D) { return !isSpelledInSourceCode(D); }
46
47SourceLocation 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 Wu5f100262018-03-09 14:00:34 +000053}
54
Eric Liu7ad16962018-06-22 10:46:59 +000055std::string printQualifiedName(const NamedDecl &ND) {
56 std::string QName;
Sam McCallc008af62018-10-20 15:30:37 +000057 raw_string_ostream OS(QName);
Eric Liu7ad16962018-06-22 10:46:59 +000058 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 Biryukov19d75602018-11-23 15:21:19 +000070static const TemplateArgumentList *
71getTemplateSpecializationArgs(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
81std::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 Wong1a1fbdc2018-11-27 04:27:00 +0000101 if (isa<NamespaceDecl>(ND))
Ilya Biryukov19d75602018-11-23 15:21:19 +0000102 return "(anonymous namespace)";
103 if (auto *Cls = llvm::dyn_cast<RecordDecl>(&ND))
104 return ("(anonymous " + Cls->getKindName() + ")").str();
Henry Wong1a1fbdc2018-11-27 04:27:00 +0000105 if (isa<EnumDecl>(ND))
Ilya Biryukov19d75602018-11-23 15:21:19 +0000106 return "(anonymous enum)";
107 return "(anonymous)";
108}
109
Eric Liu3fac4ef2018-10-17 11:19:02 +0000110std::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 McCallc008af62018-10-20 15:30:37 +0000118Optional<SymbolID> getSymbolID(const Decl *D) {
119 SmallString<128> USR;
Haojian Wuc6ddb462018-08-07 08:57:52 +0000120 if (index::generateUSRForDecl(D, USR))
121 return None;
122 return SymbolID(USR);
123}
124
Sam McCallc008af62018-10-20 15:30:37 +0000125Optional<SymbolID> getSymbolID(const IdentifierInfo &II, const MacroInfo *MI,
126 const SourceManager &SM) {
Eric Liud25f1212018-09-06 09:59:37 +0000127 if (MI == nullptr)
128 return None;
Sam McCallc008af62018-10-20 15:30:37 +0000129 SmallString<128> USR;
Eric Liud25f1212018-09-06 09:59:37 +0000130 if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR))
131 return None;
132 return SymbolID(USR);
133}
134
Haojian Wu5f100262018-03-09 14:00:34 +0000135} // namespace clangd
136} // namespace clang