From f1906e93cab1fd0ed62e9e962a087b419724fc5d Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Mon, 22 Aug 2022 10:40:55 +0800 Subject: [PATCH] debug: Add printing option for locations with cfg-text. --- cfg/CFG.cc | 22 ++++++++++++++++++---- cfg/CFG.h | 4 ++-- docs/scip-ruby/CONTRIBUTING.md | 4 ++-- main/options/options.cc | 2 ++ main/options/options.h | 1 + main/pipeline/pipeline.cc | 3 +++ 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cfg/CFG.cc b/cfg/CFG.cc index c4714f3375..7027acb1f7 100644 --- a/cfg/CFG.cc +++ b/cfg/CFG.cc @@ -238,7 +238,7 @@ string CFG::toString(const core::GlobalState &gs) const { return to_string(buf); } -string CFG::toTextualString(const core::GlobalState &gs) const { +string CFG::toTextualString(const core::GlobalState &gs, optional file) const { fmt::memory_buffer buf; string symbolName = this->symbol.showFullName(gs); fmt::format_to(std::back_inserter(buf), "method {} {{\n\n", symbolName); @@ -251,7 +251,7 @@ string CFG::toTextualString(const core::GlobalState &gs) const { } } - fmt::format_to(std::back_inserter(buf), "{}\n", basicBlock->toTextualString(gs, *this)); + fmt::format_to(std::back_inserter(buf), "{}\n", basicBlock->toTextualString(gs, file, *this)); } fmt::format_to(std::back_inserter(buf), "}}"); return to_string(buf); @@ -366,7 +366,7 @@ string BasicBlock::toString(const core::GlobalState &gs, const CFG &cfg) const { return to_string(buf); } -string BasicBlock::toTextualString(const core::GlobalState &gs, const CFG &cfg) const { +string BasicBlock::toTextualString(const core::GlobalState &gs, optional file, const CFG &cfg) const { fmt::memory_buffer buf; fmt::format_to(std::back_inserter(buf), "bb{}[rubyRegionId={}, firstDead={}]({}):\n", this->id, this->rubyRegionId, this->firstDeadInstructionIdx, @@ -377,7 +377,21 @@ string BasicBlock::toTextualString(const core::GlobalState &gs, const CFG &cfg) fmt::format_to(std::back_inserter(buf), " # outerLoops: {}\n", this->outerLoops); } for (const Binding &exp : this->exprs) { - fmt::format_to(std::back_inserter(buf), " {} = {}\n", exp.bind.toString(gs, cfg), + string positionText = ""; + if (file) { + if (exp.loc.exists() && !exp.loc.empty()) { + auto lineCol = core::Loc(file.value(), exp.loc).position(gs); + positionText = + lineCol.first.line == lineCol.second.line + ? fmt::format(" @ {}:{}-{}", lineCol.first.line, lineCol.first.column, lineCol.second.column) + : fmt::format(" @ {}:{}-{}:{}", lineCol.first.line, lineCol.first.column, lineCol.second.line, + lineCol.second.column); + } else { + positionText = " @ <>"; + } + } + + fmt::format_to(std::back_inserter(buf), " {}{} = {}\n", exp.bind.toString(gs, cfg), positionText, exp.value.toString(gs, cfg)); } diff --git a/cfg/CFG.h b/cfg/CFG.h index 4628a7eff0..c306004b60 100644 --- a/cfg/CFG.h +++ b/cfg/CFG.h @@ -105,7 +105,7 @@ class BasicBlock final { std::optional maybeGetUpdateKnowledgeReceiver(const cfg::CFG &inWhat) const; std::string toString(const core::GlobalState &gs, const CFG &cfg) const; - std::string toTextualString(const core::GlobalState &gs, const CFG &cfg) const; + std::string toTextualString(const core::GlobalState &gs, std::optional file, const CFG &cfg) const; std::string showRaw(const core::GlobalState &gs, const CFG &cfg) const; }; @@ -166,7 +166,7 @@ class CFG final { // Abbreviated debug output in dot format, useful if you already know what you're looking at std::string toString(const core::GlobalState &gs) const; // As above, but without dot annotations - std::string toTextualString(const core::GlobalState &gs) const; + std::string toTextualString(const core::GlobalState &gs, std::optional = std::nullopt) const; // Verbose debug output std::string showRaw(core::Context ctx) const; diff --git a/docs/scip-ruby/CONTRIBUTING.md b/docs/scip-ruby/CONTRIBUTING.md index 7997e578f2..46cb130459 100644 --- a/docs/scip-ruby/CONTRIBUTING.md +++ b/docs/scip-ruby/CONTRIBUTING.md @@ -199,8 +199,8 @@ along with the control flow graph all at once. Typically, I'll copy over the minimized code to the root and run: -``` -./bazel build //main:scip-ruby --config=dbg && ./bazel-out/darwin-dbg/bin/main/scip-ruby tmp.rb -p cfg-text --index-file /dev/null +```bash +./bazel build //main:scip-ruby --config=dbg && ./bazel-out/darwin-dbg/bin/main/scip-ruby tmp.rb -p cfg-text-loc --index-file /dev/null ``` Alternately, it may be useful to create a `tmp.rb` diff --git a/main/options/options.cc b/main/options/options.cc index 225ca1f359..51062f2e38 100644 --- a/main/options/options.cc +++ b/main/options/options.cc @@ -55,6 +55,7 @@ const vector print_options({ {"cfg", &Printers::CFG}, {"cfg-raw", &Printers::CFGRaw}, {"cfg-text", &Printers::CFGText}, + {"cfg-text-loc", &Printers::CFGTextLoc}, {"symbol-table", &Printers::SymbolTable}, {"symbol-table-raw", &Printers::SymbolTableRaw}, {"symbol-table-json", &Printers::SymbolTableJson}, @@ -128,6 +129,7 @@ vector> Printers::printers() { ASTRaw, CFG, CFGText, + CFGTextLoc, CFGRaw, SymbolTable, SymbolTableRaw, diff --git a/main/options/options.h b/main/options/options.h index d07d91c03b..4c6868e9d6 100644 --- a/main/options/options.h +++ b/main/options/options.h @@ -60,6 +60,7 @@ struct Printers { PrinterConfig ASTRaw; PrinterConfig CFG; PrinterConfig CFGText; + PrinterConfig CFGTextLoc; PrinterConfig CFGRaw; PrinterConfig TypedSource; PrinterConfig SymbolTable; diff --git a/main/pipeline/pipeline.cc b/main/pipeline/pipeline.cc index 713f135a8b..59b7fdd0c7 100644 --- a/main/pipeline/pipeline.cc +++ b/main/pipeline/pipeline.cc @@ -78,6 +78,9 @@ class CFGCollectorAndTyper { if (print.CFGText.enabled) { print.CFG.fmt("{}\n\n", cfg->toTextualString(ctx)); } + if (print.CFGTextLoc.enabled) { + print.CFG.fmt("{}\n\n", cfg->toTextualString(ctx, ctx.file)); + } if (print.CFGRaw.enabled) { print.CFGRaw.fmt("{}\n\n", cfg->showRaw(ctx)); }