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/Basic/FileManager.h" |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 8fd57fe | 2009-12-03 07:20:04 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/ASTUnit.h" |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 8fd57fe | 2009-12-03 07:20:04 +0000 | [diff] [blame] | 19 | #include "clang/Index/CallGraph.h" |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 20 | #include "clang/Index/Indexer.h" |
| 21 | #include "clang/Index/TranslationUnit.h" |
| 22 | #include "clang/Index/DeclReferenceMap.h" |
| 23 | #include "clang/Index/SelectorMap.h" |
Douglas Gregor | f45d673 | 2010-04-06 01:25:58 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | using namespace clang; |
| 28 | using namespace idx; |
| 29 | |
| 30 | static llvm::cl::list<std::string> |
| 31 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>")); |
| 32 | |
Zhongxing Xu | 81cc955 | 2010-07-02 07:03:03 +0000 | [diff] [blame] | 33 | static llvm::cl::opt<bool> |
| 34 | ViewCallGraph("view-call-graph", llvm::cl::desc("Display the call graph.")); |
| 35 | |
| 36 | static llvm::cl::opt<std::string> |
| 37 | AnalyzeFunction("analyze-function", |
| 38 | llvm::cl::desc("Specify the entry function.")); |
Zhongxing Xu | 9b80ceb | 2010-07-02 06:58:30 +0000 | [diff] [blame] | 39 | |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | // A thin wrapper over ASTUnit implementing the TranslationUnit interface. |
| 42 | class ASTUnitTU : public TranslationUnit { |
| 43 | ASTUnit *AST; |
| 44 | DeclReferenceMap DeclRefMap; |
| 45 | SelectorMap SelMap; |
| 46 | |
| 47 | public: |
| 48 | ASTUnitTU(ASTUnit *ast) |
| 49 | : AST(ast), DeclRefMap(AST->getASTContext()), SelMap(AST->getASTContext()) { |
| 50 | } |
| 51 | |
| 52 | virtual ASTContext &getASTContext() { |
| 53 | return AST->getASTContext(); |
| 54 | } |
| 55 | |
| 56 | virtual DeclReferenceMap &getDeclReferenceMap() { |
| 57 | return DeclRefMap; |
| 58 | } |
| 59 | |
| 60 | virtual SelectorMap &getSelectorMap() { |
| 61 | return SelMap; |
| 62 | } |
| 63 | }; |
| 64 | } |
| 65 | |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 66 | int main(int argc, char **argv) { |
| 67 | llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa"); |
Argyrios Kyrtzidis | 0594545 | 2009-07-29 23:39:09 +0000 | [diff] [blame] | 68 | std::vector<ASTUnit*> ASTUnits; |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 69 | |
Zhongxing Xu | 6d956df | 2010-07-02 06:39:46 +0000 | [diff] [blame] | 70 | Program Prog; |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 71 | Indexer Idxer(Prog); |
Zhongxing Xu | 6d956df | 2010-07-02 06:39:46 +0000 | [diff] [blame] | 72 | |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 73 | if (InputFilenames.empty()) |
| 74 | return 0; |
| 75 | |
Daniel Dunbar | bb3503a | 2009-12-06 09:56:30 +0000 | [diff] [blame] | 76 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 7969f93 | 2010-04-06 04:03:12 +0000 | [diff] [blame] | 77 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags |
| 78 | = CompilerInstance::createDiagnostics(DiagOpts, argc, argv); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 79 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| 80 | const std::string &InFile = InputFilenames[i]; |
Douglas Gregor | f45d673 | 2010-04-06 01:25:58 +0000 | [diff] [blame] | 81 | llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, Diags)); |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 82 | if (!AST) |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 83 | return 1; |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 84 | |
Argyrios Kyrtzidis | 0594545 | 2009-07-29 23:39:09 +0000 | [diff] [blame] | 85 | ASTUnits.push_back(AST.take()); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Zhongxing Xu | 9b80ceb | 2010-07-02 06:58:30 +0000 | [diff] [blame] | 88 | if (ViewCallGraph) { |
| 89 | llvm::OwningPtr<CallGraph> CG; |
| 90 | CG.reset(new CallGraph(Prog)); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 91 | |
Zhongxing Xu | 9b80ceb | 2010-07-02 06:58:30 +0000 | [diff] [blame] | 92 | for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) |
| 93 | CG->addTU(ASTUnits[i]->getASTContext()); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 94 | |
Zhongxing Xu | 9b80ceb | 2010-07-02 06:58:30 +0000 | [diff] [blame] | 95 | CG->ViewCallGraph(); |
| 96 | return 0; |
| 97 | } |
Zhongxing Xu | 81cc955 | 2010-07-02 07:03:03 +0000 | [diff] [blame] | 98 | |
| 99 | if (AnalyzeFunction.empty()) |
| 100 | return 0; |
| 101 | |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 102 | // Feed all ASTUnits to the Indexer. |
| 103 | for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) { |
| 104 | ASTUnitTU TU(ASTUnits[i]); |
| 105 | Idxer.IndexAST(&TU); |
| 106 | } |
| 107 | |
Zhongxing Xu | dc01a15 | 2010-07-06 05:55:13 +0000 | [diff] [blame^] | 108 | Entity Ent = Entity::get(AnalyzeFunction, Prog); |
| 109 | FunctionDecl *FD; |
| 110 | TranslationUnit *TU; |
| 111 | llvm::tie(FD, TU) = Idxer.getDefinitionFor(Ent); |
| 112 | |
| 113 | if (!FD) |
| 114 | return 0; |
Zhongxing Xu | 81cc955 | 2010-07-02 07:03:03 +0000 | [diff] [blame] | 115 | return 0; |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 116 | } |