Skip to content

Commit 65eccb4

Browse files
committed
[clang-tidy] Allow use of -list-checks option without need to pass source files.
Initialize CommonOptionsParser with ZeroOrOne NumOccurrenceFlag so callers can pass -list-checks without the need to pass additional positional parameters, then add dummy file if none were supplied. http://reviews.llvm.org/D12070 Patch by Don Hinton! llvm-svn: 245205
1 parent 3474b55 commit 65eccb4

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,21 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
262262
}
263263

264264
static int clangTidyMain(int argc, const char **argv) {
265-
CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
265+
CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory,
266+
cl::ZeroOrMore);
266267

267268
auto OptionsProvider = createOptionsProvider();
268269
if (!OptionsProvider)
269270
return 1;
270271

271-
std::string FileName = OptionsParser.getSourcePathList().front();
272+
StringRef FileName("dummy");
273+
auto PathList = OptionsParser.getSourcePathList();
274+
if (!PathList.empty()) {
275+
FileName = OptionsParser.getSourcePathList().front();
276+
}
272277
ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FileName);
273278
std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
274279

275-
// FIXME: Allow using --list-checks without positional arguments.
276280
if (ListChecks) {
277281
llvm::outs() << "Enabled checks:";
278282
for (auto CheckName : EnabledChecks)
@@ -283,8 +287,9 @@ static int clangTidyMain(int argc, const char **argv) {
283287

284288
if (DumpConfig) {
285289
EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions);
286-
llvm::outs() << configurationAsText(ClangTidyOptions::getDefaults()
287-
.mergeWith(EffectiveOptions))
290+
llvm::outs() << configurationAsText(
291+
ClangTidyOptions::getDefaults().mergeWith(
292+
EffectiveOptions))
288293
<< "\n";
289294
return 0;
290295
}
@@ -295,12 +300,18 @@ static int clangTidyMain(int argc, const char **argv) {
295300
return 1;
296301
}
297302

303+
if (PathList.empty()) {
304+
llvm::errs() << "Error: no input files specified.\n";
305+
llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
306+
return 1;
307+
}
308+
298309
ProfileData Profile;
299310

300311
std::vector<ClangTidyError> Errors;
301312
ClangTidyStats Stats =
302313
runClangTidy(std::move(OptionsProvider), OptionsParser.getCompilations(),
303-
OptionsParser.getSourcePathList(), &Errors,
314+
PathList, &Errors,
304315
EnableCheckProfile ? &Profile : nullptr);
305316
bool FoundErrors =
306317
std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Check names may only contain alphanumeric characters, '-', '_', and '.'.
2-
// RUN: clang-tidy -checks=* -list-checks - -- | grep '^ ' | cut -b5- | not grep -v '^[a-zA-Z0-9_.\-]\+$'
2+
// RUN: clang-tidy -checks=* -list-checks | grep '^ ' | cut -b5- | not grep -v '^[a-zA-Z0-9_.\-]\+$'

0 commit comments

Comments
 (0)