Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 373b807f authored by Pavel Labath's avatar Pavel Labath
Browse files

Add support for static analysis to clang-check

Summary:
This adds a command line argument '-analyze' to clang-check which runs the
clang static analyzer on the source files.

Reviewers: klimek

CC: cfe-commits, revane

Differential Revision: http://llvm-reviews.chandlerc.com/D926

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183399 91177308-0d34-0410-b5e6-96231b3b80d8
parent 63d5335a
Branches
No related tags found
No related merge requests found
// RUN: clang-check -analyze "%s" -- -c 2>&1 | FileCheck %s
// CHECK: Dereference of null pointer
int a(int *x) { if(x){} *x = 47; }
......@@ -14,6 +14,7 @@ target_link_libraries(clang-check
clangTooling
clangBasic
clangRewriteFrontend
clangStaticAnalyzerFrontend
)
install(TARGETS clang-check
......
......@@ -21,6 +21,7 @@
#include "clang/Driver/Options.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
#include "clang/Rewrite/Frontend/FixItRewriter.h"
#include "clang/Rewrite/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
......@@ -62,6 +63,9 @@ static cl::opt<bool> ASTPrint(
static cl::opt<std::string> ASTDumpFilter(
"ast-dump-filter",
cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter)));
static cl::opt<bool> Analyze(
"analyze",
cl::desc(Options->getOptionHelpText(options::OPT_analyze)));
static cl::opt<bool> Fixit(
"fixit",
......@@ -136,6 +140,10 @@ public:
: Extra(Extra), Pos(Pos) {
}
InsertAdjuster(const char *Extra, Position Pos)
: Extra(1, std::string(Extra)), Pos(Pos) {
}
virtual CommandLineArguments
Adjust(const CommandLineArguments &Args) LLVM_OVERRIDE {
CommandLineArguments Return(Args);
......@@ -182,13 +190,33 @@ int main(int argc, const char **argv) {
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
if (ArgsAfter.size() > 0)
Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsAfter, InsertAdjuster::END));
if (ArgsBefore.size() > 0)
Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsBefore, InsertAdjuster::BEGIN));
// Clear adjusters because -fsyntax-only is inserted by the default chain.
Tool.clearArgumentsAdjusters();
Tool.appendArgumentsAdjuster(new ClangStripOutputAdjuster());
if (ArgsAfter.size() > 0) {
Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsAfter,
InsertAdjuster::END));
}
if (ArgsBefore.size() > 0) {
Tool.appendArgumentsAdjuster(new InsertAdjuster(ArgsBefore,
InsertAdjuster::BEGIN));
}
// Running the analyzer requires --analyze. Other modes can work with the
// -fsyntax-only option.
Tool.appendArgumentsAdjuster(new InsertAdjuster(
Analyze ? "--analyze" : "-fsyntax-only", InsertAdjuster::BEGIN));
clang_check::ClangCheckActionFactory CheckFactory;
FrontendActionFactory *FrontendFactory;
// Choose the correct factory based on the selected mode.
if (Analyze)
FrontendFactory = newFrontendActionFactory<clang::ento::AnalysisAction>();
else if (Fixit)
FrontendFactory = newFrontendActionFactory<FixItAction>();
else
FrontendFactory = newFrontendActionFactory(&CheckFactory);
if (Fixit)
return Tool.run(newFrontendActionFactory<FixItAction>());
clang_check::ClangCheckActionFactory Factory;
return Tool.run(newFrontendActionFactory(&Factory));
return Tool.run(FrontendFactory);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment