#include "Mapper.h"
#include "BitcodeWriter.h"
#include "Serialize.h"
#include "clang/AST/Comment.h"
#include "clang/Index/USRGeneration.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Error.h"
namespace clang {
namespace doc {
void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
TraverseDecl(Context.getTranslationUnitDecl());
}
template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
if (D->getASTContext().getSourceManager().isInSystemHeader(D->getLocation()))
return true;
if (D->getParentFunctionOrMethod())
return true;
llvm::SmallString<128> USR;
if (index::generateUSRForDecl(D, USR))
return true;
bool IsFileInRootDir;
llvm::SmallString<128> File =
getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);
auto I = serialize::emitInfo(D, getComment(D, D->getASTContext()),
getLine(D, D->getASTContext()), File,
IsFileInRootDir, CDCtx.PublicOnly);
if (I.first)
CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.first->USR)),
serialize::serialize(I.first));
if (I.second)
CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.second->USR)),
serialize::serialize(I.second));
return true;
}
bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
return mapDecl(D);
}
bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
return mapDecl(D);
}
bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
if (isa<CXXMethodDecl>(D))
return true;
return mapDecl(D);
}
bool MapASTVisitor::VisitTypedefDecl(const TypedefDecl *D) {
return mapDecl(D);
}
bool MapASTVisitor::VisitTypeAliasDecl(const TypeAliasDecl *D) {
return mapDecl(D);
}
comments::FullComment *
MapASTVisitor::getComment(const NamedDecl *D, const ASTContext &Context) const {
RawComment *Comment = Context.getRawCommentForDeclNoCache(D);
if (Comment) {
Comment->setAttached();
return Comment->parse(Context, nullptr, D);
}
return nullptr;
}
int MapASTVisitor::getLine(const NamedDecl *D,
const ASTContext &Context) const {
return Context.getSourceManager().getPresumedLoc(D->getBeginLoc()).getLine();
}
llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
const ASTContext &Context,
llvm::StringRef RootDir,
bool &IsFileInRootDir) const {
llvm::SmallString<128> File(Context.getSourceManager()
.getPresumedLoc(D->getBeginLoc())
.getFilename());
IsFileInRootDir = false;
if (RootDir.empty() || !File.starts_with(RootDir))
return File;
IsFileInRootDir = true;
llvm::SmallString<128> Prefix(RootDir);
if (!llvm::sys::path::is_separator(Prefix.back()))
Prefix += llvm::sys::path::get_separator();
llvm::sys::path::replace_path_prefix(File, Prefix, "");
return File;
}
}
}