forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verilog_equivalence.h
85 lines (73 loc) · 3.79 KB
/
verilog_equivalence.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef VERIBLE_VERILOG_ANALYSIS_VERILOG_EQUIVALENCE_H_
#define VERIBLE_VERILOG_ANALYSIS_VERILOG_EQUIVALENCE_H_
#include <functional>
#include <iosfwd>
#include "absl/strings/string_view.h"
#include "common/text/token_info.h"
#include "common/text/token_stream_view.h"
namespace verilog {
// Encode different results of diff-ing two inputs.
enum class DiffStatus {
kEquivalent, // inputs are considered equivalent
kDifferent, // inputs are considered different
kLeftError, // some error processing left input
kRightError, // some error processing right input
};
std::ostream &operator<<(std::ostream &, DiffStatus);
// Compares two strings for equivalence.
// Returns a DiffStatus that captures 'equivalence' ignoring tokens filtered
// out by remove_predicate, and using the equal_comparator binary predicate.
// 'lexer' tokenizes a string into substrings (tokens), and is used to
// recursively lex expandable tokens as determined by recursion_predicate.
// (Some tokens like macro definition bodies are initially read as one large
// token by some lexers.)
// 'token_printer' reports diagnostics on token differences and errors.
// If errstream is provided, print detailed error message to that stream.
// TODO(fangism): move this to language-agnostic common/analysis library.
DiffStatus LexicallyEquivalent(
absl::string_view left, absl::string_view right,
const std::function<bool(absl::string_view, verible::TokenSequence *)>
&lexer,
const std::function<bool(const verible::TokenInfo &)> &recursion_predicate,
const std::function<bool(const verible::TokenInfo &)> &remove_predicate,
const std::function<bool(const verible::TokenInfo &,
const verible::TokenInfo &)> &equal_comparator,
const std::function<void(const verible::TokenInfo &, std::ostream &)>
&token_printer,
std::ostream *errstream = nullptr);
// Returns a DiffStatus that captures 'equivalence' ignoring tokens filtered
// out by remove_predicate, and using the equal_comparator binary predicate.
// If errstream is provided, print detailed error message to that stream.
DiffStatus VerilogLexicallyEquivalent(
absl::string_view left, absl::string_view right,
const std::function<bool(const verible::TokenInfo &)> &remove_predicate,
const std::function<bool(const verible::TokenInfo &,
const verible::TokenInfo &)> &equal_comparator,
std::ostream *errstream = nullptr);
// Returns true if both token sequences are equivalent, ignoring whitespace.
// If errstream is provided, print detailed error message to that stream.
DiffStatus FormatEquivalent(absl::string_view left, absl::string_view right,
std::ostream *errstream = nullptr);
// Similar to FormatEquivalent except that:
// 1) whitespaces must match
// 2) identifiers only need to match in length and not string content to be
// considered equal.
// Such equivalence is good for formatter test cases.
DiffStatus ObfuscationEquivalent(absl::string_view left,
absl::string_view right,
std::ostream *errstream = nullptr);
} // namespace verilog
#endif // VERIBLE_VERILOG_ANALYSIS_VERILOG_EQUIVALENCE_H_