diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/PrintFunctionNames/PrintFunctionNames.cpp | 4 | ||||
-rw-r--r-- | examples/analyzer-plugin/CMakeLists.txt | 14 | ||||
-rw-r--r-- | examples/analyzer-plugin/MainCallChecker.cpp | 52 | ||||
-rw-r--r-- | examples/analyzer-plugin/Makefile | 20 | ||||
-rw-r--r-- | examples/clang-interpreter/main.cpp | 8 |
5 files changed, 92 insertions, 6 deletions
diff --git a/examples/PrintFunctionNames/PrintFunctionNames.cpp b/examples/PrintFunctionNames/PrintFunctionNames.cpp index cc138f56dbbae..fde60955aa00f 100644 --- a/examples/PrintFunctionNames/PrintFunctionNames.cpp +++ b/examples/PrintFunctionNames/PrintFunctionNames.cpp @@ -45,9 +45,9 @@ protected: // Example error handling. if (args[i] == "-an-error") { - Diagnostic &D = CI.getDiagnostics(); + DiagnosticsEngine &D = CI.getDiagnostics(); unsigned DiagID = D.getCustomDiagID( - Diagnostic::Error, "invalid argument '" + args[i] + "'"); + DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'"); D.Report(DiagID); return false; } diff --git a/examples/analyzer-plugin/CMakeLists.txt b/examples/analyzer-plugin/CMakeLists.txt new file mode 100644 index 0000000000000..865422684ca53 --- /dev/null +++ b/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 SampleAnalyzerPlugin) + +set_target_properties(SampleAnalyzerPlugin + PROPERTIES + LINKER_LANGUAGE CXX + PREFIX "") diff --git a/examples/analyzer-plugin/MainCallChecker.cpp b/examples/analyzer-plugin/MainCallChecker.cpp new file mode 100644 index 0000000000000..85f775483d4d4 --- /dev/null +++ b/examples/analyzer-plugin/MainCallChecker.cpp @@ -0,0 +1,52 @@ +#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<CallExpr> > { + mutable llvm::OwningPtr<BugType> BT; + +public: + void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; +}; +} // end anonymous namespace + +void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { + const ProgramState *state = C.getState(); + const Expr *Callee = CE->getCallee(); + const FunctionDecl *FD = state->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.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<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main"); +} + +extern "C" +const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING; diff --git a/examples/analyzer-plugin/Makefile b/examples/analyzer-plugin/Makefile new file mode 100644 index 0000000000000..5537ee03d880f --- /dev/null +++ b/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 +SHARED_LIBRARY = 1 + +include $(CLANG_LEVEL)/Makefile + +ifeq ($(OS),Darwin) + LDFLAGS=-Wl,-undefined,dynamic_lookup +endif diff --git a/examples/clang-interpreter/main.cpp b/examples/clang-interpreter/main.cpp index 16f4dab05d709..c9734e5fadaca 100644 --- a/examples/clang-interpreter/main.cpp +++ b/examples/clang-interpreter/main.cpp @@ -22,12 +22,13 @@ #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallString.h" #include "llvm/Config/config.h" +#include "llvm/ExecutionEngine/JIT.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/Host.h" #include "llvm/Support/Path.h" -#include "llvm/Target/TargetSelect.h" +#include "llvm/Support/TargetSelect.h" using namespace clang; using namespace clang::driver; @@ -74,10 +75,9 @@ int main(int argc, const char **argv, char * const *envp) { new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions()); llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); - Diagnostic Diags(DiagID, DiagClient); + DiagnosticsEngine Diags(DiagID, DiagClient); Driver TheDriver(Path.str(), llvm::sys::getHostTriple(), - "a.out", /*IsProduction=*/false, /*CXXIsProduction=*/false, - Diags); + "a.out", /*IsProduction=*/false, Diags); TheDriver.setTitle("clang interpreter"); // FIXME: This is a hack to try to force the driver to do something we can |