From 222e2a7620e6520ffaf4fc4e69d79c18da31542e Mon Sep 17 00:00:00 2001 From: "Zancanaro; Carlo" Date: Mon, 24 Sep 2012 09:58:17 +1000 Subject: Add the clang library to the repo (with some of my changes, too). --- clang/examples/analyzer-plugin/CMakeLists.txt | 14 ++++++ clang/examples/analyzer-plugin/MainCallChecker.cpp | 53 ++++++++++++++++++++++ clang/examples/analyzer-plugin/Makefile | 20 ++++++++ 3 files changed, 87 insertions(+) create mode 100644 clang/examples/analyzer-plugin/CMakeLists.txt create mode 100644 clang/examples/analyzer-plugin/MainCallChecker.cpp create mode 100644 clang/examples/analyzer-plugin/Makefile (limited to 'clang/examples/analyzer-plugin') diff --git a/clang/examples/analyzer-plugin/CMakeLists.txt b/clang/examples/analyzer-plugin/CMakeLists.txt new file mode 100644 index 0000000..2b9d825 --- /dev/null +++ b/clang/examples/analyzer-plugin/CMakeLists.txt @@ -0,0 +1,14 @@ +set(MODULE TRUE) + +set( LLVM_USED_LIBS + clangStaticAnalyzerCore + ) + +set( LLVM_LINK_COMPONENTS support mc) + +add_clang_library(SampleAnalyzerPlugin MainCallChecker.cpp) + +set_target_properties(SampleAnalyzerPlugin + PROPERTIES + LINKER_LANGUAGE CXX + PREFIX "") diff --git a/clang/examples/analyzer-plugin/MainCallChecker.cpp b/clang/examples/analyzer-plugin/MainCallChecker.cpp new file mode 100644 index 0000000..48a9795 --- /dev/null +++ b/clang/examples/analyzer-plugin/MainCallChecker.cpp @@ -0,0 +1,53 @@ +#include "clang/StaticAnalyzer/Core/Checker.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" +#include "clang/StaticAnalyzer/Core/CheckerRegistry.h" +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" + +using namespace clang; +using namespace ento; + +namespace { +class MainCallChecker : public Checker < check::PreStmt > { + mutable OwningPtr BT; + +public: + void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; +}; +} // end anonymous namespace + +void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { + const ProgramStateRef state = C.getState(); + const LocationContext *LC = C.getLocationContext(); + const Expr *Callee = CE->getCallee(); + const FunctionDecl *FD = state->getSVal(Callee, LC).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.generateSink(); + if (!N) + return; + + if (!BT) + BT.reset(new BugType("call to main", "example analyzer plugin")); + + BugReport *report = new BugReport(*BT, BT->getName(), N); + report->addRange(Callee->getSourceRange()); + C.EmitReport(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; diff --git a/clang/examples/analyzer-plugin/Makefile b/clang/examples/analyzer-plugin/Makefile new file mode 100644 index 0000000..8b83bef --- /dev/null +++ b/clang/examples/analyzer-plugin/Makefile @@ -0,0 +1,20 @@ +##===- examples/analyzer-plugin/Makefile -------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +CLANG_LEVEL := ../.. +LIBRARYNAME = SampleAnalyzerPlugin + +LINK_LIBS_IN_SHARED = 0 +LOADABLE_MODULE = 1 + +include $(CLANG_LEVEL)/Makefile + +ifeq ($(OS),Darwin) + LDFLAGS=-Wl,-undefined,dynamic_lookup +endif -- cgit v1.2.3