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" |
Zhongxing Xu | 65d336b | 2010-07-06 09:18:02 +0000 | [diff] [blame] | 17 | #include "clang/Checker/PathSensitive/AnalysisManager.h" |
| 18 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
| 19 | #include "clang/Checker/PathSensitive/GRTransferFuncs.h" |
| 20 | #include "clang/Checker/Checkers/LocalCheckers.h" |
Daniel Dunbar | 8fd57fe | 2009-12-03 07:20:04 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/ASTUnit.h" |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 8fd57fe | 2009-12-03 07:20:04 +0000 | [diff] [blame] | 23 | #include "clang/Index/CallGraph.h" |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 24 | #include "clang/Index/Indexer.h" |
| 25 | #include "clang/Index/TranslationUnit.h" |
| 26 | #include "clang/Index/DeclReferenceMap.h" |
| 27 | #include "clang/Index/SelectorMap.h" |
Zhongxing Xu | 65d336b | 2010-07-06 09:18:02 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | f45d673 | 2010-04-06 01:25:58 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | using namespace clang; |
| 33 | using namespace idx; |
| 34 | |
| 35 | static llvm::cl::list<std::string> |
| 36 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>")); |
| 37 | |
Zhongxing Xu | 81cc955 | 2010-07-02 07:03:03 +0000 | [diff] [blame] | 38 | static llvm::cl::opt<bool> |
| 39 | ViewCallGraph("view-call-graph", llvm::cl::desc("Display the call graph.")); |
| 40 | |
| 41 | static llvm::cl::opt<std::string> |
| 42 | AnalyzeFunction("analyze-function", |
| 43 | llvm::cl::desc("Specify the entry function.")); |
Zhongxing Xu | 9b80ceb | 2010-07-02 06:58:30 +0000 | [diff] [blame] | 44 | |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | // A thin wrapper over ASTUnit implementing the TranslationUnit interface. |
| 47 | class ASTUnitTU : public TranslationUnit { |
| 48 | ASTUnit *AST; |
| 49 | DeclReferenceMap DeclRefMap; |
| 50 | SelectorMap SelMap; |
| 51 | |
| 52 | public: |
| 53 | ASTUnitTU(ASTUnit *ast) |
| 54 | : AST(ast), DeclRefMap(AST->getASTContext()), SelMap(AST->getASTContext()) { |
| 55 | } |
| 56 | |
| 57 | virtual ASTContext &getASTContext() { |
| 58 | return AST->getASTContext(); |
| 59 | } |
Zhongxing Xu | 65d336b | 2010-07-06 09:18:02 +0000 | [diff] [blame] | 60 | |
| 61 | virtual Preprocessor &getPreprocessor() { |
| 62 | return AST->getPreprocessor(); |
| 63 | } |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 64 | |
| 65 | virtual DeclReferenceMap &getDeclReferenceMap() { |
| 66 | return DeclRefMap; |
| 67 | } |
| 68 | |
| 69 | virtual SelectorMap &getSelectorMap() { |
| 70 | return SelMap; |
| 71 | } |
| 72 | }; |
| 73 | } |
| 74 | |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 75 | int main(int argc, char **argv) { |
| 76 | llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa"); |
Argyrios Kyrtzidis | 0594545 | 2009-07-29 23:39:09 +0000 | [diff] [blame] | 77 | std::vector<ASTUnit*> ASTUnits; |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 78 | |
Zhongxing Xu | 6d956df | 2010-07-02 06:39:46 +0000 | [diff] [blame] | 79 | Program Prog; |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 80 | Indexer Idxer(Prog); |
Zhongxing Xu | 6d956df | 2010-07-02 06:39:46 +0000 | [diff] [blame] | 81 | |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 82 | if (InputFilenames.empty()) |
| 83 | return 0; |
| 84 | |
Daniel Dunbar | bb3503a | 2009-12-06 09:56:30 +0000 | [diff] [blame] | 85 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 7969f93 | 2010-04-06 04:03:12 +0000 | [diff] [blame] | 86 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags |
| 87 | = CompilerInstance::createDiagnostics(DiagOpts, argc, argv); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 88 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| 89 | const std::string &InFile = InputFilenames[i]; |
Douglas Gregor | f45d673 | 2010-04-06 01:25:58 +0000 | [diff] [blame] | 90 | llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, Diags)); |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 91 | if (!AST) |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 92 | return 1; |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 93 | |
Argyrios Kyrtzidis | 0594545 | 2009-07-29 23:39:09 +0000 | [diff] [blame] | 94 | ASTUnits.push_back(AST.take()); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Zhongxing Xu | 9b80ceb | 2010-07-02 06:58:30 +0000 | [diff] [blame] | 97 | if (ViewCallGraph) { |
| 98 | llvm::OwningPtr<CallGraph> CG; |
| 99 | CG.reset(new CallGraph(Prog)); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 100 | |
Zhongxing Xu | 9b80ceb | 2010-07-02 06:58:30 +0000 | [diff] [blame] | 101 | for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) |
| 102 | CG->addTU(ASTUnits[i]->getASTContext()); |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 103 | |
Zhongxing Xu | 9b80ceb | 2010-07-02 06:58:30 +0000 | [diff] [blame] | 104 | CG->ViewCallGraph(); |
| 105 | return 0; |
| 106 | } |
Zhongxing Xu | 81cc955 | 2010-07-02 07:03:03 +0000 | [diff] [blame] | 107 | |
| 108 | if (AnalyzeFunction.empty()) |
| 109 | return 0; |
| 110 | |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 111 | // Feed all ASTUnits to the Indexer. |
| 112 | for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) { |
Zhongxing Xu | 65d336b | 2010-07-06 09:18:02 +0000 | [diff] [blame] | 113 | ASTUnitTU *TU = new ASTUnitTU(ASTUnits[i]); |
| 114 | Idxer.IndexAST(TU); |
Zhongxing Xu | bed95e2 | 2010-07-02 11:52:15 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Zhongxing Xu | dc01a15 | 2010-07-06 05:55:13 +0000 | [diff] [blame] | 117 | Entity Ent = Entity::get(AnalyzeFunction, Prog); |
| 118 | FunctionDecl *FD; |
| 119 | TranslationUnit *TU; |
| 120 | llvm::tie(FD, TU) = Idxer.getDefinitionFor(Ent); |
| 121 | |
| 122 | if (!FD) |
| 123 | return 0; |
Zhongxing Xu | 65d336b | 2010-07-06 09:18:02 +0000 | [diff] [blame] | 124 | |
| 125 | // Create an analysis engine. |
| 126 | Preprocessor &PP = TU->getPreprocessor(); |
| 127 | |
| 128 | // Hard code options for now. |
| 129 | AnalysisManager AMgr(TU->getASTContext(), PP.getDiagnostics(), |
| 130 | PP.getLangOptions(), /* PathDiagnostic */ 0, |
| 131 | CreateRegionStoreManager, |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame^] | 132 | CreateRangeConstraintManager, &Idxer, |
Zhongxing Xu | 65d336b | 2010-07-06 09:18:02 +0000 | [diff] [blame] | 133 | /* MaxNodes */ 300000, /* MaxLoop */ 3, |
| 134 | /* VisualizeEG */ false, /* VisualizeEGUbi */ false, |
| 135 | /* PurgeDead */ true, /* EagerlyAssume */ false, |
| 136 | /* TrimGraph */ false, /* InlineCall */ true); |
| 137 | |
| 138 | GRTransferFuncs* TF = MakeCFRefCountTF(AMgr.getASTContext(), /*GC*/false, |
| 139 | AMgr.getLangOptions()); |
| 140 | GRExprEngine Eng(AMgr, TF); |
| 141 | |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame^] | 142 | Eng.ExecuteWorkList(AMgr.getStackFrame(FD, TU), AMgr.getMaxNodes()); |
Zhongxing Xu | 65d336b | 2010-07-06 09:18:02 +0000 | [diff] [blame] | 143 | |
Zhongxing Xu | 81cc955 | 2010-07-02 07:03:03 +0000 | [diff] [blame] | 144 | return 0; |
Zhongxing Xu | dc3240c | 2009-07-16 01:00:25 +0000 | [diff] [blame] | 145 | } |