clang-tools 20.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 "BitcodeWriter.h"
11#include "Serialize.h"
12#include "clang/AST/Comment.h"
13#include "clang/Index/USRGeneration.h"
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/ADT/StringSet.h"
16#include "llvm/Support/Mutex.h"
17
18namespace clang {
19namespace doc {
20
21static llvm::StringSet<> USRVisited;
22static llvm::sys::Mutex USRVisitedGuard;
23
24template <typename T> bool isTypedefAnonRecord(const T *D) {
25 if (const auto *C = dyn_cast<CXXRecordDecl>(D)) {
26 return C->getTypedefNameForAnonDecl();
27 }
28 return false;
29}
30
31void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
32 TraverseDecl(Context.getTranslationUnitDecl());
33}
34
35template <typename T>
36bool MapASTVisitor::mapDecl(const T *D, bool IsDefinition) {
37 // If we're looking a decl not in user files, skip this decl.
38 if (D->getASTContext().getSourceManager().isInSystemHeader(D->getLocation()))
39 return true;
40
41 // Skip function-internal decls.
42 if (D->getParentFunctionOrMethod())
43 return true;
44
45 llvm::SmallString<128> USR;
46 // If there is an error generating a USR for the decl, skip this decl.
47 if (index::generateUSRForDecl(D, USR))
48 return true;
49 // Prevent Visiting USR twice
50 {
51 std::lock_guard<llvm::sys::Mutex> Guard(USRVisitedGuard);
52 StringRef Visited = USR.str();
53 if (USRVisited.count(Visited) && !isTypedefAnonRecord<T>(D))
54 return true;
55 // We considered a USR to be visited only when its defined
56 if (IsDefinition)
57 USRVisited.insert(Visited);
58 }
59 bool IsFileInRootDir;
60 llvm::SmallString<128> File =
61 getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);
62 auto I = serialize::emitInfo(D, getComment(D, D->getASTContext()),
63 getLine(D, D->getASTContext()), File,
64 IsFileInRootDir, CDCtx.PublicOnly);
65
66 // A null in place of I indicates that the serializer is skipping this decl
67 // for some reason (e.g. we're only reporting public decls).
68 if (I.first)
69 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.first->USR)),
70 serialize::serialize(I.first));
71 if (I.second)
72 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.second->USR)),
73 serialize::serialize(I.second));
74 return true;
75}
76
77bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
78 return mapDecl(D, /*isDefinition=*/true);
79}
80
81bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) {
82 return mapDecl(D, D->isThisDeclarationADefinition());
83}
84
85bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) {
86 return mapDecl(D, D->isThisDeclarationADefinition());
87}
88
89bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
90 return mapDecl(D, D->isThisDeclarationADefinition());
91}
92
93bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
94 // Don't visit CXXMethodDecls twice
95 if (isa<CXXMethodDecl>(D))
96 return true;
97 return mapDecl(D, D->isThisDeclarationADefinition());
98}
99
100bool MapASTVisitor::VisitTypedefDecl(const TypedefDecl *D) {
101 return mapDecl(D, /*isDefinition=*/true);
102}
103
104bool MapASTVisitor::VisitTypeAliasDecl(const TypeAliasDecl *D) {
105 return mapDecl(D, /*isDefinition=*/true);
106}
107
108comments::FullComment *
109MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
110 RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
111 // FIXME: Move setAttached to the initial comment parsing.
112 if (Comment) {
113 Comment->setAttached();
114 return Comment->parse(Context, nullptr, D);
115 }
116 return nullptr;
117}
118
119int MapASTVisitor::getLine(const NamedDecl *D,
120 const ASTContext &Context) const {
121 return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
122}
123
124llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
125 const ASTContext &Context,
126 llvm::StringRef RootDir,
127 bool &IsFileInRootDir) const {
128 llvm::SmallString<128> File(Context.getSourceManager()
129 .getPresumedLoc(D->getBeginLoc())
130 .getFilename());
131 IsFileInRootDir = false;
132 if (RootDir.empty() || !File.starts_with(RootDir))
133 return File;
134 IsFileInRootDir = true;
135 llvm::SmallString<128> Prefix(RootDir);
136 // replace_path_prefix removes the exact prefix provided. The result of
137 // calling that function on ("A/B/C.c", "A/B", "") would be "/C.c", which
138 // starts with a / that is not needed. This is why we fix Prefix so it always
139 // ends with a / and the result has the desired format.
140 if (!llvm::sys::path::is_separator(Prefix.back()))
141 Prefix += llvm::sys::path::get_separator();
142 llvm::sys::path::replace_path_prefix(File, Prefix, "");
143 return File;
144}
145
146} // namespace doc
147} // namespace clang
const Criteria C
std::string USR
void HandleTranslationUnit(ASTContext &Context) override
Definition: Mapper.cpp:31
bool VisitEnumDecl(const EnumDecl *D)
Definition: Mapper.cpp:85
bool VisitTypedefDecl(const TypedefDecl *D)
Definition: Mapper.cpp:100
bool VisitCXXMethodDecl(const CXXMethodDecl *D)
Definition: Mapper.cpp:89
bool VisitTypeAliasDecl(const TypeAliasDecl *D)
Definition: Mapper.cpp:104
bool VisitRecordDecl(const RecordDecl *D)
Definition: Mapper.cpp:81
bool VisitFunctionDecl(const FunctionDecl *D)
Definition: Mapper.cpp:93
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Mapper.cpp:77
std::pair< std::unique_ptr< Info >, std::unique_ptr< Info > > emitInfo(const NamespaceDecl *D, const FullComment *FC, int LineNumber, llvm::StringRef File, bool IsFileInRootDir, bool PublicOnly)
Definition: Serialize.cpp:642
static std::string serialize(T &I)
Definition: Serialize.cpp:192
static llvm::sys::Mutex USRVisitedGuard
Definition: Mapper.cpp:22
bool isTypedefAnonRecord(const T *D)
Definition: Mapper.cpp:24
static llvm::StringSet USRVisited
Definition: Mapper.cpp:21
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
tooling::ExecutionContext * ECtx