Skip to content

Commit c441f65

Browse files
committed
[clang][dataflow] Add (initial) debug printing for Value and Environment.
Also adds uses of the new printing in analysis inner loop. Differential Revision: https://reviews.llvm.org/D141716
1 parent 2bcedd4 commit c441f65

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ class Environment {
430430
}
431431

432432
LLVM_DUMP_METHOD void dump() const;
433+
LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
433434

434435
private:
435436
/// Creates a value appropriate for `Type`, if `Type` is supported, otherwise

clang/include/clang/Analysis/FlowSensitive/Value.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/DenseMap.h"
2020
#include "llvm/ADT/StringMap.h"
2121
#include "llvm/ADT/StringRef.h"
22+
#include "llvm/Support/raw_ostream.h"
2223
#include <cassert>
2324
#include <utility>
2425

@@ -310,6 +311,8 @@ class StructValue final : public Value {
310311
llvm::DenseMap<const ValueDecl *, Value *> Children;
311312
};
312313

314+
raw_ostream &operator<<(raw_ostream &OS, const Value &Val);
315+
313316
} // namespace dataflow
314317
} // namespace clang
315318

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,29 @@ bool Environment::flowConditionImplies(BoolValue &Val) const {
786786
return DACtx->flowConditionImplies(*FlowConditionToken, Val);
787787
}
788788

789-
void Environment::dump() const {
789+
void Environment::dump(raw_ostream &OS) const {
790+
// FIXME: add printing for remaining fields and allow caller to decide what
791+
// fields are printed.
792+
OS << "DeclToLoc:\n";
793+
for (auto [D, L] : DeclToLoc)
794+
OS << " [" << D->getName() << ", " << L << "]\n";
795+
796+
OS << "ExprToLoc:\n";
797+
for (auto [E, L] : ExprToLoc)
798+
OS << " [" << E << ", " << L << "]\n";
799+
800+
OS << "LocToVal:\n";
801+
for (auto [L, V] : LocToVal) {
802+
OS << " [" << L << ", " << V << ": " << *V << "]\n";
803+
}
804+
805+
OS << "FlowConditionToken:\n";
790806
DACtx->dumpFlowCondition(*FlowConditionToken);
791807
}
792808

809+
void Environment::dump() const {
810+
dump(llvm::dbgs());
811+
}
812+
793813
} // namespace dataflow
794814
} // namespace clang

clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
#include "llvm/ADT/ArrayRef.h"
3333
#include "llvm/ADT/DenseSet.h"
3434
#include "llvm/ADT/STLExtras.h"
35+
#include "llvm/Support/Debug.h"
3536
#include "llvm/Support/Error.h"
36-
#include "llvm/Support/ErrorHandling.h"
37+
38+
#define DEBUG_TYPE "clang-dataflow"
3739

3840
namespace clang {
3941
namespace dataflow {
@@ -431,6 +433,8 @@ runTypeErasedDataflowAnalysis(
431433
std::min(RelativeMaxIterations, AbsoluteMaxIterations);
432434
uint32_t Iterations = 0;
433435
while (const CFGBlock *Block = Worklist.dequeue()) {
436+
LLVM_DEBUG(llvm::dbgs()
437+
<< "Processing Block " << Block->getBlockID() << "\n");
434438
if (++Iterations > MaxIterations) {
435439
return llvm::createStringError(std::errc::timed_out,
436440
"maximum number of iterations reached");
@@ -440,8 +444,16 @@ runTypeErasedDataflowAnalysis(
440444
BlockStates[Block->getBlockID()];
441445
TypeErasedDataflowAnalysisState NewBlockState =
442446
transferCFGBlock(*Block, AC);
447+
LLVM_DEBUG({
448+
llvm::errs() << "New Env:\n";
449+
NewBlockState.Env.dump();
450+
});
443451

444452
if (OldBlockState) {
453+
LLVM_DEBUG({
454+
llvm::errs() << "Old Env:\n";
455+
OldBlockState->Env.dump();
456+
});
445457
if (isLoopHead(*Block)) {
446458
LatticeJoinEffect Effect1 = Analysis.widenTypeErased(
447459
NewBlockState.Lattice, OldBlockState->Lattice);

clang/lib/Analysis/FlowSensitive/Value.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "clang/Analysis/FlowSensitive/Value.h"
14+
#include "clang/Analysis/FlowSensitive/DebugSupport.h"
1415
#include "llvm/Support/Casting.h"
1516

1617
namespace clang {
@@ -35,5 +36,21 @@ bool areEquivalentValues(const Value &Val1, const Value &Val2) {
3536
areEquivalentIndirectionValues(Val1, Val2)));
3637
}
3738

39+
raw_ostream &operator<<(raw_ostream &OS, const Value &Val) {
40+
switch (Val.getKind()) {
41+
case Value::Kind::Reference: {
42+
const auto *RV = cast<ReferenceValue>(&Val);
43+
return OS << "Reference(" << &RV->getReferentLoc() << ")";
44+
}
45+
case Value::Kind::Pointer: {
46+
const auto *PV = dyn_cast<PointerValue>(&Val);
47+
return OS << "Pointer(" << &PV->getPointeeLoc() << ")";
48+
}
49+
// FIXME: support remaining cases.
50+
default:
51+
return OS << debugString(Val.getKind());
52+
}
53+
}
54+
3855
} // namespace dataflow
3956
} // namespace clang

0 commit comments

Comments
 (0)