Skip to content

Commit 2e09fe1

Browse files
committed
Validate configs for all inputs files up front
And make ClangTidyContext::getOptionsForFile resilient to errors at that point.
1 parent 64b4b67 commit 2e09fe1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,16 @@ const ClangTidyOptions &ClangTidyContext::getOptions() const {
265265
ClangTidyOptions ClangTidyContext::getOptionsForFile(StringRef File) const {
266266
// Merge options on top of getDefaults() as a safeguard against options with
267267
// unset values.
268-
return ClangTidyOptions::getDefaults().merge(
269-
*OptionsProvider->getOptions(File), 0);
268+
ClangTidyOptions defaultOptions = ClangTidyOptions::getDefaults();
269+
llvm::ErrorOr<ClangTidyOptions> fileOptions =
270+
OptionsProvider->getOptions(File);
271+
272+
// If there was an error parsing the options, just use the default options.
273+
// Ideally, the options for each file should be validated before this point.
274+
if (!fileOptions)
275+
return defaultOptions;
276+
277+
return defaultOptions.merge(*fileOptions, 0);
270278
}
271279

272280
void ClangTidyContext::setEnableProfiling(bool P) { Profile = P; }

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

+11
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,17 @@ int clangTidyMain(int argc, const char **argv) {
630630
if (!EffectiveOptions)
631631
return 1;
632632

633+
// Validate the configuration files associated with all input files so we can
634+
// return an error up front.
635+
if (PathList.size() > 1) {
636+
for (auto iter = PathList.begin() + 1; iter != PathList.end(); ++iter) {
637+
llvm::ErrorOr<ClangTidyOptions> Options =
638+
OptionsProvider->getOptions(*iter);
639+
if (!Options)
640+
return 1;
641+
}
642+
}
643+
633644
std::vector<std::string> EnabledChecks =
634645
getCheckNames(*EffectiveOptions, AllowEnablingAnalyzerAlphaCheckers);
635646

0 commit comments

Comments
 (0)