blob: fb0b42af3ee5ad246ddfebd86d4294adaba35c5d [file] [log] [blame]
Julie Hockette975a472018-03-22 23:34:46 +00001//===-- 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
17using clang::comments::FullComment;
18
19namespace clang {
20namespace doc {
21
22void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
23 TraverseDecl(Context.getTranslationUnitDecl());
24}
25
26template <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
Julie Hocketta9cb2dd2018-08-02 18:01:37 +000036 std::string info = serialize::emitInfo(
Julie Hocketteb50a2e2018-07-20 18:49:55 +000037 D, getComment(D, D->getASTContext()), getLine(D, D->getASTContext()),
38 getFile(D, D->getASTContext()), CDCtx.PublicOnly);
39
Julie Hocketta9cb2dd2018-08-02 18:01:37 +000040 if (info != "")
41 CDCtx.ECtx->reportResult(
42 llvm::toHex(llvm::toStringRef(serialize::hashUSR(USR))), info);
43
Julie Hockette975a472018-03-22 23:34:46 +000044 return true;
45}
46
47bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
48 return mapDecl(D);
49}
50
51bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
52
53bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
54
55bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
56 return mapDecl(D);
57}
58
59bool 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
66comments::FullComment *
67MapASTVisitor::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
77int MapASTVisitor::getLine(const NamedDecl *D,
78 const ASTContext &Context) const {
79 return Context.getSourceManager().getPresumedLoc(D->getLocStart()).getLine();
80}
81
82llvm::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