blob: dff8cb0b524990b6b7193f2725c37acb350900d6 [file] [log] [blame]
Zhongxing Xu22daf792009-07-16 01:03:49 +00001//===--- 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 Xudc3240c2009-07-16 01:00:25 +000015#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"
21using namespace clang;
22using namespace idx;
23
24static llvm::cl::list<std::string>
25InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>"));
26
27// FIXME: this duplicates the one in index-test.cpp.
28class TUnit : public TranslationUnit {
29public:
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
37int 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
68 CG->dump();
69}