diff options
Diffstat (limited to 'clang/test/Analysis/plugins')
7 files changed, 118 insertions, 0 deletions
diff --git a/clang/test/Analysis/plugins/CMakeLists.txt b/clang/test/Analysis/plugins/CMakeLists.txt new file mode 100644 index 00000000000..3f538d9e0ef --- /dev/null +++ b/clang/test/Analysis/plugins/CMakeLists.txt @@ -0,0 +1,10 @@ +add_subdirectory(SampleAnalyzer) +add_subdirectory(CheckerDependencyHandling) + +set(CLANG_ANALYZER_PLUGIN_DEPS + SampleAnalyzerPlugin + CheckerDependencyHandlingAnalyzerPlugin + ) + +add_custom_target(clang-analyzer-plugin + DEPENDS ${CLANG_ANALYZER_PLUGIN_DEPS}) diff --git a/clang/test/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt b/clang/test/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt new file mode 100644 index 00000000000..80e2cdbd3a2 --- /dev/null +++ b/clang/test/Analysis/plugins/CheckerDependencyHandling/CMakeLists.txt @@ -0,0 +1,11 @@ +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() diff --git a/clang/test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandling.cpp b/clang/test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandling.cpp new file mode 100644 index 00000000000..be8e1200d0b --- /dev/null +++ b/clang/test/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<check::BeginFunction> { + void checkBeginFunction(CheckerContext &Ctx) const {} +}; +struct DependendentChecker : public Checker<check::BeginFunction> { + void checkBeginFunction(CheckerContext &Ctx) const {} +}; +} // end anonymous namespace + +// Register plugin! +extern "C" void clang_registerCheckers(CheckerRegistry ®istry) { + registry.addChecker<Dependency>("example.Dependency", "", ""); + registry.addChecker<DependendentChecker>("example.DependendentChecker", "", + ""); + + registry.addDependency("example.DependendentChecker", "example.Dependency"); +} + +extern "C" const char clang_analyzerAPIVersionString[] = + CLANG_ANALYZER_API_VERSION_STRING; diff --git a/clang/test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports b/clang/test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports new file mode 100644 index 00000000000..8d9ff882cfb --- /dev/null +++ b/clang/test/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports @@ -0,0 +1,2 @@ +clang_registerCheckers +clang_analyzerAPIVersionString diff --git a/clang/test/Analysis/plugins/SampleAnalyzer/CMakeLists.txt b/clang/test/Analysis/plugins/SampleAnalyzer/CMakeLists.txt new file mode 100644 index 00000000000..7c7b2aec198 --- /dev/null +++ b/clang/test/Analysis/plugins/SampleAnalyzer/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/test/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp b/clang/test/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp new file mode 100644 index 00000000000..8bd4085108e --- /dev/null +++ b/clang/test/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<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/test/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports b/clang/test/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports new file mode 100644 index 00000000000..8d9ff882cfb --- /dev/null +++ b/clang/test/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports @@ -0,0 +1,2 @@ +clang_registerCheckers +clang_analyzerAPIVersionString |