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" |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Error.h" |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 17 | |
| 18 | using clang::comments::FullComment; |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace doc { |
| 22 | |
| 23 | void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) { |
| 24 | TraverseDecl(Context.getTranslationUnitDecl()); |
| 25 | } |
| 26 | |
| 27 | template <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 Hockett | 4955422 | 2018-08-13 18:05:50 +0000 | [diff] [blame] | 32 | // Skip function-internal decls. |
Julie Hockett | c84155f | 2018-08-14 15:38:59 +0000 | [diff] [blame] | 33 | if (D->getParentFunctionOrMethod()) |
Julie Hockett | 4955422 | 2018-08-13 18:05:50 +0000 | [diff] [blame] | 34 | return true; |
| 35 | |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 36 | 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 Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 41 | auto I = serialize::emitInfo( |
Julie Hockett | eb50a2e | 2018-07-20 18:49:55 +0000 | [diff] [blame] | 42 | D, getComment(D, D->getASTContext()), getLine(D, D->getASTContext()), |
| 43 | getFile(D, D->getASTContext()), CDCtx.PublicOnly); |
| 44 | |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 45 | // 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 Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) { |
| 54 | return mapDecl(D); |
| 55 | } |
| 56 | |
| 57 | bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); } |
| 58 | |
| 59 | bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); } |
| 60 | |
| 61 | bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) { |
| 62 | return mapDecl(D); |
| 63 | } |
| 64 | |
| 65 | bool 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 | |
| 72 | comments::FullComment * |
| 73 | MapASTVisitor::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 | |
| 83 | int MapASTVisitor::getLine(const NamedDecl *D, |
| 84 | const ASTContext &Context) const { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 85 | return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine(); |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | llvm::StringRef MapASTVisitor::getFile(const NamedDecl *D, |
| 89 | const ASTContext &Context) const { |
| 90 | return Context.getSourceManager() |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 91 | .getPresumedLoc(D->getBeginLoc()) |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 92 | .getFilename(); |
| 93 | } |
| 94 | |
| 95 | } // namespace doc |
| 96 | } // namespace clang |