blob: e5510ccee5a79cea9b1b18643e7702d3947e6cd4 [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"
18
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/raw_ostream.h"
23#include "llvm/Support/Casting.h"
24
25namespace llvm {
26 class Module;
27 class Value;
28 class Function;
29
30 /// The interface for consumers of difference data.
31 struct Consumer {
32 /// Record that a local context has been entered. Left and
33 /// Right are IR "containers" of some sort which are being
34 /// considered for structural equivalence: global variables,
35 /// functions, blocks, instructions, etc.
36 virtual void enterContext(Value *Left, Value *Right) = 0;
37
38 /// Record that a local context has been exited.
39 virtual void exitContext() = 0;
40
41 /// Record a difference within the current context.
42 virtual void log(StringRef Text) = 0;
43
44 /// Record a formatted difference within the current context.
45 virtual void logf(const LogBuilder &Log) = 0;
46
47 /// Record a line-by-line instruction diff.
48 virtual void logd(const DiffLogBuilder &Log) = 0;
49
50 protected:
51 virtual ~Consumer() {}
52 };
53
54 class DiffConsumer : public Consumer {
55 private:
56 struct DiffContext {
57 DiffContext(Value *L, Value *R)
58 : L(L), R(R), Differences(false), IsFunction(isa<Function>(L)) {}
59 Value *L;
60 Value *R;
61 bool Differences;
62 bool IsFunction;
63 DenseMap<Value*,unsigned> LNumbering;
64 DenseMap<Value*,unsigned> RNumbering;
65 };
66
67 raw_ostream &out;
68 Module *LModule;
69 Module *RModule;
70 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:
79 DiffConsumer(Module *L, Module *R)
80 : out(errs()), LModule(L), RModule(R), Differences(false), Indent(0) {}
81
82 bool hadDifferences() const;
83 void enterContext(Value *L, Value *R);
84 void exitContext();
85 void log(StringRef text);
86 void logf(const LogBuilder &Log);
87 void logd(const DiffLogBuilder &Log);
88 };
89}
90
91#endif