diff options
author | Kristof Umann <kristof.umann@ericsson.com> | 2019-05-15 20:19:51 +0000 |
---|---|---|
committer | Kristof Umann <kristof.umann@ericsson.com> | 2019-05-15 20:19:51 +0000 |
commit | 750a45fe2551b18a12e58f61a5defbdd23a95411 (patch) | |
tree | 7e1d5fbad1b31d1a0b2d0c6a75a5732d48737feb /clang/examples | |
parent | 7641f310d7b0e763e6df6060a672901e915a2a5a (diff) | |
download | llvm-750a45fe2551b18a12e58f61a5defbdd23a95411.zip llvm-750a45fe2551b18a12e58f61a5defbdd23a95411.tar.gz llvm-750a45fe2551b18a12e58f61a5defbdd23a95411.tar.bz2 |
Revert "[analyzer] Add a test for plugins using checker dependencies"
Buildbots don't seem to find the new plugin.
llvm-svn: 360805
Diffstat (limited to 'clang/examples')
-rw-r--r-- | clang/examples/CMakeLists.txt | 3 | ||||
-rw-r--r-- | clang/examples/analyzer-plugin/CMakeLists.txt | 11 | ||||
-rw-r--r-- | clang/examples/analyzer-plugin/MainCallChecker.cpp | 54 | ||||
-rw-r--r-- | clang/examples/analyzer-plugin/SampleAnalyzerPlugin.exports | 2 |
4 files changed, 70 insertions, 0 deletions
diff --git a/clang/examples/CMakeLists.txt b/clang/examples/CMakeLists.txt index e4fedf3..8c26548 100644 --- a/clang/examples/CMakeLists.txt +++ b/clang/examples/CMakeLists.txt @@ -3,6 +3,9 @@ if(NOT CLANG_BUILD_EXAMPLES) set(EXCLUDE_FROM_ALL ON) endif() +if(CLANG_ENABLE_STATIC_ANALYZER) +add_subdirectory(analyzer-plugin) +endif() add_subdirectory(clang-interpreter) add_subdirectory(PrintFunctionNames) add_subdirectory(AnnotateFunctions) diff --git a/clang/examples/analyzer-plugin/CMakeLists.txt b/clang/examples/analyzer-plugin/CMakeLists.txt new file mode 100644 index 0000000..7c7b2ae --- /dev/null +++ b/clang/examples/analyzer-plugin/CMakeLists.txt @@ -0,0 +1,11 @@ +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() diff --git a/clang/examples/analyzer-plugin/MainCallChecker.cpp b/clang/examples/analyzer-plugin/MainCallChecker.cpp new file mode 100644 index 0000000..77316d6 --- /dev/null +++ b/clang/examples/analyzer-plugin/MainCallChecker.cpp @@ -0,0 +1,54 @@ +#include "clang/StaticAnalyzer/Core/Checker.h" +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" +#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" + +using namespace clang; +using namespace ento; + +namespace { +class MainCallChecker : public Checker < check::PreStmt<CallExpr> > { + mutable std::unique_ptr<BugType> 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<BugReport> report = + llvm::make_unique<BugReport>(*BT, BT->getName(), N); + report->addRange(Callee->getSourceRange()); + C.emitReport(std::move(report)); + } +} + +// Register plugin! +extern "C" +void clang_registerCheckers (CheckerRegistry ®istry) { + registry.addChecker<MainCallChecker>( + "example.MainCallChecker", "Disallows calls to functions called main", + ""); +} + +extern "C" +const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING; diff --git a/clang/examples/analyzer-plugin/SampleAnalyzerPlugin.exports b/clang/examples/analyzer-plugin/SampleAnalyzerPlugin.exports new file mode 100644 index 0000000..8d9ff88 --- /dev/null +++ b/clang/examples/analyzer-plugin/SampleAnalyzerPlugin.exports @@ -0,0 +1,2 @@ +clang_registerCheckers +clang_analyzerAPIVersionString |