Index: lib/Analysis/CMakeLists.txt =================================================================== --- lib/Analysis/CMakeLists.txt +++ lib/Analysis/CMakeLists.txt @@ -34,3 +34,5 @@ clangBasic clangLex ) + +add_subdirectory(plugins) Index: lib/Analysis/plugins/CMakeLists.txt =================================================================== --- lib/Analysis/plugins/CMakeLists.txt +++ lib/Analysis/plugins/CMakeLists.txt @@ -0,0 +1,5 @@ +if(LLVM_ENABLE_PLUGINS) + add_subdirectory(SampleAnalyzer) + add_subdirectory(CheckerDependencyHandling) + add_subdirectory(CheckerOptionHandling) +endif() Index: lib/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt =================================================================== --- lib/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt +++ lib/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt @@ -0,0 +1,10 @@ +set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/CheckerDependencyHandlingAnalyzerPlugin.exports) +add_llvm_library(CheckerDependencyHandlingAnalyzerPlugin MODULE CheckerDependencyHandling.cpp PLUGIN_TOOL clang) + +target_link_libraries(CheckerDependencyHandlingAnalyzerPlugin PRIVATE + clangAnalysis + clangAST + clangStaticAnalyzerCore + clangStaticAnalyzerFrontend + LLVMSupport + ) Index: lib/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandling.cpp =================================================================== --- lib/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandling.cpp +++ lib/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandling.cpp @@ -0,0 +1,28 @@ +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" +#include "clang/StaticAnalyzer/Core/Checker.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" +#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" + +using namespace clang; +using namespace ento; + +namespace { +struct Dependency : public Checker { + void checkBeginFunction(CheckerContext &Ctx) const {} +}; +struct DependendentChecker : public Checker { + void checkBeginFunction(CheckerContext &Ctx) const {} +}; +} // end anonymous namespace + +// Register plugin! +extern "C" void clang_registerCheckers(CheckerRegistry ®istry) { + registry.addChecker("example.Dependency", "", ""); + registry.addChecker("example.DependendentChecker", "", + ""); + + registry.addDependency("example.DependendentChecker", "example.Dependency"); +} + +extern "C" const char clang_analyzerAPIVersionString[] = + CLANG_ANALYZER_API_VERSION_STRING; Index: lib/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports =================================================================== --- lib/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports +++ lib/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports @@ -0,0 +1,2 @@ +clang_registerCheckers +clang_analyzerAPIVersionString Index: lib/Analysis/plugins/CheckerOptionHandling/CMakeLists.txt =================================================================== --- lib/Analysis/plugins/CheckerOptionHandling/CMakeLists.txt +++ lib/Analysis/plugins/CheckerOptionHandling/CMakeLists.txt @@ -0,0 +1,10 @@ +set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/CheckerOptionHandlingAnalyzerPlugin.exports) +add_llvm_library(CheckerOptionHandlingAnalyzerPlugin MODULE CheckerOptionHandling.cpp PLUGIN_TOOL clang) + +target_link_libraries(CheckerOptionHandlingAnalyzerPlugin PRIVATE + clangAnalysis + clangAST + clangStaticAnalyzerCore + clangStaticAnalyzerFrontend + LLVMSupport + ) Index: lib/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandling.cpp =================================================================== --- lib/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandling.cpp +++ lib/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandling.cpp @@ -0,0 +1,44 @@ +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" +#include "clang/StaticAnalyzer/Core/Checker.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" +#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" + +using namespace clang; +using namespace ento; + +namespace { +struct MyChecker : public Checker { + void checkBeginFunction(CheckerContext &Ctx) const {} +}; + +void registerMyChecker(CheckerManager &Mgr) { + MyChecker *Checker = Mgr.registerChecker(); + llvm::outs() << "Example option is set to " + << (Mgr.getAnalyzerOptions().getCheckerBooleanOption( + Checker, "ExampleOption") + ? "true" + : "false") + << '\n'; +} + +bool shouldRegisterMyChecker(const LangOptions &LO) { return true; } + +} // end anonymous namespace + +// Register plugin! +extern "C" void clang_registerCheckers(CheckerRegistry ®istry) { + registry.addChecker(registerMyChecker, shouldRegisterMyChecker, + "example.MyChecker", "Example Description", + "example.mychecker.documentation.nonexistent.html", + /*isHidden*/false); + + registry.addCheckerOption(/*OptionType*/ "bool", + /*CheckerFullName*/ "example.MyChecker", + /*OptionName*/ "ExampleOption", + /*DefaultValStr*/ "false", + /*Description*/ "This is an example checker opt.", + /*DevelopmentStage*/ "released"); +} + +extern "C" const char clang_analyzerAPIVersionString[] = + CLANG_ANALYZER_API_VERSION_STRING; Index: lib/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandlingAnalyzerPlugin.exports =================================================================== --- lib/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandlingAnalyzerPlugin.exports +++ lib/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandlingAnalyzerPlugin.exports @@ -0,0 +1,2 @@ +clang_registerCheckers +clang_analyzerAPIVersionString Index: lib/Analysis/plugins/SampleAnalyzer/CMakeLists.txt =================================================================== --- lib/Analysis/plugins/SampleAnalyzer/CMakeLists.txt +++ lib/Analysis/plugins/SampleAnalyzer/CMakeLists.txt @@ -0,0 +1,10 @@ +set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/SampleAnalyzerPlugin.exports) +add_llvm_library(SampleAnalyzerPlugin MODULE MainCallChecker.cpp PLUGIN_TOOL clang) + +target_link_libraries(SampleAnalyzerPlugin PRIVATE + clangAnalysis + clangAST + clangStaticAnalyzerCore + clangStaticAnalyzerFrontend + LLVMSupport + ) Index: lib/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp =================================================================== --- lib/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp +++ lib/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp @@ -0,0 +1,54 @@ +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" +#include "clang/StaticAnalyzer/Core/Checker.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" +#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" + +using namespace clang; +using namespace ento; + +namespace { +class MainCallChecker : public Checker> { + mutable std::unique_ptr BT; + +public: + void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; +}; +} // end anonymous namespace + +void MainCallChecker::checkPreStmt(const CallExpr *CE, + CheckerContext &C) const { + const Expr *Callee = CE->getCallee(); + const FunctionDecl *FD = C.getSVal(Callee).getAsFunctionDecl(); + + if (!FD) + return; + + // Get the name of the callee. + IdentifierInfo *II = FD->getIdentifier(); + if (!II) // if no identifier, not a simple C function + return; + + if (II->isStr("main")) { + ExplodedNode *N = C.generateErrorNode(); + if (!N) + return; + + if (!BT) + BT.reset(new BugType(this, "call to main", "example analyzer plugin")); + + std::unique_ptr report = + llvm::make_unique(*BT, BT->getName(), N); + report->addRange(Callee->getSourceRange()); + C.emitReport(std::move(report)); + } +} + +// Register plugin! +extern "C" void clang_registerCheckers(CheckerRegistry ®istry) { + registry.addChecker( + "example.MainCallChecker", "Disallows calls to functions called main", + ""); +} + +extern "C" const char clang_analyzerAPIVersionString[] = + CLANG_ANALYZER_API_VERSION_STRING; Index: lib/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports =================================================================== --- lib/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports +++ lib/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports @@ -0,0 +1,2 @@ +clang_registerCheckers +clang_analyzerAPIVersionString Index: test/Analysis/lit.local.cfg =================================================================== --- test/Analysis/lit.local.cfg +++ test/Analysis/lit.local.cfg @@ -18,7 +18,5 @@ config.substitutions.append(('%diff_sarif', '''diff -U1 -w -I ".*file:.*%basename_t" -I '"version":' -I "2\.0\.0\-csd\.[0-9]*\.beta\."''')) -config.excludes.add('plugins') - if not config.root.clang_staticanalyzer: config.unsupported = True Index: test/Analysis/plugins/CMakeLists.txt =================================================================== --- test/Analysis/plugins/CMakeLists.txt +++ test/Analysis/plugins/CMakeLists.txt @@ -1,12 +0,0 @@ -add_subdirectory(SampleAnalyzer) -add_subdirectory(CheckerDependencyHandling) -add_subdirectory(CheckerOptionHandling) - -set(CLANG_ANALYZER_PLUGIN_DEPS - SampleAnalyzerPlugin - CheckerDependencyHandlingAnalyzerPlugin - CheckerOptionHandlingAnalyzerPlugin - ) - -add_custom_target(clang-analyzer-plugin - DEPENDS ${CLANG_ANALYZER_PLUGIN_DEPS}) Index: test/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt =================================================================== --- test/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt +++ test/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt @@ -1,11 +0,0 @@ -set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/CheckerDependencyHandlingAnalyzerPlugin.exports) -add_llvm_library(CheckerDependencyHandlingAnalyzerPlugin MODULE CheckerDependencyHandling.cpp PLUGIN_TOOL clang) - -if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) - target_link_libraries(CheckerDependencyHandlingAnalyzerPlugin PRIVATE - clangAnalysis - clangAST - clangStaticAnalyzerCore - LLVMSupport - ) -endif() Index: test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandling.cpp =================================================================== --- test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandling.cpp +++ test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandling.cpp @@ -1,28 +0,0 @@ -#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" -#include "clang/StaticAnalyzer/Core/Checker.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" -#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" - -using namespace clang; -using namespace ento; - -namespace { -struct Dependency : public Checker { - void checkBeginFunction(CheckerContext &Ctx) const {} -}; -struct DependendentChecker : public Checker { - void checkBeginFunction(CheckerContext &Ctx) const {} -}; -} // end anonymous namespace - -// Register plugin! -extern "C" void clang_registerCheckers(CheckerRegistry ®istry) { - registry.addChecker("example.Dependency", "", ""); - registry.addChecker("example.DependendentChecker", "", - ""); - - registry.addDependency("example.DependendentChecker", "example.Dependency"); -} - -extern "C" const char clang_analyzerAPIVersionString[] = - CLANG_ANALYZER_API_VERSION_STRING; Index: test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports =================================================================== --- test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports +++ test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports @@ -1,2 +0,0 @@ -clang_registerCheckers -clang_analyzerAPIVersionString Index: test/Analysis/plugins/CheckerOptionHandling/CMakeLists.txt =================================================================== --- test/Analysis/plugins/CheckerOptionHandling/CMakeLists.txt +++ test/Analysis/plugins/CheckerOptionHandling/CMakeLists.txt @@ -1,11 +0,0 @@ -set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/CheckerOptionHandlingAnalyzerPlugin.exports) -add_llvm_library(CheckerOptionHandlingAnalyzerPlugin MODULE CheckerOptionHandling.cpp PLUGIN_TOOL clang) - -if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) - target_link_libraries(CheckerOptionHandlingAnalyzerPlugin PRIVATE - clangAnalysis - clangAST - clangStaticAnalyzerCore - LLVMSupport - ) -endif() Index: test/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandling.cpp =================================================================== --- test/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandling.cpp +++ test/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandling.cpp @@ -1,44 +0,0 @@ -#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" -#include "clang/StaticAnalyzer/Core/Checker.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" -#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" - -using namespace clang; -using namespace ento; - -namespace { -struct MyChecker : public Checker { - void checkBeginFunction(CheckerContext &Ctx) const {} -}; - -void registerMyChecker(CheckerManager &Mgr) { - MyChecker *Checker = Mgr.registerChecker(); - llvm::outs() << "Example option is set to " - << (Mgr.getAnalyzerOptions().getCheckerBooleanOption( - Checker, "ExampleOption") - ? "true" - : "false") - << '\n'; -} - -bool shouldRegisterMyChecker(const LangOptions &LO) { return true; } - -} // end anonymous namespace - -// Register plugin! -extern "C" void clang_registerCheckers(CheckerRegistry ®istry) { - registry.addChecker(registerMyChecker, shouldRegisterMyChecker, - "example.MyChecker", "Example Description", - "example.mychecker.documentation.nonexistent.html", - /*isHidden*/false); - - registry.addCheckerOption(/*OptionType*/ "bool", - /*CheckerFullName*/ "example.MyChecker", - /*OptionName*/ "ExampleOption", - /*DefaultValStr*/ "false", - /*Description*/ "This is an example checker opt.", - /*DevelopmentStage*/ "released"); -} - -extern "C" const char clang_analyzerAPIVersionString[] = - CLANG_ANALYZER_API_VERSION_STRING; Index: test/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandlingAnalyzerPlugin.exports =================================================================== --- test/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandlingAnalyzerPlugin.exports +++ test/Analysis/plugins/CheckerOptionHandling/CheckerOptionHandlingAnalyzerPlugin.exports @@ -1,2 +0,0 @@ -clang_registerCheckers -clang_analyzerAPIVersionString Index: test/Analysis/plugins/SampleAnalyzer/CMakeLists.txt =================================================================== --- test/Analysis/plugins/SampleAnalyzer/CMakeLists.txt +++ test/Analysis/plugins/SampleAnalyzer/CMakeLists.txt @@ -1,11 +0,0 @@ -set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/SampleAnalyzerPlugin.exports) -add_llvm_library(SampleAnalyzerPlugin MODULE MainCallChecker.cpp PLUGIN_TOOL clang) - -if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) - target_link_libraries(SampleAnalyzerPlugin PRIVATE - clangAnalysis - clangAST - clangStaticAnalyzerCore - LLVMSupport - ) -endif() Index: test/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp =================================================================== --- test/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp +++ test/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp @@ -1,54 +0,0 @@ -#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" -#include "clang/StaticAnalyzer/Core/Checker.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" -#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" - -using namespace clang; -using namespace ento; - -namespace { -class MainCallChecker : public Checker> { - mutable std::unique_ptr BT; - -public: - void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; -}; -} // end anonymous namespace - -void MainCallChecker::checkPreStmt(const CallExpr *CE, - CheckerContext &C) const { - const Expr *Callee = CE->getCallee(); - const FunctionDecl *FD = C.getSVal(Callee).getAsFunctionDecl(); - - if (!FD) - return; - - // Get the name of the callee. - IdentifierInfo *II = FD->getIdentifier(); - if (!II) // if no identifier, not a simple C function - return; - - if (II->isStr("main")) { - ExplodedNode *N = C.generateErrorNode(); - if (!N) - return; - - if (!BT) - BT.reset(new BugType(this, "call to main", "example analyzer plugin")); - - std::unique_ptr report = - llvm::make_unique(*BT, BT->getName(), N); - report->addRange(Callee->getSourceRange()); - C.emitReport(std::move(report)); - } -} - -// Register plugin! -extern "C" void clang_registerCheckers(CheckerRegistry ®istry) { - registry.addChecker( - "example.MainCallChecker", "Disallows calls to functions called main", - ""); -} - -extern "C" const char clang_analyzerAPIVersionString[] = - CLANG_ANALYZER_API_VERSION_STRING; Index: test/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports =================================================================== --- test/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports +++ test/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports @@ -1,2 +0,0 @@ -clang_registerCheckers -clang_analyzerAPIVersionString Index: test/CMakeLists.txt =================================================================== --- test/CMakeLists.txt +++ test/CMakeLists.txt @@ -126,27 +126,13 @@ endif() if (CLANG_ENABLE_STATIC_ANALYZER) - add_subdirectory(Analysis/plugins) - list(APPEND CLANG_TEST_DEPS clang-analyzer-plugin) - - # check-all would launch those tests via check-clang. - set(EXCLUDE_FROM_ALL ON) - - add_lit_testsuite(check-clang-analyzer "Running the Clang analyzer tests" - ${CMAKE_CURRENT_BINARY_DIR}/Analysis - PARAMS ${ANALYZER_TEST_PARAMS} - DEPENDS ${CLANG_TEST_DEPS}) - set_target_properties(check-clang-analyzer PROPERTIES FOLDER "Clang tests") - - if (LLVM_WITH_Z3) - add_lit_testsuite(check-clang-analyzer-z3 "Running the Clang analyzer tests, using Z3 as a solver" - ${CMAKE_CURRENT_BINARY_DIR}/Analysis - PARAMS ${ANALYZER_TEST_PARAMS_Z3} - DEPENDS ${CLANG_TEST_DEPS}) - set_target_properties(check-clang-analyzer-z3 PROPERTIES FOLDER "Clang tests") + if (LLVM_ENABLE_PLUGINS) + set(CLANG_ANALYZER_PLUGIN_DEPS + SampleAnalyzerPlugin + CheckerDependencyHandlingAnalyzerPlugin + CheckerOptionHandlingAnalyzerPlugin + ) endif() - - set(EXCLUDE_FROM_ALL OFF) endif() add_custom_target(clang-test-depends DEPENDS ${CLANG_TEST_DEPS})