summaryrefslogtreecommitdiffstats
path: root/examples/wpa/clang-wpa.cpp
diff options
context:
space:
mode:
authorrdivacky <rdivacky@FreeBSD.org>2010-07-13 17:21:42 +0000
committerrdivacky <rdivacky@FreeBSD.org>2010-07-13 17:21:42 +0000
commit1928da94b55683957759d5c5ff4593a118773394 (patch)
tree48b44512b5db8ced345df4a1a56b5065cf2a14d9 /examples/wpa/clang-wpa.cpp
parent53992adde3eda3ccf9da63bc7e45673f043de18f (diff)
downloadFreeBSD-src-1928da94b55683957759d5c5ff4593a118773394.zip
FreeBSD-src-1928da94b55683957759d5c5ff4593a118773394.tar.gz
Update clang to r108243.
Diffstat (limited to 'examples/wpa/clang-wpa.cpp')
-rw-r--r--examples/wpa/clang-wpa.cpp101
1 files changed, 95 insertions, 6 deletions
diff --git a/examples/wpa/clang-wpa.cpp b/examples/wpa/clang-wpa.cpp
index b515e33..74ec368 100644
--- a/examples/wpa/clang-wpa.cpp
+++ b/examples/wpa/clang-wpa.cpp
@@ -14,9 +14,18 @@
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
+#include "clang/Checker/PathSensitive/AnalysisManager.h"
+#include "clang/Checker/PathSensitive/GRExprEngine.h"
+#include "clang/Checker/PathSensitive/GRTransferFuncs.h"
+#include "clang/Checker/Checkers/LocalCheckers.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Index/CallGraph.h"
+#include "clang/Index/Indexer.h"
+#include "clang/Index/TranslationUnit.h"
+#include "clang/Index/DeclReferenceMap.h"
+#include "clang/Index/SelectorMap.h"
+#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
@@ -26,11 +35,50 @@ using namespace idx;
static llvm::cl::list<std::string>
InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>"));
+static llvm::cl::opt<bool>
+ViewCallGraph("view-call-graph", llvm::cl::desc("Display the call graph."));
+
+static llvm::cl::opt<std::string>
+AnalyzeFunction("analyze-function",
+ llvm::cl::desc("Specify the entry function."));
+
+namespace {
+// A thin wrapper over ASTUnit implementing the TranslationUnit interface.
+class ASTUnitTU : public TranslationUnit {
+ ASTUnit *AST;
+ DeclReferenceMap DeclRefMap;
+ SelectorMap SelMap;
+
+public:
+ ASTUnitTU(ASTUnit *ast)
+ : AST(ast), DeclRefMap(AST->getASTContext()), SelMap(AST->getASTContext()) {
+ }
+
+ virtual ASTContext &getASTContext() {
+ return AST->getASTContext();
+ }
+
+ virtual Preprocessor &getPreprocessor() {
+ return AST->getPreprocessor();
+ }
+
+ virtual DeclReferenceMap &getDeclReferenceMap() {
+ return DeclRefMap;
+ }
+
+ virtual SelectorMap &getSelectorMap() {
+ return SelMap;
+ }
+};
+}
+
int main(int argc, char **argv) {
llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa");
- FileManager FileMgr;
std::vector<ASTUnit*> ASTUnits;
+ Program Prog;
+ Indexer Idxer(Prog);
+
if (InputFilenames.empty())
return 0;
@@ -46,11 +94,52 @@ int main(int argc, char **argv) {
ASTUnits.push_back(AST.take());
}
- llvm::OwningPtr<CallGraph> CG;
- CG.reset(new CallGraph());
+ if (ViewCallGraph) {
+ llvm::OwningPtr<CallGraph> CG;
+ CG.reset(new CallGraph(Prog));
+
+ for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i)
+ CG->addTU(ASTUnits[i]->getASTContext());
+
+ CG->ViewCallGraph();
+ return 0;
+ }
+
+ if (AnalyzeFunction.empty())
+ return 0;
+
+ // Feed all ASTUnits to the Indexer.
+ for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) {
+ ASTUnitTU *TU = new ASTUnitTU(ASTUnits[i]);
+ Idxer.IndexAST(TU);
+ }
+
+ Entity Ent = Entity::get(AnalyzeFunction, Prog);
+ FunctionDecl *FD;
+ TranslationUnit *TU;
+ llvm::tie(FD, TU) = Idxer.getDefinitionFor(Ent);
+
+ if (!FD)
+ return 0;
+
+ // Create an analysis engine.
+ Preprocessor &PP = TU->getPreprocessor();
+
+ // Hard code options for now.
+ AnalysisManager AMgr(TU->getASTContext(), PP.getDiagnostics(),
+ PP.getLangOptions(), /* PathDiagnostic */ 0,
+ CreateRegionStoreManager,
+ CreateRangeConstraintManager,
+ /* MaxNodes */ 300000, /* MaxLoop */ 3,
+ /* VisualizeEG */ false, /* VisualizeEGUbi */ false,
+ /* PurgeDead */ true, /* EagerlyAssume */ false,
+ /* TrimGraph */ false, /* InlineCall */ true);
- for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i)
- CG->addTU(ASTUnits[i]->getASTContext());
+ GRTransferFuncs* TF = MakeCFRefCountTF(AMgr.getASTContext(), /*GC*/false,
+ AMgr.getLangOptions());
+ GRExprEngine Eng(AMgr, TF);
- CG->ViewCallGraph();
+ Eng.ExecuteWorkList(AMgr.getStackFrame(FD), AMgr.getMaxNodes());
+
+ return 0;
}
OpenPOWER on IntegriCloud