blob: 0b00bd1f7ae00186d1873b5732bc76bb3919e1f9 [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
Julie Hockett49554222018-08-13 18:05:50 +000032 // Skip function-internal decls.
Julie Hockettc84155f2018-08-14 15:38:59 +000033 if (D->getParentFunctionOrMethod())
Julie Hockett49554222018-08-13 18:05:50 +000034 return true;
35
Julie Hockette975a472018-03-22 23:34:46 +000036 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
Julie Hockett8899c292018-08-02 20:10:17 +000041 auto I = serialize::emitInfo(
Julie Hocketteb50a2e2018-07-20 18:49:55 +000042 D, getComment(D, D->getASTContext()), getLine(D, D->getASTContext()),
43 getFile(D, D->getASTContext()), CDCtx.PublicOnly);
44
Julie Hockett8899c292018-08-02 20:10:17 +000045 // 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)),
49 serialize::serialize(I));
Julie Hockette975a472018-03-22 23:34:46 +000050 return true;
51}
52
53bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
54 return mapDecl(D);
55}
56
57bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
58
59bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
60
61bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
62 return mapDecl(D);
63}
64
65bool 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
72comments::FullComment *
73MapASTVisitor::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
83int MapASTVisitor::getLine(const NamedDecl *D,
84 const ASTContext &Context) const {
Stephen Kelly43465bf2018-08-09 22:42:26 +000085 return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
Julie Hockette975a472018-03-22 23:34:46 +000086}
87
88llvm::StringRef MapASTVisitor::getFile(const NamedDecl *D,
89 const ASTContext &Context) const {
90 return Context.getSourceManager()
Stephen Kelly43465bf2018-08-09 22:42:26 +000091 .getPresumedLoc(D->getBeginLoc())
Julie Hockette975a472018-03-22 23:34:46 +000092 .getFilename();
93}
94
95} // namespace doc
96} // namespace clang