xref: /llvm-project/llvm/test/TableGen/assert.td (revision e31e6f259ed22acfb2135700cb0482f84d6ea386)
1// RUN: not llvm-tblgen %s 2>&1 | FileCheck %s -DFILE=%s
2
3// -----------------------------------------------------------------------------
4// Test the assert statement at top level.
5// -----------------------------------------------------------------------------
6
7// CHECK: [[FILE]]:[[@LINE+4]]:8: error: assertion failed: primary name is too long
8// CHECK-NOT: note: primary name is too short
9defvar Name = "Grace Brewster Murray Hopper";
10assert !ge(!size(Name), 20), "primary name is too short: " # Name;
11assert !le(!size(Name), 20), "primary name is too long: " # Name;
12
13// CHECK: assertion failed: first name is incorrect
14def Rec01 {
15  string name = "Fred Smith";
16}
17
18assert !eq(!substr(Rec01.name, 0, 3), "Jane"),
19       !strconcat("first name is incorrect: ", Rec01.name);
20
21// CHECK: assertion failed: record Rec02 is broken
22def Rec02 {
23  bit broken = true;
24}
25assert !not(Rec02.broken), "record Rec02 is broken";
26
27// CHECK: assertion failed: cube of 9 should be 729
28class Cube<int n> {
29  int result = !mul(n, n, n);
30}
31assert !eq(Cube<9>.result, 81), "cube of 9 should be 729";
32
33// Verify that the assert fails only once.
34// CHECK: assertion failed: foreach i cannot be 2
35// CHECK-NOT: assertion failed: foreach i cannot be 2
36foreach i = 1...3 in {
37  assert !ne(i, 2), "foreach i cannot be 2";
38  def bar_ # i;
39}
40
41// -----------------------------------------------------------------------------
42// Test the assert statement in a record definition.
43// -----------------------------------------------------------------------------
44
45// CHECK: [[FILE]]:[[@LINE+6]]:10: error: assertion failed: primary first name is not "Grack"
46// CHECK-NOT: primary first name is not "Grace"
47// CHECK: [[FILE]]:[[@LINE+6]]:10: error: assertion failed: foo field should be
48// CHECK: [[FILE]]:[[@LINE+1]]:5: error: assertion failed in this record
49def Rec10 {
50  assert !eq(!substr(Name, 0, 5), "Grace"), "primary first name is not \"Grace\"";
51  assert !eq(!substr(Name, 0, 5), "Grack"), "primary first name is not \"Grack\"";
52  string foo = "Foo";
53  assert !eq(foo, "foo"), "foo field should be \"Foo\"";
54}
55
56// CHECK: [[FILE]]:[[@LINE+4]]:10: error: assertion failed: magic field is incorrect: 42
57// CHECK: [[FILE]]:[[@LINE+1]]:5: error: assertion failed in this record
58def Rec11 {
59  int magic = 13;
60  assert !eq(magic, 13), "magic field is incorrect: " # magic;
61  let magic = 42;
62}
63
64// CHECK: [[FILE]]:[[@LINE+5]]:10: error: assertion failed: var field has wrong value
65// CHECK: [[FILE]]:[[@LINE+1]]:5: error: assertion failed in this record
66def Rec12 {
67  defvar prefix = "foo_";
68  string var = prefix # "snork";
69  assert !eq(var, "foo_snorx"), "var field has wrong value: " # var;
70}
71
72// CHECK: assertion failed: kind field has wrong value
73class Kind {
74  int kind = 7;
75}
76
77// CHECK: [[FILE]]:[[@LINE+1]]:5: error: assertion failed in this record
78def Rec13 : Kind {
79  let kind = 8;
80  assert !eq(kind, 7), "kind field has wrong value: " # kind;
81}
82
83// CHECK: assertion failed: double_result should be
84// CHECK: [[FILE]]:[[@LINE+1]]:5: error: assertion failed in this record
85def Rec14 : Cube<3> {
86  int double_result = !mul(result, 2);
87  assert !eq(double_result, 53), "double_result should be 54";
88}
89
90// -----------------------------------------------------------------------------
91// Test the assert statement in a class definition.
92// -----------------------------------------------------------------------------
93
94class PersonName<string name> {
95  assert !le(!size(name), 32), "person name is too long: " # name;
96  string Name = name;
97}
98
99class Person<string name, int age> : PersonName<name> {
100  assert !and(!ge(age, 1), !le(age, 120)),
101         "person age is invalid: " # age;
102  int Age = age;
103}
104
105def Rec20 : Person<"Donald Knuth", 60>;
106
107// CHECK: assertion failed: person name is too long
108// CHECK: [[FILE]]:[[@LINE+1]]:5: error: assertion failed in this record
109def Rec21 : Person<"Donald Uh Oh This Name Is Too Long Knuth", 50>;
110
111// CHECK: assertion failed: person age is invalid
112// CHECK: [[FILE]]:[[@LINE+1]]:5: error: assertion failed in this record
113def Rec22 : Person<"Donald Knuth", 150>;
114
115// Test the assert statement in an anonymous class invocation.
116def Rec30 {
117  string Name = Person<"Margaret Heafield Hamilton", 25>.Name;
118  int Age = Person<"Margaret Heafield Hamilton", 25>.Age;
119}
120
121// CHECK: assertion failed: person name is too long
122// CHECK: [[FILE]]:[[@LINE+2]]:17: error: assertion failed in this record
123def Rec31 {
124  string Name = Person<"Margaret Heafield And More Middle Names Hamilton", 25>.Name;
125  int Age = Person<"Margaret Heafield Hamilton", 25>.Age;
126}
127
128// CHECK: assertion failed: person age is invalid: 0
129// CHECK: [[FILE]]:[[@LINE+3]]:13: error: assertion failed in this record
130def Rec32 {
131  string Name = Person<"Margaret Heafield Hamilton", 25>.Name;
132  int Age = Person<"Margaret Heafield Hamilton", 0>.Age;
133}
134
135// -----------------------------------------------------------------------------
136// Test the assert statement in a multiclass.
137// -----------------------------------------------------------------------------
138
139// CHECK: assertion failed: MC1 id string is too long
140// CHECK: assertion failed: MC1 seq is too high
141multiclass MC1<string id, int seq> {
142  assert !le(!size(id), 5), "MC1 id string is too long";
143  assert !le(seq, 999999), "MC1 seq is too high";
144
145  def _mc1 {
146    string ID = id;
147    int Seq = seq;
148  }
149}
150
151defm Rec40 : MC1<"ILISP", 999>;
152defm Rec41 : MC1<"ILISPX", 999>;
153defm Rec42 : MC1<"ILISP", 999999999>;
154
155// CHECK: assertion failed: MC2 phrase must be secret: secrex code
156multiclass MC2<string phr> {
157  assert !eq(!substr(phr, 0, 6), "secret"), "MC2 phrase must be secret: " # phr;
158
159  def _mc2 {
160    string phrase = phr;
161  }
162}
163
164multiclass MC3<string phr> {
165  defm _mc3 : MC2<phr>;
166}
167
168defm Rec43 : MC3<"secrex code">;
169
170// CHECK: assertion failed: MC2 phrase must be secret: xecret code
171multiclass MC4<string phr> : MC2<phr> {
172  def _def;
173}
174
175defm Rec44 : MC4<"xecret code">;
176
177// Test a defm in a multiclass that inherits from a class with asserts.
178
179// CHECK: assertion failed: MC5 name must include a space: Ada_Lovelace
180// CHECK: assertion failed: person age is invalid: 666
181multiclass MC5<string phr, string name, int age> {
182  assert !ne(!find(name, " "), -1), "MC5 name must include a space: " # name;
183
184  defm _mc5 : MC2<phr>, Person<name, age>;
185}
186
187defm Rec45 : MC5<"secret password", "Ada_Lovelace", 666>;
188