Skip to content

Commit 04bb9f4

Browse files
committed
[clang][deps] Support single-file mode for all formats (llvm#88764)
The `clang-scan-deps` tool can be used for fast scanning of batches of compilation commands passed in via the `-compilation-database` option. This gets awkward in our tests where we have to resort to using `.in`/`.template` JSON files and running them through `sed` in order to embed LIT's `%t` variable into them. However, most of our tests only need to pass single compilation command, so this dance is entirely unnecessary. This patch makes sure the existing "per-file" mode (where the compilation command is passed in-line after the `--` argument) works for all output formats, not only `P1689`. (cherry picked from commit eafd515)
1 parent ee08005 commit 04bb9f4

File tree

2 files changed

+39
-59
lines changed

2 files changed

+39
-59
lines changed

clang/test/ClangScanDeps/error.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
// RUN: rm -rf %t.dir
2-
// RUN: rm -rf %t.cdb
3-
// RUN: mkdir -p %t.dir
4-
// RUN: cp %s %t.dir/regular_cdb_input.cpp
5-
// RUN: sed -e "s|DIR|%/t.dir|g" %S/Inputs/regular_cdb.json > %t.cdb
6-
// RUN: sed -e "s|DIR|%/t.dir|g" %S/Inputs/regular_cdb_clangcl.json > %t_clangcl.cdb
7-
//
8-
// RUN: not clang-scan-deps -compilation-database %t.cdb -j 1 2>%t.dir/errs
9-
// RUN: echo EOF >> %t.dir/errs
10-
// RUN: FileCheck %s --input-file %t.dir/errs
11-
12-
// RUN: not clang-scan-deps -compilation-database %t_clangcl.cdb -j 1 2>%t.dir/errs_clangcl
13-
// RUN: echo EOF >> %t.dir/errs_clangcl
14-
// RUN: FileCheck %s --input-file %t.dir/errs_clangcl
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
153

4+
//--- missing_header.c
165
#include "missing.h"
176

18-
// CHECK: Error while scanning dependencies
19-
// CHECK-NEXT: error: no such file or directory:
20-
// CHECK-NEXT: error: no input files
21-
// CHECK-NEXT: error:
22-
// CHECK-NEXT: Error while scanning dependencies
23-
// CHECK-NEXT: fatal error: 'missing.h' file not found
24-
// CHECK-NEXT: Error while scanning dependencies
25-
// CHECK-NEXT: fatal error: 'missing.h' file not found
26-
// CHECK-NEXT: EOF
7+
// RUN: not clang-scan-deps -- %clang -c %t/missing_tu.c 2>%t/missing_tu.errs
8+
// RUN: echo EOF >> %t/missing_tu.errs
9+
// RUN: cat %t/missing_tu.errs | sed 's:\\\\\?:/:g' | FileCheck %s --check-prefix=CHECK-MISSING-TU -DPREFIX=%/t
10+
// CHECK-MISSING-TU: Error while scanning dependencies for [[PREFIX]]/missing_tu.c
11+
// CHECK-MISSING-TU-NEXT: error: no such file or directory: '[[PREFIX]]/missing_tu.c'
12+
// CHECK-MISSING-TU-NEXT: error: no input files
13+
// CHECK-MISSING-TU-NEXT: error:
14+
// CHECK-MISSING-TU-NEXT: EOF
15+
16+
// RUN: not clang-scan-deps -- %clang -c %t/missing_header.c 2>%t/missing_header.errs
17+
// RUN: echo EOF >> %t/missing_header.errs
18+
// RUN: cat %t/missing_header.errs | sed 's:\\\\\?:/:g' | FileCheck %s --check-prefix=CHECK-MISSING-HEADER -DPREFIX=%/t
19+
// CHECK-MISSING-HEADER: Error while scanning dependencies for [[PREFIX]]/missing_header.c
20+
// CHECK-MISSING-HEADER-NEXT: fatal error: 'missing.h' file not found
21+
// CHECK-MISSING-HEADER-NEXT: EOF

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

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ static bool RoundTripArgs = DoRoundTripDefault;
115115
static void ParseArgs(int argc, char **argv) {
116116
ScanDepsOptTable Tbl;
117117
llvm::StringRef ToolName = argv[0];
118-
llvm::BumpPtrAllocator A;
119-
llvm::StringSaver Saver{A};
118+
llvm::BumpPtrAllocator Alloc;
119+
llvm::StringSaver Saver{Alloc};
120120
llvm::opt::InputArgList Args =
121121
Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
122122
llvm::errs() << Msg << '\n';
@@ -207,14 +207,8 @@ static void ParseArgs(int argc, char **argv) {
207207
}
208208
}
209209

210-
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_compilation_database_EQ)) {
210+
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_compilation_database_EQ))
211211
CompilationDB = A->getValue();
212-
} else if (Format != ScanningOutputFormat::P1689) {
213-
llvm::errs() << ToolName
214-
<< ": for the --compiilation-database option: must be "
215-
"specified at least once!";
216-
std::exit(1);
217-
}
218212

219213
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_module_name_EQ))
220214
ModuleName = A->getValue();
@@ -265,9 +259,8 @@ static void ParseArgs(int argc, char **argv) {
265259

266260
RoundTripArgs = Args.hasArg(OPT_round_trip_args);
267261

268-
if (auto *A = Args.getLastArgNoClaim(OPT_DASH_DASH))
269-
CommandLine.insert(CommandLine.end(), A->getValues().begin(),
270-
A->getValues().end());
262+
if (const llvm::opt::Arg *A = Args.getLastArgNoClaim(OPT_DASH_DASH))
263+
CommandLine.assign(A->getValues().begin(), A->getValues().end());
271264
}
272265

273266
class SharedStream {
@@ -975,39 +968,29 @@ static std::string getModuleCachePath(ArrayRef<std::string> Args) {
975968
return std::string(Path);
976969
}
977970

978-
// getCompilationDataBase - If -compilation-database is set, load the
979-
// compilation database from the specified file. Otherwise if the we're
980-
// generating P1689 format, trying to generate the compilation database
981-
// form specified command line after the positional parameter "--".
971+
/// Attempts to construct the compilation database from '-compilation-database'
972+
/// or from the arguments following the positional '--'.
982973
static std::unique_ptr<tooling::CompilationDatabase>
983-
getCompilationDataBase(int argc, char **argv, std::string &ErrorMessage) {
974+
getCompilationDatabase(int argc, char **argv, std::string &ErrorMessage) {
984975
llvm::InitLLVM X(argc, argv);
985976
ParseArgs(argc, argv);
986977

978+
if (!(CommandLine.empty() ^ CompilationDB.empty())) {
979+
llvm::errs() << "The compilation command line must be provided either via "
980+
"'-compilation-database' or after '--'.";
981+
return nullptr;
982+
}
983+
987984
if (!CompilationDB.empty())
988985
return tooling::JSONCompilationDatabase::loadFromFile(
989986
CompilationDB, ErrorMessage,
990987
tooling::JSONCommandLineSyntax::AutoDetect);
991988

992-
if (Format != ScanningOutputFormat::P1689) {
993-
llvm::errs() << "the --compilation-database option: must be specified at "
994-
"least once!";
995-
return nullptr;
996-
}
997-
998-
// Trying to get the input file, the output file and the command line options
999-
// from the positional parameter "--".
1000-
char **DoubleDash = std::find(argv, argv + argc, StringRef("--"));
1001-
if (DoubleDash == argv + argc) {
1002-
llvm::errs() << "The command line arguments is required after '--' in "
1003-
"P1689 per file mode.";
1004-
return nullptr;
1005-
}
1006-
1007989
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
1008990
CompilerInstance::createDiagnostics(new DiagnosticOptions);
1009991
driver::Driver TheDriver(CommandLine[0], llvm::sys::getDefaultTargetTriple(),
1010992
*Diags);
993+
TheDriver.setCheckInputsExist(false);
1011994
std::unique_ptr<driver::Compilation> C(
1012995
TheDriver.BuildCompilation(CommandLine));
1013996
if (!C)
@@ -1022,7 +1005,8 @@ getCompilationDataBase(int argc, char **argv, std::string &ErrorMessage) {
10221005

10231006
FrontendOptions &FEOpts = CI->getFrontendOpts();
10241007
if (FEOpts.Inputs.size() != 1) {
1025-
llvm::errs() << "Only one input file is allowed in P1689 per file mode.";
1008+
llvm::errs()
1009+
<< "Exactly one input file is required in the per-file mode ('--').\n";
10261010
return nullptr;
10271011
}
10281012

@@ -1031,8 +1015,9 @@ getCompilationDataBase(int argc, char **argv, std::string &ErrorMessage) {
10311015
auto LastCmd = C->getJobs().end();
10321016
LastCmd--;
10331017
if (LastCmd->getOutputFilenames().size() != 1) {
1034-
llvm::errs() << "The command line should provide exactly one output file "
1035-
"in P1689 per file mode.\n";
1018+
llvm::errs()
1019+
<< "Exactly one output file is required in the per-file mode ('--').\n";
1020+
return nullptr;
10361021
}
10371022
StringRef OutputFile = LastCmd->getOutputFilenames().front();
10381023

@@ -1072,7 +1057,7 @@ getCompilationDataBase(int argc, char **argv, std::string &ErrorMessage) {
10721057
int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
10731058
std::string ErrorMessage;
10741059
std::unique_ptr<tooling::CompilationDatabase> Compilations =
1075-
getCompilationDataBase(argc, argv, ErrorMessage);
1060+
getCompilationDatabase(argc, argv, ErrorMessage);
10761061
if (!Compilations) {
10771062
llvm::errs() << ErrorMessage << "\n";
10781063
return 1;

0 commit comments

Comments
 (0)