clang-tools  7.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 
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  llvm::SmallString<128> USR;
32  // If there is an error generating a USR for the decl, skip this decl.
33  if (index::generateUSRForDecl(D, USR))
34  return true;
35 
36  std::string info = serialize::emitInfo(
37  D, getComment(D, D->getASTContext()), getLine(D, D->getASTContext()),
38  getFile(D, D->getASTContext()), CDCtx.PublicOnly);
39 
40  if (info != "")
41  CDCtx.ECtx->reportResult(
42  llvm::toHex(llvm::toStringRef(serialize::hashUSR(USR))), info);
43 
44  return true;
45 }
46 
47 bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
48  return mapDecl(D);
49 }
50 
51 bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
52 
53 bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
54 
55 bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
56  return mapDecl(D);
57 }
58 
59 bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
60  // Don't visit CXXMethodDecls twice
61  if (dyn_cast<CXXMethodDecl>(D))
62  return true;
63  return mapDecl(D);
64 }
65 
66 comments::FullComment *
67 MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
68  RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
69  // FIXME: Move setAttached to the initial comment parsing.
70  if (Comment) {
71  Comment->setAttached();
72  return Comment->parse(Context, nullptr, D);
73  }
74  return nullptr;
75 }
76 
77 int MapASTVisitor::getLine(const NamedDecl *D,
78  const ASTContext &Context) const {
79  return Context.getSourceManager().getPresumedLoc(D->getLocStart()).getLine();
80 }
81 
82 llvm::StringRef MapASTVisitor::getFile(const NamedDecl *D,
83  const ASTContext &Context) const {
84  return Context.getSourceManager()
85  .getPresumedLoc(D->getLocStart())
86  .getFilename();
87 }
88 
89 } // namespace doc
90 } // namespace clang
void HandleTranslationUnit(ASTContext &Context) override
Definition: Mapper.cpp:22
bool VisitEnumDecl(const EnumDecl *D)
Definition: Mapper.cpp:53
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Mapper.cpp:47
bool VisitRecordDecl(const RecordDecl *D)
Definition: Mapper.cpp:51
bool VisitFunctionDecl(const FunctionDecl *D)
Definition: Mapper.cpp:59
tooling::ExecutionContext * ECtx
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::string emitInfo(const NamespaceDecl *D, const FullComment *FC, int LineNumber, llvm::StringRef File, bool PublicOnly)
Definition: Serialize.cpp:309
SymbolID hashUSR(llvm::StringRef USR)
Definition: Serialize.cpp:24
bool VisitCXXMethodDecl(const CXXMethodDecl *D)
Definition: Mapper.cpp:55