[PATCH] D141716: [clang][dataflow] Add (initial) debug printing for `Value` and `Environment`.

2023-01-19 Thread Yitzhak Mandelbaum via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
ymandel marked an inline comment as done.
Closed by commit rGc441f65f9183: [clang][dataflow] Add (initial) debug printing 
for `Value` and `Environment`. (authored by ymandel).

Changed prior to commit:
  https://reviews.llvm.org/D141716?vs=490470=490496#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141716/new/

https://reviews.llvm.org/D141716

Files:
  clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
  clang/include/clang/Analysis/FlowSensitive/Value.h
  clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
  clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
  clang/lib/Analysis/FlowSensitive/Value.cpp

Index: clang/lib/Analysis/FlowSensitive/Value.cpp
===
--- clang/lib/Analysis/FlowSensitive/Value.cpp
+++ clang/lib/Analysis/FlowSensitive/Value.cpp
@@ -11,6 +11,7 @@
 //===--===//
 
 #include "clang/Analysis/FlowSensitive/Value.h"
+#include "clang/Analysis/FlowSensitive/DebugSupport.h"
 #include "llvm/Support/Casting.h"
 
 namespace clang {
@@ -35,5 +36,21 @@
  areEquivalentIndirectionValues(Val1, Val2)));
 }
 
+raw_ostream <<(raw_ostream , const Value ) {
+  switch (Val.getKind()) {
+  case Value::Kind::Reference: {
+const auto *RV = cast();
+return OS << "Reference(" << >getReferentLoc() << ")";
+  }
+  case Value::Kind::Pointer: {
+const auto *PV = dyn_cast();
+return OS << "Pointer(" << >getPointeeLoc() << ")";
+  }
+  // FIXME: support remaining cases.
+  default:
+return OS << debugString(Val.getKind());
+  }
+}
+
 } // namespace dataflow
 } // namespace clang
Index: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
===
--- clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
+++ clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
@@ -32,8 +32,10 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/Error.h"
-#include "llvm/Support/ErrorHandling.h"
+
+#define DEBUG_TYPE "clang-dataflow"
 
 namespace clang {
 namespace dataflow {
@@ -431,6 +433,8 @@
   std::min(RelativeMaxIterations, AbsoluteMaxIterations);
   uint32_t Iterations = 0;
   while (const CFGBlock *Block = Worklist.dequeue()) {
+LLVM_DEBUG(llvm::dbgs()
+   << "Processing Block " << Block->getBlockID() << "\n");
 if (++Iterations > MaxIterations) {
   return llvm::createStringError(std::errc::timed_out,
  "maximum number of iterations reached");
@@ -440,8 +444,16 @@
 BlockStates[Block->getBlockID()];
 TypeErasedDataflowAnalysisState NewBlockState =
 transferCFGBlock(*Block, AC);
+LLVM_DEBUG({
+  llvm::errs() << "New Env:\n";
+  NewBlockState.Env.dump();
+});
 
 if (OldBlockState) {
+  LLVM_DEBUG({
+llvm::errs() << "Old Env:\n";
+OldBlockState->Env.dump();
+  });
   if (isLoopHead(*Block)) {
 LatticeJoinEffect Effect1 = Analysis.widenTypeErased(
 NewBlockState.Lattice, OldBlockState->Lattice);
Index: clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
===
--- clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -786,9 +786,29 @@
   return DACtx->flowConditionImplies(*FlowConditionToken, Val);
 }
 
-void Environment::dump() const {
+void Environment::dump(raw_ostream ) const {
+  // FIXME: add printing for remaining fields and allow caller to decide what
+  // fields are printed.
+  OS << "DeclToLoc:\n";
+  for (auto [D, L] : DeclToLoc)
+OS << "  [" << D->getName() << ", " << L << "]\n";
+
+  OS << "ExprToLoc:\n";
+  for (auto [E, L] : ExprToLoc)
+OS << "  [" << E << ", " << L << "]\n";
+
+  OS << "LocToVal:\n";
+  for (auto [L, V] : LocToVal) {
+OS << "  [" << L << ", " << V << ": " << *V << "]\n";
+  }
+
+  OS << "FlowConditionToken:\n";
   DACtx->dumpFlowCondition(*FlowConditionToken);
 }
 
+void Environment::dump() const {
+  dump(llvm::dbgs());
+}
+
 } // namespace dataflow
 } // namespace clang
Index: clang/include/clang/Analysis/FlowSensitive/Value.h
===
--- clang/include/clang/Analysis/FlowSensitive/Value.h
+++ clang/include/clang/Analysis/FlowSensitive/Value.h
@@ -19,6 +19,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
 #include 
 #include 
 
@@ -310,6 +311,8 @@
   llvm::DenseMap Children;
 };

[PATCH] D141716: [clang][dataflow] Add (initial) debug printing for `Value` and `Environment`.

2023-01-19 Thread Yitzhak Mandelbaum via Phabricator via cfe-commits
ymandel marked an inline comment as done.
ymandel added inline comments.



Comment at: clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp:792
+  // fields are printed.
+  llvm::dbgs() << "DeclToLoc:\n";
+  for (auto [D, L] : DeclToLoc)

sgatev wrote:
> Shouldn't this be `OS`? Same for those below.
Yes, good catch!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141716/new/

https://reviews.llvm.org/D141716

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D141716: [clang][dataflow] Add (initial) debug printing for `Value` and `Environment`.

2023-01-19 Thread Yitzhak Mandelbaum via Phabricator via cfe-commits
ymandel updated this revision to Diff 490470.
ymandel added a comment.

address comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141716/new/

https://reviews.llvm.org/D141716

Files:
  clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
  clang/include/clang/Analysis/FlowSensitive/Value.h
  clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
  clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
  clang/lib/Analysis/FlowSensitive/Value.cpp

Index: clang/lib/Analysis/FlowSensitive/Value.cpp
===
--- clang/lib/Analysis/FlowSensitive/Value.cpp
+++ clang/lib/Analysis/FlowSensitive/Value.cpp
@@ -11,6 +11,7 @@
 //===--===//
 
 #include "clang/Analysis/FlowSensitive/Value.h"
+#include "clang/Analysis/FlowSensitive/DebugSupport.h"
 #include "llvm/Support/Casting.h"
 
 namespace clang {
@@ -35,5 +36,21 @@
  areEquivalentIndirectionValues(Val1, Val2)));
 }
 
+raw_ostream <<(raw_ostream , const Value ) {
+  switch (Val.getKind()) {
+  case Value::Kind::Reference: {
+const auto *RV = cast();
+return OS << "Reference(" << >getReferentLoc() << ")";
+  }
+  case Value::Kind::Pointer: {
+const auto *PV = dyn_cast();
+return OS << "Pointer(" << >getPointeeLoc() << ")";
+  }
+  // FIXME: support remaining cases.
+  default:
+return OS << debugString(Val.getKind());
+  }
+}
+
 } // namespace dataflow
 } // namespace clang
Index: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
===
--- clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
+++ clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
@@ -32,8 +32,10 @@
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/Error.h"
-#include "llvm/Support/ErrorHandling.h"
+
+#define DEBUG_TYPE "clang-dataflow"
 
 namespace clang {
 namespace dataflow {
@@ -431,6 +433,8 @@
   std::min(RelativeMaxIterations, AbsoluteMaxIterations);
   uint32_t Iterations = 0;
   while (const CFGBlock *Block = Worklist.dequeue()) {
+LLVM_DEBUG(llvm::dbgs()
+   << "Processing Block " << Block->getBlockID() << "\n");
 if (++Iterations > MaxIterations) {
   return llvm::createStringError(std::errc::timed_out,
  "maximum number of iterations reached");
@@ -440,8 +444,16 @@
 BlockStates[Block->getBlockID()];
 TypeErasedDataflowAnalysisState NewBlockState =
 transferCFGBlock(*Block, AC);
+LLVM_DEBUG({
+  llvm::errs() << "New Env:\n";
+  NewBlockState.Env.dump();
+});
 
 if (OldBlockState) {
+  LLVM_DEBUG({
+llvm::errs() << "Old Env:\n";
+OldBlockState->Env.dump();
+  });
   if (isLoopHead(*Block)) {
 LatticeJoinEffect Effect1 = Analysis.widenTypeErased(
 NewBlockState.Lattice, OldBlockState->Lattice);
Index: clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
===
--- clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -786,9 +786,29 @@
   return DACtx->flowConditionImplies(*FlowConditionToken, Val);
 }
 
-void Environment::dump() const {
+void Environment::dump(raw_ostream ) const {
+  // FIXME: add printing for remaining fields and allow caller to decide what
+  // fields are printed.
+  OS << "DeclToLoc:\n";
+  for (auto [D, L] : DeclToLoc)
+OS << "  [" << D->getName() << ", " << L << "]\n";
+
+  OS << "ExprToLoc:\n";
+  for (auto [E, L] : ExprToLoc)
+OS << "  [" << E << ", " << L << "]\n";
+
+  OS << "LocToVal:\n";
+  for (auto [L, V] : LocToVal) {
+OS << "  [" << L << ", " << V << ": " << *V << "]\n";
+  }
+
+  OS << "FlowConditionToken:\n";
   DACtx->dumpFlowCondition(*FlowConditionToken);
 }
 
+void Environment::dump() const {
+  dump(llvm::dbgs());
+}
+
 } // namespace dataflow
 } // namespace clang
Index: clang/include/clang/Analysis/FlowSensitive/Value.h
===
--- clang/include/clang/Analysis/FlowSensitive/Value.h
+++ clang/include/clang/Analysis/FlowSensitive/Value.h
@@ -19,6 +19,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
 #include 
 #include 
 
@@ -310,6 +311,8 @@
   llvm::DenseMap Children;
 };
 
+raw_ostream <<(raw_ostream , const Value );
+
 } // namespace dataflow
 } // namespace clang
 
Index: clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
===
--- 

[PATCH] D141716: [clang][dataflow] Add (initial) debug printing for `Value` and `Environment`.

2023-01-16 Thread Stanislav Gatev via Phabricator via cfe-commits
sgatev accepted this revision.
sgatev added inline comments.



Comment at: clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp:792
+  // fields are printed.
+  llvm::dbgs() << "DeclToLoc:\n";
+  for (auto [D, L] : DeclToLoc)

Shouldn't this be `OS`? Same for those below.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141716/new/

https://reviews.llvm.org/D141716

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D141716: [clang][dataflow] Add (initial) debug printing for `Value` and `Environment`.

2023-01-13 Thread Yitzhak Mandelbaum via Phabricator via cfe-commits
ymandel created this revision.
ymandel added reviewers: sgatev, xazax.hun, gribozavr2.
Herald added subscribers: martong, rnkovacs.
Herald added a reviewer: NoQ.
Herald added a project: All.
ymandel requested review of this revision.
Herald added a project: clang.

Also adds uses of the new printing in analysis inner loop.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141716

Files:
  clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
  clang/include/clang/Analysis/FlowSensitive/Value.h
  clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
  clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
  clang/lib/Analysis/FlowSensitive/Value.cpp

Index: clang/lib/Analysis/FlowSensitive/Value.cpp
===
--- clang/lib/Analysis/FlowSensitive/Value.cpp
+++ clang/lib/Analysis/FlowSensitive/Value.cpp
@@ -11,6 +11,7 @@
 //===--===//
 
 #include "clang/Analysis/FlowSensitive/Value.h"
+#include "clang/Analysis/FlowSensitive/DebugSupport.h"
 #include "llvm/Support/Casting.h"
 
 namespace clang {
@@ -35,5 +36,21 @@
  areEquivalentIndirectionValues(Val1, Val2)));
 }
 
+raw_ostream <<(raw_ostream , const Value ) {
+  switch (Val.getKind()) {
+  case Value::Kind::Reference: {
+const auto *RV = cast();
+return OS << "Reference(" << >getReferentLoc() << ")";
+  }
+  case Value::Kind::Pointer: {
+const auto *PV = dyn_cast();
+return OS << "Pointer(" << >getPointeeLoc() << ")";
+  }
+  // FIXME: support remaining cases.
+  default:
+return OS << debugString(Val.getKind());
+  }
+}
+
 } // namespace dataflow
 } // namespace clang
Index: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
===
--- clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
+++ clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
@@ -32,8 +32,10 @@
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/Error.h"
-#include "llvm/Support/ErrorHandling.h"
+
+#define DEBUG_TYPE "clang-dataflow"
 
 namespace clang {
 namespace dataflow {
@@ -431,6 +433,8 @@
   std::min(RelativeMaxIterations, AbsoluteMaxIterations);
   uint32_t Iterations = 0;
   while (const CFGBlock *Block = Worklist.dequeue()) {
+LLVM_DEBUG(llvm::dbgs()
+   << "Processing Block " << Block->getBlockID() << "\n");
 if (++Iterations > MaxIterations) {
   return llvm::createStringError(std::errc::timed_out,
  "maximum number of iterations reached");
@@ -440,8 +444,16 @@
 BlockStates[Block->getBlockID()];
 TypeErasedDataflowAnalysisState NewBlockState =
 transferCFGBlock(*Block, AC);
+LLVM_DEBUG({
+  llvm::errs() << "New Env:\n";
+  NewBlockState.Env.dump();
+});
 
 if (OldBlockState) {
+  LLVM_DEBUG({
+llvm::errs() << "Old Env:\n";
+OldBlockState->Env.dump();
+  });
   if (isLoopHead(*Block)) {
 LatticeJoinEffect Effect1 = Analysis.widenTypeErased(
 NewBlockState.Lattice, OldBlockState->Lattice);
Index: clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
===
--- clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -786,9 +786,29 @@
   return DACtx->flowConditionImplies(*FlowConditionToken, Val);
 }
 
-void Environment::dump() const {
+void Environment::dump(raw_ostream ) const {
+  // FIXME: add printing for remaining and allow controller to decide what
+  // fields are printed.
+  llvm::dbgs() << "DeclToLoc:\n";
+  for (auto [D, L] : DeclToLoc)
+llvm::dbgs() << "  [" << D->getName() << ", " << L << "]\n";
+
+  llvm::dbgs() << "ExprToLoc:\n";
+  for (auto [E, L] : ExprToLoc)
+llvm::dbgs() << "  [" << E << ", " << L << "]\n";
+
+  llvm::dbgs() << "LocToVal:\n";
+  for (auto [L, V] : LocToVal) {
+llvm::dbgs() << "  [" << L << ", " << V << ": " << *V << "]\n";
+  }
+
+  llvm::dbgs() << "FlowConditionToken:\n";
   DACtx->dumpFlowCondition(*FlowConditionToken);
 }
 
+void Environment::dump() const {
+  dump(llvm::dbgs());
+}
+
 } // namespace dataflow
 } // namespace clang
Index: clang/include/clang/Analysis/FlowSensitive/Value.h
===
--- clang/include/clang/Analysis/FlowSensitive/Value.h
+++ clang/include/clang/Analysis/FlowSensitive/Value.h
@@ -19,6 +19,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
 #include 
 #include 
 
@@ -310,6 +311,8 @@
   llvm::DenseMap Children;
 };
 
+raw_ostream <<(raw_ostream , const Value );
+
 } //