blob: 4456c1ed9b893fa071ac37af57887041a91751bf [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"
Julie Hockett8899c292018-08-02 20:10:17 +000016#include "llvm/Support/Error.h"
Julie Hockette975a472018-03-22 23:34:46 +000017
18using clang::comments::FullComment;
19
20namespace clang {
21namespace doc {
22
23void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
24 TraverseDecl(Context.getTranslationUnitDecl());
25}
26
27template <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 llvm::SmallString<128> USR;
33 // If there is an error generating a USR for the decl, skip this decl.
34 if (index::generateUSRForDecl(D, USR))
35 return true;
36
Julie Hockett8899c292018-08-02 20:10:17 +000037 auto I = serialize::emitInfo(
Julie Hocketteb50a2e2018-07-20 18:49:55 +000038 D, getComment(D, D->getASTContext()), getLine(D, D->getASTContext()),
39 getFile(D, D->getASTContext()), CDCtx.PublicOnly);
40
Julie Hockett8899c292018-08-02 20:10:17 +000041 // A null in place of I indicates that the serializer is skipping this decl
42 // for some reason (e.g. we're only reporting public decls).
43 if (I)
44 CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I->USR)),
45 serialize::serialize(I));
Julie Hockette975a472018-03-22 23:34:46 +000046 return true;
47}
48
49bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
50 return mapDecl(D);
51}
52
53bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
54
55bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
56
57bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
58 return mapDecl(D);
59}
60
61bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
62 // Don't visit CXXMethodDecls twice
63 if (dyn_cast<CXXMethodDecl>(D))
64 return true;
65 return mapDecl(D);
66}
67
68comments::FullComment *
69MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
70 RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
71 // FIXME: Move setAttached to the initial comment parsing.
72 if (Comment) {
73 Comment->setAttached();
74 return Comment->parse(Context, nullptr, D);
75 }
76 return nullptr;
77}
78
79int MapASTVisitor::getLine(const NamedDecl *D,
80 const ASTContext &Context) const {
81 return Context.getSourceManager().getPresumedLoc(D->getLocStart()).getLine();
82}
83
84llvm::StringRef MapASTVisitor::getFile(const NamedDecl *D,
85 const ASTContext &Context) const {
86 return Context.getSourceManager()
87 .getPresumedLoc(D->getLocStart())
88 .getFilename();
89}
90
91} // namespace doc
92} // namespace clang