[clang-check] Adjust argument adjusters for clang-check to strip options blocking the static analyzer
Output generation options (like `-save-temps`) will make the analyzer not executed even `--analyze` option is provided in the driver arguments. Besides, the original approach of adding `--analyze` option will not work when (more than one) `-fsyntax-only` options are provided in the driver arguments. This patch fixes these two problems by using the syntax-only adjuster to remove output generation options and manually filter out redundant `-fsyntax-only` options. In the new implementation, the adjusters added by `ClangTool` will not be removed but used as dependencies for clang-check adjusters for analyzer options. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D116329
This commit is contained in:
parent
263d1986e3
commit
bba729af3e
19
clang/test/Tooling/clang-check-analyze-save-temps.cpp
Normal file
19
clang/test/Tooling/clang-check-analyze-save-temps.cpp
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// Check whether output generation options (like -save-temps) will not affect
|
||||||
|
// the execution of the analyzer.
|
||||||
|
|
||||||
|
// RUN: clang-check -analyze %s -- -save-temps -c -Xclang -verify
|
||||||
|
|
||||||
|
// Check whether redundant -fsyntax-only options will affect the execution of
|
||||||
|
// the analyzer.
|
||||||
|
|
||||||
|
// RUN: clang-check -analyze %s -- \
|
||||||
|
// RUN: -fsyntax-only -c -fsyntax-only -Xclang -verify 2>&1 | \
|
||||||
|
// RUN: FileCheck %s --allow-empty
|
||||||
|
|
||||||
|
// CHECK-NOT: argument unused during compilation: '--analyze'
|
||||||
|
|
||||||
|
void a(int *x) {
|
||||||
|
if (x) {
|
||||||
|
}
|
||||||
|
*x = 47; // expected-warning {{Dereference of null pointer}}
|
||||||
|
}
|
|
@ -208,27 +208,37 @@ int main(int argc, const char **argv) {
|
||||||
ClangTool Tool(OptionsParser.getCompilations(),
|
ClangTool Tool(OptionsParser.getCompilations(),
|
||||||
OptionsParser.getSourcePathList());
|
OptionsParser.getSourcePathList());
|
||||||
|
|
||||||
// Clear adjusters because -fsyntax-only is inserted by the default chain.
|
if (Analyze) {
|
||||||
Tool.clearArgumentsAdjusters();
|
// Set output path if is provided by user.
|
||||||
|
//
|
||||||
|
// As the original -o options have been removed by default via the
|
||||||
|
// strip-output adjuster, we only need to add the analyzer -o options here
|
||||||
|
// when it is provided by users.
|
||||||
|
if (!AnalyzerOutput.empty())
|
||||||
|
Tool.appendArgumentsAdjuster(
|
||||||
|
getInsertArgumentAdjuster(CommandLineArguments{"-o", AnalyzerOutput},
|
||||||
|
ArgumentInsertPosition::END));
|
||||||
|
|
||||||
// Reset output path if is provided by user.
|
// Running the analyzer requires --analyze. Other modes can work with the
|
||||||
Tool.appendArgumentsAdjuster(
|
// -fsyntax-only option.
|
||||||
Analyze ? [&](const CommandLineArguments &Args, StringRef File) {
|
//
|
||||||
auto Ret = getClangStripOutputAdjuster()(Args, File);
|
// The syntax-only adjuster is installed by default.
|
||||||
if (!AnalyzerOutput.empty()) {
|
// Good: It also strips options that trigger extra output, like -save-temps.
|
||||||
Ret.emplace_back("-o");
|
// Bad: We don't want the -fsyntax-only when executing the static analyzer.
|
||||||
Ret.emplace_back(AnalyzerOutput);
|
//
|
||||||
}
|
// To enable the static analyzer, we first strip all -fsyntax-only options
|
||||||
return Ret;
|
// and then add an --analyze option to the front.
|
||||||
}
|
Tool.appendArgumentsAdjuster(
|
||||||
: getClangStripOutputAdjuster());
|
[&](const CommandLineArguments &Args, StringRef /*unused*/) {
|
||||||
|
CommandLineArguments AdjustedArgs;
|
||||||
Tool.appendArgumentsAdjuster(getClangStripDependencyFileAdjuster());
|
for (const std::string &Arg : Args)
|
||||||
|
if (Arg != "-fsyntax-only")
|
||||||
// Running the analyzer requires --analyze. Other modes can work with the
|
AdjustedArgs.emplace_back(Arg);
|
||||||
// -fsyntax-only option.
|
return AdjustedArgs;
|
||||||
Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster(
|
});
|
||||||
Analyze ? "--analyze" : "-fsyntax-only", ArgumentInsertPosition::BEGIN));
|
Tool.appendArgumentsAdjuster(
|
||||||
|
getInsertArgumentAdjuster("--analyze", ArgumentInsertPosition::BEGIN));
|
||||||
|
}
|
||||||
|
|
||||||
ClangCheckActionFactory CheckFactory;
|
ClangCheckActionFactory CheckFactory;
|
||||||
std::unique_ptr<FrontendActionFactory> FrontendFactory;
|
std::unique_ptr<FrontendActionFactory> FrontendFactory;
|
||||||
|
|
Loading…
Reference in a new issue