clang-tools  8.0.0
Mapper.cpp
Go to the documentation of this file.
1 //===-- Mapper.cpp - ClangDoc Mapper ----------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Mapper.h"
11 #include "BitcodeWriter.h"
12 #include "Serialize.h"
13 #include "clang/AST/Comment.h"
14 #include "clang/Index/USRGeneration.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/Error.h"
17 
18 using clang::comments::FullComment;
19 
20 namespace clang {
21 namespace doc {
22 
23 void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
24  TraverseDecl(Context.getTranslationUnitDecl());
25 }
26 
27 template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
28  // If we're looking a decl not in user files, skip this decl.
29  if (D->getASTContext().getSourceManager().isInSystemHeader(D->getLocation()))
30  return true;
31 
32  // Skip function-internal decls.
33  if (D->getParentFunctionOrMethod())
34  return true;
35 
36  llvm::SmallString<128> USR;
37  // If there is an error generating a USR for the decl, skip this decl.
38  if (index::generateUSRForDecl(D, USR))
39  return true;
40 
41  auto I = serialize::emitInfo(
42  D, getComment(D, D->getASTContext()), getLine(D, D->getASTContext()),
43  getFile(D, D->getASTContext()), CDCtx.PublicOnly);
44 
45  // A null in place of I indicates that the serializer is skipping this decl
46  // for some reason (e.g. we're only reporting public decls).
47  if (I)
48  CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I->USR)),
50  return true;
51 }
52 
53 bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
54  return mapDecl(D);
55 }
56 
57 bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
58 
59 bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
60 
61 bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
62  return mapDecl(D);
63 }
64 
65 bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
66  // Don't visit CXXMethodDecls twice
67  if (dyn_cast<CXXMethodDecl>(D))
68  return true;
69  return mapDecl(D);
70 }
71 
72 comments::FullComment *
73 MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
74  RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
75  // FIXME: Move setAttached to the initial comment parsing.
76  if (Comment) {
77  Comment->setAttached();
78  return Comment->parse(Context, nullptr, D);
79  }
80  return nullptr;
81 }
82 
83 int MapASTVisitor::getLine(const NamedDecl *D,
84  const ASTContext &Context) const {
85  return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
86 }
87 
88 llvm::StringRef MapASTVisitor::getFile(const NamedDecl *D,
89  const ASTContext &Context) const {
90  return Context.getSourceManager()
91  .getPresumedLoc(D->getBeginLoc())
92  .getFilename();
93 }
94 
95 } // namespace doc
96 } // namespace clang
void HandleTranslationUnit(ASTContext &Context) override
Definition: Mapper.cpp:23
bool VisitEnumDecl(const EnumDecl *D)
Definition: Mapper.cpp:59
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Mapper.cpp:53
static std::string serialize(T &I)
Definition: Serialize.cpp:147
bool VisitRecordDecl(const RecordDecl *D)
Definition: Mapper.cpp:57
const Decl * D
Definition: XRefs.cpp:79
bool VisitFunctionDecl(const FunctionDecl *D)
Definition: Mapper.cpp:65
tooling::ExecutionContext * ECtx
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::unique_ptr< Info > emitInfo(const NamespaceDecl *D, const FullComment *FC, int LineNumber, llvm::StringRef File, bool PublicOnly)
Definition: Serialize.cpp:327
bool VisitCXXMethodDecl(const CXXMethodDecl *D)
Definition: Mapper.cpp:61