[PATCH] Add support for static analysis to clang-check
Pavel Labath
labath at google.com
Thu Jun 6 05:04:01 PDT 2013
Hi klimek,
This adds a command line argument '-analyze' to clang-check which runs the
clang static analyzer on the source files.
http://llvm-reviews.chandlerc.com/D926
Files:
test/Tooling/clang-check-analyzer.cpp
tools/clang-check/CMakeLists.txt
tools/clang-check/ClangCheck.cpp
Index: test/Tooling/clang-check-analyzer.cpp
===================================================================
--- /dev/null
+++ test/Tooling/clang-check-analyzer.cpp
@@ -0,0 +1,4 @@
+// RUN: clang-check -analyze "%s" -- -c 2>&1 | FileCheck %s
+
+// CHECK: Dereference of null pointer
+int a(int *x) { if(x){} *x = 47; }
Index: tools/clang-check/CMakeLists.txt
===================================================================
--- tools/clang-check/CMakeLists.txt
+++ tools/clang-check/CMakeLists.txt
@@ -14,6 +14,7 @@
clangTooling
clangBasic
clangRewriteFrontend
+ clangStaticAnalyzerFrontend
)
install(TARGETS clang-check
Index: tools/clang-check/ClangCheck.cpp
===================================================================
--- tools/clang-check/ClangCheck.cpp
+++ tools/clang-check/ClangCheck.cpp
@@ -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<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 @@
: 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 @@
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);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D926.1.patch
Type: text/x-patch
Size: 3481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130606/38436586/attachment.bin>
More information about the cfe-commits
mailing list