11#include "clang/AST/Comment.h"
12#include "clang/UnifiedSymbolResolution/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())
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);
86 serialize::Serializer Serializer;
87 CP = Serializer.emitInfo(D, getComment(D, D->getASTContext()),
91 auto &[Child, Parent] = CP;
94 llvm::TimeTraceScope TS(
"serialized info into bitcode");
98 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Child->USR)),
101 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Parent->USR)),
108 return mapDecl(D,
true);
112 return mapDecl(D, D->isThisDeclarationADefinition());
116 return mapDecl(D, D->isThisDeclarationADefinition());
120 return mapDecl(D, D->isThisDeclarationADefinition());
125 if (isa<CXXMethodDecl>(D))
127 return mapDecl(D, D->isThisDeclarationADefinition());
131 return mapDecl(D,
true);
135 return mapDecl(D,
true);
139 return mapDecl(D,
true);
143 if (D->isCXXClassMember())
145 return mapDecl(D, D->isThisDeclarationADefinition());
148comments::FullComment *
149MapASTVisitor::getComment(
const NamedDecl *D,
const ASTContext &
Context)
const {
150 RawComment *Comment =
Context.getRawCommentForDeclNoCache(D);
153 Comment->setAttached();
154 return Comment->parse(
Context,
nullptr, D);
159int MapASTVisitor::getLine(
const NamedDecl *D,
160 const ASTContext &Context)
const {
161 return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
164llvm::SmallString<128> MapASTVisitor::getFile(
const NamedDecl *D,
166 llvm::StringRef RootDir,
167 bool &IsFileInRootDir)
const {
168 llvm::SmallString<128>
File(Context.getSourceManager()
169 .getPresumedLoc(
D->getBeginLoc())
171 IsFileInRootDir =
false;
172 if (RootDir.empty() || !
File.starts_with(RootDir))
174 IsFileInRootDir =
true;
175 llvm::SmallString<128> Prefix(RootDir);
180 if (!llvm::sys::path::is_separator(Prefix.back()))
181 Prefix += llvm::sys::path::get_separator();
182 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)
static std::string serialize(T &I, DiagnosticsEngine &Diags)
static llvm::sys::SmartMutex< true > USRVisitedGuard
static bool isTypedefAnonRecord(const T *D)
std::unique_ptr< T > OwnedPtr
static llvm::StringSet USRVisited
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//