11#include "clang/AST/Comment.h"
12#include "clang/Index/USRGeneration.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/ADT/StringSet.h"
15#include "llvm/Support/Mutex.h"
16#include "llvm/Support/TimeProfiler.h"
25 if (
const auto *C = dyn_cast<CXXRecordDecl>(D)) {
26 return C->getTypedefNameForAnonDecl();
31Location MapASTVisitor::getDeclLocation(
const NamedDecl *D)
const {
33 llvm::SmallString<128> File =
34 getFile(D, D->getASTContext(), CDCtx.
SourceRoot, IsFileInRootDir);
35 SourceManager &SM = D->getASTContext().getSourceManager();
36 int Start = SM.getPresumedLoc(D->getBeginLoc()).getLine();
37 int End = SM.getPresumedLoc(D->getEndLoc()).getLine();
39 return Location(Start, End, File, IsFileInRootDir);
44 llvm::timeTraceProfilerInitialize(200,
"clang-doc");
45 TraverseDecl(Context.getTranslationUnitDecl());
47 llvm::timeTraceProfilerFinishThread();
51bool MapASTVisitor::mapDecl(
const T *D,
bool IsDefinition) {
52 llvm::TimeTraceScope TS(
"Mapping declaration");
54 llvm::TimeTraceScope TS(
"Preamble");
56 if (D->getASTContext().getSourceManager().isInSystemHeader(
61 if (D->getParentFunctionOrMethod())
65 std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>> CP;
68 llvm::TimeTraceScope TS(
"emit info from astnode");
69 llvm::SmallString<128> USR;
71 if (index::generateUSRForDecl(D, USR))
76 StringRef Visited = USR.str();
84 llvm::SmallString<128> File =
85 getFile(D, D->getASTContext(), CDCtx.
SourceRoot, IsFileInRootDir);
90 auto &[Child, Parent] = CP;
93 llvm::TimeTraceScope TS(
"serialized info into bitcode");
97 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Child->USR)),
100 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Parent->USR)),
107 return mapDecl(D,
true);
111 return mapDecl(D, D->isThisDeclarationADefinition());
115 return mapDecl(D, D->isThisDeclarationADefinition());
119 return mapDecl(D, D->isThisDeclarationADefinition());
124 if (isa<CXXMethodDecl>(D))
126 return mapDecl(D, D->isThisDeclarationADefinition());
130 return mapDecl(D,
true);
134 return mapDecl(D,
true);
138 return mapDecl(D,
true);
142 if (D->isCXXClassMember())
144 return mapDecl(D, D->isThisDeclarationADefinition());
147comments::FullComment *
148MapASTVisitor::getComment(
const NamedDecl *D,
const ASTContext &Context)
const {
149 RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
152 Comment->setAttached();
153 return Comment->parse(Context,
nullptr, D);
158int MapASTVisitor::getLine(
const NamedDecl *D,
159 const ASTContext &Context)
const {
160 return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
163llvm::SmallString<128> MapASTVisitor::getFile(
const NamedDecl *D,
164 const ASTContext &Context,
165 llvm::StringRef RootDir,
166 bool &IsFileInRootDir)
const {
167 llvm::SmallString<128>
File(Context.getSourceManager()
168 .getPresumedLoc(
D->getBeginLoc())
170 IsFileInRootDir =
false;
171 if (RootDir.empty() || !
File.starts_with(RootDir))
173 IsFileInRootDir =
true;
174 llvm::SmallString<128> Prefix(RootDir);
179 if (!llvm::sys::path::is_separator(Prefix.back()))
180 Prefix += llvm::sys::path::get_separator();
181 llvm::sys::path::replace_path_prefix(File, Prefix,
"");
void HandleTranslationUnit(ASTContext &Context) override
bool VisitEnumDecl(const EnumDecl *D)
bool VisitTypedefDecl(const TypedefDecl *D)
bool VisitCXXMethodDecl(const CXXMethodDecl *D)
bool VisitTypeAliasDecl(const TypeAliasDecl *D)
bool VisitVarDecl(const VarDecl *D)
bool VisitRecordDecl(const RecordDecl *D)
bool VisitFunctionDecl(const FunctionDecl *D)
bool VisitNamespaceDecl(const NamespaceDecl *D)
bool VisitConceptDecl(const ConceptDecl *D)
std::pair< std::unique_ptr< Info >, std::unique_ptr< Info > > emitInfo(const NamespaceDecl *D, const FullComment *FC, Location Loc, bool PublicOnly)
static std::string serialize(T &I)
static llvm::sys::SmartMutex< true > USRVisitedGuard
static bool isTypedefAnonRecord(const T *D)
static llvm::StringSet USRVisited
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//