[UpdateCCTestChecks] Implement --global-hex-value-regex

For example, in OpenMP offload codegen tests, global variables like
`.offload_maptypes*` are much easier to read in hex.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D104743
This commit is contained in:
Joel E. Denny 2021-07-20 11:17:56 -04:00
parent 2f5b2ea6cd
commit 5b0a948a81
4 changed files with 64 additions and 1 deletions

View File

@ -0,0 +1,10 @@
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s | FileCheck %s
void foo() {
static int hex = 0x10;
static int dec = 10;
}
void bar() {
static int hex = 0x20;
static int dec = 20;
}

View File

@ -0,0 +1,25 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals --global-value-regex "foo\..*" "bar\..*" --global-hex-value-regex ".*\.hex"
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s | FileCheck %s
//.
// CHECK: @foo.hex = internal global i32 [[#0x10]], align 4
// CHECK: @foo.dec = internal global i32 10, align 4
// CHECK: @bar.hex = internal global i32 [[#0x20]], align 4
// CHECK: @bar.dec = internal global i32 20, align 4
//.
// CHECK-LABEL: @foo(
// CHECK-NEXT: entry:
// CHECK-NEXT: ret void
//
void foo() {
static int hex = 0x10;
static int dec = 10;
}
// CHECK-LABEL: @bar(
// CHECK-NEXT: entry:
// CHECK-NEXT: ret void
//
void bar() {
static int hex = 0x20;
static int dec = 20;
}

View File

@ -0,0 +1,19 @@
RUN: rm -rf %t && mkdir %t
# Check --global-hex-value-regex.
RUN: cp %S/Inputs/global-hex-value-regex.c %t/test.c
RUN: %update_cc_test_checks %t/test.c --check-globals \
RUN: --global-value-regex "foo\..*" "bar\..*" \
RUN: --global-hex-value-regex ".*\.hex"
RUN: diff -u %S/Inputs/global-hex-value-regex.c.expected %t/test.c
# Check that the generated directives actually work correctly.
RUN: cp %S/Inputs/lit.cfg.example %t/lit.cfg
# Show lit failures while avoiding confusing FileCheck input dump nesting.
RUN: %lit %t
# Lit was successful. Sanity-check the results with deterministic test order.
RUN: rm %t/.lit_test_times.txt
RUN: %lit %t 2>&1 | FileCheck %s
CHECK: Testing: 1 tests
CHECK: PASS: {{.*}} test.c

View File

@ -38,10 +38,13 @@ def parse_commandline_args(parser):
help='Add a prefix to FileCheck IR value names to avoid conflicts with scripted names')
parser.add_argument('--global-value-regex', nargs='+', default=[],
help='List of regular expressions that a global value declaration must match to generate a check (has no effect if checking globals is not enabled)')
parser.add_argument('--global-hex-value-regex', nargs='+', default=[],
help='List of regular expressions such that, for matching global value declarations, literal integer values should be encoded in hex in the associated FileCheck directives')
args = parser.parse_args()
global _verbose, _global_value_regex
global _verbose, _global_value_regex, _global_hex_value_regex
_verbose = args.verbose
_global_value_regex = args.global_value_regex
_global_hex_value_regex = args.global_hex_value_regex
return args
@ -607,6 +610,12 @@ def generalize_check_lines(lines, is_analyze, vars_seen, global_vars_seen):
for i, line in enumerate(lines):
# An IR variable named '%.' matches the FileCheck regex string.
line = line.replace('%.', '%dot')
for regex in _global_hex_value_regex:
if re.match('^@' + regex + ' = ', line):
line = re.sub(r'\bi([0-9]+) ([0-9]+)',
lambda m : 'i' + m.group(1) + ' [[#' + hex(int(m.group(2))) + ']]',
line)
break
# Ignore any comments, since the check lines will too.
scrubbed_line = SCRUB_IR_COMMENT_RE.sub(r'', line)
lines[i] = scrubbed_line