From: Ted Kremenek Date: Wed, 2 Jul 2008 18:23:21 +0000 (+0000) Subject: Moved logic for -dump-cfg and -view-cfg into AnalysisConsumer. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=902141f74ac3a2cd41966a98cd43e8702bc05ce9;p=clang Moved logic for -dump-cfg and -view-cfg into AnalysisConsumer. Renamed -dump-cfg to -cfg-dump, and -view-cfg to -cfg-view. This naming better matches the same options for asts (e.g. -ast-dump). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53041 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp index 2d6eb69ac2..4e909fc908 100644 --- a/Driver/ASTConsumers.cpp +++ b/Driver/ASTConsumers.cpp @@ -14,12 +14,10 @@ #include "ASTConsumers.h" #include "HTMLDiagnostics.h" #include "clang/AST/TranslationUnit.h" -#include "clang/Analysis/PathDiagnostic.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/FileManager.h" #include "clang/AST/AST.h" #include "clang/AST/ASTConsumer.h" -#include "clang/AST/CFG.h" #include "llvm/Support/Streams.h" #include "llvm/Support/Timer.h" #include "llvm/ADT/OwningPtr.h" @@ -518,92 +516,6 @@ namespace { ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } - -//===----------------------------------------------------------------------===// -// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit -// the CFGs for all function definitions. - -namespace { - -class CFGVisitor : public ASTConsumer { - std::string FName; -public: - CFGVisitor(const std::string& fname) : FName(fname) {} - CFGVisitor() : FName("") {} - - // CFG Visitor interface to be implemented by subclass. - virtual void VisitCFG(CFG& C, Decl& CD) = 0; - virtual bool printFuncDeclStart() { return true; } - - virtual void HandleTopLevelDecl(Decl *D); -}; - -} // end anonymous namespace - -void CFGVisitor::HandleTopLevelDecl(Decl *D) { - - CFG *C = NULL; - - if (FunctionDecl *FD = dyn_cast(D)) { - - if (!FD->getBody()) - return; - - if (FName.size() > 0 && FName != FD->getIdentifier()->getName()) - return; - - if (printFuncDeclStart()) { - DeclPrinter().PrintFunctionDeclStart(FD); - llvm::cerr << '\n'; - } - - C = CFG::buildCFG(FD->getBody()); - } - else if (ObjCMethodDecl *MD = dyn_cast(D)) { - - if (!MD->getBody()) - return; - - if (FName.size() > 0 && FName != MD->getSelector().getName()) - return; - - if (printFuncDeclStart()) { - DeclPrinter().PrintObjCMethodDecl(MD); - llvm::cerr << '\n'; - } - - C = CFG::buildCFG(MD->getBody()); - } - - if (C) { - VisitCFG(*C, *D); - delete C; - } -} - -//===----------------------------------------------------------------------===// -// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz - -namespace { - class CFGDumper : public CFGVisitor { - const bool UseGraphviz; - public: - CFGDumper(bool use_graphviz, const std::string& fname) - : CFGVisitor(fname), UseGraphviz(use_graphviz) {} - - virtual void VisitCFG(CFG& C, Decl&) { - if (UseGraphviz) - C.viewCFG(); - else - C.dump(); - } - }; -} // end anonymous namespace - -ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs, const std::string& FName) { - return new CFGDumper(ViewGraphs, FName); -} - //===----------------------------------------------------------------------===// // AST Serializer diff --git a/Driver/ASTConsumers.h b/Driver/ASTConsumers.h index 35ecc89eed..c3cadfdcec 100644 --- a/Driver/ASTConsumers.h +++ b/Driver/ASTConsumers.h @@ -36,8 +36,6 @@ ASTConsumer *CreateASTDumper(); ASTConsumer *CreateASTViewer(); -ASTConsumer *CreateCFGDumper(bool ViewGraphs, const std::string& FName); - ASTConsumer *CreateCodeRewriterTest(const std::string& InFile, const std::string& OutFile, Diagnostic &Diags, diff --git a/Driver/AnalysisConsumer.cpp b/Driver/AnalysisConsumer.cpp index ecb4ad23a0..f8d9a250ba 100644 --- a/Driver/AnalysisConsumer.cpp +++ b/Driver/AnalysisConsumer.cpp @@ -337,6 +337,16 @@ static void ActionLiveness(AnalysisManager& mgr) { mgr.getLiveVariables()->dumpBlockLiveness(mgr.getSourceManager()); } +static void ActionCFGDump(AnalysisManager& mgr) { + mgr.DisplayFunction(); + mgr.getCFG()->dump(); +} + +static void ActionCFGView(AnalysisManager& mgr) { + mgr.DisplayFunction(); + mgr.getCFG()->viewCFG(); +} + //===----------------------------------------------------------------------===// // AnalysisConsumer creation. //===----------------------------------------------------------------------===// @@ -376,6 +386,14 @@ ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End, C->addCodeAction(&ActionSimpleChecks); break; + case CFGDump: + C->addCodeAction(&ActionCFGDump); + break; + + case CFGView: + C->addCodeAction(&ActionCFGView); + break; + default: break; } diff --git a/Driver/AnalysisConsumer.h b/Driver/AnalysisConsumer.h index 95de6b56a7..83f3cf67eb 100644 --- a/Driver/AnalysisConsumer.h +++ b/Driver/AnalysisConsumer.h @@ -17,6 +17,8 @@ namespace clang { enum Analyses { + CFGDump, + CFGView, WarnDeadStores, WarnUninitVals, DisplayLiveVariables, diff --git a/Driver/clang.cpp b/Driver/clang.cpp index 0816f52fa4..0c6f30e20a 100644 --- a/Driver/clang.cpp +++ b/Driver/clang.cpp @@ -72,8 +72,6 @@ enum ProgActions { ASTPrint, // Parse ASTs and print them. ASTDump, // Parse ASTs and dump them. ASTView, // Parse ASTs and view them in Graphviz. - ParseCFGDump, // Parse ASTS. Build CFGs. Print CFGs. - ParseCFGView, // Parse ASTS. Build CFGs. View CFGs. TestSerialization, // Run experimental serialization code. ParsePrintCallbacks, // Parse and print each callback. ParseSyntaxOnly, // Parse and perform semantic analysis. @@ -108,10 +106,6 @@ ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, "Build ASTs and then debug dump them"), clEnumValN(ASTView, "ast-view", "Build ASTs and view them with GraphViz"), - clEnumValN(ParseCFGDump, "dump-cfg", - "Run parser, then build and print CFGs"), - clEnumValN(ParseCFGView, "view-cfg", - "Run parser, then build and view CFGs with Graphviz"), clEnumValN(TestSerialization, "test-pickling", "Run prototype serialization code"), clEnumValN(EmitLLVM, "emit-llvm", @@ -168,6 +162,8 @@ AnalyzeAll("checker-opt-analyze-headers", static llvm::cl::list AnalysisList(llvm::cl::desc("Available Source Code Analyses:"), llvm::cl::values( +clEnumValN(CFGDump, "cfg-dump", "Display Control-Flow Graphs"), +clEnumValN(CFGView, "cfg-view", "View Control-Flow Graphs using GraphViz"), clEnumValN(DisplayLiveVariables, "dump-live-variables", "Print results of live variable analysis"), clEnumValN(WarnDeadStores, "warn-dead-stores", @@ -1189,12 +1185,7 @@ static ASTConsumer* CreateASTConsumer(const std::string& InFile, case EmitHTML: return CreateHTMLPrinter(OutputFile, Diag, PP, PPF); - - case ParseCFGDump: - case ParseCFGView: - return CreateCFGDumper(ProgAction == ParseCFGView, - AnalyzeSpecificFunction); - + case TestSerialization: return CreateSerializationTest(Diag, FileMgr);