diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/CMakeLists.txt | 5 | ||||
-rw-r--r-- | examples/Makefile | 2 | ||||
-rw-r--r-- | examples/PrintFunctionNames/PrintFunctionNames.cpp | 4 | ||||
-rw-r--r-- | examples/PrintFunctionNames/README.txt | 4 | ||||
-rw-r--r-- | examples/analyzer-plugin/CMakeLists.txt | 2 | ||||
-rw-r--r-- | examples/analyzer-plugin/MainCallChecker.cpp | 7 | ||||
-rw-r--r-- | examples/analyzer-plugin/Makefile | 2 | ||||
-rw-r--r-- | examples/clang-interpreter/CMakeLists.txt | 1 | ||||
-rw-r--r-- | examples/clang-interpreter/Makefile | 5 | ||||
-rw-r--r-- | examples/clang-interpreter/main.cpp | 16 | ||||
-rw-r--r-- | examples/wpa/CMakeLists.txt | 26 | ||||
-rw-r--r-- | examples/wpa/Makefile | 26 | ||||
-rw-r--r-- | examples/wpa/clang-wpa.cpp | 181 |
13 files changed, 30 insertions, 251 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 317bc81..19d8869 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,7 @@ +if(NOT CLANG_BUILD_EXAMPLES) + set(EXCLUDE_FROM_ALL ON) +endif() + +add_subdirectory(analyzer-plugin) add_subdirectory(clang-interpreter) add_subdirectory(PrintFunctionNames) diff --git a/examples/Makefile b/examples/Makefile index 8cb431d..d8d9028 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -9,6 +9,6 @@ CLANG_LEVEL := .. -PARALLEL_DIRS := clang-interpreter PrintFunctionNames +PARALLEL_DIRS := analyzer-plugin clang-interpreter PrintFunctionNames include $(CLANG_LEVEL)/Makefile diff --git a/examples/PrintFunctionNames/PrintFunctionNames.cpp b/examples/PrintFunctionNames/PrintFunctionNames.cpp index fde6095..ce8f208 100644 --- a/examples/PrintFunctionNames/PrintFunctionNames.cpp +++ b/examples/PrintFunctionNames/PrintFunctionNames.cpp @@ -23,12 +23,14 @@ namespace { class PrintFunctionsConsumer : public ASTConsumer { public: - virtual void HandleTopLevelDecl(DeclGroupRef DG) { + virtual bool HandleTopLevelDecl(DeclGroupRef DG) { for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) { const Decl *D = *i; if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n"; } + + return true; } }; diff --git a/examples/PrintFunctionNames/README.txt b/examples/PrintFunctionNames/README.txt index 4c284cd..23ab5f0 100644 --- a/examples/PrintFunctionNames/README.txt +++ b/examples/PrintFunctionNames/README.txt @@ -7,6 +7,10 @@ Once the plugin is built, you can run it using: -- Linux: $ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.so -plugin print-fns some-input-file.c +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.so -plugin print-fns -plugin-arg-print-fns help -plugin-arg-print-fns --example-argument some-input-file.c +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.so -plugin print-fns -plugin-arg-print-fns -an-error some-input-file.c Mac: $ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.dylib -plugin print-fns some-input-file.c +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.dylib -plugin print-fns -plugin-arg-print-fns help -plugin-arg-print-fns --example-argument some-input-file.c +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.dylib -plugin print-fns -plugin-arg-print-fns -an-error some-input-file.c diff --git a/examples/analyzer-plugin/CMakeLists.txt b/examples/analyzer-plugin/CMakeLists.txt index 8654226..2b9d825 100644 --- a/examples/analyzer-plugin/CMakeLists.txt +++ b/examples/analyzer-plugin/CMakeLists.txt @@ -6,7 +6,7 @@ set( LLVM_USED_LIBS set( LLVM_LINK_COMPONENTS support mc) -add_clang_library(SampleAnalyzerPlugin SampleAnalyzerPlugin) +add_clang_library(SampleAnalyzerPlugin MainCallChecker.cpp) set_target_properties(SampleAnalyzerPlugin PROPERTIES diff --git a/examples/analyzer-plugin/MainCallChecker.cpp b/examples/analyzer-plugin/MainCallChecker.cpp index 85f7754..48a9795 100644 --- a/examples/analyzer-plugin/MainCallChecker.cpp +++ b/examples/analyzer-plugin/MainCallChecker.cpp @@ -8,7 +8,7 @@ using namespace ento; namespace { class MainCallChecker : public Checker < check::PreStmt<CallExpr> > { - mutable llvm::OwningPtr<BugType> BT; + mutable OwningPtr<BugType> BT; public: void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; @@ -16,9 +16,10 @@ public: } // end anonymous namespace void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { - const ProgramState *state = C.getState(); + const ProgramStateRef state = C.getState(); + const LocationContext *LC = C.getLocationContext(); const Expr *Callee = CE->getCallee(); - const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl(); + const FunctionDecl *FD = state->getSVal(Callee, LC).getAsFunctionDecl(); if (!FD) return; diff --git a/examples/analyzer-plugin/Makefile b/examples/analyzer-plugin/Makefile index 5537ee0..8b83bef 100644 --- a/examples/analyzer-plugin/Makefile +++ b/examples/analyzer-plugin/Makefile @@ -11,7 +11,7 @@ CLANG_LEVEL := ../.. LIBRARYNAME = SampleAnalyzerPlugin LINK_LIBS_IN_SHARED = 0 -SHARED_LIBRARY = 1 +LOADABLE_MODULE = 1 include $(CLANG_LEVEL)/Makefile diff --git a/examples/clang-interpreter/CMakeLists.txt b/examples/clang-interpreter/CMakeLists.txt index a747b92..4d5d4bf 100644 --- a/examples/clang-interpreter/CMakeLists.txt +++ b/examples/clang-interpreter/CMakeLists.txt @@ -25,6 +25,7 @@ set(LLVM_LINK_COMPONENTS bitwriter codegen ipo + linker selectiondag ) diff --git a/examples/clang-interpreter/Makefile b/examples/clang-interpreter/Makefile index b565bb1..420a066 100644 --- a/examples/clang-interpreter/Makefile +++ b/examples/clang-interpreter/Makefile @@ -16,10 +16,11 @@ NO_INSTALL = 1 TOOL_NO_EXPORTS = 1 LINK_COMPONENTS := jit interpreter nativecodegen bitreader bitwriter ipo \ - selectiondag asmparser instrumentation + linker selectiondag asmparser instrumentation USEDLIBS = clangFrontend.a clangSerialization.a clangDriver.a clangCodeGen.a \ clangParse.a clangSema.a clangStaticAnalyzerFrontend.a \ clangStaticAnalyzerCheckers.a clangStaticAnalyzerCore.a \ - clangAnalysis.a clangRewrite.a clangAST.a clangLex.a clangBasic.a + clangAnalysis.a clangRewrite.a \ + clangEdit.a clangAST.a clangLex.a clangBasic.a include $(CLANG_LEVEL)/Makefile diff --git a/examples/clang-interpreter/main.cpp b/examples/clang-interpreter/main.cpp index c9734e5..c39468a 100644 --- a/examples/clang-interpreter/main.cpp +++ b/examples/clang-interpreter/main.cpp @@ -18,10 +18,8 @@ #include "clang/Frontend/TextDiagnosticPrinter.h" #include "llvm/Module.h" -#include "llvm/Config/config.h" #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" @@ -48,7 +46,7 @@ static int Execute(llvm::Module *Mod, char * const *envp) { llvm::InitializeNativeTarget(); std::string Error; - llvm::OwningPtr<llvm::ExecutionEngine> EE( + OwningPtr<llvm::ExecutionEngine> EE( llvm::ExecutionEngine::createJIT(Mod, &Error)); if (!EE) { llvm::errs() << "unable to make execution engine: " << Error << "\n"; @@ -74,9 +72,9 @@ int main(int argc, const char **argv, char * const *envp) { TextDiagnosticPrinter *DiagClient = new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions()); - llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); + IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); DiagnosticsEngine Diags(DiagID, DiagClient); - Driver TheDriver(Path.str(), llvm::sys::getHostTriple(), + Driver TheDriver(Path.str(), llvm::sys::getDefaultTargetTriple(), "a.out", /*IsProduction=*/false, Diags); TheDriver.setTitle("clang interpreter"); @@ -85,7 +83,7 @@ int main(int argc, const char **argv, char * const *envp) { // (basically, exactly one input, and the operation mode is hard wired). llvm::SmallVector<const char *, 16> Args(argv, argv + argc); Args.push_back("-fsyntax-only"); - llvm::OwningPtr<Compilation> C(TheDriver.BuildCompilation(Args)); + OwningPtr<Compilation> C(TheDriver.BuildCompilation(Args)); if (!C) return 0; @@ -95,7 +93,7 @@ int main(int argc, const char **argv, char * const *envp) { // failed. Extract that job from the compilation. const driver::JobList &Jobs = C->getJobs(); if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) { - llvm::SmallString<256> Msg; + SmallString<256> Msg; llvm::raw_svector_ostream OS(Msg); C->PrintJob(OS, C->getJobs(), "; ", true); Diags.Report(diag::err_fe_expected_compiler_job) << OS.str(); @@ -110,7 +108,7 @@ int main(int argc, const char **argv, char * const *envp) { // Initialize a compiler invocation object from the clang (-cc1) arguments. const driver::ArgStringList &CCArgs = Cmd->getArguments(); - llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); + OwningPtr<CompilerInvocation> CI(new CompilerInvocation); CompilerInvocation::CreateFromArgs(*CI, const_cast<const char **>(CCArgs.data()), const_cast<const char **>(CCArgs.data()) + @@ -142,7 +140,7 @@ int main(int argc, const char **argv, char * const *envp) { CompilerInvocation::GetResourcesPath(argv[0], MainAddr); // Create and execute the frontend to generate an LLVM bitcode module. - llvm::OwningPtr<CodeGenAction> Act(new EmitLLVMOnlyAction()); + OwningPtr<CodeGenAction> Act(new EmitLLVMOnlyAction()); if (!Clang.ExecuteAction(*Act)) return 1; diff --git a/examples/wpa/CMakeLists.txt b/examples/wpa/CMakeLists.txt deleted file mode 100644 index ad1bb8e..0000000 --- a/examples/wpa/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -set(LLVM_USED_LIBS - clangIndex - clangFrontend - clangDriver - clangSema - clangAnalysis - clangSerialization - clangStaticAnalyzerFrontend - clangStaticAnalyzerCheckers - clangStaticAnalyzerCore - clangRewrite - clangAST - clangParse - clangLex - clangBasic) - -set( LLVM_LINK_COMPONENTS - bitreader - mc - core - ) - -add_clang_executable(clang-wpa - clang-wpa.cpp - ) -add_dependencies(clang-wpa clang-headers) diff --git a/examples/wpa/Makefile b/examples/wpa/Makefile deleted file mode 100644 index 2cfedbc..0000000 --- a/examples/wpa/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -##===- examples/wpa/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 := ../.. - -TOOLNAME = clang-wpa -NO_INSTALL = 1 - -# No plugins, optimize startup time. -TOOL_NO_EXPORTS = 1 - -LINK_COMPONENTS := asmparser bitreader mc core -USEDLIBS = clangStaticAnalyzerFrontend.a \ - clangStaticAnalyzerCheckers.a \ - clangStaticAnalyzerCore.a \ - clangIndex.a clangFrontend.a clangDriver.a \ - clangParse.a clangSema.a clangAnalysis.a clangSerialization.a \ - clangAST.a clangLex.a clangBasic.a - -include $(CLANG_LEVEL)/Makefile diff --git a/examples/wpa/clang-wpa.cpp b/examples/wpa/clang-wpa.cpp deleted file mode 100644 index 89eddb3..0000000 --- a/examples/wpa/clang-wpa.cpp +++ /dev/null @@ -1,181 +0,0 @@ -//===--- clang-wpa.cpp - clang whole program analyzer ---------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This tool reads a sequence of precompiled AST files, and do various -// cross translation unit analyses. -// -//===----------------------------------------------------------------------===// - -#include "clang/Basic/FileManager.h" -#include "clang/Basic/SourceManager.h" -#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/TransferFuncs.h" -#include "clang/StaticAnalyzer/Core/CheckerManager.h" -#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h" -#include "clang/Frontend/ASTUnit.h" -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Index/CallGraph.h" -#include "clang/Index/Indexer.h" -#include "clang/Index/TranslationUnit.h" -#include "clang/Index/DeclReferenceMap.h" -#include "clang/Index/SelectorMap.h" -#include "clang/Lex/Preprocessor.h" -#include "clang/Basic/TargetInfo.h" -#include "llvm/ADT/IntrusiveRefCntPtr.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/raw_ostream.h" -using namespace clang; -using namespace idx; - -static llvm::cl::list<std::string> -InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>")); - -static llvm::cl::opt<bool> -ViewCallGraph("view-call-graph", llvm::cl::desc("Display the call graph.")); - -static llvm::cl::opt<std::string> -AnalyzeFunction("analyze-function", - llvm::cl::desc("Specify the entry function.")); - -namespace { -// A thin wrapper over ASTUnit implementing the TranslationUnit interface. -class ASTUnitTU : public TranslationUnit { - ASTUnit *AST; - DeclReferenceMap DeclRefMap; - SelectorMap SelMap; - -public: - ASTUnitTU(ASTUnit *ast) - : AST(ast), DeclRefMap(AST->getASTContext()), SelMap(AST->getASTContext()) { - } - - virtual ASTContext &getASTContext() { - return AST->getASTContext(); - } - - virtual Preprocessor &getPreprocessor() { - return AST->getPreprocessor(); - } - - virtual Diagnostic &getDiagnostic() { - return AST->getDiagnostics(); - } - - virtual DeclReferenceMap &getDeclReferenceMap() { - return DeclRefMap; - } - - virtual SelectorMap &getSelectorMap() { - return SelMap; - } -}; -} - -int main(int argc, char **argv) { - llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa"); - std::vector<ASTUnit*> ASTUnits; - - Program Prog; - Indexer Idxer(Prog); - - if (InputFilenames.empty()) - return 0; - - DiagnosticOptions DiagOpts; - llvm::IntrusiveRefCntPtr<Diagnostic> Diags - = CompilerInstance::createDiagnostics(DiagOpts, argc, argv); - for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { - const std::string &InFile = InputFilenames[i]; - llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromASTFile(InFile, Diags, - FileSystemOptions(), - false, 0, 0, true)); - if (!AST) - return 1; - - ASTUnits.push_back(AST.take()); - } - - if (ViewCallGraph) { - llvm::OwningPtr<CallGraph> CG; - CG.reset(new CallGraph(Prog)); - - for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) - CG->addTU(ASTUnits[i]->getASTContext()); - - CG->ViewCallGraph(); - return 0; - } - - if (AnalyzeFunction.empty()) - return 0; - - // Feed all ASTUnits to the Indexer. - for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) { - ASTUnitTU *TU = new ASTUnitTU(ASTUnits[i]); - Idxer.IndexAST(TU); - } - - Entity Ent = Entity::get(AnalyzeFunction, Prog); - FunctionDecl *FD; - TranslationUnit *TU; - llvm::tie(FD, TU) = Idxer.getDefinitionFor(Ent); - - if (!FD) - return 0; - - // Create an analysis engine. - Preprocessor &PP = TU->getPreprocessor(); - - AnalyzerOptions Opts; - - // Hard code options and checkers for now. - - Opts.MaxNodes = 300000; - Opts.MaxLoop = 3; - Opts.InlineCall = true; - Opts.CFGAddImplicitDtors = true; - Opts.EagerlyTrimEGraph = true; - - Opts.CheckersControlList.push_back(std::make_pair("core", true)); - if (PP.getTargetInfo().getTriple().getOS() != llvm::Triple::Win32) - Opts.CheckersControlList.push_back(std::make_pair("unix", true)); - if (PP.getTargetInfo().getTriple().getVendor() == llvm::Triple::Apple) - Opts.CheckersControlList.push_back(std::make_pair("macosx", true)); - - // Checks to perform for Objective-C/Objective-C++. - if (PP.getLangOptions().ObjC1) - Opts.CheckersControlList.push_back(std::make_pair("cocoa", true)); - - llvm::OwningPtr<ento::CheckerManager> checkerMgr; - checkerMgr.reset(ento::registerCheckers(Opts, PP.getLangOptions(), - PP.getDiagnostics())); - - using namespace clang::ento; - AnalysisManager AMgr(TU->getASTContext(), PP.getDiagnostics(), - PP.getLangOptions(), /* PathDiagnostic */ 0, - CreateRegionStoreManager, - CreateRangeConstraintManager, checkerMgr.get(), &Idxer, - Opts.MaxNodes, Opts.MaxLoop, - Opts.VisualizeEGDot, Opts.VisualizeEGUbi, - Opts.PurgeDead, Opts.EagerlyAssume, - Opts.TrimGraph, Opts.InlineCall, - Opts.UnoptimizedCFG, Opts.CFGAddImplicitDtors, - Opts.CFGAddInitializers, - Opts.EagerlyTrimEGraph); - - TransferFuncs* TF = MakeCFRefCountTF(AMgr.getASTContext(), /*GC*/false, - AMgr.getLangOptions()); - ExprEngine Eng(AMgr, TF); - - Eng.ExecuteWorkList(AMgr.getStackFrame(FD, TU), AMgr.getMaxNodes()); - - return 0; -} |