Zhongxing Xu | 22daf79 | 2009-07-16 01:03:49 +0000 | [diff] [blame] | 1 | //===--- clang-wpa.cpp - clang whole program analyzer ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tool reads a sequence of precompiled AST files, and do various |
| 11 | // cross translation unit analyses. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/CallGraph.h" |
| 16 | |
| 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "clang/Index/TranslationUnit.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | using namespace clang; |
| 22 | using namespace idx; |
| 23 | |
| 24 | static llvm::cl::list<std::string> |
| 25 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>")); |
| 26 | |
| 27 | // FIXME: this duplicates the one in index-test.cpp. |
| 28 | class TUnit : public TranslationUnit { |
| 29 | public: |
| 30 | TUnit(ASTUnit *ast, const std::string &filename) |
| 31 | : AST(ast), Filename(filename) {} |
| 32 | ASTContext &getASTContext() { return AST->getASTContext(); } |
| 33 | llvm::OwningPtr<ASTUnit> AST; |
| 34 | std::string Filename; |
| 35 | }; |
| 36 | |
| 37 | int main(int argc, char **argv) { |
| 38 | llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa"); |
| 39 | FileManager FileMgr; |
| 40 | std::vector<TUnit*> TUnits; |
| 41 | |
| 42 | if (InputFilenames.empty()) |
| 43 | return 0; |
| 44 | |
| 45 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| 46 | const std::string &InFile = InputFilenames[i]; |
| 47 | |
| 48 | std::string ErrMsg; |
| 49 | llvm::OwningPtr<ASTUnit> AST; |
| 50 | |
| 51 | AST.reset(ASTUnit::LoadFromPCHFile(InFile, FileMgr, &ErrMsg)); |
| 52 | |
| 53 | if (!AST) { |
| 54 | llvm::errs() << "[" << InFile << "] error: " << ErrMsg << '\n'; |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | TUnit *TU = new TUnit(AST.take(), InFile); |
| 59 | TUnits.push_back(TU); |
| 60 | } |
| 61 | |
| 62 | llvm::OwningPtr<CallGraph> CG; |
| 63 | CG.reset(new CallGraph()); |
| 64 | |
| 65 | for (unsigned i = 0, e = TUnits.size(); i != e; ++i) |
| 66 | CG->addTU(*(TUnits[i]->AST)); |
| 67 | |
Zhongxing Xu | 56a5d80 | 2009-07-23 13:39:38 +0000 | [diff] [blame^] | 68 | CG->ViewCallGraph(); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 69 | } |