Skip to content

Commit 566c731

Browse files
authored
[UTC] Indent switch cases (#165212)
LLVM prints switch cases indented by 2 additional spaces, as follows: ```LLVM switch i32 %x, label %default [ i32 0, label %phi i32 1, label %phi ] ``` Since this only changes the output IR of update_test_checks.py and does not change the logic of the File Check Pattern, there seems to be no need to update the existing test cases.
1 parent 29c830c commit 566c731

File tree

5 files changed

+175
-2
lines changed

5 files changed

+175
-2
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
; RUN: opt < %s -S | FileCheck %s
2+
3+
; Test whether the UTC format the switch-cases correctly, which requires TWO extra spaces.
4+
5+
define i8 @testi8(i8 %x) {
6+
switch i8 %x, label %default [
7+
i8 0, label %case1
8+
i8 1, label %case2
9+
i8 2, label %case3
10+
i8 3, label %case3
11+
]
12+
default:
13+
ret i8 0
14+
case1:
15+
ret i8 1
16+
case2:
17+
ret i8 2
18+
case3:
19+
ret i8 3
20+
}
21+
22+
define i32 @testi32(i32 %x) {
23+
switch i32 %x, label %default [
24+
i32 0, label %case1
25+
i32 1, label %case2
26+
i32 2, label %case3
27+
i32 3, label %case3
28+
]
29+
default:
30+
ret i32 0
31+
case1:
32+
ret i32 1
33+
case2:
34+
ret i32 2
35+
case3:
36+
ret i32 3
37+
}
38+
39+
define i128 @testi128(i128 %x) {
40+
switch i128 %x, label %default [
41+
i128 0, label %case1
42+
i128 1, label %case2
43+
i128 2, label %case3
44+
i128 3, label %case3
45+
]
46+
default:
47+
ret i128 0
48+
case1:
49+
ret i128 1
50+
case2:
51+
ret i128 2
52+
case3:
53+
ret i128 3
54+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 7
2+
; RUN: opt < %s -S | FileCheck %s
3+
4+
; Test whether the UTC format the switch-cases correctly, which requires TWO extra spaces.
5+
6+
define i8 @testi8(i8 %x) {
7+
; CHECK-LABEL: define i8 @testi8(
8+
; CHECK-SAME: i8 [[X:%.*]]) {
9+
; CHECK-NEXT: switch i8 [[X]], label %[[DEFAULT:.*]] [
10+
; CHECK-NEXT: i8 0, label %[[CASE1:.*]]
11+
; CHECK-NEXT: i8 1, label %[[CASE2:.*]]
12+
; CHECK-NEXT: i8 2, label %[[CASE3:.*]]
13+
; CHECK-NEXT: i8 3, label %[[CASE3]]
14+
; CHECK-NEXT: ]
15+
; CHECK: [[DEFAULT]]:
16+
; CHECK-NEXT: ret i8 0
17+
; CHECK: [[CASE1]]:
18+
; CHECK-NEXT: ret i8 1
19+
; CHECK: [[CASE2]]:
20+
; CHECK-NEXT: ret i8 2
21+
; CHECK: [[CASE3]]:
22+
; CHECK-NEXT: ret i8 3
23+
;
24+
switch i8 %x, label %default [
25+
i8 0, label %case1
26+
i8 1, label %case2
27+
i8 2, label %case3
28+
i8 3, label %case3
29+
]
30+
default:
31+
ret i8 0
32+
case1:
33+
ret i8 1
34+
case2:
35+
ret i8 2
36+
case3:
37+
ret i8 3
38+
}
39+
40+
define i32 @testi32(i32 %x) {
41+
; CHECK-LABEL: define i32 @testi32(
42+
; CHECK-SAME: i32 [[X:%.*]]) {
43+
; CHECK-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
44+
; CHECK-NEXT: i32 0, label %[[CASE1:.*]]
45+
; CHECK-NEXT: i32 1, label %[[CASE2:.*]]
46+
; CHECK-NEXT: i32 2, label %[[CASE3:.*]]
47+
; CHECK-NEXT: i32 3, label %[[CASE3]]
48+
; CHECK-NEXT: ]
49+
; CHECK: [[DEFAULT]]:
50+
; CHECK-NEXT: ret i32 0
51+
; CHECK: [[CASE1]]:
52+
; CHECK-NEXT: ret i32 1
53+
; CHECK: [[CASE2]]:
54+
; CHECK-NEXT: ret i32 2
55+
; CHECK: [[CASE3]]:
56+
; CHECK-NEXT: ret i32 3
57+
;
58+
switch i32 %x, label %default [
59+
i32 0, label %case1
60+
i32 1, label %case2
61+
i32 2, label %case3
62+
i32 3, label %case3
63+
]
64+
default:
65+
ret i32 0
66+
case1:
67+
ret i32 1
68+
case2:
69+
ret i32 2
70+
case3:
71+
ret i32 3
72+
}
73+
74+
define i128 @testi128(i128 %x) {
75+
; CHECK-LABEL: define i128 @testi128(
76+
; CHECK-SAME: i128 [[X:%.*]]) {
77+
; CHECK-NEXT: switch i128 [[X]], label %[[DEFAULT:.*]] [
78+
; CHECK-NEXT: i128 0, label %[[CASE1:.*]]
79+
; CHECK-NEXT: i128 1, label %[[CASE2:.*]]
80+
; CHECK-NEXT: i128 2, label %[[CASE3:.*]]
81+
; CHECK-NEXT: i128 3, label %[[CASE3]]
82+
; CHECK-NEXT: ]
83+
; CHECK: [[DEFAULT]]:
84+
; CHECK-NEXT: ret i128 0
85+
; CHECK: [[CASE1]]:
86+
; CHECK-NEXT: ret i128 1
87+
; CHECK: [[CASE2]]:
88+
; CHECK-NEXT: ret i128 2
89+
; CHECK: [[CASE3]]:
90+
; CHECK-NEXT: ret i128 3
91+
;
92+
switch i128 %x, label %default [
93+
i128 0, label %case1
94+
i128 1, label %case2
95+
i128 2, label %case3
96+
i128 3, label %case3
97+
]
98+
default:
99+
ret i128 0
100+
case1:
101+
ret i128 1
102+
case2:
103+
ret i128 2
104+
case3:
105+
ret i128 3
106+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## switch_case test checking that update_test_checks.py works correctly
2+
# RUN: cp -f %S/Inputs/switch_case.ll %t.ll && %update_test_checks %t.ll --version 7
3+
# RUN: diff -u %t.ll %S/Inputs/switch_case.ll.expected

llvm/utils/UpdateTestChecks/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'none' and 'all'. 'smart' is the default.
3030
5: Basic block labels are matched by FileCheck expressions
3131
6: The semantics of TBAA checks has been incorporated in the check lines.
32+
7: Indent switch-cases correctly.
3233
"""
3334
DEFAULT_VERSION = 6
3435

@@ -606,6 +607,7 @@ def invoke_tool(exe, cmd_args, ir, preprocess_cmd=None, verbose=False):
606607
DEBUG_ONLY_ARG_RE = re.compile(r"-debug-only[= ]([^ ]+)")
607608

608609
IS_DEBUG_RECORD_RE = re.compile(r"^(\s+)#dbg_")
610+
IS_SWITCH_CASE_RE = re.compile(r"^\s+i\d+ \d+, label %\w+")
609611

610612
SCRUB_LEADING_WHITESPACE_RE = re.compile(r"^(\s+)")
611613
SCRUB_WHITESPACE_RE = re.compile(r"(?!^(| \w))[ \t]+", flags=re.M)

llvm/utils/update_test_checks.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,17 @@ def update_test(ti: common.TestInfo):
260260
skip_same_checks=dropped_previous_line,
261261
):
262262
# This input line of the function body will go as-is into the output.
263-
# Except make leading whitespace uniform: 2 spaces. 4 for debug records.
263+
# Except make leading whitespace uniform: 2 spaces. 4 for debug records/switch cases.
264264
indent = (
265-
" " if not common.IS_DEBUG_RECORD_RE.match(input_line) else " "
265+
" " * 4
266+
if (
267+
common.IS_DEBUG_RECORD_RE.match(input_line)
268+
or (
269+
ti.args.version > 6
270+
and common.IS_SWITCH_CASE_RE.match(input_line)
271+
)
272+
)
273+
else " " * 2
266274
)
267275
input_line = common.SCRUB_LEADING_WHITESPACE_RE.sub(indent, input_line)
268276
output_lines.append(input_line)

0 commit comments

Comments
 (0)