Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [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 | |
| 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Frontend/ASTUnit.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "clang/Index/CallGraph.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | using namespace clang; |
| 23 | using namespace idx; |
| 24 | |
| 25 | static llvm::cl::list<std::string> |
| 26 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>")); |
| 27 | |
| 28 | int main(int argc, char **argv) { |
| 29 | llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa"); |
| 30 | FileManager FileMgr; |
| 31 | std::vector<ASTUnit*> ASTUnits; |
| 32 | |
| 33 | if (InputFilenames.empty()) |
| 34 | return 0; |
| 35 | |
| 36 | DiagnosticOptions DiagOpts; |
| 37 | llvm::OwningPtr<Diagnostic> Diags( |
| 38 | CompilerInstance::createDiagnostics(DiagOpts, argc, argv)); |
| 39 | |
| 40 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| 41 | const std::string &InFile = InputFilenames[i]; |
| 42 | llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, *Diags)); |
| 43 | if (!AST) |
| 44 | return 1; |
| 45 | |
| 46 | ASTUnits.push_back(AST.take()); |
| 47 | } |
| 48 | |
| 49 | llvm::OwningPtr<CallGraph> CG; |
| 50 | CG.reset(new CallGraph()); |
| 51 | |
| 52 | for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) |
| 53 | CG->addTU(ASTUnits[i]->getASTContext()); |
| 54 | |
| 55 | CG->ViewCallGraph(); |
| 56 | } |