clang-tools 23.0.0git
Mapper.cpp
Go to the documentation of this file.
1//===-- Mapper.cpp - ClangDoc Mapper ----------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Mapper.h"
10#include "Serialize.h"
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"
17
18namespace clang {
19namespace doc {
20
21static llvm::StringSet<> USRVisited;
22static llvm::sys::SmartMutex<true> USRVisitedGuard;
23
24template <typename T> static bool isTypedefAnonRecord(const T *D) {
25 if (const auto *C = dyn_cast<CXXRecordDecl>(D)) {
26 return C->getTypedefNameForAnonDecl();
27 }
28 return false;
29}
30
31Location MapASTVisitor::getDeclLocation(const NamedDecl *D) const {
32 bool IsFileInRootDir;
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();
38
39 return Location(Start, End, File, IsFileInRootDir);
40}
41
43 if (CDCtx.FTimeTrace)
44 llvm::timeTraceProfilerInitialize(200, "clang-doc");
45 TraverseDecl(Context.getTranslationUnitDecl());
46
47 TransientArena.Reset();
48
49 if (CDCtx.FTimeTrace)
50 llvm::timeTraceProfilerFinishThread();
51}
52
53template <typename T>
54bool MapASTVisitor::mapDecl(const T *D, bool IsDefinition) {
55 llvm::TimeTraceScope TS("Mapping declaration");
56 {
57 llvm::TimeTraceScope TS("Preamble");
58 // If we're looking a decl not in user files, skip this decl.
59 if (D->getASTContext().getSourceManager().isInSystemHeader(
60 D->getLocation()))
61 return true;
62
63 // Skip function-internal decls.
64 if (D->getParentFunctionOrMethod())
65 return true;
66 }
67
68 std::pair<Info *, Info *> CP;
69
70 {
71 llvm::TimeTraceScope TS("emit info from astnode");
72 llvm::SmallString<128> USR;
73 // If there is an error generating a USR for the decl, skip this decl.
74 if (index::generateUSRForDecl(D, USR))
75 return true;
76 // Prevent Visiting USR twice
77 {
78 llvm::sys::SmartScopedLock<true> Guard(USRVisitedGuard);
79 StringRef Visited = USR.str();
80 if (USRVisited.count(Visited) && !isTypedefAnonRecord<T>(D))
81 return true;
82 // We considered a USR to be visited only when its defined
83 if (IsDefinition)
84 USRVisited.insert(Visited);
85 }
86 bool IsFileInRootDir;
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()),
91 getDeclLocation(D), CDCtx.PublicOnly);
92 }
93
94 auto &[Child, Parent] = CP;
95
96 {
97 llvm::TimeTraceScope TS("serialized info into bitcode");
98 // A null in place of a valid Info indicates that the serializer is skipping
99 // this decl for some reason (e.g. we're only reporting public decls).
100 if (Child)
101 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Child->USR)),
102 serialize::serialize(*Child, CDCtx.Diags));
103 if (Parent)
104 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Parent->USR)),
105 serialize::serialize(*Parent, CDCtx.Diags));
106 }
107 return true;
108}
109
110bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
111 return mapDecl(D, /*isDefinition=*/true);
112}
113
114bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) {
115 return mapDecl(D, D->isThisDeclarationADefinition());
116}
117
118bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) {
119 return mapDecl(D, D->isThisDeclarationADefinition());
120}
121
122bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
123 return mapDecl(D, D->isThisDeclarationADefinition());
124}
125
126bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
127 // Don't visit CXXMethodDecls twice
128 if (isa<CXXMethodDecl>(D))
129 return true;
130 return mapDecl(D, D->isThisDeclarationADefinition());
131}
132
133bool MapASTVisitor::VisitTypedefDecl(const TypedefDecl *D) {
134 return mapDecl(D, /*isDefinition=*/true);
135}
136
137bool MapASTVisitor::VisitTypeAliasDecl(const TypeAliasDecl *D) {
138 return mapDecl(D, /*isDefinition=*/true);
139}
140
141bool MapASTVisitor::VisitConceptDecl(const ConceptDecl *D) {
142 return mapDecl(D, true);
143}
144
145bool MapASTVisitor::VisitVarDecl(const VarDecl *D) {
146 if (D->isCXXClassMember())
147 return true;
148 return mapDecl(D, D->isThisDeclarationADefinition());
149}
150
151comments::FullComment *
152MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
153 RawComment *Comment = Context.getRawCommentNoCache(D);
154 // FIXME: Move setAttached to the initial comment parsing.
155 if (Comment) {
156 Comment->setAttached();
157 return Comment->parse(Context, nullptr, D);
158 }
159 return nullptr;
160}
161
162int MapASTVisitor::getLine(const NamedDecl *D,
163 const ASTContext &Context) const {
164 return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
165}
166
167llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
168 const ASTContext &Context,
169 llvm::StringRef RootDir,
170 bool &IsFileInRootDir) const {
171 llvm::SmallString<128> File(Context.getSourceManager()
172 .getPresumedLoc(D->getBeginLoc())
173 .getFilename());
174 IsFileInRootDir = false;
175 if (RootDir.empty() || !File.starts_with(RootDir))
176 return File;
177 IsFileInRootDir = true;
178 llvm::SmallString<128> Prefix(RootDir);
179 // replace_path_prefix removes the exact prefix provided. The result of
180 // calling that function on ("A/B/C.c", "A/B", "") would be "/C.c", which
181 // starts with a / that is not needed. This is why we fix Prefix so it always
182 // ends with a / and the result has the desired format.
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, "");
186 return File;
187}
188
189} // namespace doc
190} // namespace clang
void HandleTranslationUnit(ASTContext &Context) override
Definition Mapper.cpp:42
bool VisitEnumDecl(const EnumDecl *D)
Definition Mapper.cpp:118
bool VisitTypedefDecl(const TypedefDecl *D)
Definition Mapper.cpp:133
bool VisitCXXMethodDecl(const CXXMethodDecl *D)
Definition Mapper.cpp:122
bool VisitTypeAliasDecl(const TypeAliasDecl *D)
Definition Mapper.cpp:137
bool VisitVarDecl(const VarDecl *D)
Definition Mapper.cpp:145
bool VisitRecordDecl(const RecordDecl *D)
Definition Mapper.cpp:114
bool VisitFunctionDecl(const FunctionDecl *D)
Definition Mapper.cpp:126
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition Mapper.cpp:110
bool VisitConceptDecl(const ConceptDecl *D)
Definition Mapper.cpp:141
static std::string serialize(const T &I, DiagnosticsEngine &Diags)
static llvm::sys::SmartMutex< true > USRVisitedGuard
Definition Mapper.cpp:22
static bool isTypedefAnonRecord(const T *D)
Definition Mapper.cpp:24
thread_local llvm::BumpPtrAllocator TransientArena
static llvm::StringSet USRVisited
Definition Mapper.cpp:21
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//