Skip to content

[clang-doc] Reenable time trace support #141139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang-tools-extra/clang-doc/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "BitcodeReader.h"
#include "llvm/ADT/IndexedMap.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>

Expand Down Expand Up @@ -672,6 +673,7 @@ llvm::Error ClangDocBitcodeReader::readRecord(unsigned ID, T I) {

template <>
llvm::Error ClangDocBitcodeReader::readRecord(unsigned ID, Reference *I) {
llvm::TimeTraceScope("Reducing infos", "readRecord");
Record R;
llvm::StringRef Blob;
llvm::Expected<unsigned> MaybeRecID = Stream.readRecord(ID, R, &Blob);
Expand All @@ -683,6 +685,7 @@ llvm::Error ClangDocBitcodeReader::readRecord(unsigned ID, Reference *I) {
// Read a block of records into a single info.
template <typename T>
llvm::Error ClangDocBitcodeReader::readBlock(unsigned ID, T I) {
llvm::TimeTraceScope("Reducing infos", "readBlock");
if (llvm::Error Err = Stream.EnterSubBlock(ID))
return Err;

Expand Down Expand Up @@ -713,6 +716,7 @@ llvm::Error ClangDocBitcodeReader::readBlock(unsigned ID, T I) {

template <typename T>
llvm::Error ClangDocBitcodeReader::readSubBlock(unsigned ID, T I) {
llvm::TimeTraceScope("Reducing infos", "readSubBlock");
switch (ID) {
// Blocks can only have certain types of sub blocks.
case BI_COMMENT_BLOCK_ID: {
Expand Down Expand Up @@ -819,6 +823,7 @@ llvm::Error ClangDocBitcodeReader::readSubBlock(unsigned ID, T I) {

ClangDocBitcodeReader::Cursor
ClangDocBitcodeReader::skipUntilRecordOrBlock(unsigned &BlockOrRecordID) {
llvm::TimeTraceScope("Reducing infos", "skipUntilRecordOrBlock");
BlockOrRecordID = 0;

while (!Stream.AtEndOfStream()) {
Expand Down Expand Up @@ -880,6 +885,7 @@ llvm::Error ClangDocBitcodeReader::validateStream() {
}

llvm::Error ClangDocBitcodeReader::readBlockInfoBlock() {
llvm::TimeTraceScope("Reducing infos", "readBlockInfoBlock");
Expected<std::optional<llvm::BitstreamBlockInfo>> MaybeBlockInfo =
Stream.ReadBlockInfoBlock();
if (!MaybeBlockInfo)
Expand All @@ -895,6 +901,7 @@ llvm::Error ClangDocBitcodeReader::readBlockInfoBlock() {
template <typename T>
llvm::Expected<std::unique_ptr<Info>>
ClangDocBitcodeReader::createInfo(unsigned ID) {
llvm::TimeTraceScope("Reducing infos", "createInfo");
std::unique_ptr<Info> I = std::make_unique<T>();
if (auto Err = readBlock(ID, static_cast<T *>(I.get())))
return std::move(Err);
Expand All @@ -903,6 +910,7 @@ ClangDocBitcodeReader::createInfo(unsigned ID) {

llvm::Expected<std::unique_ptr<Info>>
ClangDocBitcodeReader::readBlockToInfo(unsigned ID) {
llvm::TimeTraceScope("Reducing infos", "readBlockToInfo");
switch (ID) {
case BI_NAMESPACE_BLOCK_ID:
return createInfo<NamespaceInfo>(ID);
Expand Down
32 changes: 21 additions & 11 deletions clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Mustache.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TimeProfiler.h"

using namespace llvm;
using namespace llvm::json;
Expand Down Expand Up @@ -125,13 +126,18 @@ static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
Error MustacheHTMLGenerator::generateDocs(
StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
const clang::doc::ClangDocContext &CDCtx) {
if (auto Err = setupTemplateFiles(CDCtx))
return Err;
{
llvm::TimeTraceScope TS("Setup Templates");
if (auto Err = setupTemplateFiles(CDCtx))
return Err;
}

// Track which directories we already tried to create.
StringSet<> CreatedDirs;
// Collect all output by file name and create the necessary directories.
StringMap<std::vector<doc::Info *>> FileToInfos;
for (const auto &Group : Infos) {
llvm::TimeTraceScope TS("setup directories");
doc::Info *Info = Group.getValue().get();

SmallString<128> Path;
Expand All @@ -148,15 +154,19 @@ Error MustacheHTMLGenerator::generateDocs(
FileToInfos[Path].push_back(Info);
}

for (const auto &Group : FileToInfos) {
std::error_code FileErr;
raw_fd_ostream InfoOS(Group.getKey(), FileErr, sys::fs::OF_None);
if (FileErr)
return createFileOpenError(Group.getKey(), FileErr);

for (const auto &Info : Group.getValue())
if (Error Err = generateDocForInfo(Info, InfoOS, CDCtx))
return Err;
{
llvm::TimeTraceScope TS("Generate Docs");
for (const auto &Group : FileToInfos) {
llvm::TimeTraceScope TS("Info to Doc");
std::error_code FileErr;
raw_fd_ostream InfoOS(Group.getKey(), FileErr, sys::fs::OF_None);
if (FileErr)
return createFileOpenError(Group.getKey(), FileErr);

for (const auto &Info : Group.getValue())
if (Error Err = generateDocForInfo(Info, InfoOS, CDCtx))
return Err;
}
}
return Error::success();
}
Expand Down
84 changes: 51 additions & 33 deletions clang-tools-extra/clang-doc/Mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//===----------------------------------------------------------------------===//

#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/ADT/StringSet.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/TimeProfiler.h"

namespace clang {
namespace doc {
Expand All @@ -40,48 +40,66 @@ Location MapASTVisitor::getDeclLocation(const NamedDecl *D) const {
}

void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
if (CDCtx.FTimeTrace)
llvm::timeTraceProfilerInitialize(200, "clang-doc");
TraverseDecl(Context.getTranslationUnitDecl());
if (CDCtx.FTimeTrace)
llvm::timeTraceProfilerFinishThread();
}

template <typename T>
bool MapASTVisitor::mapDecl(const T *D, bool IsDefinition) {
// If we're looking a decl not in user files, skip this decl.
if (D->getASTContext().getSourceManager().isInSystemHeader(D->getLocation()))
return true;
llvm::TimeTraceScope TS("Mapping declaration");
{
llvm::TimeTraceScope TS("Preamble");
// If we're looking a decl not in user files, skip this decl.
if (D->getASTContext().getSourceManager().isInSystemHeader(
D->getLocation()))
return true;

// Skip function-internal decls.
if (D->getParentFunctionOrMethod())
return true;
// Skip function-internal decls.
if (D->getParentFunctionOrMethod())
return true;
}

std::pair<std::unique_ptr<Info>, std::unique_ptr<Info>> CP;

llvm::SmallString<128> USR;
// If there is an error generating a USR for the decl, skip this decl.
if (index::generateUSRForDecl(D, USR))
return true;
// Prevent Visiting USR twice
{
llvm::sys::SmartScopedLock<true> Guard(USRVisitedGuard);
StringRef Visited = USR.str();
if (USRVisited.count(Visited) && !isTypedefAnonRecord<T>(D))
llvm::TimeTraceScope TS("emit info from astnode");
llvm::SmallString<128> USR;
// If there is an error generating a USR for the decl, skip this decl.
if (index::generateUSRForDecl(D, USR))
return true;
// We considered a USR to be visited only when its defined
if (IsDefinition)
USRVisited.insert(Visited);
// Prevent Visiting USR twice
{
llvm::sys::SmartScopedLock<true> Guard(USRVisitedGuard);
StringRef Visited = USR.str();
if (USRVisited.count(Visited) && !isTypedefAnonRecord<T>(D))
return true;
// We considered a USR to be visited only when its defined
if (IsDefinition)
USRVisited.insert(Visited);
}
bool IsFileInRootDir;
llvm::SmallString<128> File =
getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);
CP = serialize::emitInfo(D, getComment(D, D->getASTContext()),
getDeclLocation(D), CDCtx.PublicOnly);
}

auto &[Child, Parent] = CP;

{
llvm::TimeTraceScope TS("serialized info into bitcode");
// A null in place of a valid Info indicates that the serializer is skipping
// this decl for some reason (e.g. we're only reporting public decls).
if (Child)
CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Child->USR)),
serialize::serialize(Child));
if (Parent)
CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Parent->USR)),
serialize::serialize(Parent));
}
bool IsFileInRootDir;
llvm::SmallString<128> File =
getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);
auto [Child, Parent] =
serialize::emitInfo(D, getComment(D, D->getASTContext()),
getDeclLocation(D), CDCtx.PublicOnly);

// A null in place of a valid Info indicates that the serializer is skipping
// this decl for some reason (e.g. we're only reporting public decls).
if (Child)
CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Child->USR)),
serialize::serialize(Child));
if (Parent)
CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(Parent->USR)),
serialize::serialize(Parent));
return true;
}

Expand Down
6 changes: 4 additions & 2 deletions clang-tools-extra/clang-doc/Representation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,11 @@ ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
StringRef OutDirectory, StringRef SourceRoot,
StringRef RepositoryUrl,
StringRef RepositoryLinePrefix, StringRef Base,
std::vector<std::string> UserStylesheets)
std::vector<std::string> UserStylesheets,
bool FTimeTrace)
: ECtx(ECtx), ProjectName(ProjectName), PublicOnly(PublicOnly),
OutDirectory(OutDirectory), UserStylesheets(UserStylesheets), Base(Base) {
FTimeTrace(FTimeTrace), OutDirectory(OutDirectory),
UserStylesheets(UserStylesheets), Base(Base) {
llvm::SmallString<128> SourceRootDir(SourceRoot);
if (SourceRoot.empty())
// If no SourceRoot was provided the current path is used as the default
Expand Down
5 changes: 4 additions & 1 deletion clang-tools-extra/clang-doc/Representation.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,13 @@ struct ClangDocContext {
ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName,
bool PublicOnly, StringRef OutDirectory, StringRef SourceRoot,
StringRef RepositoryUrl, StringRef RepositoryCodeLinePrefix,
StringRef Base, std::vector<std::string> UserStylesheets);
StringRef Base, std::vector<std::string> UserStylesheets,
bool FTimeTrace = false);
tooling::ExecutionContext *ECtx;
std::string ProjectName; // Name of project clang-doc is documenting.
bool PublicOnly; // Indicates if only public declarations are documented.
bool FTimeTrace; // Indicates if ftime trace is turned on
int Granularity; // Granularity of ftime trace
std::string OutDirectory; // Directory for outputting generated files.
std::string SourceRoot; // Directory where processed files are stored. Links
// to definition locations will only be generated if
Expand Down
Loading