Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 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 | |
Julie Hockett | a9cb2dd | 2018-08-02 18:01:37 +0000 | [diff] [blame] | 36 | std::string info = serialize::emitInfo( |
Julie Hockett | eb50a2e | 2018-07-20 18:49:55 +0000 | [diff] [blame] | 37 | D, getComment(D, D->getASTContext()), getLine(D, D->getASTContext()), |
| 38 | getFile(D, D->getASTContext()), CDCtx.PublicOnly); |
| 39 | |
Julie Hockett | a9cb2dd | 2018-08-02 18:01:37 +0000 | [diff] [blame] | 40 | if (info != "") |
| 41 | CDCtx.ECtx->reportResult( |
| 42 | llvm::toHex(llvm::toStringRef(serialize::hashUSR(USR))), info); |
| 43 | |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 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 |