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 if (CDCtx.FTimeTrace)
47 llvm::timeTraceProfilerFinishThread();
48}
49
50template <typename T>
51bool MapASTVisitor::mapDecl(const T *D, bool IsDefinition) {
52 llvm::TimeTraceScope TS("Mapping declaration");
53 {
54 llvm::TimeTraceScope TS("Preamble");
55 // If we're looking a decl not in user files, skip this decl.
56 if (D->getASTContext().getSourceManager().isInSystemHeader(
57 D->getLocation()))
58 return true;
59
60 // Skip function-internal decls.
61 if (D->getParentFunctionOrMethod())
62 return true;
63 }
64
65 std::pair<OwnedPtr<Info>, OwnedPtr<Info>> CP;
66
67 {
68 llvm::TimeTraceScope TS("emit info from astnode");
69 llvm::SmallString<128> USR;
70 // If there is an error generating a USR for the decl, skip this decl.
71 if (index::generateUSRForDecl(D, USR))
72 return true;
73 // Prevent Visiting USR twice
74 {
75 llvm::sys::SmartScopedLock<true> Guard(USRVisitedGuard);
76 StringRef Visited = USR.str();
77 if (USRVisited.count(Visited) && !isTypedefAnonRecord<T>(D))
78 return true;
79 // We considered a USR to be visited only when its defined
80 if (IsDefinition)
81 USRVisited.insert(Visited);
82 }
83 bool IsFileInRootDir;
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()),
88 getDeclLocation(D), CDCtx.PublicOnly);
89 }
90
91 auto &[Child, Parent] = CP;
92
93 {
94 llvm::TimeTraceScope TS("serialized info into bitcode");
95 // A null in place of a valid Info indicates that the serializer is skipping
96 // this decl for some reason (e.g. we're only reporting public decls).
97 if (Child)
98 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Child->USR)),
99 serialize::serialize(Child, CDCtx.Diags));
100 if (Parent)
101 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Parent->USR)),
102 serialize::serialize(Parent, CDCtx.Diags));
103 }
104 return true;
105}
106
107bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
108 return mapDecl(D, /*isDefinition=*/true);
109}
110
111bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) {
112 return mapDecl(D, D->isThisDeclarationADefinition());
113}
114
115bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) {
116 return mapDecl(D, D->isThisDeclarationADefinition());
117}
118
119bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
120 return mapDecl(D, D->isThisDeclarationADefinition());
121}
122
123bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
124 // Don't visit CXXMethodDecls twice
125 if (isa<CXXMethodDecl>(D))
126 return true;
127 return mapDecl(D, D->isThisDeclarationADefinition());
128}
129
130bool MapASTVisitor::VisitTypedefDecl(const TypedefDecl *D) {
131 return mapDecl(D, /*isDefinition=*/true);
132}
133
134bool MapASTVisitor::VisitTypeAliasDecl(const TypeAliasDecl *D) {
135 return mapDecl(D, /*isDefinition=*/true);
136}
137
138bool MapASTVisitor::VisitConceptDecl(const ConceptDecl *D) {
139 return mapDecl(D, true);
140}
141
142bool MapASTVisitor::VisitVarDecl(const VarDecl *D) {
143 if (D->isCXXClassMember())
144 return true;
145 return mapDecl(D, D->isThisDeclarationADefinition());
146}
147
148comments::FullComment *
149MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
150 RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
151 // FIXME: Move setAttached to the initial comment parsing.
152 if (Comment) {
153 Comment->setAttached();
154 return Comment->parse(Context, nullptr, D);
155 }
156 return nullptr;
157}
158
159int MapASTVisitor::getLine(const NamedDecl *D,
160 const ASTContext &Context) const {
161 return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
162}
163
164llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
165 const ASTContext &Context,
166 llvm::StringRef RootDir,
167 bool &IsFileInRootDir) const {
168 llvm::SmallString<128> File(Context.getSourceManager()
169 .getPresumedLoc(D->getBeginLoc())
170 .getFilename());
171 IsFileInRootDir = false;
172 if (RootDir.empty() || !File.starts_with(RootDir))
173 return File;
174 IsFileInRootDir = true;
175 llvm::SmallString<128> Prefix(RootDir);
176 // replace_path_prefix removes the exact prefix provided. The result of
177 // calling that function on ("A/B/C.c", "A/B", "") would be "/C.c", which
178 // starts with a / that is not needed. This is why we fix Prefix so it always
179 // ends with a / and the result has the desired format.
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, "");
183 return File;
184}
185
186} // namespace doc
187} // namespace clang
void HandleTranslationUnit(ASTContext &Context) override
Definition Mapper.cpp:42
bool VisitEnumDecl(const EnumDecl *D)
Definition Mapper.cpp:115
bool VisitTypedefDecl(const TypedefDecl *D)
Definition Mapper.cpp:130
bool VisitCXXMethodDecl(const CXXMethodDecl *D)
Definition Mapper.cpp:119
bool VisitTypeAliasDecl(const TypeAliasDecl *D)
Definition Mapper.cpp:134
bool VisitVarDecl(const VarDecl *D)
Definition Mapper.cpp:142
bool VisitRecordDecl(const RecordDecl *D)
Definition Mapper.cpp:111
bool VisitFunctionDecl(const FunctionDecl *D)
Definition Mapper.cpp:123
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition Mapper.cpp:107
bool VisitConceptDecl(const ConceptDecl *D)
Definition Mapper.cpp:138
static std::string serialize(T &I, DiagnosticsEngine &Diags)
static llvm::sys::SmartMutex< true > USRVisitedGuard
Definition Mapper.cpp:22
static bool isTypedefAnonRecord(const T *D)
Definition Mapper.cpp:24
std::unique_ptr< T > OwnedPtr
static llvm::StringSet USRVisited
Definition Mapper.cpp:21
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//