Skip to content

Commit

Permalink
[clang-doc] fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterChou1 committed Jul 23, 2024
2 parents cbe1802 + 7868c04 commit aa23c92
Show file tree
Hide file tree
Showing 17 changed files with 1,321 additions and 79 deletions.
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 @@ -670,6 +671,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 @@ -681,6 +683,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 @@ -711,6 +714,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 @@ -817,6 +821,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 @@ -878,6 +883,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 @@ -894,6 +900,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 @@ -902,6 +909,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
10 changes: 10 additions & 0 deletions clang-tools-extra/clang-doc/Mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
#include "clang/Index/USRGeneration.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/TimeProfiler.h"

namespace clang {
namespace doc {

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) {
Expand All @@ -30,6 +35,7 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
if (D->getParentFunctionOrMethod())
return true;

llvm::timeTraceProfilerBegin("Mapping declaration", "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))
Expand All @@ -40,7 +46,10 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
auto I = serialize::emitInfo(D, getComment(D, D->getASTContext()),
getLine(D, D->getASTContext()), File,
IsFileInRootDir, CDCtx.PublicOnly);
llvm::timeTraceProfilerEnd();

llvm::timeTraceProfilerBegin("Mapping declaration",
"serialized info into bitcode");
// A null in place of I indicates that the serializer is skipping this decl
// for some reason (e.g. we're only reporting public decls).
if (I.first)
Expand All @@ -49,6 +58,7 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
if (I.second)
CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.second->USR)),
serialize::serialize(I.second));
llvm::timeTraceProfilerEnd();
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 @@ -368,9 +368,11 @@ ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
StringRef ProjectName, bool PublicOnly,
StringRef OutDirectory, StringRef SourceRoot,
StringRef RepositoryUrl,
std::vector<std::string> UserStylesheets)
std::vector<std::string> UserStylesheets,
bool FTimeTrace)
: ECtx(ECtx), ProjectName(ProjectName), PublicOnly(PublicOnly),
OutDirectory(OutDirectory), UserStylesheets(UserStylesheets) {
OutDirectory(OutDirectory), UserStylesheets(UserStylesheets),
FTimeTrace(FTimeTrace) {
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 @@ -482,10 +482,13 @@ struct ClangDocContext {
ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName,
bool PublicOnly, StringRef OutDirectory, StringRef SourceRoot,
StringRef RepositoryUrl,
std::vector<std::string> UserStylesheets);
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
120 changes: 68 additions & 52 deletions clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include <atomic>
#include <mutex>
#include <string>
#include <exception>
#include <typeinfo>
#include <stdexcept>


using namespace clang::ast_matchers;
using namespace clang::tooling;
Expand Down Expand Up @@ -103,6 +100,11 @@ URL of repository that hosts code.
Used for links to definition locations.)"),
llvm::cl::cat(ClangDocCategory));

static llvm::cl::opt<bool> FTimeTrace("ftime-trace", llvm::cl::desc(R"(
Turn on time profiler. Generates clang-doc-tracing.json)"),
llvm::cl::init(false),
llvm::cl::cat(ClangDocCategory));

enum OutputFormatTy {
md,
yaml,
Expand Down Expand Up @@ -209,19 +211,6 @@ llvm::Error getHtmlAssetFiles(const char *Argv0,
return getDefaultAssetFiles(Argv0, CDCtx);
}

void handle_eptr(std::exception_ptr eptr) // passing by value is OK
{
try
{
if (eptr)
std::rethrow_exception(eptr);
}
catch(const std::exception& e)
{
llvm::outs() << "Caught exception: '" << e.what() << "'\n";
}
}

int main(int argc, const char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
std::error_code OK;
Expand All @@ -246,9 +235,14 @@ Example usage for a project using a compile commands database:
return 1;
}

// turns on ftime trace profiling
if (FTimeTrace)
llvm::timeTraceProfilerInitialize(200, "clang-doc");

llvm::TimeTraceScope("main");

// Fail early if an invalid format was provided.
std::string Format = getFormatString();
llvm::outs() << "Unoptimized\n";
llvm::outs() << "Emiting docs in " << Format << " format.\n";
auto G = doc::findGeneratorByName(Format);
if (!G) {
Expand All @@ -270,8 +264,8 @@ Example usage for a project using a compile commands database:
OutDirectory,
SourceRoot,
RepositoryUrl,
{UserStylesheets.begin(), UserStylesheets.end()}
};
{UserStylesheets.begin(), UserStylesheets.end()},
FTimeTrace};

if (Format == "html") {
if (auto Err = getHtmlAssetFiles(argv[0], CDCtx)) {
Expand All @@ -280,6 +274,7 @@ Example usage for a project using a compile commands database:
}
}

llvm::timeTraceProfilerBegin("Mapping declaration", "total runtime");
// Mapping phase
llvm::outs() << "Mapping decls...\n";
auto Err =
Expand All @@ -294,24 +289,28 @@ Example usage for a project using a compile commands database:
return 1;
}
}
llvm::timeTraceProfilerEnd();

// Collect values into output by key.
// In ToolResults, the Key is the hashed USR and the value is the
// bitcode-encoded representation of the Info object.
llvm::timeTraceProfilerBegin("Collect Info", "total runtime");
llvm::outs() << "Collecting infos...\n";
llvm::StringMap<std::vector<StringRef>> USRToBitcode;
Executor->get()->getToolResults()->forEachResult(
[&](StringRef Key, StringRef Value) {
auto R = USRToBitcode.try_emplace(Key, std::vector<StringRef>());
R.first->second.emplace_back(Value);
});
llvm::timeTraceProfilerEnd();

// Collects all Infos according to their unique USR value. This map is added
// to from the thread pool below and is protected by the USRToInfoMutex.
llvm::sys::Mutex USRToInfoMutex;
llvm::StringMap<std::unique_ptr<doc::Info>> USRToInfo;

// First reducing phase (reduce all decls into one info per decl).
llvm::timeTraceProfilerBegin("Reducing infos", "total runtime");
llvm::outs() << "Reducing " << USRToBitcode.size() << " infos...\n";
std::atomic<bool> Error;
Error = false;
Expand All @@ -320,50 +319,57 @@ Example usage for a project using a compile commands database:
llvm::DefaultThreadPool Pool(llvm::hardware_concurrency(ExecutorConcurrency));
for (auto &Group : USRToBitcode) {
Pool.async([&]() {
try {
std::vector<std::unique_ptr<doc::Info>> Infos;
for (auto &Bitcode : Group.getValue()) {
llvm::BitstreamCursor Stream(Bitcode);
doc::ClangDocBitcodeReader Reader(Stream);
auto ReadInfos = Reader.readBitcode();
if (!ReadInfos) {
llvm::errs() << toString(ReadInfos.takeError()) << "\n";
Error = true;
return;
}
std::move(ReadInfos->begin(), ReadInfos->end(),
std::back_inserter(Infos));
}

auto Reduced = doc::mergeInfos(Infos);
if (!Reduced) {
llvm::errs() << llvm::toString(Reduced.takeError());
if (FTimeTrace)
llvm::timeTraceProfilerInitialize(200, "clang-doc");

llvm::timeTraceProfilerBegin("Reducing infos", "decoding bitcode");
std::vector<std::unique_ptr<doc::Info>> Infos;
for (auto &Bitcode : Group.getValue()) {
llvm::BitstreamCursor Stream(Bitcode);
doc::ClangDocBitcodeReader Reader(Stream);
auto ReadInfos = Reader.readBitcode();
if (!ReadInfos) {
llvm::errs() << toString(ReadInfos.takeError()) << "\n";
Error = true;
return;
}
std::move(ReadInfos->begin(), ReadInfos->end(),
std::back_inserter(Infos));
}
llvm::timeTraceProfilerEnd();

// Add a reference to this Info in the Index
{
std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get());
}
llvm::timeTraceProfilerBegin("Reducing infos", "merging bitcode");
auto Reduced = doc::mergeInfos(Infos);
if (!Reduced) {
llvm::errs() << llvm::toString(Reduced.takeError());
return;
}
llvm::timeTraceProfilerEnd();

// Save in the result map (needs a lock due to threaded access).
{
std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
USRToInfo[Group.getKey()] = std::move(Reduced.get());
}
} catch (...) {
std::exception_ptr P = std::current_exception();
handle_eptr(P);
// Add a reference to this Info in the Index
{
std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get());
}
// Save in the result map (needs a lock due to threaded access).

{
std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
USRToInfo[Group.getKey()] = std::move(Reduced.get());
}

if (CDCtx.FTimeTrace)
llvm::timeTraceProfilerFinishThread();
});
}
llvm::timeTraceProfilerEnd();

Pool.wait();

if (Error)
return 1;

llvm::timeTraceProfilerBegin("Writing output", "total runtime");
// Ensure the root output directory exists.
if (std::error_code Err = llvm::sys::fs::create_directories(OutDirectory);
Err != std::error_code()) {
Expand All @@ -384,6 +390,16 @@ Example usage for a project using a compile commands database:
if (Err) {
llvm::outs() << "warning: " << toString(std::move(Err)) << "\n";
}

llvm::timeTraceProfilerEnd();

if (FTimeTrace) {
std::error_code EC;
llvm::raw_fd_ostream OS("clang-doc-tracing.json", EC,
llvm::sys::fs::OF_Text);
if (!EC)
llvm::timeTraceProfilerWrite(OS);
else
return 1;
}
return 0;
}
4 changes: 4 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3232,6 +3232,10 @@ def fimplicit_module_maps : Flag <["-"], "fimplicit-module-maps">, Group<f_Group
Visibility<[ClangOption, CC1Option, CLOption]>,
HelpText<"Implicitly search the file system for module map files.">,
MarshallingInfoFlag<HeaderSearchOpts<"ImplicitModuleMaps">>;
defm modulemap_allow_subdirectory_search : BoolFOption <"modulemap-allow-subdirectory-search",
HeaderSearchOpts<"AllowModuleMapSubdirectorySearch">, DefaultTrue,
PosFlag<SetTrue, [], [], "Allow to search for module maps in subdirectories of search paths">,
NegFlag<SetFalse>, BothFlags<[NoXarchOption], [ClangOption, CC1Option]>>;
defm modules : BoolFOption<"modules",
LangOpts<"Modules">, Default<fcxx_modules.KeyPath>,
PosFlag<SetTrue, [], [ClangOption, CC1Option],
Expand Down
9 changes: 8 additions & 1 deletion clang/include/clang/Lex/HeaderSearchOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ class HeaderSearchOptions {
LLVM_PREFERRED_TYPE(bool)
unsigned ModulesIncludeVFSUsage : 1;

/// Whether we should look for a module in module maps only in provided
/// header search paths or if we are allowed to look for module maps in
/// subdirectories of provided paths too.
LLVM_PREFERRED_TYPE(bool)
unsigned AllowModuleMapSubdirectorySearch : 1;

HeaderSearchOptions(StringRef _Sysroot = "/")
: Sysroot(_Sysroot), ModuleFormat("raw"), DisableModuleHash(false),
ImplicitModuleMaps(false), ModuleMapFileHomeIsCwd(false),
Expand All @@ -285,7 +291,8 @@ class HeaderSearchOptions {
ModulesSkipHeaderSearchPaths(false),
ModulesSkipPragmaDiagnosticMappings(false),
ModulesPruneNonAffectingModuleMaps(true), ModulesHashContent(false),
ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false) {}
ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false),
AllowModuleMapSubdirectorySearch(true) {}

/// AddPath - Add the \p Path path to the specified \p Group list.
void AddPath(StringRef Path, frontend::IncludeDirGroup Group,
Expand Down
Loading

0 comments on commit aa23c92

Please sign in to comment.