aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2023-03-08 16:13:28 +0100
committerSam McCall <sam.mccall@gmail.com>2023-04-19 15:37:06 +0200
commita443b3d18ef4d01e767994845b3f2819480a7b48 (patch)
tree681aeb8b5c1dfe24275c1f6aca8b0bc7301cc732 /clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
parent50639d3d0dfcc62405a04763a7603021408cfa84 (diff)
downloadllvm-a443b3d18ef4d01e767994845b3f2819480a7b48.zip
llvm-a443b3d18ef4d01e767994845b3f2819480a7b48.tar.gz
llvm-a443b3d18ef4d01e767994845b3f2819480a7b48.tar.bz2
[dataflow] add HTML logger: browse code/cfg/analysis timeline/state
With -dataflow-log=/dir we will write /dir/0.html etc for each function analyzed. These files show the function's code and CFG, and the path through the CFG taken by the analysis. At each analysis point we can see the lattice state. Currently the lattice state dump is not terribly useful but we can improve this: showing values associated with the current Expr, simplifying flow condition, highlighting changes etc. (Trying not to let this patch scope-creep too much, so I ripped out the half-finished features) Demo: https://htmlpreview.github.io/?https://gist.githubusercontent.com/sam-mccall/1746985bf13406bd19181af281aea9ff/raw/9718fdd48406dabccb3092acd983b4bd55da9dfa/analysis.html Differential Revision: https://reviews.llvm.org/D146591
Diffstat (limited to 'clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp')
-rw-r--r--clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp41
1 files changed, 36 insertions, 5 deletions
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
index ad57fd1..5dd390e 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -20,14 +20,17 @@
#include "llvm/ADT/SetOperations.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
#include <cassert>
#include <memory>
#include <utility>
-static llvm::cl::opt<std::string>
- DataflowLog("dataflow-log", llvm::cl::Hidden, llvm::cl::ValueOptional,
- llvm::cl::desc("Emit log of dataflow analysis. With no arg, "
- "writes textual log to stderr."));
+static llvm::cl::opt<std::string> DataflowLog(
+ "dataflow-log", llvm::cl::Hidden, llvm::cl::ValueOptional,
+ llvm::cl::desc("Emit log of dataflow analysis. With no arg, writes textual "
+ "log to stderr. With an arg, writes HTML logs under the "
+ "specified directory (one per analyzed function)."));
namespace clang {
namespace dataflow {
@@ -218,6 +221,34 @@ DataflowAnalysisContext::getControlFlowContext(const FunctionDecl *F) {
return nullptr;
}
+static std::unique_ptr<Logger> makeLoggerFromCommandLine() {
+ if (DataflowLog.empty())
+ return Logger::textual(llvm::errs());
+
+ llvm::StringRef Dir = DataflowLog;
+ if (auto EC = llvm::sys::fs::create_directories(Dir))
+ llvm::errs() << "Failed to create log dir: " << EC.message() << "\n";
+ // All analysis runs within a process will log to the same directory.
+ // Share a counter so they don't all overwrite each other's 0.html.
+ // (Don't share a logger, it's not threadsafe).
+ static std::atomic<unsigned> Counter = {0};
+ auto StreamFactory =
+ [Dir(Dir.str())]() mutable -> std::unique_ptr<llvm::raw_ostream> {
+ llvm::SmallString<256> File(Dir);
+ llvm::sys::path::append(File,
+ std::to_string(Counter.fetch_add(1)) + ".html");
+ std::error_code EC;
+ auto OS = std::make_unique<llvm::raw_fd_ostream>(File, EC);
+ if (EC) {
+ llvm::errs() << "Failed to create log " << File << ": " << EC.message()
+ << "\n";
+ return std::make_unique<llvm::raw_null_ostream>();
+ }
+ return OS;
+ };
+ return Logger::html(std::move(StreamFactory));
+}
+
DataflowAnalysisContext::DataflowAnalysisContext(std::unique_ptr<Solver> S,
Options Opts)
: S(std::move(S)), A(std::make_unique<Arena>()), Opts(Opts) {
@@ -227,7 +258,7 @@ DataflowAnalysisContext::DataflowAnalysisContext(std::unique_ptr<Solver> S,
// based tools.
if (Opts.Log == nullptr) {
if (DataflowLog.getNumOccurrences() > 0) {
- LogOwner = Logger::textual(llvm::errs());
+ LogOwner = makeLoggerFromCommandLine();
this->Opts.Log = LogOwner.get();
// FIXME: if the flag is given a value, write an HTML log to a file.
} else {