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 @@ -105,6 +105,11 @@ Sets a filecheck pattern variable ``VAR`` with value ``VALUE`` that can be used in ``CHECK:`` lines. +.. option:: -D#= + + Sets a filecheck numeric variable ``VAR`` to ```` that can be used + in ``CHECK:`` lines. + .. option:: -version Show the version number of this program. @@ -560,8 +565,52 @@ This makes it easier to ensure that individual tests are not affected by variables set in preceding tests. -FileCheck Numeric Expressions -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +FileCheck Numeric Variables and Expressions +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:program:`FileCheck` also allows to check for numeric values satisfying a +numeric expression constraint based on numeric variables. This allows to +capture numeric relations between two numbers in a text to check, such as the +the need for consecutive registers to be used. + +The syntax to check a numeric expression constraint is +``[[#]]`` where: + +*```` is the name of a numeric variable defined on the command line. + +*```` is an optional numeric operation to perform on the value of +````. Currently supported numeric operations are ``+`` and ``-``. + +*```` is the immediate value that constitutes the second operand of +the numeric operation . It must be present if ```` is present, +absent otherwise. + +For example: + +.. code-block:: llvm + + ; CHECK: add r[[#REG]], r[[#REG]], r[[#REG+1]] + +The above example would match the lines: + +.. code-block:: llvm + + add r5, r5, r6 + +but would not match the lines: + +.. code-block:: llvm + + add r5, r5, r7 + +due to ``7`` being unequal to ``5 + 1``. + +In the same way as for pattern variables, ``--enable-var-scope`` only consider +global numeric variables that start with ``$`` and undefined local variables at +the beginning of each CHECK-LABEL block. + +FileCheck Pseudo Numeric Variables +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sometimes there's a need to verify output which contains line numbers of the match file, e.g. when testing compiler diagnostics. This introduces a certain @@ -569,11 +618,9 @@ line numbers in the same file, which have to be updated whenever line numbers change due to text addition or deletion. -To support this case, FileCheck allows using ``[[#@LINE]]``, -``[[#@LINE+]]``, ``[[#@LINE-]]`` numeric expressions in -patterns, with arbitrary number of space between each elements of the -expression. These expressions expand to a number of the line where a pattern -is located (with an optional integer offset). +To support this case, FileCheck understands the ``@LINE`` pseudo numeric +variable which evaluate to the line number of the CHECK pattern where it is +found. This way match patterns can be put near the relevant test lines and include relative line number references, for example: diff --git a/llvm/include/llvm/Support/FileCheck.h b/llvm/include/llvm/Support/FileCheck.h --- a/llvm/include/llvm/Support/FileCheck.h +++ b/llvm/include/llvm/Support/FileCheck.h @@ -40,20 +40,86 @@ // Numeric expression handling code. //===----------------------------------------------------------------------===// +class FileCheckASTBinop; + /// Class representing a numeric expression. class FileCheckNumExpr { private: - /// Value of the numeric expression.. + /// Pointer to AST of the numeric expression. + std::unique_ptr AST; + +public: + /// Generic constructor for a numeric expression whose equality constraint is + /// represented by \p AST. + FileCheckNumExpr(FileCheckASTBinop *AST) : AST(std::move(AST)) {} + + /// Return pointer to AST of the numeric expression. Pointer is guaranteed to + /// be valid as long as this object is. + FileCheckASTBinop *getAST() const { return AST.get(); } +}; + +/// Class representing a numeric variable with a given value in the AST of a +/// numeric expression. +class FileCheckNumVar { +private: + /// Name of the numeric variable. + StringRef Name; + + /// Whether variable is defined and thus Value is set. + bool Defined; + + /// Value of numeric variable if defined. uint64_t Value; public: - /// Constructor for a numeric expression with a known value at parse time, - /// e.g. the implicit numeric expression defining the @LINE numeric pseudo - /// variable. - explicit FileCheckNumExpr(uint64_t Value) : Value(Value) {} + /// Constructor for numeric variable \p Name with a known \p Value at parse + /// time (e.g. the @LINE numeric variable). + explicit FileCheckNumVar(StringRef Name, uint64_t Value) + : Name(Name), Defined(true), Value(Value) {} + + /// Return name of that numeric variable. + StringRef getName() const { return Name; } + + /// Return value of this numeric variable. + llvm::Optional getValue() const; + + /// Set value of this numeric variable if not defined. Return whether + /// the variable was already defined. + bool setValue(uint64_t Value); - /// Return the value being matched against. - uint64_t getValue() const { return Value; } + /// Clear value of this numeric variable. Return whether the variable was + /// already undefined. + bool clearValue(); +}; + +/// Type of functions evaluating a given binary operation. +using binop_eval_t = uint64_t (*)(uint64_t, uint64_t); + +/// Class representing a single binary operation in the AST of a numeric +/// expression. +class FileCheckASTBinop { +private: + /// Left operand. + FileCheckNumVar *Opl; + + /// Right operand. + uint64_t Opr; + + /// Pointer to function that can evaluate this binary operation. + binop_eval_t EvalBinop; + +public: + FileCheckASTBinop(binop_eval_t EvalBinop, FileCheckNumVar *OperandLeft, + uint64_t OperandRight) + : Opl(OperandLeft), Opr(OperandRight), EvalBinop(EvalBinop) {} + + /// Evaluate the value of the binary operation represented by this AST. Uses + /// EvalBinop to perform the binary operation. + llvm::Optional eval() const; + + /// Return the name of the undefined variable used in this substitution if + /// any or an empty string otherwise. + StringRef getUndefVarName() const; }; class FileCheckPatternContext; @@ -174,19 +240,34 @@ /// back-references are used for uses after any the other definition. StringMap GlobalVariableTable; + /// Map of all pattern variables defined so far. Used at parse time to detect + /// a name conflict between a numeric variable and a pattern variable when + /// the former is defined on a later line than the latter. + StringMap DefinedVariableTable; + + /// When matching a given pattern, this holds the pointers to the classes + /// representing the AST of all numeric variables defined in previous + /// patterns along with their values. In a pattern, only the last definition + /// for a given variable is recorded in this table. + StringMap GlobalNumericVariableTable; + /// Vector holding pointers to all numeric expressions parsed. Used to /// automatically free the numeric expressions once they are guaranteed to no /// longer be used. std::vector> NumExprs; + /// Vector holding pointers to all numeric variables parsed. Used to + /// automatically free them once they are guaranteed to no longer be used. + std::vector> NumVars; + public: /// Return the value of pattern variable \p VarName or None if no such /// variable has been defined. llvm::Optional getPatternVarValue(StringRef VarName); - /// Define pattern variables from definitions given on the command line - /// passed as a vector of VAR=VAL strings in \p CmdlineDefines. Report any - /// error to \p SM and return whether an error occured. + /// Define pattern and numeric variables from definitions given on the + /// command line passed as a vector of VAR=VAL strings in \p CmdlineDefines. + /// Report any error to \p SM and return whether an error occured. bool defineCmdlineVariables(std::vector &CmdlineDefines, SourceMgr &SM); @@ -197,7 +278,11 @@ private: /// Register a numeric expression for destruction when the context is /// destroyed. - template FileCheckNumExpr *registerNumExpr(Types... Args); + FileCheckNumExpr *registerNumExpr(FileCheckASTBinop *AST); + + /// Make a new numeric variable and register it for destruction when the + /// context is destroyed. + FileCheckNumVar *makeNumVar(StringRef Name, uint64_t Value); }; class FileCheckPattern { @@ -213,12 +298,12 @@ /// Entries in this vector represent uses of a pattern variable or a numeric /// expression in the pattern that need to be substituted in the regexp - /// pattern at match time, e.g. "foo[[bar]]baz[[#@LINE+1]]". In this case, - /// the RegExStr will contain "foobaz" and we'll get two entries in this - /// vector that tells us to insert the value of pattern variable "bar" at - /// offset 3 and the value of numeric expression "@LINE+1" at offset 6. Uses - /// are represented by a FileCheckPatternSubst class to abstract whether it - /// is a pattern variable or a numeric expression. + /// pattern at match time, e.g. "foo[[bar]]baz[[#N+1]]". In this case, the + /// RegExStr will contain "foobaz" and we'll get two entries in this vector + /// that tells us to insert the value of pattern variable "bar" at offset 3 + /// and the value of numeric expression "N+1" at offset 6. Uses are + /// represented by a FileCheckPatternSubst class to abstract whether it is a + /// pattern variable or a numeric expression. std::vector Substs; /// Maps names of pattern variables defined in a pattern to the parenthesized @@ -232,8 +317,12 @@ /// iterating over values. std::map VariableDefs; - /// Pointer to the class instance shared by all patterns holding a table with - /// the values of live variables at the start of any given CHECK line. + /// Pointer to a class instance holding the global state shared by all + /// patterns: + /// - tables with the values of live pattern and numeric variables at + /// the start of any given CHECK line; + /// - table holding whether a pattern variable has been defined at any given + /// point during the parsing phase. FileCheckPatternContext *Context; Check::FileCheckType CheckTy; @@ -260,11 +349,13 @@ /// variable and \p TrailIdx to the position of the last character that is /// part of the variable name. Otherwise, only return true. static bool parseVariable(StringRef Str, bool &IsPseudo, unsigned &TrailIdx); - /// Parse a numeric expression involving pseudo variable \p Name with the - /// string corresponding to the operation being performed in \p Trailer. - /// Return the class representing the numeric expression or nullptr if - /// parsing fails in which case errors are reported on \p SM. - FileCheckNumExpr *parseNumericExpression(StringRef Name, StringRef Trailer, + /// Parse a numeric expression involving (pseudo if \p IsPseudo is true) + /// variable \p Name with the string corresponding to the operation being + /// performed in \p Trailer. Return the class representing the numeric + /// expression or nullptr if parsing fails in which case errors are reported + /// on \p SM. + FileCheckNumExpr *parseNumericExpression(StringRef Name, bool IsPseudo, + StringRef Trailer, const SourceMgr &SM) const; bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM, unsigned LineNumber, const FileCheckRequest &Req); 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 @@ -24,9 +24,47 @@ using namespace llvm; +llvm::Optional FileCheckNumVar::getValue() const { + if (!Defined) + return llvm::None; + return Value; +} + +bool FileCheckNumVar::setValue(uint64_t Value) { + if (Defined) + return true; + this->Value = Value; + this->Defined = true; + return false; +} + +bool FileCheckNumVar::clearValue() { + if (!Defined) + return true; + Defined = false; + return false; +} + +llvm::Optional FileCheckASTBinop::eval() const { + llvm::Optional Opl = this->Opl->getValue(); + // Variable has been undefined. + if (!Opl) + return llvm::None; + return EvalBinop(*Opl, Opr); +} + +StringRef FileCheckASTBinop::getUndefVarName() const { + if (!Opl->getValue()) + return Opl->getName(); + return StringRef(); +} + llvm::Optional FileCheckPatternSubst::getSubstitute() const { if (IsNumExpr) { - return utostr(NumExpr->getValue()); + llvm::Optional EvaluatedValue = NumExpr->getAST()->eval(); + if (!EvaluatedValue) + return llvm::None; + return utostr(*EvaluatedValue); } else { // Look up the value and escape it so that we can put it into the // regex. @@ -38,15 +76,16 @@ } StringRef FileCheckPatternSubst::getUndefVarName() const { - // Parsing guarantees only @LINE is ever referenced and it is not undefined - // by ClearLocalVars. if (IsNumExpr) - return StringRef(); - - if (!Context->getPatternVarValue(SubstStr)) - return SubstStr; + // Although use of undefined numeric variable is tested at parse time, + // numeric variable can get undefined later by ClearLocalVariables. + return NumExpr->getAST()->getUndefVarName(); + else { + if (!Context->getPatternVarValue(SubstStr)) + return SubstStr; - return StringRef(); + return StringRef(); + } } bool FileCheckPattern::isValidVarNameStart(char C) { @@ -91,31 +130,66 @@ return C; } +static uint64_t doAdd(uint64_t Opl, uint64_t Opr) { return Opl + Opr; } +static uint64_t doSub(uint64_t Opl, uint64_t Opr) { return Opl - Opr; } + FileCheckNumExpr * -FileCheckPattern::parseNumericExpression(StringRef Name, StringRef Trailer, +FileCheckPattern::parseNumericExpression(StringRef Name, bool IsPseudo, + StringRef Trailer, const SourceMgr &SM) const { - if (!Name.equals("@LINE")) { + if (IsPseudo && !Name.equals("@LINE")) { SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, "invalid pseudo numeric variable '" + Name + "'"); return nullptr; } - // Check if this is a supported operation and select function to perform it. + // This method is indirectly called from ParsePattern for all numeric + // variable definition and uses in the order in which they appear in the + // CHECK pattern. For each definition, the pointer to the corresponding AST + // class instance is stored in GlobalNumericVariableTable. Therefore the + // pointer we get below is for the AST class instance corresponding to the + // last definition of the variable before this use. + auto VarTableIter = Context->GlobalNumericVariableTable.find(Name); + if (VarTableIter == Context->GlobalNumericVariableTable.end()) { + SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, + "using undefined numeric variable '" + Name + "'"); + return nullptr; + } + + FileCheckNumVar *Opl = VarTableIter->second; + + // Check if this is a supported operation and selection function to perform + // it. skipWhitespace(Trailer); - if (Trailer.empty()) - return Context->registerNumExpr(LineNumber); + if (Trailer.empty()) { + return Context->registerNumExpr(new FileCheckASTBinop(doAdd, Opl, 0)); + } SMLoc OpLoc = SMLoc::getFromPointer(Trailer.data()); char Operator = next(Trailer); + binop_eval_t EvalBinop; + switch (Operator) { + case '+': + EvalBinop = doAdd; + break; + case '-': + EvalBinop = doSub; + break; + default: + SM.PrintMessage(OpLoc, SourceMgr::DK_Error, + Twine("unsupported numeric operation '") + Twine(Operator) + + "'"); + return nullptr; + } // Parse right operand. skipWhitespace(Trailer); if (Trailer.empty()) { SM.PrintMessage(SMLoc::getFromPointer(Trailer.data()), SourceMgr::DK_Error, - "missing operand in numeric expression '" + Trailer + "'"); + "missing operand in numeric expression"); return nullptr; } - uint64_t Offset; - if (Trailer.consumeInteger(10, Offset)) { + uint64_t Opr; + if (Trailer.consumeInteger(10, Opr)) { SM.PrintMessage(SMLoc::getFromPointer(Trailer.data()), SourceMgr::DK_Error, "invalid offset in numeric expression '" + Trailer + "'"); return nullptr; @@ -128,21 +202,7 @@ return nullptr; } - uint64_t Value; - switch (Operator) { - case '+': - Value = LineNumber + Offset; - break; - case '-': - Value = LineNumber - Offset; - break; - default: - SM.PrintMessage(OpLoc, SourceMgr::DK_Error, - Twine("unsupported numeric operation '") + Twine(Operator) + - "'"); - return nullptr; - } - return Context->registerNumExpr(Value); + return Context->registerNumExpr(new FileCheckASTBinop(EvalBinop, Opl, Opr)); } /// Parses the given string into the Pattern. @@ -159,6 +219,12 @@ this->LineNumber = LineNumber; PatternLoc = SMLoc::getFromPointer(PatternStr.data()); + // Create fake @LINE pseudo variable definition. + StringRef LinePseudo = "@LINE"; + uint64_t LineNumber64 = LineNumber; + auto LinePseudoVar = Context->makeNumVar(LinePseudo, LineNumber64); + Context->GlobalNumericVariableTable[LinePseudo] = LinePseudoVar; + if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines)) // Ignore trailing whitespace. while (!PatternStr.empty() && @@ -287,21 +353,33 @@ StringRef Trailer = MatchStr.substr(TrailIdx); bool IsVarDef = (VarEndIdx != StringRef::npos); - if (IsVarDef && (IsPseudo || !Trailer.consume_front(":"))) { - SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data()), - SourceMgr::DK_Error, - "invalid name in pattern variable definition"); - return true; + if (IsVarDef) { + if (IsPseudo || !Trailer.consume_front(":")) { + SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data()), + SourceMgr::DK_Error, + "invalid name in pattern variable definition"); + return true; + } + + // Detect collision between pattern and numeric variable when the former + // is created later than the latter. + if (Context->GlobalNumericVariableTable.find(Name) != + Context->GlobalNumericVariableTable.end()) { + SM.PrintMessage( + SMLoc::getFromPointer(MatchStr.data()), SourceMgr::DK_Error, + "numeric variable with name '" + Name + "' already exists"); + return true; + } } - if (!IsVarDef && IsPseudo) { - NumExpr = parseNumericExpression(Name, Trailer, SM); + if (IsNumExpr || (!IsVarDef && IsPseudo)) { + NumExpr = parseNumericExpression(Name, IsPseudo, Trailer, SM); if (NumExpr == nullptr) return true; IsNumExpr = true; } - // Handle [[foo]]. + // Handle variable use: [[foo]] and [[#]]. if (!IsVarDef) { // Handle use of pattern variables that were defined earlier on the // same line by emitting a backreference. @@ -329,6 +407,12 @@ // Handle [[foo:.*]]. VariableDefs[Name] = CurParen; + // Mark pattern variable as defined to detect collision between pattern + // and numeric variable in DefineCmdlineVariables when the latter is + // created later than the former. We cannot reuse GlobalVariableTable for + // that by populating it with an empty string since we would then loose + // the ability to detect use of undefined variable in Match(). + Context->DefinedVariableTable[Name] = true; RegExStr += '('; ++CurParen; @@ -582,12 +666,18 @@ return VarIter->second; } -template -FileCheckNumExpr *FileCheckPatternContext::registerNumExpr(Types... Args) { - NumExprs.emplace_back(new FileCheckNumExpr(Args...)); +FileCheckNumExpr * +FileCheckPatternContext::registerNumExpr(FileCheckASTBinop *AST) { + NumExprs.emplace_back(new FileCheckNumExpr(AST)); return NumExprs.back().get(); } +FileCheckNumVar *FileCheckPatternContext::makeNumVar(StringRef Name, + uint64_t Value) { + NumVars.emplace_back(new FileCheckNumVar(Name, Value)); + return NumVars.back().get(); +} + /// Finds the closing sequence of a regex variable usage or definition. /// /// \p Str has to point in the beginning of the definition (right after the @@ -1483,7 +1573,7 @@ bool FileCheckPatternContext::defineCmdlineVariables( std::vector &CmdlineDefines, SourceMgr &SM) { - assert(GlobalVariableTable.empty() && + assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() && "Overriding defined variable with command-line variable definitions"); if (CmdlineDefines.empty()) @@ -1501,6 +1591,9 @@ CmdlineDefsDiag += (Prefix1 + Twine(++I) + Prefix2 + CmdlineDef + "\n").str(); + // Create a buffer with fake command line content in order to display + // parsing diagnostic with location information and point to the + // global definition with invalid syntax. std::unique_ptr CmdLineDefsDiagBuffer = MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines"); StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer(); @@ -1510,27 +1603,90 @@ CmdlineDefsDiagRef.split(CmdlineDefsDiagVec, '\n', -1 /*MaxSplit*/, false /*KeepEmpty*/); for (StringRef CmdlineDefDiag : CmdlineDefsDiagVec) { - unsigned NameStart = CmdlineDefDiag.find(Prefix2) + Prefix2.size(); - if (CmdlineDefDiag.substr(NameStart).find('=') == StringRef::npos) { - SM.PrintMessage(SMLoc::getFromPointer(CmdlineDefDiag.data()), + unsigned DefStart = CmdlineDefDiag.find(Prefix2) + Prefix2.size(); + StringRef CmdlineDef = CmdlineDefDiag.substr(DefStart); + if (CmdlineDef.find('=') == StringRef::npos) { + SM.PrintMessage(SMLoc::getFromPointer(CmdlineDef.data()), SourceMgr::DK_Error, "Missing equal sign in global definition"); ErrorFound = true; continue; } - std::pair CmdlineNameVal = - CmdlineDefDiag.substr(NameStart).split('='); - StringRef Name = CmdlineNameVal.first; - bool IsPseudo; - unsigned TrailIdx; - if (FileCheckPattern::parseVariable(Name, IsPseudo, TrailIdx) || IsPseudo || - TrailIdx != Name.size() || Name.empty()) { - SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, - "invalid name for variable definition '" + Name + "'"); - ErrorFound = true; - continue; + + // Numeric variable definition. + if (CmdlineDef[0] == '#') { + bool IsPseudo; + unsigned TrailIdx; + size_t EqIdx = CmdlineDef.find('='); + StringRef CmdlineName = CmdlineDef.substr(1, EqIdx - 1); + if (FileCheckPattern::parseVariable(CmdlineName, IsPseudo, TrailIdx) || + IsPseudo || TrailIdx != CmdlineName.size() || CmdlineName.empty()) { + SM.PrintMessage(SMLoc::getFromPointer(CmdlineName.data()), + SourceMgr::DK_Error, + "invalid name in numeric variable definition '" + + CmdlineName + "'"); + ErrorFound = true; + continue; + } + + // Detect collision between pattern and numeric variable when the latter + // is created later than the former. + if (DefinedVariableTable.find(CmdlineName) != + DefinedVariableTable.end()) { + SM.PrintMessage( + SMLoc::getFromPointer(CmdlineName.data()), SourceMgr::DK_Error, + "pattern variable with name '" + CmdlineName + "' already exists"); + ErrorFound = true; + continue; + } + + StringRef CmdlineVal = CmdlineDef.substr(EqIdx + 1); + uint64_t Val; + if (CmdlineVal.getAsInteger(10, Val)) { + SM.PrintMessage(SMLoc::getFromPointer(CmdlineVal.data()), + SourceMgr::DK_Error, + "invalid value in numeric variable definition '" + + CmdlineVal + "'"); + ErrorFound = true; + continue; + } + auto NumVarDef = makeNumVar(CmdlineName, Val); + + // Record this variable definition. + GlobalNumericVariableTable[CmdlineName] = NumVarDef; + } else { + // Pattern variable definition. + std::pair CmdlineNameVal = CmdlineDef.split('='); + StringRef Name = CmdlineNameVal.first; + bool IsPseudo; + unsigned TrailIdx; + if (FileCheckPattern::parseVariable(Name, IsPseudo, TrailIdx) || + IsPseudo || TrailIdx != Name.size() || Name.empty()) { + SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, + "invalid name in pattern variable definition '" + Name + + "'"); + ErrorFound = true; + continue; + } + + // Detect collision between pattern and numeric variable when the former + // is created later than the latter. + if (GlobalNumericVariableTable.find(Name) != + GlobalNumericVariableTable.end()) { + SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, + "numeric variable with name '" + Name + + "' already exists"); + ErrorFound = true; + continue; + } + GlobalVariableTable.insert(CmdlineNameVal); + // Mark pattern variable as defined to detect collision between pattern + // and numeric variable in DefineCmdlineVariables when the latter is + // created later than the former. We cannot reuse GlobalVariableTable for + // that by populating it with an empty string since we would then loose + // the ability to detect use of undefined variable in Match(). + DefinedVariableTable[Name] = true; } - GlobalVariableTable.insert(CmdlineNameVal); } return ErrorFound; @@ -1542,6 +1698,14 @@ if (Var.first()[0] != '$') LocalPatternVars.push_back(Var.first()); + // Numeric expression substitution read the value of variable directly, not + // via GlobalNUmericVariableTable. Therefore we clear local variable by + // clearing their value which will lead to an numeric expression substitution + // failure. + for (const auto &Var : GlobalNumericVariableTable) + if (Var.first()[0] != '$') + Var.getValue()->clearValue(); + for (const auto &Var : LocalPatternVars) GlobalVariableTable.erase(Var); } @@ -1580,7 +1744,10 @@ ++j; } - if (Req.EnableVarScope) + // Do not clear the first region as it's the one before the first + // CHECK-LABEL and it would clear variable defined on the command-line + // before they get used. + if (i != 0 && Req.EnableVarScope) PatternContext.clearLocalVars(); for (; i != j; ++i) { diff --git a/llvm/test/FileCheck/defines.txt b/llvm/test/FileCheck/defines.txt --- a/llvm/test/FileCheck/defines.txt +++ b/llvm/test/FileCheck/defines.txt @@ -11,6 +11,16 @@ ; RUN: not FileCheck -D10VALUE=10 -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix ERRCLIFMT ; RUN: not FileCheck -D@VALUE=10 -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix ERRCLIPSEUDO ; RUN: not FileCheck -D'VALUE + 2=10' -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix ERRCLITRAIL + +; RUN: FileCheck -D#NUMVAL=12 -check-prefix CHECKNUM -input-file %s %s +; RUN: not FileCheck -D#NUMVAL=8 -check-prefix CHECKNUM -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix NUMERRMSG +; RUN: not FileCheck -D#NUMVAL=12 -check-prefix NUMNOT -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix NOT-NUMERRMSG +; RUN: FileCheck -D#NUMVAL=8 -check-prefixes NUMNOT -input-file %s %s +; RUN: not FileCheck -D#10VALUE=10 -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix NUMERRCLIFMT +; RUN: not FileCheck -D#@VALUE=10 -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix NUMERRCLIPSEUDO +; RUN: not FileCheck -D#'VALUE + 2=10' -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix NUMERRCLITRAIL +; RUN: not FileCheck -D#VALUE1=3 -D#VALUE2='VALUE1 + 2' -input-file %s %s 2>&1 | FileCheck %s --strict-whitespace -check-prefix NUMERRCLIEXPR + Value = 10 ; CHECK: Value = [[VALUE]] ; NOT-NOT: Value = [[VALUE]] @@ -35,14 +45,43 @@ Empty value = @@ ; EMPTY: Empty value = @[[VALUE]]@ -; ERRCLIFMT: Global defines:1:19: error: invalid name for variable definition '10VALUE' +; ERRCLIFMT: Global defines:1:19: error: invalid name in pattern variable definition '10VALUE' ; ERRCLIFMT-NEXT: Global define #1: 10VALUE=10 ; ERRCLIFMT-NEXT: {{^ \^$}} -; ERRCLIPSEUDO: Global defines:1:19: error: invalid name for variable definition '@VALUE' +; ERRCLIPSEUDO: Global defines:1:19: error: invalid name in pattern variable definition '@VALUE' ; ERRCLIPSEUDO-NEXT: Global define #1: @VALUE=10 ; ERRCLIPSEUDO-NEXT: {{^ \^$}} -; ERRCLITRAIL: Global defines:1:19: error: invalid name for variable definition 'VALUE + 2' +; ERRCLITRAIL: Global defines:1:19: error: invalid name in pattern variable definition 'VALUE + 2' ; ERRCLITRAIL-NEXT: Global define #1: VALUE + 2=10 ; ERRCLITRAIL-NEXT: {{^ \^$}} + +Numeric value #2 = 12 +; CHECKNUM: Numeric value #2 = [[#NUMVAL]] +; NUMNOT-NOT: Numeric value #2 = [[#NUMVAL]] + +; NUMERRMSG: defines.txt:[[#@LINE-3]]:13: error: CHECKNUM: expected string not found in input +; NUMERRMSG: defines.txt:1:1: note: scanning from here +; NUMERRMSG: defines.txt:1:1: note: with numeric expression "NUMVAL" equal to "8" +; NUMERRMSG: defines.txt:[[#@LINE-7]]:1: note: possible intended match here + +; NOT-NUMERRMSG: defines.txt:[[#@LINE-7]]:15: error: {{NUMNOT}}-NOT: excluded string found in input +; NOT-NUMERRMSG: defines.txt:[[#@LINE-10]]:1: note: found here +; NOT-NUMERRMSG: defines.txt:[[#@LINE-11]]:1: note: with numeric expression "NUMVAL" equal to "12" + +; NUMERRCLIFMT: Global defines:1:20: error: invalid name in numeric variable definition '10VALUE' +; NUMERRCLIFMT-NEXT: Global define #1: #10VALUE=10 +; NUMERRCLIFMT-NEXT: {{^ \^$}} + +; NUMERRCLIPSEUDO: Global defines:1:20: error: invalid name in numeric variable definition '@VALUE' +; NUMERRCLIPSEUDO-NEXT: Global define #1: #@VALUE=10 +; NUMERRCLIPSEUDO-NEXT: {{^ \^$}} + +; NUMERRCLITRAIL: Global defines:1:20: error: invalid name in numeric variable definition 'VALUE + 2' +; NUMERRCLITRAIL-NEXT: Global define #1: #VALUE + 2=10 +; NUMERRCLITRAIL-NEXT: {{^ \^$}} + +; NUMERRCLIEXPR: Global defines:2:27: error: invalid value in numeric variable definition 'VALUE1 + 2' +; NUMERRCLIEXPR-NEXT: Global define #2: #VALUE2=VALUE1 + 2 +; NUMERRCLIEXPR-NEXT: {{^ \^$}} diff --git a/llvm/test/FileCheck/numeric-expression.txt b/llvm/test/FileCheck/numeric-expression.txt new file mode 100644 --- /dev/null +++ b/llvm/test/FileCheck/numeric-expression.txt @@ -0,0 +1,95 @@ +; RUN: FileCheck -D#VAR1=11 -input-file %s %s +; RUN: not FileCheck -check-prefix UNDEF-USE -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix UNDEF-USE-MSG %s +; RUN: not FileCheck -D#VAR1=11 -check-prefixes CHECK,INVAL-OP -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix INVAL-OP-MSG %s +; RUN: not FileCheck -D#VAR1=11 -D#NUMVAR=42 -check-prefixes CONFLICT,CONFLICT1 -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix CLI-INPUT-PAT-CONFLICT %s +; RUN: not FileCheck -D#VAR1=11 -D#NUMVAR=42 -DNUMVAR=foobar -check-prefix CONFLICT -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix CLI-CLI-PAT-CONFLICT %s +; RUN: not FileCheck -D#VAR1=11 -DPATVAR=foobar -D#PATVAR=42 -check-prefix CONFLICT -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix CLI-CLI-NUM-CONFLICT %s + + +; We ensure we attemt to match all lines with digits by using CHECK-NEXT +; directives for the checks to ensure each line of input is covered. + + +; Numeric expressions using variables defined on the command-line without +; spaces +USE NO SPC +11 +12 +10 +; CHECK-LABEL: USE NO SPC +; CHECK-NEXT: [[#VAR1]] +; CHECK-NEXT: [[#VAR1+1]] +; CHECK-NEXT: [[#VAR1-1]] + + +; Numeric expressions using variables defined on the command-line in alternate +; spacing +USE ALT SPC +11 +11 +12 +12 +12 +12 +10 +10 +10 +10 +; CHECK-LABEL: USE ALT SPC +; CHECK-NEXT: [[# VAR1]] +; CHECK-NEXT: [[# VAR1 ]] +; CHECK-NEXT: [[# VAR1+1]] +; CHECK-NEXT: [[# VAR1 +1]] +; CHECK-NEXT: [[# VAR1 + 1]] +; CHECK-NEXT: [[# VAR1 + 1 ]] +; CHECK-NEXT: [[# VAR1-1]] +; CHECK-NEXT: [[# VAR1 -1]] +; CHECK-NEXT: [[# VAR1 - 1]] +; CHECK-NEXT: [[# VAR1 - 1 ]] + + +; Numeric expressions using variables defined on the command-line and an +; immediate interpreted as an unsigned value +; Note: 9223372036854775819 = 0x8000000000000000 + 11 +; 9223372036854775808 = 0x8000000000000000 +USE UNSIGNED IMM +9223372036854775819 +; CHECK-LABEL: USE UNSIGNED IMM +; CHECK-NEXT: [[#VAR1+9223372036854775808]] + + +; Numeric expression using undefined variable +UNDEF VAR USE +UNDEFVAR: 11 +; UNDEF-USE-LABEL: UNDEF VAR USE +; UNDEF-USE-NEXT: UNDEFVAR: [[#UNDEFVAR]] +; UNDEF-USE-MSG: numeric-expression.txt:[[#@LINE-1]]:32: error: using undefined numeric variable 'UNDEFVAR' +; UNDEF-USE-MSG-NEXT: ; {{U}}NDEF-USE-NEXT: UNDEFVAR: {{\[\[#UNDEFVAR\]\]}} +; UNDEF-USE-MSG-NEXT: {{^ \^$}} + + +; Numeric expression with unsupported operator +INVALID OPERATOR +VAR1*2: 22 +; INVAL-OP-LABEL: INVALID OPERATOR +; INVAL-OP-NEXT: VAR1*2: [[#VAR1*2]] +; INVAL-OP-MSG: numeric-expression.txt:[[#@LINE-1]]:33: error: unsupported numeric operation '*' +; INVAL-OP-MSG-NEXT: ; {{I}}NVAL-OP-NEXT: VAR1*2: {{\[\[#VAR1\*2\]\]}} +; INVAL-OP-MSG-NEXT: {{^ \^$}} + + +; Name conflict between Numeric variable definition and pattern variable +; definition +PATVAR NUMVAR CONFLICT +foobar +; CONFLICT-LABEL: PATVAR NUMVAR CONFLICT +; CONFLICT1-NEXT: [[NUMVAR:foo.*]] +; CLI-INPUT-PAT-CONFLICT: numeric-expression.txt:[[#@LINE-1]]:21: error: numeric variable with name 'NUMVAR' already exists +; CLI-INPUT-PAT-CONFLICT-NEXT: ; {{C}}ONFLICT1-NEXT: {{\[\[NUMVAR:foo\.\*\]\]}} +; CLI-INPUT-PAT-CONFLICT-NEXT: {{^ \^$}} +; CLI-CLI-PAT-CONFLICT: Global defines:3:19: error: numeric variable with name 'NUMVAR' already exists +; CLI-CLI-PAT-CONFLICT-NEXT: Global define #3: NUMVAR=foobar +; CLI-CLI-PAT-CONFLICT-NEXT: {{^ \^$}} +; CLI-CLI-NUM-CONFLICT: Global defines:3:20: error: pattern variable with name 'PATVAR' already exists +; CLI-CLI-NUM-CONFLICT-NEXT: Global define #3: #PATVAR=42 +; CLI-CLI-NUM-CONFLICT-NEXT: {{^ \^$}} diff --git a/llvm/test/FileCheck/regex-scope.txt b/llvm/test/FileCheck/regex-scope.txt deleted file mode 100644 --- a/llvm/test/FileCheck/regex-scope.txt +++ /dev/null @@ -1,23 +0,0 @@ -// RUN: FileCheck -input-file %s %s -// RUN: FileCheck -check-prefixes CHECK,GLOBAL -input-file %s %s -// RUN: FileCheck -check-prefixes CHECK,LOCAL -input-file %s %s -// RUN: FileCheck -check-prefixes CHECK,GLOBAL --enable-var-scope -input-file %s %s -// RUN: not FileCheck -check-prefixes CHECK,LOCAL --enable-var-scope -input-file %s %s - -local -global -; CHECK: [[LOCAL:loc.*]] -; CHECK: [[$GLOBAL:glo.*]] - -local2 -global2 -; CHECK: [[LOCAL]]2 -; CHECK: [[$GLOBAL]]2 - -barrier: -; CHECK-LABEL: barrier - -local3 -global3 -; LOCAL: [[LOCAL]]3 -; GLOBAL: [[$GLOBAL]]3 diff --git a/llvm/test/FileCheck/var-scope.txt b/llvm/test/FileCheck/var-scope.txt new file mode 100644 --- /dev/null +++ b/llvm/test/FileCheck/var-scope.txt @@ -0,0 +1,27 @@ +// RUN: FileCheck -D#LOCNUM=1 -D'#$GLOBNUM=1' -input-file %s %s +// RUN: FileCheck -D#LOCNUM=1 -D'#$GLOBNUM=1' -check-prefixes CHECK,GLOBAL -input-file %s %s +// RUN: FileCheck -D#LOCNUM=1 -D'#$GLOBNUM=1' -check-prefixes CHECK,LOCAL3 -input-file %s %s +// RUN: FileCheck -D#LOCNUM=1 -D'#$GLOBNUM=1' -check-prefixes CHECK,GLOBAL --enable-var-scope -input-file %s %s +// RUN: not FileCheck -D#LOCNUM=1 -D#$GLOBNUM=1 -check-prefixes CHECK,LOCAL1 --enable-var-scope -input-file %s %s +// RUN: not FileCheck -D#LOCNUM=1 -D#$GLOBNUM=1 -check-prefixes CHECK,LOCAL2 --enable-var-scope -input-file %s %s +// RUN: not FileCheck -D#LOCNUM=1 -D#$GLOBNUM=1 -check-prefixes CHECK,LOCAL3 --enable-var-scope -input-file %s %s + +local1 +global1 +; CHECK: [[LOCAL:loc[^[:digit:]]*]][[#LOCNUM]] +; CHECK: [[$GLOBAL:glo[^[:digit:]]*]][[#$GLOBNUM]] + +local2 +global2 +; CHECK: [[LOCAL]][[#LOCNUM+1]] +; CHECK: [[$GLOBAL]][[#$GLOBNUM+1]] + +barrier: +; CHECK-LABEL: barrier + +local3 +global3 +; LOCAL1: [[LOCAL]]3 +; LOCAL2: local[[#LOCNUM+2]] +; LOCAL3: [[LOCAL]][[#LOCNUM+2]] +; GLOBAL: [[$GLOBAL]][[#$GLOBNUM+2]] diff --git a/llvm/test/FileCheck/verbose.txt b/llvm/test/FileCheck/verbose.txt --- a/llvm/test/FileCheck/verbose.txt +++ b/llvm/test/FileCheck/verbose.txt @@ -1,6 +1,6 @@ -; RUN: FileCheck -input-file %s %s 2>&1 | FileCheck -check-prefix QUIET --allow-empty %s -; RUN: FileCheck -v -input-file %s %s 2>&1 | FileCheck -check-prefix V %s -; RUN: FileCheck -vv -input-file %s %s 2>&1 | FileCheck -check-prefixes V,VV %s +; RUN: FileCheck -D#NUMVAR=42 -input-file %s %s 2>&1 | FileCheck -check-prefix QUIET --allow-empty %s +; RUN: FileCheck -v -D#NUMVAR=42 -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix V %s +; RUN: FileCheck -vv -D#NUMVAR=42 -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefixes V,VV %s foo bar @@ -29,6 +29,39 @@ VV-NEXT: {{^}}bar{{$}} VV-NEXT: {{^}}^{{$}} +NUMVAR:42 +NUMVAR - 1:41 +CHECK: NUMVAR:[[#NUMVAR]] +CHECK-NOT: [[#NUMVAR + 1]] +CHECK-NEXT: NUMVAR - 1:[[#NUMVAR - 1]] + +V: verbose.txt:[[#@LINE-4]]:8: remark: {{C}}HECK: expected string found in input +V-NEXT: {{C}}HECK: {{NUMVAR:[[][[]#NUMVAR[]][]]$}} +V-NEXT: {{^ \^$}} +V-NEXT: verbose.txt:[[#@LINE-9]]:1: note: found here +V-NEXT: {{^}}NUMVAR:42{{$}} +V-NEXT: {{^}}^~~~~~~~~{{$}} +V-NEXT: verbose.txt:[[#@LINE-12]]:1: note: with numeric expression "NUMVAR" equal to "42" +V-NEXT: {{^}}NUMVAR:42{{$}} +V-NEXT: {{^}}^~~~~~~~~{{$}} + +V-NEXT: verbose.txt:[[#@LINE-12]]:13: remark: {{C}}HECK-NEXT: expected string found in input +V-NEXT: {{C}}HECK-NEXT: {{NUMVAR - 1:[[][[]#NUMVAR - 1[]][]]$}} +V-NEXT: {{^ \^$}} +V-NEXT: verbose.txt:[[#@LINE-18]]:1: note: found here +V-NEXT: {{^}}NUMVAR - 1:41{{$}} +V-NEXT: {{^}}^~~~~~~~~~~~~{{$}} +V-NEXT: verbose.txt:[[#@LINE-21]]:1: note: with numeric expression "NUMVAR - 1" equal to "41" +V-NEXT: {{^}}NUMVAR - 1:41{{$}} +V-NEXT: {{^}}^~~~~~~~~~~~~{{$}} + +VV-NEXT: verbose.txt:[[#@LINE-23]]:12: remark: {{C}}HECK-NOT: excluded string not found in input +VV-NEXT: {{C}}HECK-NOT: {{[[][[]#NUMVAR [+] 1[]][]]$}} +VV-NEXT: {{^ \^$}} +VV-NEXT: verbose.txt:[[#@LINE-28]]:1: note: scanning from here +VV-NEXT: {{^}}NUMVAR - 1:41{{$}} +VV-NEXT: {{^}}^{{$}} + before empty after empty @@ -99,7 +132,7 @@ VV-NEXT: verbose.txt:[[@LINE-9]]:19: remark: implicit EOF: expected string found in input VV-NEXT: {{C}}HECK-NOT: {{[{][{]z[}][}]yx$}} -VV-NEXT: {{^ \^$}} +VV-NEXT: {{^ \^$}} VV-NEXT: verbose.txt:[[@LINE+13]]:1: note: found here VV-NOT: {{.}} VV: {{^\^$}} 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 @@ -14,6 +14,49 @@ class FileCheckTest : public ::testing::Test {}; +TEST_F(FileCheckTest, NumVarValueGetClearSet) { + FileCheckNumVar FooVar = FileCheckNumVar("FOO", 42); + + // Defined variable: getValue returns a value, setValue fails. + llvm::Optional Value = FooVar.getValue(); + EXPECT_TRUE(Value); + EXPECT_EQ((uint64_t)42, *Value); + EXPECT_TRUE(FooVar.setValue((uint64_t)43)); + + // Clearing variable: getValue fails, clearValue again fails. + EXPECT_FALSE(FooVar.clearValue()); + Value = FooVar.getValue(); + EXPECT_FALSE(Value); + EXPECT_TRUE(FooVar.clearValue()); + + // Undefined variable: setValue works, getValue returns value set. + EXPECT_FALSE(FooVar.setValue((uint64_t)43)); + Value = FooVar.getValue(); + EXPECT_TRUE(Value); + EXPECT_EQ((uint64_t)43, *Value); +} + +uint64_t doAdd(uint64_t OpL, uint64_t OpR) { return OpL + OpR; } + +TEST_F(FileCheckTest, BinopEvalUndef) { + auto FooVar = new FileCheckNumVar("FOO", 42); + auto Binop = new FileCheckASTBinop(doAdd, FooVar, 18); + + // Defined variable: eval returns right value, no undef variable returned. + llvm::Optional Value = Binop->eval(); + EXPECT_TRUE(Value); + EXPECT_EQ((uint64_t)60, *Value); + StringRef UndefVar = Binop->getUndefVarName(); + EXPECT_EQ("", UndefVar); + + // Undefined variable: eval fails, undef variable returned. + FooVar->clearValue(); + Value = Binop->eval(); + EXPECT_FALSE(Value); + UndefVar = Binop->getUndefVarName(); + EXPECT_EQ("FOO", UndefVar); +} + TEST_F(FileCheckTest, ValidVarNameStart) { EXPECT_TRUE(FileCheckPattern::isValidVarNameStart('a')); EXPECT_TRUE(FileCheckPattern::isValidVarNameStart('G')); @@ -93,11 +136,21 @@ class ExprTester { private: SourceMgr SM; + struct FileCheckRequest Req; FileCheckPatternContext Context; FileCheckPattern P = FileCheckPattern(Check::CheckPlain, &Context); public: + ExprTester() { + std::vector GlobalDefines; + GlobalDefines.emplace_back(std::string("#FOO=42")); + Context.defineCmdlineVariables(GlobalDefines, SM); + // Call ParsePattern to have @LINE defined. + P.ParsePattern("N/A", "CHECK", SM, 1, Req); + } + bool parse_expect(std::string &VarName, std::string &Trailer) { + bool IsPseudo = VarName[0] == '@'; StringRef NameTrailer = StringRef(VarName + Trailer); std::unique_ptr Buffer = MemoryBuffer::getMemBufferCopy(NameTrailer, "TestBuffer"); @@ -105,7 +158,8 @@ SM.AddNewSourceBuffer(std::move(Buffer), SMLoc()); StringRef VarNameRef = NameTrailerRef.substr(0, VarName.size()); StringRef TrailerRef = NameTrailerRef.substr(VarName.size()); - return P.parseNumericExpression(VarNameRef, TrailerRef, SM) == nullptr; + return P.parseNumericExpression(VarNameRef, IsPseudo, TrailerRef, SM) == + nullptr; } }; @@ -121,6 +175,14 @@ Trailer = ""; EXPECT_FALSE(Tester.parse_expect(VarName, Trailer)); + // Defined variable + VarName = "FOO"; + EXPECT_FALSE(Tester.parse_expect(VarName, Trailer)); + + // Undefined variable + VarName = "UNDEF"; + EXPECT_TRUE(Tester.parse_expect(VarName, Trailer)); + // Wrong Pseudovar. VarName = "@FOO"; EXPECT_TRUE(Tester.parse_expect(VarName, Trailer)); @@ -153,15 +215,24 @@ GlobalDefines.emplace_back(std::string("FOO=BAR")); Context.defineCmdlineVariables(GlobalDefines, SM); + // Substitution of undefined pattern variable fails. FileCheckPatternSubst Subst = FileCheckPatternSubst(&Context, "VAR404", 42); EXPECT_FALSE(Subst.getSubstitute()); - FileCheckNumExpr NumExpr = FileCheckNumExpr(42); + // Substitution of defined numeric variable returns the right value. + auto LineVar = new FileCheckNumVar("@LINE", 42); + auto Binop = new FileCheckASTBinop(doAdd, LineVar, 0); + FileCheckNumExpr NumExpr = FileCheckNumExpr(Binop); Subst = FileCheckPatternSubst(&Context, "@LINE", &NumExpr, 12); llvm::Optional Value = Subst.getSubstitute(); EXPECT_TRUE(Value); EXPECT_EQ("42", *Value); + // Substitution of undefined numeric variable fails. + LineVar->clearValue(); + EXPECT_FALSE(Subst.getSubstitute()); + + // Substitution of defined pattern variable returns the right value. FileCheckPattern P = FileCheckPattern(Check::CheckPlain, &Context); Subst = FileCheckPatternSubst(&Context, "FOO", 42); Value = Subst.getSubstitute(); @@ -176,18 +247,32 @@ GlobalDefines.emplace_back(std::string("FOO=BAR")); Context.defineCmdlineVariables(GlobalDefines, SM); + // Undef var in pattern variable substitution with undefined variable returns + // the variable. FileCheckPatternSubst Subst = FileCheckPatternSubst(&Context, "VAR404", 42); StringRef UndefVar = Subst.getUndefVarName(); EXPECT_EQ("VAR404", UndefVar); - FileCheckNumExpr NumExpr = FileCheckNumExpr(42); - Subst = FileCheckPatternSubst(&Context, "@LINE", &NumExpr, 12); + // Undef var in pattern variable substitution with defined variable is empty. + Subst = FileCheckPatternSubst(&Context, "FOO", 42); UndefVar = Subst.getUndefVarName(); EXPECT_EQ("", UndefVar); - Subst = FileCheckPatternSubst(&Context, "FOO", 42); + // Undef var in numeric expression substitution with defined variable is + // empty. + auto LineVar = new FileCheckNumVar("@LINE", 42); + auto Binop = new FileCheckASTBinop(doAdd, LineVar, 0); + FileCheckNumExpr NumExpr = FileCheckNumExpr(Binop); + Subst = FileCheckPatternSubst(&Context, "@LINE", &NumExpr, 12); UndefVar = Subst.getUndefVarName(); EXPECT_EQ("", UndefVar); + + // Undef var in valid numeric expression substitution is empty. + // Undef var in numeric expression substitution with undefined variable + // returns the variable. + LineVar->clearValue(); + UndefVar = Subst.getUndefVarName(); + EXPECT_EQ("@LINE", UndefVar); } TEST_F(FileCheckTest, FileCheckContext) {