Skip to content

Commit

Permalink
[clang][deps] Add -o flag to specify output path (#88767)
Browse files Browse the repository at this point in the history
This makes it possible to pass "-o /dev/null" to `clang-scan-deps` and
skip some potentially expensive work, making timings less noisy. Also
removes the need for stream redirection.
  • Loading branch information
jansvoboda11 committed Apr 17, 2024
1 parent eafd515 commit 6a4eaf9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clang/test/ClangScanDeps/module-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// RUN: rm -f %t/cdb_pch.json
// RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch/cdb_pch.json > %t/cdb_pch.json
// RUN: clang-scan-deps -compilation-database %t/cdb_pch.json -format experimental-full \
// RUN: -module-files-dir %t/build > %t/result_pch.json
// RUN: -module-files-dir %t/build -o %t/result_pch.json

// Explicitly build the PCH:
//
Expand Down
34 changes: 30 additions & 4 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ enum ResourceDirRecipeKind {
RDRK_InvokeCompiler,
};

static std::string OutputFileName = "-";
static ScanningMode ScanMode = ScanningMode::DependencyDirectivesScan;
static ScanningOutputFormat Format = ScanningOutputFormat::Make;
static ScanningOptimizations OptimizeArgs;
Expand Down Expand Up @@ -175,6 +176,9 @@ static void ParseArgs(int argc, char **argv) {
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_module_files_dir_EQ))
ModuleFilesDir = A->getValue();

if (const llvm::opt::Arg *A = Args.getLastArg(OPT_o))
OutputFileName = A->getValue();

EagerLoadModules = Args.hasArg(OPT_eager_load_pcm);

if (const llvm::opt::Arg *A = Args.getLastArg(OPT_j)) {
Expand Down Expand Up @@ -419,6 +423,11 @@ class FullDeps {
}

void printFullOutput(raw_ostream &OS) {
// Skip sorting modules and constructing the JSON object if the output
// cannot be observed anyway. This makes timings less noisy.
if (&OS == &llvm::nulls())
return;

// Sort the modules by name to get a deterministic order.
std::vector<IndexedModuleID> ModuleIDs;
for (auto &&M : Modules)
Expand Down Expand Up @@ -849,8 +858,25 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
});

SharedStream Errs(llvm::errs());
// Print out the dependency results to STDOUT by default.
SharedStream DependencyOS(llvm::outs());

std::optional<llvm::raw_fd_ostream> FileOS;
llvm::raw_ostream &ThreadUnsafeDependencyOS = [&]() -> llvm::raw_ostream & {
if (OutputFileName == "-")
return llvm::outs();

if (OutputFileName == "/dev/null")
return llvm::nulls();

std::error_code EC;
FileOS.emplace(OutputFileName, EC);
if (EC) {
llvm::errs() << "Failed to open output file '" << OutputFileName
<< "': " << llvm::errorCodeToError(EC) << '\n';
std::exit(1);
}
return *FileOS;
}();
SharedStream DependencyOS(ThreadUnsafeDependencyOS);

std::vector<tooling::CompileCommand> Inputs =
AdjustingCompilations->getAllCompileCommands();
Expand Down Expand Up @@ -991,9 +1017,9 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
HadErrors = true;

if (Format == ScanningOutputFormat::Full)
FD->printFullOutput(llvm::outs());
FD->printFullOutput(ThreadUnsafeDependencyOS);
else if (Format == ScanningOutputFormat::P1689)
PD.printDependencies(llvm::outs());
PD.printDependencies(ThreadUnsafeDependencyOS);

return HadErrors;
}
4 changes: 3 additions & 1 deletion clang/tools/clang-scan-deps/Opts.td
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ multiclass Eq<string name, string help> {
def help : Flag<["--"], "help">, HelpText<"Display this help">;
def version : Flag<["--"], "version">, HelpText<"Display the version">;

def o : Arg<"o", "Destination of the primary output">;

defm mode : Eq<"mode", "The preprocessing mode used to compute the dependencies">;

defm format : Eq<"format", "The output format for the dependencies">;
Expand All @@ -37,4 +39,4 @@ def verbose : F<"v", "Use verbose output">;

def round_trip_args : F<"round-trip-args", "verify that command-line arguments are canonical by parsing and re-serializing">;

def DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>;
def DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>;

0 comments on commit 6a4eaf9

Please sign in to comment.