From c704f4fbd0a41090e68aaf056b31803481f535ff Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Fri, 28 Sep 2018 18:49:21 +0000 Subject: [PATCH] [analyzer] Provide an option to dump generated exploded graphs to a given file. Dumping graphs instead of opening them is often very useful, e.g. for transfer or converting to SVG. Basic sanity check for generated exploded graphs. Differential Revision: https://reviews.llvm.org/D52637 llvm-svn: 343352 --- clang/include/clang/Driver/CC1Options.td | 3 ++ .../clang/StaticAnalyzer/Core/AnalyzerOptions.h | 3 ++ .../StaticAnalyzer/Core/PathSensitive/ExprEngine.h | 12 ++++++++ clang/lib/Frontend/CompilerInvocation.cpp | 1 + clang/lib/StaticAnalyzer/Core/ExprEngine.cpp | 35 ++++++++++++++++++---- .../StaticAnalyzer/Frontend/AnalysisConsumer.cpp | 3 ++ clang/test/Analysis/dump_egraph.c | 15 ++++++++++ 7 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 clang/test/Analysis/dump_egraph.c diff --git a/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td index 511dded..e4fd133 100644 --- a/clang/include/clang/Driver/CC1Options.td +++ b/clang/include/clang/Driver/CC1Options.td @@ -80,6 +80,9 @@ def trim_egraph : Flag<["-"], "trim-egraph">, HelpText<"Only show error-related paths in the analysis graph">; def analyzer_viz_egraph_graphviz : Flag<["-"], "analyzer-viz-egraph-graphviz">, HelpText<"Display exploded graph using GraphViz">; +def analyzer_dump_egraph : Separate<["-"], "analyzer-dump-egraph">, + HelpText<"Dump exploded graph to the specified file">; +def analyzer_dump_egraph_EQ : Joined<["-"], "analyzer-dump-egraph=">, Alias; def analyzer_inline_max_stack_depth : Separate<["-"], "analyzer-inline-max-stack-depth">, HelpText<"Bound on stack depth while inlining (4 by default)">; diff --git a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h index 67de75b..715cc2b 100644 --- a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h +++ b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h @@ -140,6 +140,9 @@ public: std::string AnalyzeSpecificFunction; + /// File path to which the exploded graph should be dumped. + std::string DumpExplodedGraphTo; + /// Store full compiler invocation for reproducible instructions in the /// generated report. std::string FullCompilerInvocation; diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h index a8a273b..c9c2b4f 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h @@ -204,6 +204,18 @@ public: void enqueueEndOfPath(ExplodedNodeSet &S); void GenerateCallExitNode(ExplodedNode *N); + + /// Dump graph to the specified filename. + /// If filename is empty, generate a temporary one. + /// \return The filename the graph is written into. + std::string DumpGraph(bool trim = false, StringRef Filename=""); + + /// Dump the graph consisting of the given nodes to a specified filename. + /// Generate a temporary filename if it's not provided. + /// \return The filename the graph is written into. + std::string DumpGraph(ArrayRef Nodes, + StringRef Filename = ""); + /// Visualize the ExplodedGraph created by executing the simulation. void ViewGraph(bool trim = false); diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 55745b1..782c796 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -284,6 +284,7 @@ static bool ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args, Opts.visualizeExplodedGraphWithGraphViz = Args.hasArg(OPT_analyzer_viz_egraph_graphviz); + Opts.DumpExplodedGraphTo = Args.getLastArgValue(OPT_analyzer_dump_egraph); Opts.NoRetryExhausted = Args.hasArg(OPT_analyzer_disable_retry_exhausted); Opts.AnalyzeAll = Args.hasArg(OPT_analyzer_opt_analyze_headers); Opts.AnalyzerDisplayProgress = Args.hasArg(OPT_analyzer_display_progress); diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp index 90f8920..c19f056 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -3058,6 +3058,23 @@ struct DOTGraphTraits : public DefaultDOTGraphTraits { void ExprEngine::ViewGraph(bool trim) { #ifndef NDEBUG + std::string Filename = DumpGraph(trim); + llvm::DisplayGraph(Filename, false, llvm::GraphProgram::DOT); +#endif + llvm::errs() << "Warning: viewing graph requires assertions" << "\n"; +} + + +void ExprEngine::ViewGraph(ArrayRef Nodes) { +#ifndef NDEBUG + std::string Filename = DumpGraph(Nodes); + llvm::DisplayGraph(Filename, false, llvm::GraphProgram::DOT); +#endif + llvm::errs() << "Warning: viewing graph requires assertions" << "\n"; +} + +std::string ExprEngine::DumpGraph(bool trim, StringRef Filename) { +#ifndef NDEBUG if (trim) { std::vector Src; @@ -3067,22 +3084,30 @@ void ExprEngine::ViewGraph(bool trim) { const auto *N = const_cast(EI->begin()->getErrorNode()); if (N) Src.push_back(N); } - - ViewGraph(Src); + return DumpGraph(Src, Filename); } else { - llvm::ViewGraph(&G, "ExprEngine"); + return llvm::WriteGraph(&G, "ExprEngine", /*ShortNames=*/false, + /*Title=*/"Exploded Graph", /*Filename=*/Filename); } #endif + llvm::errs() << "Warning: dumping graph requires assertions" << "\n"; + return ""; } -void ExprEngine::ViewGraph(ArrayRef Nodes) { +std::string ExprEngine::DumpGraph(ArrayRef Nodes, + StringRef Filename) { #ifndef NDEBUG std::unique_ptr TrimmedG(G.trim(Nodes)); if (!TrimmedG.get()) { llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n"; } else { - llvm::ViewGraph(TrimmedG.get(), "TrimmedExprEngine"); + return llvm::WriteGraph(TrimmedG.get(), "TrimmedExprEngine", + /*ShortNames=*/false, + /*Title=*/"Trimmed Exploded Graph", + /*Filename=*/Filename); } #endif + llvm::errs() << "Warning: dumping graph requires assertions" << "\n"; + return ""; } diff --git a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp index d42e3a9..ad17dae 100644 --- a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp +++ b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp @@ -745,6 +745,9 @@ void AnalysisConsumer::ActionExprEngine(Decl *D, bool ObjCGCEnabled, Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D), Mgr->options.getMaxNodesPerTopLevelFunction()); + if (!Mgr->options.DumpExplodedGraphTo.empty()) + Eng.DumpGraph(Mgr->options.TrimGraph, Mgr->options.DumpExplodedGraphTo); + // Visualize the exploded graph. if (Mgr->options.visualizeExplodedGraphWithGraphViz) Eng.ViewGraph(Mgr->options.TrimGraph); diff --git a/clang/test/Analysis/dump_egraph.c b/clang/test/Analysis/dump_egraph.c new file mode 100644 index 0000000..70b7e1f --- /dev/null +++ b/clang/test/Analysis/dump_egraph.c @@ -0,0 +1,15 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-dump-egraph=%t.dot %s +// RUN: cat %t.dot | FileCheck %s +// REQUIRES: asserts + +int getJ(); + +int foo() { + int *x = 0; + return *x; +} + +// CHECK: digraph "Exploded Graph" { +// CHECK: Edge: (B2, B1) +// CHECK: Block Entrance: B1 +// CHECK: Bug report attached -- 2.7.4