brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.3 KiB · 497b80c Raw
187 lines · cpp
1//===-- Mapper.cpp - ClangDoc Mapper ----------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "Mapper.h"10#include "Serialize.h"11#include "clang/AST/Comment.h"12#include "clang/Index/USRGeneration.h"13#include "llvm/ADT/StringExtras.h"14#include "llvm/ADT/StringSet.h"15#include "llvm/Support/Mutex.h"16#include "llvm/Support/TimeProfiler.h"17 18namespace clang {19namespace doc {20 21static llvm::StringSet<> USRVisited;22static llvm::sys::SmartMutex<true> USRVisitedGuard;23 24template <typename T> static bool isTypedefAnonRecord(const T *D) {25  if (const auto *C = dyn_cast<CXXRecordDecl>(D)) {26    return C->getTypedefNameForAnonDecl();27  }28  return false;29}30 31Location MapASTVisitor::getDeclLocation(const NamedDecl *D) const {32  bool IsFileInRootDir;33  llvm::SmallString<128> File =34      getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);35  SourceManager &SM = D->getASTContext().getSourceManager();36  int Start = SM.getPresumedLoc(D->getBeginLoc()).getLine();37  int End = SM.getPresumedLoc(D->getEndLoc()).getLine();38 39  return Location(Start, End, File, IsFileInRootDir);40}41 42void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {43  if (CDCtx.FTimeTrace)44    llvm::timeTraceProfilerInitialize(200, "clang-doc");45  TraverseDecl(Context.getTranslationUnitDecl());46  if (CDCtx.FTimeTrace)47    llvm::timeTraceProfilerFinishThread();48}49 50template <typename T>51bool MapASTVisitor::mapDecl(const T *D, bool IsDefinition) {52  llvm::TimeTraceScope TS("Mapping declaration");53  {54    llvm::TimeTraceScope TS("Preamble");55    // If we're looking a decl not in user files, skip this decl.56    if (D->getASTContext().getSourceManager().isInSystemHeader(57            D->getLocation()))58      return true;59 60    // Skip function-internal decls.61    if (D->getParentFunctionOrMethod())62      return true;63  }64 65  std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>> CP;66 67  {68    llvm::TimeTraceScope TS("emit info from astnode");69    llvm::SmallString<128> USR;70    // If there is an error generating a USR for the decl, skip this decl.71    if (index::generateUSRForDecl(D, USR))72      return true;73    // Prevent Visiting USR twice74    {75      llvm::sys::SmartScopedLock<true> Guard(USRVisitedGuard);76      StringRef Visited = USR.str();77      if (USRVisited.count(Visited) && !isTypedefAnonRecord<T>(D))78        return true;79      // We considered a USR to be visited only when its defined80      if (IsDefinition)81        USRVisited.insert(Visited);82    }83    bool IsFileInRootDir;84    llvm::SmallString<128> File =85        getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);86    CP = serialize::emitInfo(D, getComment(D, D->getASTContext()),87                             getDeclLocation(D), CDCtx.PublicOnly);88  }89 90  auto &[Child, Parent] = CP;91 92  {93    llvm::TimeTraceScope TS("serialized info into bitcode");94    // A null in place of a valid Info indicates that the serializer is skipping95    // this decl for some reason (e.g. we're only reporting public decls).96    if (Child)97      CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Child->USR)),98                               serialize::serialize(Child));99    if (Parent)100      CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Parent->USR)),101                               serialize::serialize(Parent));102  }103  return true;104}105 106bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {107  return mapDecl(D, /*isDefinition=*/true);108}109 110bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) {111  return mapDecl(D, D->isThisDeclarationADefinition());112}113 114bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) {115  return mapDecl(D, D->isThisDeclarationADefinition());116}117 118bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {119  return mapDecl(D, D->isThisDeclarationADefinition());120}121 122bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {123  // Don't visit CXXMethodDecls twice124  if (isa<CXXMethodDecl>(D))125    return true;126  return mapDecl(D, D->isThisDeclarationADefinition());127}128 129bool MapASTVisitor::VisitTypedefDecl(const TypedefDecl *D) {130  return mapDecl(D, /*isDefinition=*/true);131}132 133bool MapASTVisitor::VisitTypeAliasDecl(const TypeAliasDecl *D) {134  return mapDecl(D, /*isDefinition=*/true);135}136 137bool MapASTVisitor::VisitConceptDecl(const ConceptDecl *D) {138  return mapDecl(D, true);139}140 141bool MapASTVisitor::VisitVarDecl(const VarDecl *D) {142  if (D->isCXXClassMember())143    return true;144  return mapDecl(D, D->isThisDeclarationADefinition());145}146 147comments::FullComment *148MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {149  RawComment *Comment = Context.getRawCommentForDeclNoCache(D);150  // FIXME: Move setAttached to the initial comment parsing.151  if (Comment) {152    Comment->setAttached();153    return Comment->parse(Context, nullptr, D);154  }155  return nullptr;156}157 158int MapASTVisitor::getLine(const NamedDecl *D,159                           const ASTContext &Context) const {160  return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();161}162 163llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,164                                              const ASTContext &Context,165                                              llvm::StringRef RootDir,166                                              bool &IsFileInRootDir) const {167  llvm::SmallString<128> File(Context.getSourceManager()168                                  .getPresumedLoc(D->getBeginLoc())169                                  .getFilename());170  IsFileInRootDir = false;171  if (RootDir.empty() || !File.starts_with(RootDir))172    return File;173  IsFileInRootDir = true;174  llvm::SmallString<128> Prefix(RootDir);175  // replace_path_prefix removes the exact prefix provided. The result of176  // calling that function on ("A/B/C.c", "A/B", "") would be "/C.c", which177  // starts with a / that is not needed. This is why we fix Prefix so it always178  // ends with a / and the result has the desired format.179  if (!llvm::sys::path::is_separator(Prefix.back()))180    Prefix += llvm::sys::path::get_separator();181  llvm::sys::path::replace_path_prefix(File, Prefix, "");182  return File;183}184 185} // namespace doc186} // namespace clang187