blob: 9d4a61f77f59c2d66abc7d53bd45b8e3d1f58e8d [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.
Sam McCall72ccf002018-11-09 12:56:49 +000026// 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 Liu48597382018-10-18 12:23:05 +000029bool isSpelledInSourceCode(const Decl *D) {
30 const auto &SM = D->getASTContext().getSourceManager();
31 auto Loc = D->getLocation();
Haojian Wu5f100262018-03-09 14:00:34 +000032 // FIXME: Revisit the strategy, the heuristic is limitted when handling
33 // macros, we should use the location where the whole definition occurs.
Eric Liu48597382018-10-18 12:23:05 +000034 if (Loc.isMacroID()) {
35 std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM);
Sam McCall72ccf002018-11-09 12:56:49 +000036 if (StringRef(PrintLoc).startswith("<scratch"))
Eric Liu48597382018-10-18 12:23:05 +000037 return false;
Haojian Wu5f100262018-03-09 14:00:34 +000038 }
Eric Liu48597382018-10-18 12:23:05 +000039 return true;
40}
41
42bool isImplementationDetail(const Decl *D) { return !isSpelledInSourceCode(D); }
43
44SourceLocation 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 Wu5f100262018-03-09 14:00:34 +000050}
51
Eric Liu7ad16962018-06-22 10:46:59 +000052std::string printQualifiedName(const NamedDecl &ND) {
53 std::string QName;
Sam McCallc008af62018-10-20 15:30:37 +000054 raw_string_ostream OS(QName);
Eric Liu7ad16962018-06-22 10:46:59 +000055 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 Biryukov19d75602018-11-23 15:21:19 +000067static const TemplateArgumentList *
68getTemplateSpecializationArgs(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
78std::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 Biryukov4174d092018-11-26 09:57:41 +000098 if (llvm::dyn_cast<NamespaceDecl>(&ND))
Ilya Biryukov19d75602018-11-23 15:21:19 +000099 return "(anonymous namespace)";
100 if (auto *Cls = llvm::dyn_cast<RecordDecl>(&ND))
101 return ("(anonymous " + Cls->getKindName() + ")").str();
Ilya Biryukov4174d092018-11-26 09:57:41 +0000102 if (llvm::dyn_cast<EnumDecl>(&ND))
Ilya Biryukov19d75602018-11-23 15:21:19 +0000103 return "(anonymous enum)";
104 return "(anonymous)";
105}
106
Eric Liu3fac4ef2018-10-17 11:19:02 +0000107std::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 McCallc008af62018-10-20 15:30:37 +0000115Optional<SymbolID> getSymbolID(const Decl *D) {
116 SmallString<128> USR;
Haojian Wuc6ddb462018-08-07 08:57:52 +0000117 if (index::generateUSRForDecl(D, USR))
118 return None;
119 return SymbolID(USR);
120}
121
Sam McCallc008af62018-10-20 15:30:37 +0000122Optional<SymbolID> getSymbolID(const IdentifierInfo &II, const MacroInfo *MI,
123 const SourceManager &SM) {
Eric Liud25f1212018-09-06 09:59:37 +0000124 if (MI == nullptr)
125 return None;
Sam McCallc008af62018-10-20 15:30:37 +0000126 SmallString<128> USR;
Eric Liud25f1212018-09-06 09:59:37 +0000127 if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR))
128 return None;
129 return SymbolID(USR);
130}
131
Haojian Wu5f100262018-03-09 14:00:34 +0000132} // namespace clangd
133} // namespace clang