clang-tools  9.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 
40  auto I = serialize::emitInfo(
41  D, getComment(D, D->getASTContext()), getLine(D, D->getASTContext()),
42  getFile(D, D->getASTContext()), CDCtx.PublicOnly);
43 
44  // A null in place of I indicates that the serializer is skipping this decl
45  // for some reason (e.g. we're only reporting public decls).
46  if (I.first)
47  CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.first->USR)),
48  serialize::serialize(I.first));
49  if (I.second)
50  CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.second->USR)),
51  serialize::serialize(I.second));
52  return true;
53 }
54 
55 bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
56  return mapDecl(D);
57 }
58 
59 bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
60 
61 bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
62 
63 bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
64  return mapDecl(D);
65 }
66 
67 bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
68  // Don't visit CXXMethodDecls twice
69  if (dyn_cast<CXXMethodDecl>(D))
70  return true;
71  return mapDecl(D);
72 }
73 
74 comments::FullComment *
75 MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
76  RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
77  // FIXME: Move setAttached to the initial comment parsing.
78  if (Comment) {
79  Comment->setAttached();
80  return Comment->parse(Context, nullptr, D);
81  }
82  return nullptr;
83 }
84 
85 int MapASTVisitor::getLine(const NamedDecl *D,
86  const ASTContext &Context) const {
87  return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
88 }
89 
90 llvm::StringRef MapASTVisitor::getFile(const NamedDecl *D,
91  const ASTContext &Context) const {
92  return Context.getSourceManager()
93  .getPresumedLoc(D->getBeginLoc())
94  .getFilename();
95 }
96 
97 } // namespace doc
98 } // namespace clang
void HandleTranslationUnit(ASTContext &Context) override
Definition: Mapper.cpp:22
bool VisitEnumDecl(const EnumDecl *D)
Definition: Mapper.cpp:61
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Mapper.cpp:55
static std::string serialize(T &I)
Definition: Serialize.cpp:183
bool VisitRecordDecl(const RecordDecl *D)
Definition: Mapper.cpp:59
std::pair< std::unique_ptr< Info >, std::unique_ptr< Info > > emitInfo(const NamespaceDecl *D, const FullComment *FC, int LineNumber, llvm::StringRef File, bool PublicOnly)
Definition: Serialize.cpp:379
const Decl * D
Definition: XRefs.cpp:868
bool VisitFunctionDecl(const FunctionDecl *D)
Definition: Mapper.cpp:67
tooling::ExecutionContext * ECtx
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool VisitCXXMethodDecl(const CXXMethodDecl *D)
Definition: Mapper.cpp:63