blob: 82f5ce598b44374b675158db260dc19876a5bde7 [file] [log] [blame]
Renato Golin7d4fc4f2011-03-14 22:22:46 +00001//===-- DiffConsumer.h - Difference Consumer --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header defines the interface to the LLVM difference Consumer
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000014#ifndef LLVM_TOOLS_LLVM_DIFF_DIFFCONSUMER_H
15#define LLVM_TOOLS_LLVM_DIFF_DIFFCONSUMER_H
Renato Golin7d4fc4f2011-03-14 22:22:46 +000016
17#include "DiffLog.h"
Renato Golin7d4fc4f2011-03-14 22:22:46 +000018#include "llvm/ADT/DenseMap.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000019#include "llvm/ADT/SmallVector.h"
Benjamin Kramer06342012016-01-27 18:03:37 +000020#include "llvm/IR/Value.h"
Renato Golin7d4fc4f2011-03-14 22:22:46 +000021#include "llvm/Support/Casting.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000022#include "llvm/Support/raw_ostream.h"
Renato Golin7d4fc4f2011-03-14 22:22:46 +000023
24namespace llvm {
Mehdi Aminif6071e12016-04-18 09:17:29 +000025class StringRef;
Renato Golin7d4fc4f2011-03-14 22:22:46 +000026 class Module;
27 class Value;
28 class Function;
29
30 /// The interface for consumers of difference data.
Francois Pichet875c3ff2011-03-14 23:07:21 +000031 class Consumer {
David Blaikie2d24e2a2011-12-20 02:50:00 +000032 virtual void anchor();
Francois Pichet875c3ff2011-03-14 23:07:21 +000033 public:
Renato Golin7d4fc4f2011-03-14 22:22:46 +000034 /// Record that a local context has been entered. Left and
35 /// Right are IR "containers" of some sort which are being
36 /// considered for structural equivalence: global variables,
37 /// functions, blocks, instructions, etc.
38 virtual void enterContext(Value *Left, Value *Right) = 0;
39
40 /// Record that a local context has been exited.
41 virtual void exitContext() = 0;
42
43 /// Record a difference within the current context.
44 virtual void log(StringRef Text) = 0;
45
46 /// Record a formatted difference within the current context.
47 virtual void logf(const LogBuilder &Log) = 0;
48
49 /// Record a line-by-line instruction diff.
50 virtual void logd(const DiffLogBuilder &Log) = 0;
51
52 protected:
53 virtual ~Consumer() {}
54 };
55
56 class DiffConsumer : public Consumer {
57 private:
58 struct DiffContext {
59 DiffContext(Value *L, Value *R)
60 : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
61 Value *L;
62 Value *R;
63 bool Differences;
64 bool IsFunction;
65 DenseMap<Value*,unsigned> LNumbering;
66 DenseMap<Value*,unsigned> RNumbering;
67 };
68
69 raw_ostream &out;
Renato Golin7d4fc4f2011-03-14 22:22:46 +000070 SmallVector<DiffContext, 5> contexts;
71 bool Differences;
72 unsigned Indent;
73
74 void printValue(Value *V, bool isL);
75 void header();
76 void indent();
77
78 public:
Benjamin Kramera7542d52012-06-06 18:25:08 +000079 DiffConsumer()
80 : out(errs()), Differences(false), Indent(0) {}
Renato Golin7d4fc4f2011-03-14 22:22:46 +000081
82 bool hadDifferences() const;
Craig Topperc83e68f2014-03-08 08:27:28 +000083 void enterContext(Value *L, Value *R) override;
84 void exitContext() override;
85 void log(StringRef text) override;
86 void logf(const LogBuilder &Log) override;
87 void logd(const DiffLogBuilder &Log) override;
Renato Golin7d4fc4f2011-03-14 22:22:46 +000088 };
89}
90
91#endif