[llvm] [FileCheck] Fix --enable-var-scope for numvars after reassignment (PR #157158)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 5 11:53:20 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-testing-tools
Author: Clipi (Clipi-12)
<details>
<summary>Changes</summary>
This is my first contribution to LLVM, so [I don't have merge perms](https://llvm.org/docs/Contributing.html#how-to-submit-a-patch:~:text=If%20you%20do%20not%20have%20the%20ability%20to%20merge%20the%20PR%2C%20ask%20your%20reviewers%20to%20merge%20it%20on%20your%20behalf.%20You%20must%20do%20this%20explicitly%2C%20as%20reviewers%E2%80%99%20default%20assumption%20is%20that%20you%20are%20able%20to%20merge%20your%20own%20PR.). English is not my first language.
---
* `--enable-var-scope` makes it so that `lib/FileCheck.cpp#clearLocalVars` gets called.
* That function loops through `GlobalNumericVariableTable` and then calls `NumericVariable#clear` on every item. It also removes it from `GlobalNumericVariableTable`.
* `Pattern::match` calls `NumericVariable#setValue`, but it assumes the variable is already in `GlobalNumericVariableTable`.
Due to this behaviour, if we:
1. Set a numeric variable.
1. Clear it with a `CHECK-LABEL:`.
1. Reassign it.
1. **[Try to]** clear it again with another `CHECK-LABEL:`.
it won't actually clear it the second time, since to clear it, it needs to be in `GlobalNumericVariableTable` so that `clearLocalVars` can loop through it.
---
This patch simply inserts the numeric variable in `GlobalNumericVariableTable` right before the `NumericVariable#setValue` (in `Pattern::match`) is called.
I edited the `var-scope.txt` test to account for this patch. I also added _--implicit-check-not_ since we don't expect any other errors than the ones we have tested.
I also changed the _--check-prefixes_ since it was really confusing that both the variables and the prefixes were called the same, plus some prefixes being called LOCAL[1-3] but they had nothing to do with the file-content's "local[1-3]".
I don't know if this last part conflicts with [the "be an isolated change" rule](https://llvm.org/docs/Contributing.html#how-to-submit-a-patch:~:text=be%20an%20isolated%20change), since this is a test-case-legibility-problem and not actual code, but i can remove it and force-push the fork if desired.
---
Full diff: https://github.com/llvm/llvm-project/pull/157158.diff
2 Files Affected:
- (modified) llvm/lib/FileCheck/FileCheck.cpp (+1)
- (modified) llvm/test/FileCheck/var-scope.txt (+48-16)
``````````diff
diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index ce35a5bad7616..aed369b0d19a0 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1218,6 +1218,7 @@ Pattern::MatchResult Pattern::match(StringRef Buffer,
StringRef MatchedValue = MatchInfo[CaptureParenGroup];
ExpressionFormat Format = DefinedNumericVariable->getImplicitFormat();
APInt Value = Format.valueFromStringRepr(MatchedValue, SM);
+ Context->GlobalNumericVariableTable.try_emplace(NumericVariableDef.getKey(), DefinedNumericVariable);
DefinedNumericVariable->setValue(Value, MatchedValue);
}
diff --git a/llvm/test/FileCheck/var-scope.txt b/llvm/test/FileCheck/var-scope.txt
index 9b3ea0e95d143..b65eddb6d16a0 100644
--- a/llvm/test/FileCheck/var-scope.txt
+++ b/llvm/test/FileCheck/var-scope.txt
@@ -3,15 +3,15 @@
; Reference run: variables remain defined at all time when not using
; --enable-var-scope option.
-RUN: FileCheck --check-prefixes CHECK,LOCAL3,GLOBAL --input-file %s %s
+RUN: FileCheck --check-prefixes CHECK,CHECK-LOCAL-BOTH,CHECK-GLOBAL --input-file %s %s
-RUN: FileCheck --check-prefixes CHECK,GLOBAL --enable-var-scope --input-file %s %s
-RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,LOCAL1 --enable-var-scope --input-file %s %s 2>&1 \
-RUN: | FileCheck --check-prefix ERRUNDEFLOCAL %s
-RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,LOCAL2 --enable-var-scope --input-file %s %s 2>&1 \
-RUN: | FileCheck --check-prefix ERRUNDEFLOCNUM %s
-RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,LOCAL3 --enable-var-scope --input-file %s %s 2>&1 \
-RUN: | FileCheck --check-prefixes ERRUNDEFLOCAL,ERRUNDEFLOCNUM %s
+RUN: FileCheck --check-prefixes CHECK,CHECK-GLOBAL --enable-var-scope --input-file %s %s
+RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,CHECK-LOCAL-TEXT --enable-var-scope --input-file %s %s 2>&1 \
+RUN: | FileCheck --implicit-check-not "undefined variable:" --check-prefixes ERRUNDEF,ERRUNDEF-LOCAL %s
+RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,CHECK-LOCAL-NUM --enable-var-scope --input-file %s %s 2>&1 \
+RUN: | FileCheck --implicit-check-not "undefined variable:" --check-prefixes ERRUNDEF,ERRUNDEF-LOCNUM %s
+RUN: %ProtectFileCheckOutput not FileCheck --check-prefixes CHECK,CHECK-LOCAL-BOTH --enable-var-scope --input-file %s %s 2>&1 \
+RUN: | FileCheck --implicit-check-not "undefined variable:" --check-prefixes ERRUNDEF,ERRUNDEF-LOCAL,ERRUNDEF-LOCNUM %s
local1
global1
@@ -23,15 +23,47 @@ global2
CHECK: [[LOCAL]][[#LOCNUM+1]]
CHECK: [[$GLOBAL]][[#$GLOBNUM+1]]
-barrier:
-CHECK-LABEL: barrier
+// Barrier to clear local variables
+barrier1:
+CHECK-LABEL: barrier1
local3
global3
-LOCAL1: [[LOCAL]]3
-LOCAL2: local[[#LOCNUM+2]]
-LOCAL3: [[LOCAL]][[#LOCNUM+2]]
-GLOBAL: [[$GLOBAL]][[#$GLOBNUM+2]]
+CHECK-LOCAL-TEXT: [[LOCAL]]3
+CHECK-LOCAL-NUM: local[[#LOCNUM+2]]
+CHECK-LOCAL-BOTH: [[LOCAL]][[#LOCNUM+2]]
+CHECK-GLOBAL: [[$GLOBAL]][[#$GLOBNUM+2]]
-ERRUNDEFLOCAL: undefined variable: LOCAL
-ERRUNDEFLOCNUM: undefined variable: LOCNUM
+// Barrier to continue FileCheck execution even after the first fail
+barrier2:
+CHECK-LABEL: barrier2
+
+// Reassign the variables to check that clearing-after-reassigning works
+local4
+global4
+CHECK: [[LOCAL:loc[^[:digit:]]*]][[#LOCNUM:]]
+CHECK: [[$GLOBAL:glo[^[:digit:]]*]][[#$GLOBNUM:]]
+
+// Barrier to clear local variables
+barrier3:
+CHECK-LABEL: barrier3
+
+local5
+global5
+CHECK-LOCAL-TEXT: [[LOCAL]]5
+CHECK-LOCAL-NUM: local[[#LOCNUM+1]]
+CHECK-LOCAL-BOTH: [[LOCAL]][[#LOCNUM+1]]
+CHECK-GLOBAL: [[$GLOBAL]][[#$GLOBNUM+1]]
+
+
+// Check that the tests fail as expected
+ERRUNDEF-LOCAL: undefined variable: LOCAL
+ERRUNDEF-LOCNUM: undefined variable: LOCNUM
+ERRUNDEF-LOCAL: undefined variable: LOCAL
+ERRUNDEF-LOCNUM: undefined variable: LOCNUM
+
+// Look for "Input was:" to only match the error messages before the input-context.
+//
+// The regex /([[:space:]]|.)*/ matches all remaining characters,
+// to avoid fails due to --implicit-check-not
+ERRUNDEF: {{^Input was:([[:space:]]|.)*}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/157158
More information about the llvm-commits
mailing list