12 #include "clang/AST/ASTContext.h" 13 #include "clang/AST/Decl.h" 14 #include "clang/AST/DeclTemplate.h" 15 #include "clang/Basic/SourceLocation.h" 16 #include "clang/Basic/SourceManager.h" 17 #include "clang/Index/USRGeneration.h" 18 #include "llvm/Support/Casting.h" 19 #include "llvm/Support/ScopedPrinter.h" 31 const auto &SM = D->getASTContext().getSourceManager();
32 auto Loc = D->getLocation();
35 if (
Loc.isMacroID()) {
36 std::string PrintLoc = SM.getSpellingLoc(
Loc).printToString(SM);
37 if (llvm::StringRef(PrintLoc).startswith(
"<scratch") ||
38 llvm::StringRef(PrintLoc).startswith(
"<command line>"))
47 const auto &SM = D->getASTContext().getSourceManager();
50 return SM.getExpansionRange(D->getLocation()).getBegin();
51 return SM.getSpellingLoc(D->getLocation());
56 llvm::raw_string_ostream OS(QName);
57 PrintingPolicy Policy(ND.getASTContext().getLangOpts());
62 Policy.SuppressUnwrittenScope =
true;
63 ND.printQualifiedName(OS, Policy);
65 assert(!StringRef(QName).startswith(
"::"));
69 static const TemplateArgumentList *
71 if (
auto *Func = llvm::dyn_cast<FunctionDecl>(&ND))
72 return Func->getTemplateSpecializationArgs();
73 if (
auto *Cls = llvm::dyn_cast<ClassTemplateSpecializationDecl>(&ND))
74 return &Cls->getTemplateInstantiationArgs();
75 if (
auto *Var = llvm::dyn_cast<VarTemplateSpecializationDecl>(&ND))
76 return &Var->getTemplateInstantiationArgs();
80 std::string
printName(
const ASTContext &
Ctx,
const NamedDecl &ND) {
82 llvm::raw_string_ostream Out(Name);
83 PrintingPolicy PP(Ctx.getLangOpts());
85 if (
auto *UD = llvm::dyn_cast<UsingDirectiveDecl>(&ND)) {
86 Out <<
"using namespace ";
87 if (
auto *Qual = UD->getQualifier())
89 UD->getNominatedNamespaceAsWritten()->printName(Out);
92 ND.getDeclName().print(Out, PP);
93 if (!Out.str().empty()) {
96 printTemplateArgumentList(Out, ArgList->asArray(), PP);
100 if (isa<NamespaceDecl>(ND))
101 return "(anonymous namespace)";
102 if (
auto *Cls = llvm::dyn_cast<RecordDecl>(&ND))
103 return (
"(anonymous " + Cls->getKindName() +
")").str();
104 if (isa<EnumDecl>(ND))
105 return "(anonymous enum)";
106 return "(anonymous)";
110 for (
const auto *
Ctx = &DC;
Ctx !=
nullptr;
Ctx =
Ctx->getParent())
111 if (
const auto *NS = dyn_cast<NamespaceDecl>(
Ctx))
112 if (!NS->isAnonymousNamespace() && !NS->isInlineNamespace())
118 llvm::SmallString<128> USR;
119 if (index::generateUSRForDecl(D, USR))
126 const SourceManager &SM) {
129 llvm::SmallString<128> USR;
130 if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR))
SourceLocation Loc
'#' location in the include directive
std::string printName(const ASTContext &Ctx, const NamedDecl &ND)
Prints unqualified name of the decl for the purpose of displaying it to the user. ...
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
llvm::Optional< SymbolID > getSymbolID(const Decl *D)
Gets the symbol ID for a declaration, if possible.
std::string printNamespaceScope(const DeclContext &DC)
Returns the first enclosing namespace scope starting from DC.
SourceLocation findNameLoc(const clang::Decl *D)
Find the identifier source location of the given D.
bool isSpelledInSourceCode(const Decl *D)
static constexpr llvm::StringLiteral Name
static const TemplateArgumentList * getTemplateSpecializationArgs(const NamedDecl &ND)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool isImplementationDetail(const Decl *D)
Returns true if the declaration is considered implementation detail based on heuristics.
std::array< uint8_t, 20 > SymbolID