Skip to content

Commit d007099

Browse files
Clipi-12RoboTux
andauthored
[FileCheck] Fix --enable-var-scope for numvars after reassignment (#157158)
* When `--enable-var-scope` is active, `lib/FileCheck.cpp#clearLocalVars` gets called. * That function loops through `GlobalNumericVariableTable` and then calls `NumericVariable::clear` on most items. It also removes them from `GlobalNumericVariableTable`. * When reassigning an already cleared variable, `Pattern::match` calls `NumericVariable::setValue`, but it doesn't reinsert it into `GlobalNumericVariableTable`. Therefore, the next time `clearLocalVars` is called, it won't be able to loop through the variables. Fix it by reinserting them in `GlobalNumericVariableTable` inside `Pattern::match`. Co-authored-by: Thomas Preud'homme <thomas.preudhomme@arm.com>
1 parent 78dfbca commit d007099

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

llvm/lib/FileCheck/FileCheck.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,14 @@ Pattern::MatchResult Pattern::match(StringRef Buffer,
12181218
StringRef MatchedValue = MatchInfo[CaptureParenGroup];
12191219
ExpressionFormat Format = DefinedNumericVariable->getImplicitFormat();
12201220
APInt Value = Format.valueFromStringRepr(MatchedValue, SM);
1221+
// Numeric variables are already inserted into GlobalNumericVariableTable
1222+
// during parsing, but clearLocalVars might remove them, so we must
1223+
// reinsert them. Numeric-variable resolution does not access
1224+
// GlobalNumericVariableTable; it directly uses a pointer to the variable.
1225+
// However, other functions (such as clearLocalVars) may require active
1226+
// variables to be in the table.
1227+
Context->GlobalNumericVariableTable.try_emplace(NumericVariableDef.getKey(),
1228+
DefinedNumericVariable);
12211229
DefinedNumericVariable->setValue(Value, MatchedValue);
12221230
}
12231231

llvm/test/FileCheck/var-scope.txt

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
; Reference run: variables remain defined at all time when not using
55
; --enable-var-scope option.
6-
RUN: FileCheck --check-prefixes CHECK,LOCAL3,GLOBAL --input-file %s %s
6+
RUN: FileCheck --check-prefixes CHECK,CHECK-LOCAL-BOTH,CHECK-GLOBAL --input-file %s %s
77

8-
RUN: FileCheck --check-prefixes CHECK,GLOBAL --enable-var-scope --input-file %s %s
9-
RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,LOCAL1 --enable-var-scope --input-file %s %s 2>&1 \
10-
RUN: | FileCheck --check-prefix ERRUNDEFLOCAL %s
11-
RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,LOCAL2 --enable-var-scope --input-file %s %s 2>&1 \
12-
RUN: | FileCheck --check-prefix ERRUNDEFLOCNUM %s
13-
RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,LOCAL3 --enable-var-scope --input-file %s %s 2>&1 \
14-
RUN: | FileCheck --check-prefixes ERRUNDEFLOCAL,ERRUNDEFLOCNUM %s
8+
RUN: FileCheck --check-prefixes CHECK,CHECK-GLOBAL --enable-var-scope --input-file %s %s
9+
RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,CHECK-LOCAL-TEXT --enable-var-scope --input-file %s %s 2>&1 \
10+
RUN: | FileCheck --implicit-check-not "undefined variable:" --check-prefixes ERRUNDEF,ERRUNDEF-LOCAL %s
11+
RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,CHECK-LOCAL-NUM --enable-var-scope --input-file %s %s 2>&1 \
12+
RUN: | FileCheck --implicit-check-not "undefined variable:" --check-prefixes ERRUNDEF,ERRUNDEF-LOCNUM %s
13+
RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,CHECK-LOCAL-BOTH --enable-var-scope --input-file %s %s 2>&1 \
14+
RUN: | FileCheck --implicit-check-not "undefined variable:" --check-prefixes ERRUNDEF,ERRUNDEF-LOCAL,ERRUNDEF-LOCNUM %s
1515

1616
local1
1717
global1
@@ -23,15 +23,47 @@ global2
2323
CHECK: [[LOCAL]][[#LOCNUM+1]]
2424
CHECK: [[$GLOBAL]][[#$GLOBNUM+1]]
2525

26-
barrier:
27-
CHECK-LABEL: barrier
26+
// Barrier to clear local variables
27+
barrier1:
28+
CHECK-LABEL: barrier1
2829

2930
local3
3031
global3
31-
LOCAL1: [[LOCAL]]3
32-
LOCAL2: local[[#LOCNUM+2]]
33-
LOCAL3: [[LOCAL]][[#LOCNUM+2]]
34-
GLOBAL: [[$GLOBAL]][[#$GLOBNUM+2]]
32+
CHECK-LOCAL-TEXT: [[LOCAL]]3
33+
CHECK-LOCAL-NUM: local[[#LOCNUM+2]]
34+
CHECK-LOCAL-BOTH: [[LOCAL]][[#LOCNUM+2]]
35+
CHECK-GLOBAL: [[$GLOBAL]][[#$GLOBNUM+2]]
3536

36-
ERRUNDEFLOCAL: undefined variable: LOCAL
37-
ERRUNDEFLOCNUM: undefined variable: LOCNUM
37+
// Barrier to continue FileCheck execution even after the first fail
38+
barrier2:
39+
CHECK-LABEL: barrier2
40+
41+
// Reassign the variables to check that clearing-after-reassigning works
42+
local4
43+
global4
44+
CHECK: [[LOCAL:loc[^[:digit:]]*]][[#LOCNUM:]]
45+
CHECK: [[$GLOBAL:glo[^[:digit:]]*]][[#$GLOBNUM:]]
46+
47+
// Barrier to clear local variables
48+
barrier3:
49+
CHECK-LABEL: barrier3
50+
51+
local5
52+
global5
53+
CHECK-LOCAL-TEXT: [[LOCAL]]5
54+
CHECK-LOCAL-NUM: local[[#LOCNUM+1]]
55+
CHECK-LOCAL-BOTH: [[LOCAL]][[#LOCNUM+1]]
56+
CHECK-GLOBAL: [[$GLOBAL]][[#$GLOBNUM+1]]
57+
58+
59+
// Check that the tests fail as expected
60+
ERRUNDEF-LOCAL: undefined variable: LOCAL
61+
ERRUNDEF-LOCNUM: undefined variable: LOCNUM
62+
ERRUNDEF-LOCAL: undefined variable: LOCAL
63+
ERRUNDEF-LOCNUM: undefined variable: LOCNUM
64+
65+
// Look for "Input was:" to only match the error messages before the input-context.
66+
//
67+
// The regex /([[:space:]]|.)*/ matches all remaining characters,
68+
// to avoid fails due to --implicit-check-not
69+
ERRUNDEF: {{^Input was:([[:space:]]|.)*}}

0 commit comments

Comments
 (0)