diff --git a/llvm/docs/CommandGuide/FileCheck.rst b/llvm/docs/CommandGuide/FileCheck.rst --- a/llvm/docs/CommandGuide/FileCheck.rst +++ b/llvm/docs/CommandGuide/FileCheck.rst @@ -596,7 +596,7 @@ to the value ``61680``. The syntax to check a numeric expression constraint is -``[[#%:]]`` where: +``[[#%: ]]`` where: * ``%`` is the same matching format specifier as for defining numeric variables but acting as a printf-style format to indicate how a numeric @@ -606,6 +606,12 @@ if no numeric variable is used. In case of conflict between matching formats of several numeric variables the format specifier is mandatory. +* ```` is the matching constraint describing how the value to match + must relate to the value of the numeric expression. The only currently + accepted constraint is ``==`` for an exact match and is the default if + ```` is not provided. No matching constraint must be specified + when the ```` is empty. + * ``>`` is a numeric expression whose operands are either numeric variables previously defined or integer immediates. Currently supported numeric operations are ``+`` and ``-``. A single numeric variable or diff --git a/llvm/lib/Support/FileCheck.cpp b/llvm/lib/Support/FileCheck.cpp --- a/llvm/lib/Support/FileCheck.cpp +++ b/llvm/lib/Support/FileCheck.cpp @@ -568,10 +568,23 @@ Expr = Expr.substr(DefEnd + 1); } + // Parse matching constraint. + Expr = Expr.ltrim(SpaceChars); + StringRef ConstraintExpr = Expr; + if (stripFront(Expr, "==", true /*Optional*/)) + return nullptr; + // Parse numeric expression itself. Expr = Expr.ltrim(SpaceChars); StringRef UseExpr = Expr; - if (!Expr.empty()) { + if (Expr.empty()) { + if (Expr.size() != ConstraintExpr.size()) { + SM.PrintMessage( + SMLoc::getFromPointer(Expr.data()), SourceMgr::DK_Error, + "Empty numeric expression should not have a matching constraint"); + return nullptr; + } + } else { // First operand in legacy numeric expression is the @LINE pseudo variable. enum AllowedOperand AO = Legacy ? LegacyVar : Any; NumExprAST = parseNumericOperand(Expr, AO, SM); diff --git a/llvm/test/FileCheck/numeric-expression.txt b/llvm/test/FileCheck/numeric-expression.txt --- a/llvm/test/FileCheck/numeric-expression.txt +++ b/llvm/test/FileCheck/numeric-expression.txt @@ -165,6 +165,17 @@ ; CHECK-LABEL: USE IMPL FMT IMPL MATCH UNSIGNED IMM ; CHECK-NEXT: [[#VAR1+0x8000000000000000]] +; Numeric expressions in default matching format and explicit matching rule using +; variables defined on other lines +USE DEF FMT EXPL MATCH +11 +11 +11 +; CHECK-LABEL: USE DEF FMT EXPL MATCH +; CHECK-NEXT: [[#==VAR1]] +; CHECK-NEXT: [[# ==VAR1]] +; CHECK-NEXT: [[# == VAR1]] + ; Numeric expressions with conversion matching format and implicit matching rule ; using variables defined on other lines USE CONV FMT IMPL MATCH @@ -299,7 +310,6 @@ ; FMT-CONFLICT-MSG-NEXT: {{F}}MT-CONFLICT-NEXT: {{\[\[#VAR1 \+ VAR2\]\]}} ; FMT-CONFLICT-MSG-NEXT: {{^ \^$}} - ; Numeric expression with overflow ; RUN: not FileCheck -check-prefix OVERFLOW -input-file %s %s 2>&1 \ ; RUN: | FileCheck -check-prefix OVERFLOW-MSG %s @@ -312,7 +322,6 @@ ; OVERFLOW-MSG-NEXT: {{O}}VERFLOW-NEXT: BIGVAR: {{\[\[#BIGVAR:0x8000000000000000\+0x8000000000000000\]\]}} ; OVERFLOW-MSG-NEXT: {{^ \^$}} - ; Numeric expression with underflow ; RUN: not FileCheck -check-prefix UNDERFLOW -input-file %s %s 2>&1 \ ; RUN: | FileCheck -check-prefix UNDERFLOW-MSG %s @@ -324,3 +333,27 @@ ; UNDERFLOW-MSG: numeric-expression.txt:[[#@LINE-1]]:31: error: Unable to substitute variable or numeric expression ; UNDERFLOW-MSG-NEXT: {{U}}NDERFLOW-NEXT: TINYVAR: {{\[\[#%d,TINYVAR:-0x8000000000000000-0x8000000000000000\]\]}} ; UNDERFLOW-MSG-NEXT: {{^ \^$}} + +; Empty numeric expression with matching constraint +; RUN: not FileCheck -check-prefix EMPTY-NUMEXPR-CONSTRAINT -input-file %s %s 2>&1 \ +; RUN: | FileCheck -check-prefix EMPTY-NUMEXPR-CONSTRAINT-MSG %s + +EMPTY NUMEXPR USE WITH CONSTRAINT +18 +; EMPTY-NUMEXPR-CONSTRAINT-LABEL: EMPTY NUMEXPR USE WITH CONSTRAINT +; EMPTY-NUMEXPR-CONSTRAINT-NEXT: [[# ==]] +; EMPTY-NUMEXPR-CONSTRAINT-MSG: numeric-expression.txt:[[#@LINE-1]]:40: error: Empty numeric expression should not have a matching constraint +; EMPTY-NUMEXPR-CONSTRAINT-MSG-NEXT: {{E}}MPTY-NUMEXPR-CONSTRAINT-NEXT: {{\[\[# ==\]\]}} +; EMPTY-NUMEXPR-CONSTRAINT-MSG-NEXT: {{^ \^$}} + +; Empty numeric expression with matching constraint +; RUN: not FileCheck -check-prefix EMPTY-NUMDEF-CONSTRAINT -input-file %s %s 2>&1 \ +; RUN: | FileCheck -check-prefix EMPTY-NUMDEF-CONSTRAINT-MSG %s + +EMPTY NUMEXPR DEF WITH CONSTRAINT +18 +; EMPTY-NUMDEF-CONSTRAINT-LABEL: EMPTY NUMEXPR CONSTRAINT +; EMPTY-NUMDEF-CONSTRAINT-NEXT: [[#VARDEF: ==]] +; EMPTY-NUMDEF-CONSTRAINT-MSG: numeric-expression.txt:[[#@LINE-1]]:46: error: Empty numeric expression should not have a matching constraint +; EMPTY-NUMDEF-CONSTRAINT-MSG-NEXT: {{E}}MPTY-NUMDEF-CONSTRAINT-NEXT: {{\[\[#VARDEF: ==\]\]}} +; EMPTY-NUMDEF-CONSTRAINT-MSG-NEXT: {{^ \^$}}