diff options
author | Zhongxing Xu <xuzhongxing@gmail.com> | 2009-07-16 01:00:25 +0000 |
---|---|---|
committer | Zhongxing Xu <xuzhongxing@gmail.com> | 2009-07-16 01:00:25 +0000 |
commit | dc3240c5b7fa29084a1cab23977ad37f9df1c594 (patch) | |
tree | edf796d9a80fb25215e85e6f98298f8867307882 | |
parent | 6bd8fb50acadeeafd923e98cd6a94efeb75693dc (diff) |
Add a primitive clang whole primitive analyzer tool.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@75874 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | tools/wpa/Makefile | 15 | ||||
-rw-r--r-- | tools/wpa/clang-wpa.cpp | 55 |
2 files changed, 70 insertions, 0 deletions
diff --git a/tools/wpa/Makefile b/tools/wpa/Makefile new file mode 100644 index 0000000000..c15be3f828 --- /dev/null +++ b/tools/wpa/Makefile @@ -0,0 +1,15 @@ +LEVEL = ../../../.. + +TOOLNAME = clang-wpa +CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include +CXXFLAGS = -fno-rtti + +# No plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.config + +LINK_COMPONENTS := bitreader +USEDLIBS = clangFrontend.a clangSema.a clangAST.a clangLex.a clangBasic.a clangAnalysis.a clangIndex.a + +include $(LLVM_SRC_ROOT)/Makefile.rules diff --git a/tools/wpa/clang-wpa.cpp b/tools/wpa/clang-wpa.cpp new file mode 100644 index 0000000000..e355a94e41 --- /dev/null +++ b/tools/wpa/clang-wpa.cpp @@ -0,0 +1,55 @@ +#include "clang/Analysis/CallGraph.h" + +#include "clang/Basic/FileManager.h" +#include "clang/Index/TranslationUnit.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>")); + +// FIXME: this duplicates the one in index-test.cpp. +class TUnit : public TranslationUnit { +public: + TUnit(ASTUnit *ast, const std::string &filename) + : AST(ast), Filename(filename) {} + ASTContext &getASTContext() { return AST->getASTContext(); } + llvm::OwningPtr<ASTUnit> AST; + std::string Filename; +}; + +int main(int argc, char **argv) { + llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa"); + FileManager FileMgr; + std::vector<TUnit*> TUnits; + + if (InputFilenames.empty()) + return 0; + + for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { + const std::string &InFile = InputFilenames[i]; + + std::string ErrMsg; + llvm::OwningPtr<ASTUnit> AST; + + AST.reset(ASTUnit::LoadFromPCHFile(InFile, FileMgr, &ErrMsg)); + + if (!AST) { + llvm::errs() << "[" << InFile << "] error: " << ErrMsg << '\n'; + return 1; + } + + TUnit *TU = new TUnit(AST.take(), InFile); + TUnits.push_back(TU); + } + + llvm::OwningPtr<CallGraph> CG; + CG.reset(new CallGraph()); + + for (unsigned i = 0, e = TUnits.size(); i != e; ++i) + CG->addTU(*(TUnits[i]->AST)); + + CG->dump(); +} |