blob: 6c2209f27e66b1b59b8be11d123bfadf6f3d1c16 [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
14#ifndef _LLVM_DIFFCONSUMER_H_
15#define _LLVM_DIFFCONSUMER_H_
16
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"
Renato Golin7d4fc4f2011-03-14 22:22:46 +000020#include "llvm/ADT/StringRef.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 {
25 class Module;
26 class Value;
27 class Function;
28
29 /// The interface for consumers of difference data.
Francois Pichet875c3ff2011-03-14 23:07:21 +000030 class Consumer {
David Blaikie2d24e2a2011-12-20 02:50:00 +000031 virtual void anchor();
Francois Pichet875c3ff2011-03-14 23:07:21 +000032 public:
Renato Golin7d4fc4f2011-03-14 22:22:46 +000033 /// Record that a local context has been entered. Left and
34 /// Right are IR "containers" of some sort which are being
35 /// considered for structural equivalence: global variables,
36 /// functions, blocks, instructions, etc.
37 virtual void enterContext(Value *Left, Value *Right) = 0;
38
39 /// Record that a local context has been exited.
40 virtual void exitContext() = 0;
41
42 /// Record a difference within the current context.
43 virtual void log(StringRef Text) = 0;
44
45 /// Record a formatted difference within the current context.
46 virtual void logf(const LogBuilder &Log) = 0;
47
48 /// Record a line-by-line instruction diff.
49 virtual void logd(const DiffLogBuilder &Log) = 0;
50
51 protected:
52 virtual ~Consumer() {}
53 };
54
55 class DiffConsumer : public Consumer {
56 private:
57 struct DiffContext {
58 DiffContext(Value *L, Value *R)
59 : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
60 Value *L;
61 Value *R;
62 bool Differences;
63 bool IsFunction;
64 DenseMap<Value*,unsigned> LNumbering;
65 DenseMap<Value*,unsigned> RNumbering;
66 };
67
68 raw_ostream &out;
Renato Golin7d4fc4f2011-03-14 22:22:46 +000069 SmallVector<DiffContext, 5> contexts;
70 bool Differences;
71 unsigned Indent;
72
73 void printValue(Value *V, bool isL);
74 void header();
75 void indent();
76
77 public:
Benjamin Kramera7542d52012-06-06 18:25:08 +000078 DiffConsumer()
79 : out(errs()), Differences(false), Indent(0) {}
Renato Golin7d4fc4f2011-03-14 22:22:46 +000080
81 bool hadDifferences() const;
82 void enterContext(Value *L, Value *R);
83 void exitContext();
84 void log(StringRef text);
85 void logf(const LogBuilder &Log);
86 void logd(const DiffLogBuilder &Log);
87 };
88}
89
90#endif