Skip to content

Commit d6116b4

Browse files
committed
[clang-tidy] Add clang-additional-diagnostic-* checks that enable clang warnings
Prior to this change to enable additional clang diagnostics, one had to specify extra arguments on either the command line, or in the config file. This meant invocations like `clang-tidy ... -- -Wall`, or `clang-tidy --extra-arg=-Wall ...` as well as config files like the one below. ``` \# .clang-tidy Checks: ... ExtraArgs: ["-Wall"] ``` This patch merges this functionality with the commonly used checks interface, so the invocation for enabling the `-Wall` flag will look like `clang-tidy --checks="...,clang-additional-diagnostic-all" ...`.
1 parent 51cf252 commit d6116b4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ClangTidyModuleRegistry.h"
2121
#include "ClangTidyProfiling.h"
2222
#include "ExpandModularHeadersPPCallbacks.h"
23+
#include "GlobList.h"
2324
#include "clang-tidy-config.h"
2425
#include "clang/AST/ASTConsumer.h"
2526
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -581,7 +582,32 @@ runClangTidy(clang::tidy::ClangTidyContext &Context,
581582
return AdjustedArgs;
582583
};
583584

585+
// Add extra arguments required by clang-additional-diagnostic-* checks.
586+
ArgumentsAdjuster ClangAdditionalDiagnosticArgumentsInserter =
587+
[&Context](const CommandLineArguments &Args, StringRef Filename) {
588+
ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
589+
if (!Opts.Checks)
590+
return Args;
591+
592+
CommandLineArguments AdjustedArgs = Args;
593+
CachedGlobList Filter(*Opts.Checks);
594+
595+
for (StringRef Flag : clang::DiagnosticIDs::getDiagnosticFlags()) {
596+
if (Flag.starts_with("-Wno"))
597+
continue;
598+
599+
std::string CheckName = "clang-additional-diagnostic-";
600+
CheckName += Flag.drop_front(2);
601+
602+
if (Filter.contains(CheckName))
603+
AdjustedArgs.insert(AdjustedArgs.end(), Flag.str());
604+
}
605+
606+
return AdjustedArgs;
607+
};
608+
584609
Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
610+
Tool.appendArgumentsAdjuster(ClangAdditionalDiagnosticArgumentsInserter);
585611
Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
586612
Context.setEnableProfiling(EnableCheckProfile);
587613
Context.setProfileStoragePrefix(StoreCheckProfile);

0 commit comments

Comments
 (0)