Skip to content

Commit 6a4eaf9

Browse files
authored
[clang][deps] Add -o flag to specify output path (llvm#88767)
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.
1 parent eafd515 commit 6a4eaf9

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

clang/test/ClangScanDeps/module-format.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// RUN: rm -f %t/cdb_pch.json
1717
// RUN: sed "s|DIR|%/t|g" %S/Inputs/modules-pch/cdb_pch.json > %t/cdb_pch.json
1818
// RUN: clang-scan-deps -compilation-database %t/cdb_pch.json -format experimental-full \
19-
// RUN: -module-files-dir %t/build > %t/result_pch.json
19+
// RUN: -module-files-dir %t/build -o %t/result_pch.json
2020

2121
// Explicitly build the PCH:
2222
//

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ enum ResourceDirRecipeKind {
7272
RDRK_InvokeCompiler,
7373
};
7474

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

179+
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_o))
180+
OutputFileName = A->getValue();
181+
178182
EagerLoadModules = Args.hasArg(OPT_eager_load_pcm);
179183

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

421425
void printFullOutput(raw_ostream &OS) {
426+
// Skip sorting modules and constructing the JSON object if the output
427+
// cannot be observed anyway. This makes timings less noisy.
428+
if (&OS == &llvm::nulls())
429+
return;
430+
422431
// Sort the modules by name to get a deterministic order.
423432
std::vector<IndexedModuleID> ModuleIDs;
424433
for (auto &&M : Modules)
@@ -849,8 +858,25 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
849858
});
850859

851860
SharedStream Errs(llvm::errs());
852-
// Print out the dependency results to STDOUT by default.
853-
SharedStream DependencyOS(llvm::outs());
861+
862+
std::optional<llvm::raw_fd_ostream> FileOS;
863+
llvm::raw_ostream &ThreadUnsafeDependencyOS = [&]() -> llvm::raw_ostream & {
864+
if (OutputFileName == "-")
865+
return llvm::outs();
866+
867+
if (OutputFileName == "/dev/null")
868+
return llvm::nulls();
869+
870+
std::error_code EC;
871+
FileOS.emplace(OutputFileName, EC);
872+
if (EC) {
873+
llvm::errs() << "Failed to open output file '" << OutputFileName
874+
<< "': " << llvm::errorCodeToError(EC) << '\n';
875+
std::exit(1);
876+
}
877+
return *FileOS;
878+
}();
879+
SharedStream DependencyOS(ThreadUnsafeDependencyOS);
854880

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

9931019
if (Format == ScanningOutputFormat::Full)
994-
FD->printFullOutput(llvm::outs());
1020+
FD->printFullOutput(ThreadUnsafeDependencyOS);
9951021
else if (Format == ScanningOutputFormat::P1689)
996-
PD.printDependencies(llvm::outs());
1022+
PD.printDependencies(ThreadUnsafeDependencyOS);
9971023

9981024
return HadErrors;
9991025
}

clang/tools/clang-scan-deps/Opts.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ multiclass Eq<string name, string help> {
1111
def help : Flag<["--"], "help">, HelpText<"Display this help">;
1212
def version : Flag<["--"], "version">, HelpText<"Display the version">;
1313

14+
def o : Arg<"o", "Destination of the primary output">;
15+
1416
defm mode : Eq<"mode", "The preprocessing mode used to compute the dependencies">;
1517

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

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

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

0 commit comments

Comments
 (0)