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 @@ -691,12 +691,25 @@ * a numeric operand, or * an expression followed by an operator and a numeric operand. - A numeric operand is a previously defined numeric variable, or an integer - literal and have a 64-bit precision. The supported operators are ``+`` and - ``-``. Spaces are accepted before, after and between any of these elements. - Overflow and underflow are rejected. There is currently no support for - operator precendence, but parentheses can be used to change the evaluation - order. + A numeric operand is a previously defined numeric variable, an integer + literal, or a function call and have a 64-bit precision. The supported + operators are ``+`` and ``-``. Spaces are accepted before, after and between + any of these elements. Overflow and underflow are rejected. There is no + support for operator precendence, but parentheses can be used to change the + evaluation order. + +The syntax of a function call is ``!()`` where: + +* ``name`` is a predefined string literal. Acceptable values are: + + * add - Returns the sum of its two operands. + * div - Returns the quotient of its two operands. + * max - Returns the largest of its two operands. + * min - Returns the smallest of its two operands. + * mul - Returns the product of its two operands. + * sub - Returns the difference of its two operands. + +* ```` is a comma seperated list of expressions. For example: 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 @@ -230,6 +230,74 @@ } } +Expected llvm::operator*(const ExpressionValue &LeftOperand, + const ExpressionValue &RightOperand) { + if (LeftOperand.isNegative() && RightOperand.isNegative()) + return LeftOperand.getAbsolute() * RightOperand.getAbsolute(); + + if (LeftOperand.isNegative() || RightOperand.isNegative()) + return ExpressionValue(0) - + cantFail(LeftOperand.getAbsolute() * RightOperand.getAbsolute()); + + uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue()); + uint64_t RightValue = cantFail(RightOperand.getUnsignedValue()); + return ExpressionValue(LeftValue * RightValue); +} + +Expected llvm::operator/(const ExpressionValue &LeftOperand, + const ExpressionValue &RightOperand) { + if (LeftOperand.isNegative() && RightOperand.isNegative()) + return LeftOperand.getAbsolute() / RightOperand.getAbsolute(); + + if (LeftOperand.isNegative() || RightOperand.isNegative()) + return ExpressionValue(0) - + cantFail(LeftOperand.getAbsolute() / RightOperand.getAbsolute()); + + uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue()); + uint64_t RightValue = cantFail(RightOperand.getUnsignedValue()); + return ExpressionValue(LeftValue / RightValue); +} + +Expected llvm::max(const ExpressionValue &LeftOperand, + const ExpressionValue &RightOperand) { + if (LeftOperand.isNegative() && RightOperand.isNegative()) { + int64_t LeftValue = cantFail(LeftOperand.getSignedValue()); + int64_t RightValue = cantFail(RightOperand.getSignedValue()); + return ExpressionValue(std::max(LeftValue, RightValue)); + } + + if (!LeftOperand.isNegative() && !RightOperand.isNegative()) { + uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue()); + uint64_t RightValue = cantFail(RightOperand.getUnsignedValue()); + return ExpressionValue(std::max(LeftValue, RightValue)); + } + + if (LeftOperand.isNegative()) + return RightOperand; + + return LeftOperand; +} + +Expected llvm::min(const ExpressionValue &LeftOperand, + const ExpressionValue &RightOperand) { + if (LeftOperand.isNegative() && RightOperand.isNegative()) { + int64_t LeftValue = cantFail(LeftOperand.getSignedValue()); + int64_t RightValue = cantFail(RightOperand.getSignedValue()); + return ExpressionValue(std::min(LeftValue, RightValue)); + } + + if (!LeftOperand.isNegative() && !RightOperand.isNegative()) { + uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue()); + uint64_t RightValue = cantFail(RightOperand.getUnsignedValue()); + return ExpressionValue(std::min(LeftValue, RightValue)); + } + + if (LeftOperand.isNegative()) + return LeftOperand; + + return RightOperand; +} + Expected NumericVariableUse::eval() const { Optional Value = Variable->getValue(); if (Value) @@ -336,6 +404,9 @@ // FileCheck input canonicalization. constexpr StringLiteral SpaceChars = " \t"; +// StringRef holding the prefix used to identify the name of a function. +constexpr StringLiteral CallPrefix = "!"; + // Parsing helper function that strips the first character in S and returns it. static char popFront(StringRef &S) { char C = S.front(); @@ -432,6 +503,14 @@ return parseParenExpr(Expr, LineNumber, Context, SM); } + // Try to parse a function call. + if (Expr.startswith(CallPrefix)) { + if (AO != AllowedOperand::Any) + return ErrorDiagnostic::get(SM, Expr, "unexpected function call"); + + return parseCallExpr(Expr, LineNumber, Context, SM); + } + if (AO == AllowedOperand::LineVar || AO == AllowedOperand::Any) { // Try to parse as a numeric variable use. Expected ParseVarResult = @@ -540,6 +619,92 @@ std::move(*RightOpResult)); } +Expected> +Pattern::parseCallExpr(StringRef &Expr, Optional LineNumber, + FileCheckPatternContext *Context, const SourceMgr &SM) { + assert(Expr.startswith(CallPrefix)); + Expr.consume_front(CallPrefix); + + // Record this for diagnostics that should be tied to the function name. + SMLoc OpLoc = SMLoc::getFromPointer(Expr.data()); + + size_t FuncNameEnd = Expr.find('('); + if (FuncNameEnd == StringRef::npos) + return ErrorDiagnostic::get( + SM, Expr, "call expression missing '(' for argument list"); + + StringRef FuncName = Expr.take_front(FuncNameEnd); + if (FuncName.empty()) + return ErrorDiagnostic::get(SM, Expr, "missing function name"); + + auto OptFunc = StringSwitch>(FuncName) + .Case("add", operator+) + .Case("mul", operator*) + .Case("sub", operator-) + .Case("div", operator/) + .Case("max", max) + .Case("min", min) + .Default(None); + + if (!OptFunc) + return ErrorDiagnostic::get( + SM, Expr, Twine("call to undefined function '") + FuncName + "'"); + + // Consume function name along with leading '('; + Expr = Expr.drop_front(FuncNameEnd + 1); + Expr = Expr.ltrim(SpaceChars); + + // Parse call arguments, which are comma separated. + SmallVector, 4> Args; + while (!Expr.empty() && !Expr.startswith(")")) { + Expr = Expr.rtrim(SpaceChars); + + // Parse the argument, which is an arbitary expression. + StringRef OuterBinOpExpr = Expr; + Expected> Arg = + parseNumericOperand(Expr, AllowedOperand::Any, LineNumber, Context, SM); + while (Arg && !Expr.empty()) { + Expr = Expr.ltrim(SpaceChars); + // Have we reached an argument terminator? + if (Expr.startswith(",") || Expr.startswith(")")) + break; + + // Arg = Arg + Arg = parseBinop(OuterBinOpExpr, Expr, std::move(*Arg), false, LineNumber, + Context, SM); + } + + // Prefer an expression error over a generic invalid argument message. + if (!Arg) + return Arg.takeError(); + Args.push_back(std::move(*Arg)); + + // Have we parsed all available arguments? + Expr = Expr.ltrim(SpaceChars); + if (!Expr.consume_front(",")) + break; + + Expr = Expr.ltrim(SpaceChars); + if (Expr.startswith(")")) + return ErrorDiagnostic::get(SM, Expr, "missing argument"); + } + + if (!Expr.consume_front(")")) + return ErrorDiagnostic::get(SM, Expr, + "missing ')' at end of call expression"); + + const unsigned NumArgs = Args.size(); + if (NumArgs == 2) + return std::make_unique(Expr, *OptFunc, std::move(Args[0]), + std::move(Args[1])); + + // TODO: Support more than binop_eval_t. + return ErrorDiagnostic::get(SM, OpLoc, + Twine("function '") + FuncName + + Twine("' takes 2 arguments but ") + + Twine(NumArgs) + " given"); +} + Expected> Pattern::parseNumericSubstitutionBlock( StringRef Expr, Optional &DefinedNumericVariable, bool IsLegacyLineExpr, Optional LineNumber, @@ -549,9 +714,10 @@ DefinedNumericVariable = None; ExpressionFormat ExplicitFormat = ExpressionFormat(); - // Parse format specifier. + // Parse format specifier (NOTE: ',' is also an argument seperator). size_t FormatSpecEnd = Expr.find(','); - if (FormatSpecEnd != StringRef::npos) { + size_t FunctionStart = Expr.find(CallPrefix); + if (FormatSpecEnd != StringRef::npos && FormatSpecEnd < FunctionStart) { Expr = Expr.ltrim(SpaceChars); if (!Expr.consume_front("%")) return ErrorDiagnostic::get( diff --git a/llvm/lib/Support/FileCheckImpl.h b/llvm/lib/Support/FileCheckImpl.h --- a/llvm/lib/Support/FileCheckImpl.h +++ b/llvm/lib/Support/FileCheckImpl.h @@ -141,6 +141,14 @@ const ExpressionValue &Rhs); Expected operator-(const ExpressionValue &Lhs, const ExpressionValue &Rhs); +Expected operator*(const ExpressionValue &Lhs, + const ExpressionValue &Rhs); +Expected operator/(const ExpressionValue &Lhs, + const ExpressionValue &Rhs); +Expected max(const ExpressionValue &Lhs, + const ExpressionValue &Rhs); +Expected min(const ExpressionValue &Lhs, + const ExpressionValue &Rhs); /// Base class representing the AST of a given expression. class ExpressionAST { @@ -711,10 +719,10 @@ FileCheckPatternContext *Context, const SourceMgr &SM); enum class AllowedOperand { LineVar, LegacyLiteral, Any }; /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or - /// before input is parsed if \p LineNumber is None. Accepts both literal - /// values and numeric variables, depending on the value of \p AO. Parameter - /// \p Context points to the class instance holding the live string and - /// numeric variables. \returns the class representing that operand in the + /// before input is parsed if \p LineNumber is None. Accepts literal values, + /// numeric variables and function calls, depending on the value of \p AO. + /// Parameter \p Context points to the class instance holding the live string + /// and numeric variables. \returns the class representing that operand in the /// AST of the expression or an error holding a diagnostic against \p SM /// otherwise. If \p Expr starts with a "(" this function will attempt to /// parse a parenthesized expression. @@ -746,6 +754,15 @@ static Expected> parseParenExpr(StringRef &Expr, Optional LineNumber, FileCheckPatternContext *Context, const SourceMgr &SM); + + /// Parses \p Expr for a function call at line \p LineNumber, or before input + /// is parsed if \p LineNumber is None. Parameter \p Context points to the + /// class instance holding the live string and numeric variables. \returns the + /// class representing that call in the AST of the expression or an error + /// holding a diagnostic against \p SM otherwise. + static Expected> + parseCallExpr(StringRef &Expr, Optional LineNumber, + FileCheckPatternContext *Context, const SourceMgr &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 @@ -55,16 +55,36 @@ 11 // CHECK-NEXT: {{^}}[[#%u,UNSI]] 12 // CHECK-NEXT: {{^}}[[#%u,UNSI+1]] 10 // CHECK-NEXT: {{^}}[[#%u,UNSI-1]] +15 // CHECK-NEXT: {{^}}[[#%u,!add(UNSI,4)]] +3 // CHECK-NEXT: {{^}}[[#%u,!div(UNSI,3)]] +11 // CHECK-NEXT: {{^}}[[#%u,!max(UNSI,7)]] +99 // CHECK-NEXT: {{^}}[[#%u,!max(UNSI,99)]] +7 // CHECK-NEXT: {{^}}[[#%u,!min(UNSI,7)]] +11 // CHECK-NEXT: {{^}}[[#%u,!min(UNSI,99)]] +77 // CHECK-NEXT: {{^}}[[#%u,!mul(UNSI,7)]] +8 // CHECK-NEXT: {{^}}[[#%u,!sub(UNSI,3)]] c // CHECK-NEXT: {{^}}[[#%x,LHEX]] d // CHECK-NEXT: {{^}}[[#%x,LHEX+1]] b // CHECK-NEXT: {{^}}[[#%x,LHEX-1]] 1a // CHECK-NEXT: {{^}}[[#%x,LHEX+0xe]] 1a // CHECK-NEXT: {{^}}[[#%x,LHEX+0xE]] +e // CHECK-NEXT: {{^}}[[#%x,!add(LHEX,2)]] +6 // CHECK-NEXT: {{^}}[[#%x,!div(LHEX,2)]] +ff // CHECK-NEXT: {{^}}[[#%x,!max(LHEX,0xff)]] +a // CHECK-NEXT: {{^}}[[#%x,!min(LHEX,0xa)]] +c0 // CHECK-NEXT: {{^}}[[#%x,!mul(LHEX,16)]] +a // CHECK-NEXT: {{^}}[[#%x,!sub(LHEX,2)]] D // CHECK-NEXT: {{^}}[[#%X,UHEX]] E // CHECK-NEXT: {{^}}[[#%X,UHEX+1]] C // CHECK-NEXT: {{^}}[[#%X,UHEX-1]] 1B // CHECK-NEXT: {{^}}[[#%X,UHEX+0xe]] 1B // CHECK-NEXT: {{^}}[[#%X,UHEX+0xE]] +F // CHECK-NEXT: {{^}}[[#%X,!add(UHEX,2)]] +6 // CHECK-NEXT: {{^}}[[#%X,!div(UHEX,2)]] +FF // CHECK-NEXT: {{^}}[[#%X,!max(UHEX,0xff)]] +A // CHECK-NEXT: {{^}}[[#%X,!min(UHEX,0xa)]] +D0 // CHECK-NEXT: {{^}}[[#%X,!mul(UHEX,16)]] +B // CHECK-NEXT: {{^}}[[#%X,!sub(UHEX,2)]] -30 // CHECK-NEXT: {{^}}[[#%d,SIGN]] -29 // CHECK-NEXT: {{^}}[[#%d,SIGN+1]] -31 // CHECK-NEXT: {{^}}[[#%d,SIGN-1]] @@ -92,6 +112,14 @@ 10 // CHECK-NEXT: {{^}}[[# %u , UNSI -1]] 10 // CHECK-NEXT: {{^}}[[# %u , UNSI - 1]] 10 // CHECK-NEXT: {{^}}[[# %u , UNSI - 1 ]] +22 // CHECK-NEXT: {{^}}[[#%u, !mul(UNSI,2)]] +22 // CHECK-NEXT: {{^}}[[# %u, !mul(UNSI,2)]] +22 // CHECK-NEXT: {{^}}[[# %u , !mul(UNSI,2)]] +22 // CHECK-NEXT: {{^}}[[# %u , !mul(UNSI, 2)]] +22 // CHECK-NEXT: {{^}}[[# %u , !mul( UNSI, 2)]] +22 // CHECK-NEXT: {{^}}[[# %u , !mul( UNSI,2)]] +22 // CHECK-NEXT: {{^}}[[# %u , !mul(UNSI,2) ]] +67 // CHECK-NEXT: {{^}}[[# %u , UNSI + !div( !mul(100 , UNSI ), 20) +1 ]] ; Numeric expressions in implicit matching format and default matching rule using ; variables defined on other lines. @@ -99,16 +127,28 @@ 11 // CHECK-NEXT: {{^}}[[#UNSI]] 12 // CHECK-NEXT: {{^}}[[#UNSI+1]] 10 // CHECK-NEXT: {{^}}[[#UNSI-1]] +1 // CHECK-NEXT: {{^}}[[#!div(UNSI,9)]] +99 // CHECK-NEXT: {{^}}[[#!max(UNSI,99)]] +7 // CHECK-NEXT: {{^}}[[#!min(UNSI,7)]] +77 // CHECK-NEXT: {{^}}[[#!mul(UNSI,7)]] c // CHECK-NEXT: {{^}}[[#LHEX]] d // CHECK-NEXT: {{^}}[[#LHEX+1]] b // CHECK-NEXT: {{^}}[[#LHEX-1]] 1a // CHECK-NEXT: {{^}}[[#LHEX+0xe]] 1a // CHECK-NEXT: {{^}}[[#LHEX+0xE]] +3 // CHECK-NEXT: {{^}}[[#!div(LHEX,4)]] +ff // CHECK-NEXT: {{^}}[[#!max(LHEX,255)]] +a // CHECK-NEXT: {{^}}[[#!min(LHEX,10)]] +c0 // CHECK-NEXT: {{^}}[[#!mul(LHEX,16)]] D // CHECK-NEXT: {{^}}[[#UHEX]] E // CHECK-NEXT: {{^}}[[#UHEX+1]] C // CHECK-NEXT: {{^}}[[#UHEX-1]] 1B // CHECK-NEXT: {{^}}[[#UHEX+0xe]] 1B // CHECK-NEXT: {{^}}[[#UHEX+0xE]] +D // CHECK-NEXT: {{^}}[[#!div(UHEX,1)]] +FF // CHECK-NEXT: {{^}}[[#!max(UHEX,255)]] +A // CHECK-NEXT: {{^}}[[#!min(UHEX,10)]] +D0 // CHECK-NEXT: {{^}}[[#!mul(UHEX,16)]] -30 // CHECK-NEXT: {{^}}[[#SIGN]] -29 // CHECK-NEXT: {{^}}[[#SIGN+1]] -31 // CHECK-NEXT: {{^}}[[#SIGN-1]] @@ -367,3 +407,75 @@ UNDERFLOW-MSG: numeric-expression.txt:[[#@LINE-1]]:29: error: unable to substitute variable or numeric expression UNDERFLOW-MSG-NEXT: {{U}}NDERFLOW-NEXT: TINYVAR: {{\[\[#%d,TINYVAR:-0x8000000000000000-0x8000000000000000\]\]}} UNDERFLOW-MSG-NEXT: {{^}} ^{{$}} + +RUN: %ProtectFileCheckOutput \ +RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-MISSING-OPENING-BRACKET --input-file %s %s 2>&1 \ +RUN: | FileCheck --strict-whitespace --check-prefix CALL-MISSING-OPENING-BRACKET-MSG %s + +CALL MISSING OPENING BRACKET +30 +CALL-MISSING-OPENING-BRACKET-LABEL: CALL MISSING OPENING BRACKET +CALL-MISSING-OPENING-BRACKET-NEXT: [[#!mulNUMVAR,3)]] +CALL-MISSING-OPENING-BRACKET-MSG: numeric-expression.txt:[[#@LINE-1]]:40: error: call expression missing '(' for argument list +CALL-MISSING-OPENING-BRACKET-MSG-NEXT: {{C}}ALL-MISSING-OPENING-BRACKET-NEXT: {{\[\[#!mulNUMVAR,3\)\]\]}} +CALL-MISSING-OPENING-BRACKET-MSG-NEXT: {{^}} ^{{$}} + +RUN: %ProtectFileCheckOutput \ +RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-MISSING-CLOSING-BRACKET --input-file %s %s 2>&1 \ +RUN: | FileCheck --strict-whitespace --check-prefix CALL-MISSING-CLOSING-BRACKET-MSG %s + +CALL MISSING CLOSING BRACKET +30 +CALL-MISSING-CLOSING-BRACKET-LABEL: CALL MISSING CLOSING BRACKET +CALL-MISSING-CLOSING-BRACKET-NEXT: [[#!mul(NUMVAR,3]] +CALL-MISSING-CLOSING-BRACKET-MSG: numeric-expression.txt:[[#@LINE-1]]:52: error: missing ')' at end of call expression +CALL-MISSING-CLOSING-BRACKET-MSG-NEXT: {{C}}ALL-MISSING-CLOSING-BRACKET-NEXT: {{\[\[#!mul\(NUMVAR,3\]\]}} +CALL-MISSING-CLOSING-BRACKET-MSG-NEXT: {{^}} ^{{$}} + +RUN: %ProtectFileCheckOutput \ +RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-MISSING-NAME --input-file %s %s 2>&1 \ +RUN: | FileCheck --strict-whitespace --check-prefix CALL-MISSING-NAME-MSG %s + +CALL MISSING NAME +30 +CALL-MISSING-NAME-LABEL: CALL MISSING NAME +CALL-MISSING-NAME-NEXT: [[#!(NUMVAR)]] +CALL-MISSING-NAME-MSG: numeric-expression.txt:[[#@LINE-1]]:29: error: missing function name +CALL-MISSING-NAME-MSG-NEXT: {{C}}ALL-MISSING-NAME-NEXT: {{\[\[#!\(NUMVAR\)\]\]}} +CALL-MISSING-NAME-MSG-NEXT: {{^}} ^{{$}} + +RUN: %ProtectFileCheckOutput \ +RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-MISSING-ARGUMENT --input-file %s %s 2>&1 \ +RUN: | FileCheck --strict-whitespace --check-prefix CALL-MISSING-ARGUMENT-MSG %s + +CALL MISSING ARGUMENT +30 +CALL-MISSING-ARGUMENT-LABEL: CALL MISSING ARGUMENT +CALL-MISSING-ARGUMENT-NEXT: [[#!mul(NUMVAR,)]] +CALL-MISSING-ARGUMENT-MSG: numeric-expression.txt:[[#@LINE-1]]:44: error: missing argument +CALL-MISSING-ARGUMENT-MSG-NEXT: {{C}}ALL-MISSING-ARGUMENT-NEXT: {{\[\[#!mul\(NUMVAR,\)\]\]}} +CALL-MISSING-ARGUMENT-MSG-NEXT: {{^}} ^{{$}} + +RUN: %ProtectFileCheckOutput \ +RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-WRONG-ARGUMENT-COUNT --input-file %s %s 2>&1 \ +RUN: | FileCheck --strict-whitespace --check-prefix CALL-WRONG-ARGUMENT-COUNT-MSG %s + +CALL WRONG ARGUMENT COUNT +30 +CALL-WRONG-ARGUMENT-COUNT-LABEL: CALL WRONG ARGUMENT COUNT +CALL-WRONG-ARGUMENT-COUNT-NEXT: [[#!mul(NUMVAR)]] +CALL-WRONG-ARGUMENT-COUNT-MSG: numeric-expression.txt:[[#@LINE-1]]:37: error: function 'mul' takes 2 arguments but 1 given +CALL-WRONG-ARGUMENT-COUNT-MSG-NEXT: {{C}}ALL-WRONG-ARGUMENT-COUNT-NEXT: {{\[\[#!mul\(NUMVAR\)\]\]}} +CALL-WRONG-ARGUMENT-COUNT-MSG-NEXT: {{^}} ^{{$}} + +RUN: %ProtectFileCheckOutput \ +RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-UNDEFINED-FUNCTION --input-file %s %s 2>&1 \ +RUN: | FileCheck --strict-whitespace --check-prefix CALL-UNDEFINED-FUNCTION-MSG %s + +CALL UNDEFINED FUNCTION +30 +CALL-UNDEFINED-FUNCTION-LABEL: CALL UNDEFINED FUNCTION +CALL-UNDEFINED-FUNCTION-NEXT: [[#!bogus_function(NUMVAR)]] +CALL-UNDEFINED-FUNCTION-MSG: numeric-expression.txt:[[#@LINE-1]]:35: error: call to undefined function 'bogus_function' +CALL-UNDEFINED-FUNCTION-MSG-NEXT: {{C}}ALL-UNDEFINED-FUNCTION-NEXT: {{\[\[#!bogus_function\(NUMVAR\)\]\]}} +CALL-UNDEFINED-FUNCTION-MSG-NEXT: {{^}} ^{{$}} diff --git a/llvm/unittests/Support/FileCheckTest.cpp b/llvm/unittests/Support/FileCheckTest.cpp --- a/llvm/unittests/Support/FileCheckTest.cpp +++ b/llvm/unittests/Support/FileCheckTest.cpp @@ -1016,6 +1016,36 @@ Tester.parseSubst("(2))").takeError()); expectDiagnosticError("unsupported operation ')'", Tester.parseSubst("(1))(").takeError()); + + // Valid expression with function call. + EXPECT_THAT_EXPECTED(Tester.parseSubst("!mul(FOO,3)"), Succeeded()); + // Valid expression with nested function call. + EXPECT_THAT_EXPECTED(Tester.parseSubst("!mul(FOO, !min(BAR,10))"), + Succeeded()); + // Valid expression with function call taking expression as argument. + EXPECT_THAT_EXPECTED(Tester.parseSubst("!mul(FOO, !min(BAR,10) + 3)"), + Succeeded()); + + // Malformed call syntax. + expectDiagnosticError("call expression missing '(' for argument list", + Tester.parseSubst("!mulFOO,3)").takeError()); + expectDiagnosticError("missing ')' at end of call expression", + Tester.parseSubst("!mul(FOO,!min(BAR,7)").takeError()); + expectDiagnosticError("missing function name", + Tester.parseSubst("!(FOO)").takeError()); + expectDiagnosticError("missing argument", + Tester.parseSubst("!mul(FOO,)").takeError()); + + // Valid call, but to an unknown function. + expectDiagnosticError( + "call to undefined function 'bogus_function'", + Tester.parseSubst("!bogus_function(FOO,3)").takeError()); + + // Valid call, but with incorrect argument count. + expectDiagnosticError("function 'mul' takes 2 arguments but 1 given", + Tester.parseSubst("!mul(FOO)").takeError()); + expectDiagnosticError("function 'mul' takes 2 arguments but 3 given", + Tester.parseSubst("!mul(FOO,3,4)").takeError()); } TEST_F(FileCheckTest, ParsePattern) { @@ -1189,6 +1219,47 @@ EXPECT_THAT_EXPECTED(Tester.match("20"), Succeeded()); } +TEST_F(FileCheckTest, MatchBuiltinFunctions) { + PatternTester Tester; + // Esnure #NUMVAR has the expected value. + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#NUMVAR:]]")); + expectNotFoundError(Tester.match("FAIL").takeError()); + expectNotFoundError(Tester.match("").takeError()); + EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); + + // Check each builtin function generates the expected result. + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!add(NUMVAR,13)]]")); + EXPECT_THAT_EXPECTED(Tester.match("31"), Succeeded()); + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!mul(NUMVAR,3)]]")); + EXPECT_THAT_EXPECTED(Tester.match("54"), Succeeded()); + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!sub(NUMVAR,7)]]")); + EXPECT_THAT_EXPECTED(Tester.match("11"), Succeeded()); + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!div(NUMVAR,3)]]")); + EXPECT_THAT_EXPECTED(Tester.match("6"), Succeeded()); + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!max(NUMVAR,5)]]")); + EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!max(NUMVAR,99)]]")); + EXPECT_THAT_EXPECTED(Tester.match("99"), Succeeded()); + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!min(NUMVAR,5)]]")); + EXPECT_THAT_EXPECTED(Tester.match("5"), Succeeded()); + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!min(NUMVAR,99)]]")); + EXPECT_THAT_EXPECTED(Tester.match("18"), Succeeded()); + + // Check nested function calls. + Tester.initNextPattern(); + ASSERT_FALSE(Tester.parsePattern("[[#!mul(!min(7,2),!max(4,10))]]")); + EXPECT_THAT_EXPECTED(Tester.match("20"), Succeeded()); +} + TEST_F(FileCheckTest, Substitution) { SourceMgr SM; FileCheckPatternContext Context;