clang-tools 10.0.0

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/Support/Error.h"
16
17 using clang::comments::FullComment;
18
19 namespace clang {
20 namespace doc {
21
22 void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
23 TraverseDecl(Context.getTranslationUnitDecl());
24 }
25
26 template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
27 // If we're looking a decl not in user files, skip this decl.
28 if (D->getASTContext().getSourceManager().isInSystemHeader(D->getLocation()))
29 return true;
30
31 // Skip function-internal decls.
32 if (D->getParentFunctionOrMethod())
33 return true;
34
35 llvm::SmallString<128> USR;
36 // If there is an error generating a USR for the decl, skip this decl.
37 if (index::generateUSRForDecl(D, USR))
38 return true;
39 bool IsFileInRootDir;
40 llvm::SmallString<128> File =
41 getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);
42 auto I = serialize::emitInfo(D, getComment(D, D->getASTContext()),
43 getLine(D, D->getASTContext()), File,
44 IsFileInRootDir, CDCtx.PublicOnly);
45
46 // A null in place of I indicates that the serializer is skipping this decl
47 // for some reason (e.g. we're only reporting public decls).
48 if (I.first)
49 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.first->USR)),
50 serialize::serialize(I.first));
51 if (I.second)
52 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.second->USR)),
53 serialize::serialize(I.second));
54 return true;
55 }
56
57 bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
58 return mapDecl(D);
59 }
60
61 bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
62
63 bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
64
65 bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
66 return mapDecl(D);
67 }
68
69 bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
70 // Don't visit CXXMethodDecls twice
71 if (dyn_cast<CXXMethodDecl>(D))
72 return true;
73 return mapDecl(D);
74 }
75
76 comments::FullComment *
77 MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
78 RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
79 // FIXME: Move setAttached to the initial comment parsing.
80 if (Comment) {
81 Comment->setAttached();
82 return Comment->parse(Context, nullptr, D);
83 }
84 return nullptr;
85 }
86
87 int MapASTVisitor::getLine(const NamedDecl *D,
88 const ASTContext &Context) const {
89 return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
90 }
91
92 llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
93 const ASTContext &Context,
94 llvm::StringRef RootDir,
95 bool &IsFileInRootDir) const {
96 llvm::SmallString<128> File(Context.getSourceManager()
97 .getPresumedLoc(D->getBeginLoc())
98 .getFilename());
99 IsFileInRootDir = false;
100 if (RootDir.empty() || !File.startswith(RootDir))
101 return File;
102 IsFileInRootDir = true;
103 llvm::SmallString<128> Prefix(RootDir);
104 // replace_path_prefix removes the exact prefix provided. The result of
105 // calling that function on ("A/B/C.c", "A/B", "") would be "/C.c", which
106 // starts with a / that is not needed. This is why we fix Prefix so it always
107 // ends with a / and the result has the desired format.
108 if (!llvm::sys::path::is_separator(Prefix.back()))
109 Prefix += llvm::sys::path::get_separator();
110 llvm::sys::path::replace_path_prefix(File, Prefix, "");
111 return File;
112 }
113
114 } // namespace doc
115 } // namespace clang
Mapper.h
clang::doc::ClangDocContext::SourceRoot
std::string SourceRoot
Definition: Representation.h:425
clang::doc::serialize::emitInfo
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:491
clang::doc::MapASTVisitor::HandleTranslationUnit
void HandleTranslationUnit(ASTContext &Context) override
Definition: Mapper.cpp:22
clang::doc::MapASTVisitor::VisitEnumDecl
bool VisitEnumDecl(const EnumDecl *D)
Definition: Mapper.cpp:63
clang::doc::MapASTVisitor::VisitNamespaceDecl
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Mapper.cpp:57
clang::doc::serialize::serialize
static std::string serialize(T &I)
Definition: Serialize.cpp:182
clang::doc::MapASTVisitor::VisitRecordDecl
bool VisitRecordDecl(const RecordDecl *D)
Definition: Mapper.cpp:61
clang::doc::ClangDocContext::PublicOnly
bool PublicOnly
Definition: Representation.h:423
clang::doc::MapASTVisitor::VisitFunctionDecl
bool VisitFunctionDecl(const FunctionDecl *D)
Definition: Mapper.cpp:69
BitcodeWriter.h
clang::doc::ClangDocContext::ECtx
tooling::ExecutionContext * ECtx
Definition: Representation.h:421
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Serialize.h
clang::doc::MapASTVisitor::VisitCXXMethodDecl
bool VisitCXXMethodDecl(const CXXMethodDecl *D)
Definition: Mapper.cpp:65