diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile | 4 | ||||
-rw-r--r-- | examples/PrintFunctionNames/CMakeLists.txt | 22 | ||||
-rw-r--r-- | examples/PrintFunctionNames/Makefile | 12 | ||||
-rw-r--r-- | examples/PrintFunctionNames/PrintFunctionNames.cpp | 15 | ||||
-rw-r--r-- | examples/clang-interpreter/CMakeLists.txt | 2 | ||||
-rw-r--r-- | examples/clang-interpreter/Makefile | 12 | ||||
-rw-r--r-- | examples/clang-interpreter/main.cpp | 2 | ||||
-rw-r--r-- | examples/wpa/CMakeLists.txt | 2 | ||||
-rw-r--r-- | examples/wpa/Makefile | 17 | ||||
-rw-r--r-- | examples/wpa/clang-wpa.cpp | 101 |
10 files changed, 130 insertions, 59 deletions
diff --git a/examples/Makefile b/examples/Makefile index 869197d..c4af252 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -7,8 +7,8 @@ # ##===----------------------------------------------------------------------===## -LEVEL = ../../.. +CLANG_LEVEL := .. PARALLEL_DIRS := clang-interpreter PrintFunctionNames wpa -include $(LEVEL)/Makefile.common +include $(CLANG_LEVEL)/Makefile diff --git a/examples/PrintFunctionNames/CMakeLists.txt b/examples/PrintFunctionNames/CMakeLists.txt index 49dd22a..5ea75db 100644 --- a/examples/PrintFunctionNames/CMakeLists.txt +++ b/examples/PrintFunctionNames/CMakeLists.txt @@ -1,26 +1,10 @@ -set(SHARED_LIBRARY TRUE) +set(MODULE TRUE) set(LLVM_NO_RTTI 1) -set(LLVM_USED_LIBS - clangIndex - clangFrontend - clangDriver - clangSema - clangAnalysis - clangAST - clangParse - clangLex - clangBasic) - -set( LLVM_LINK_COMPONENTS - bitreader - mc - core - ) - add_clang_library(PrintFunctionNames PrintFunctionNames.cpp) set_target_properties(PrintFunctionNames PROPERTIES - LINKER_LANGUAGE CXX) + LINKER_LANGUAGE CXX + PREFIX "") diff --git a/examples/PrintFunctionNames/Makefile b/examples/PrintFunctionNames/Makefile index 57d3ba9..0ff5189 100644 --- a/examples/PrintFunctionNames/Makefile +++ b/examples/PrintFunctionNames/Makefile @@ -7,21 +7,13 @@ # ##===----------------------------------------------------------------------===## -LEVEL = ../../../.. +CLANG_LEVEL := ../.. LIBRARYNAME = PrintFunctionNames -CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include - -# Include this here so we can get the configuration of the targets that have -# been configured for construction. We have to do this early so we can set up -# LINK_COMPONENTS before including Makefile.rules -include $(LEVEL)/Makefile.config - LINK_LIBS_IN_SHARED = 1 SHARED_LIBRARY = 1 -LINK_COMPONENTS := bitreader mc core USEDLIBS = clangIndex.a clangFrontend.a clangDriver.a clangSema.a \ clangAnalysis.a clangAST.a clangParse.a clangLex.a clangBasic.a -include $(LEVEL)/Makefile.common +include $(CLANG_LEVEL)/Makefile diff --git a/examples/PrintFunctionNames/PrintFunctionNames.cpp b/examples/PrintFunctionNames/PrintFunctionNames.cpp index 5b7b66a..397cf84 100644 --- a/examples/PrintFunctionNames/PrintFunctionNames.cpp +++ b/examples/PrintFunctionNames/PrintFunctionNames.cpp @@ -31,11 +31,24 @@ public: } }; -class PrintFunctionNamesAction : public ASTFrontendAction { +class PrintFunctionNamesAction : public PluginASTAction { protected: ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) { return new PrintFunctionsConsumer(); } + + bool ParseArgs(const std::vector<std::string>& args) { + for (unsigned i=0; i<args.size(); ++i) + llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n"; + if (args.size() && args[0] == "help") + PrintHelp(llvm::errs()); + + return true; + } + void PrintHelp(llvm::raw_ostream& ros) { + ros << "Help for PrintFunctionNames plugin goes here\n"; + } + }; } diff --git a/examples/clang-interpreter/CMakeLists.txt b/examples/clang-interpreter/CMakeLists.txt index 0f63b5f..1aa9b2b 100644 --- a/examples/clang-interpreter/CMakeLists.txt +++ b/examples/clang-interpreter/CMakeLists.txt @@ -18,8 +18,10 @@ set(LLVM_LINK_COMPONENTS jit interpreter nativecodegen + asmparser bitreader bitwriter + codegen ipo selectiondag ) diff --git a/examples/clang-interpreter/Makefile b/examples/clang-interpreter/Makefile index fecc3f5..6fa58d2 100644 --- a/examples/clang-interpreter/Makefile +++ b/examples/clang-interpreter/Makefile @@ -7,24 +7,18 @@ # ##===----------------------------------------------------------------------===## -LEVEL = ../../../.. +CLANG_LEVEL := ../.. TOOLNAME = clang-interpreter -CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include NO_INSTALL = 1 # No plugins, optimize startup time. TOOL_NO_EXPORTS = 1 -# Include this here so we can get the configuration of the targets that have -# been configured for construction. We have to do this early so we can set up -# LINK_COMPONENTS before including Makefile.rules -include $(LEVEL)/Makefile.config - LINK_COMPONENTS := jit interpreter nativecodegen bitreader bitwriter ipo \ - selectiondag + selectiondag asmparser USEDLIBS = clangFrontend.a clangDriver.a clangCodeGen.a clangSema.a \ clangChecker.a clangAnalysis.a clangRewrite.a clangAST.a \ clangParse.a clangLex.a clangBasic.a -include $(LLVM_SRC_ROOT)/Makefile.rules +include $(CLANG_LEVEL)/Makefile diff --git a/examples/clang-interpreter/main.cpp b/examples/clang-interpreter/main.cpp index 8623954..ec4e861 100644 --- a/examples/clang-interpreter/main.cpp +++ b/examples/clang-interpreter/main.cpp @@ -7,10 +7,10 @@ // //===----------------------------------------------------------------------===// +#include "clang/CodeGen/CodeGenAction.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Tool.h" -#include "clang/Frontend/CodeGenAction.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/DiagnosticOptions.h" diff --git a/examples/wpa/CMakeLists.txt b/examples/wpa/CMakeLists.txt index 8d443d6..c2b2ce6 100644 --- a/examples/wpa/CMakeLists.txt +++ b/examples/wpa/CMakeLists.txt @@ -6,6 +6,8 @@ set(LLVM_USED_LIBS clangDriver clangSema clangAnalysis + clangChecker + clangRewrite clangAST clangParse clangLex diff --git a/examples/wpa/Makefile b/examples/wpa/Makefile index 6b7f407..bd6ebfd 100644 --- a/examples/wpa/Makefile +++ b/examples/wpa/Makefile @@ -7,22 +7,17 @@ # ##===----------------------------------------------------------------------===## -LEVEL = ../../../.. +CLANG_LEVEL := ../.. TOOLNAME = clang-wpa -CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include NO_INSTALL = 1 # No plugins, optimize startup time. TOOL_NO_EXPORTS = 1 -# Include this here so we can get the configuration of the targets that have -# been configured for construction. We have to do this early so we can set up -# LINK_COMPONENTS before including Makefile.rules -include $(LEVEL)/Makefile.config +LINK_COMPONENTS := asmparser bitreader mc core +USEDLIBS = clangChecker.a clangIndex.a clangFrontend.a clangDriver.a \ + clangSema.a clangAnalysis.a clangAST.a clangParse.a clangLex.a \ + clangBasic.a -LINK_COMPONENTS := bitreader mc core -USEDLIBS = clangIndex.a clangFrontend.a clangDriver.a clangSema.a \ - clangAnalysis.a clangAST.a clangParse.a clangLex.a clangBasic.a - -include $(LLVM_SRC_ROOT)/Makefile.rules +include $(CLANG_LEVEL)/Makefile diff --git a/examples/wpa/clang-wpa.cpp b/examples/wpa/clang-wpa.cpp index b515e33..74ec368 100644 --- a/examples/wpa/clang-wpa.cpp +++ b/examples/wpa/clang-wpa.cpp @@ -14,9 +14,18 @@ #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" +#include "clang/Checker/PathSensitive/AnalysisManager.h" +#include "clang/Checker/PathSensitive/GRExprEngine.h" +#include "clang/Checker/PathSensitive/GRTransferFuncs.h" +#include "clang/Checker/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 "llvm/ADT/IntrusiveRefCntPtr.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/raw_ostream.h" @@ -26,11 +35,50 @@ 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 DeclReferenceMap &getDeclReferenceMap() { + return DeclRefMap; + } + + virtual SelectorMap &getSelectorMap() { + return SelMap; + } +}; +} + int main(int argc, char **argv) { llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa"); - FileManager FileMgr; std::vector<ASTUnit*> ASTUnits; + Program Prog; + Indexer Idxer(Prog); + if (InputFilenames.empty()) return 0; @@ -46,11 +94,52 @@ int main(int argc, char **argv) { ASTUnits.push_back(AST.take()); } - llvm::OwningPtr<CallGraph> CG; - CG.reset(new CallGraph()); + 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(); + + // Hard code options for now. + AnalysisManager AMgr(TU->getASTContext(), PP.getDiagnostics(), + PP.getLangOptions(), /* PathDiagnostic */ 0, + CreateRegionStoreManager, + CreateRangeConstraintManager, + /* MaxNodes */ 300000, /* MaxLoop */ 3, + /* VisualizeEG */ false, /* VisualizeEGUbi */ false, + /* PurgeDead */ true, /* EagerlyAssume */ false, + /* TrimGraph */ false, /* InlineCall */ true); - for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) - CG->addTU(ASTUnits[i]->getASTContext()); + GRTransferFuncs* TF = MakeCFRefCountTF(AMgr.getASTContext(), /*GC*/false, + AMgr.getLangOptions()); + GRExprEngine Eng(AMgr, TF); - CG->ViewCallGraph(); + Eng.ExecuteWorkList(AMgr.getStackFrame(FD), AMgr.getMaxNodes()); + + return 0; } |