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());
50 llvm::timeTraceProfilerFinishThread();
54bool MapASTVisitor::mapDecl(
const T *D,
bool IsDefinition) {
55 llvm::TimeTraceScope TS(
"Mapping declaration");
57 llvm::TimeTraceScope TS(
"Preamble");
59 if (D->getASTContext().getSourceManager().isInSystemHeader(
64 if (D->getParentFunctionOrMethod())
68 std::pair<Info *, Info *> CP;
71 llvm::TimeTraceScope TS(
"emit info from astnode");
72 llvm::SmallString<128> USR;
74 if (index::generateUSRForDecl(D, USR))
79 StringRef Visited = USR.str();
87 llvm::SmallString<128> File =
88 getFile(D, D->getASTContext(), CDCtx.
SourceRoot, IsFileInRootDir);
89 serialize::Serializer Serializer;
90 CP = Serializer.emitInfo(D, getComment(D, D->getASTContext()),
94 auto &[Child, Parent] = CP;
97 llvm::TimeTraceScope TS(
"serialized info into bitcode");
101 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Child->USR)),
104 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Parent->USR)),
111 return mapDecl(D,
true);
115 return mapDecl(D, D->isThisDeclarationADefinition());
119 return mapDecl(D, D->isThisDeclarationADefinition());
123 return mapDecl(D, D->isThisDeclarationADefinition());
128 if (isa<CXXMethodDecl>(D))
130 return mapDecl(D, D->isThisDeclarationADefinition());
134 return mapDecl(D,
true);
138 return mapDecl(D,
true);
142 return mapDecl(D,
true);
146 if (D->isCXXClassMember())
148 return mapDecl(D, D->isThisDeclarationADefinition());
151comments::FullComment *
152MapASTVisitor::getComment(
const NamedDecl *D,
const ASTContext &
Context)
const {
153 RawComment *Comment =
Context.getRawCommentNoCache(D);
156 Comment->setAttached();
157 return Comment->parse(
Context,
nullptr, D);
162int MapASTVisitor::getLine(
const NamedDecl *D,
163 const ASTContext &Context)
const {
164 return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
167llvm::SmallString<128> MapASTVisitor::getFile(
const NamedDecl *D,
169 llvm::StringRef RootDir,
170 bool &IsFileInRootDir)
const {
171 llvm::SmallString<128>
File(Context.getSourceManager()
172 .getPresumedLoc(
D->getBeginLoc())
174 IsFileInRootDir =
false;
175 if (RootDir.empty() || !
File.starts_with(RootDir))
177 IsFileInRootDir =
true;
178 llvm::SmallString<128> Prefix(RootDir);
183 if (!llvm::sys::path::is_separator(Prefix.back()))
184 Prefix += llvm::sys::path::get_separator();
185 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(const T &I, DiagnosticsEngine &Diags)
static llvm::sys::SmartMutex< true > USRVisitedGuard
static bool isTypedefAnonRecord(const T *D)
thread_local llvm::BumpPtrAllocator TransientArena
static llvm::StringSet USRVisited
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//