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 ``NUMVAR`` to ```` that can be used + in ``CHECK:`` lines. + .. option:: -version Show the version number of this program. @@ -560,8 +565,51 @@ 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 checking for numeric values that satisfy a +numeric expression constraint based on numeric variables. This allows +``CHECK:`` directives to verify a numeric relation between two numbers, such as +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 line: + +.. code-block:: llvm + + add r5, r5, r6 + +but would not match the line: + +.. code-block:: llvm + + add r5, r5, r7 + +due to ``7`` being unequal to ``5 + 1``. + +The ``--enable-var-scope`` option has the same effect on numeric variables as +on pattern variables. + +FileCheck Pseudo Numeric Variables +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sometimes there's a need to verify output that contains line numbers of the match file, e.g. when testing compiler diagnostics. This introduces a certain @@ -569,11 +617,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+]]`` and ``[[#@LINE-]]`` numeric expressions in -patterns, with an arbitrary number of spaces between each element of the -expression. These expressions expand to the 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,82 @@ // Numeric expression handling code. //===----------------------------------------------------------------------===// +class FileCheckASTBinop; + /// Class representing a numeric expression. class FileCheckNumExpr { private: - /// Value of the numeric expression. - uint64_t Value; + /// 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(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; + + /// Value of numeric variable if defined or None otherwise. + llvm::Optional Value; + +public: + /// Constructor for numeric variable \p Name with a known \p Value at parse + /// time (e.g. the @LINE numeric variable). + FileCheckNumVar(StringRef Name, uint64_t Value) : Name(Name), Value(Value) {} + + /// Return name of that numeric variable. + StringRef getName() const { return Name; } + + /// Return value of this numeric variable. + llvm::Optional getValue() const { return Value; } + + /// Set value of this numeric variable if not defined. Return whether + /// the variable was already defined. + bool setValue(uint64_t 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: - /// 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) {} + FileCheckASTBinop(binop_eval_t EvalBinop, FileCheckNumVar *OperandLeft, + uint64_t OperandRight) + : Opl(OperandLeft), Opr(OperandRight), EvalBinop(EvalBinop) {} - /// Return the value being matched against. - uint64_t getValue() const { return Value; } + /// 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 +236,35 @@ /// 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 parsed numeric expressions. 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 +275,11 @@ private: /// Make a new numeric expression instance and register it for destruction /// when the context is destroyed. - template FileCheckNumExpr *makeNumExpr(Types... Args); + FileCheckNumExpr *makeNumExpr(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 +295,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 FileCheckPatternSubstitution 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 FileCheckPatternSubstitution class to abstract whether + /// it is a pattern variable or a numeric expression. std::vector Substitutions; /// Maps names of pattern variables defined in a pattern to the parenthesized @@ -232,8 +314,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 +346,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,24 +24,54 @@ using namespace llvm; +bool FileCheckNumVar::setValue(uint64_t NewValue) { + if (Value) + return true; + Value = NewValue; + return false; +} + +bool FileCheckNumVar::clearValue() { + if (!Value) + return true; + Value = llvm::None; + 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 FileCheckPatternSubstitution::getResult() const { if (IsNumExpr) { - return utostr(NumExpr->getValue()); - } else { - // Look up the value and escape it so that we can put it into the - // regex. - llvm::Optional VarVal = Context->getPatternVarValue(FromStr); - if (!VarVal) + llvm::Optional EvaluatedValue = NumExpr->getAST()->eval(); + if (!EvaluatedValue) return llvm::None; - return Regex::escape(*VarVal); + return utostr(*EvaluatedValue); } + + // Look up the value and escape it so that we can put it into the regex. + llvm::Optional VarVal = Context->getPatternVarValue(FromStr); + if (!VarVal) + return llvm::None; + return Regex::escape(*VarVal); } StringRef FileCheckPatternSubstitution::getUndefVarName() const { - // Parsing guarantees only @LINE is ever referenced and it is not undefined - // by ClearLocalVars. if (IsNumExpr) - return StringRef(); + // Although a use of an undefined numeric variable is detected at parse + // time, a numeric variable can be undefined later by ClearLocalVariables. + return NumExpr->getAST()->getUndefVarName(); if (!Context->getPatternVarValue(FromStr)) return FromStr; @@ -91,31 +121,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 definitions 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 select a function to perform + // it. Trailer = Trailer.ltrim(SpaceChars); - if (Trailer.empty()) - return Context->makeNumExpr(LineNumber); + if (Trailer.empty()) { + return Context->makeNumExpr(new FileCheckASTBinop(doAdd, Opl, 0)); + } SMLoc OpLoc = SMLoc::getFromPointer(Trailer.data()); char Operator = popFront(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. Trailer = Trailer.ltrim(SpaceChars); 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 +193,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->makeNumExpr(Value); + return Context->makeNumExpr(new FileCheckASTBinop(EvalBinop, Opl, Opr)); } /// Parses the given string into the Pattern. @@ -159,6 +210,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() && @@ -236,10 +293,10 @@ // forms: [[foo:.*]] and [[foo]]. The former matches .* (or some other // regex) and assigns it to the FileCheck variable 'foo'. The latter // substitutes foo's value. Numeric expressions start with a '#' sign after - // the double brackets and only have the substitution form. Pattern - // variables must satisfy the regular expression "[a-zA-Z_][0-9a-zA-Z_]*" - // to be valid, as this helps catch some common errors. Numeric expressions - // only support the @LINE pseudo numeric variable. + // the double brackets and only have the substitution form. Both pattern + // and numeric variables must satisfy the regular expression + // "[a-zA-Z_][0-9a-zA-Z_]*" to be valid, as this helps catch some common + // errors. if (PatternStr.startswith("[[")) { StringRef UnparsedPatternStr = PatternStr.substr(2); // Find the closing bracket pair ending the match. End is going to be an @@ -289,21 +346,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. @@ -584,12 +653,17 @@ return VarIter->second; } -template -FileCheckNumExpr *FileCheckPatternContext::makeNumExpr(Types... Args) { - NumExprs.emplace_back(new FileCheckNumExpr(Args...)); +FileCheckNumExpr *FileCheckPatternContext::makeNumExpr(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 @@ -1485,7 +1559,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()) @@ -1503,6 +1577,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(); @@ -1512,27 +1589,91 @@ 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 the pattern variable as defined to detect collisions between + // pattern and numeric variables 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 lose the ability to detect the use of an undefined + // variable in Match(). + DefinedVariableTable[Name] = true; } - GlobalVariableTable.insert(CmdlineNameVal); } return ErrorFound; @@ -1544,8 +1685,22 @@ if (Var.first()[0] != '$') LocalPatternVars.push_back(Var.first()); + // Numeric expression substitution reads the value of a variable directly, + // not via GlobalNumericVariableTable. Therefore we clear local variables by + // clearing their value which will lead to a numeric expression substitution + // failure. We also mark the variable for removal from + // GlobalNumericVariableTable since this is what defineCmdlineVariables + // checks to decide that no global variable has been defined. + for (const auto &Var : GlobalNumericVariableTable) + if (Var.first()[0] != '$') { + Var.getValue()->clearValue(); + LocalNumericVars.push_back(Var.first()); + } + for (const auto &Var : LocalPatternVars) GlobalVariableTable.erase(Var); + for (const auto &Var : LocalNumericVars) + GlobalNumericVariableTable.erase(Var); } /// Check the input to FileCheck provided in the \p Buffer against the \p @@ -1582,7 +1737,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 variables 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')); @@ -90,22 +133,38 @@ EXPECT_EQ(TrailIdx, VarName.size() - 1); } +static StringRef bufferize(SourceMgr &SM, StringRef Str) { + std::unique_ptr Buffer = + MemoryBuffer::getMemBufferCopy(Str, "TestBuffer"); + StringRef StrBufferRef = Buffer->getBuffer(); + SM.AddNewSourceBuffer(std::move(Buffer), SMLoc()); + return StrBufferRef; +} + 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 parseExpect(std::string &VarName, std::string &Trailer) { + bool IsPseudo = VarName[0] == '@'; std::string NameTrailer = VarName + Trailer; - std::unique_ptr Buffer = - MemoryBuffer::getMemBufferCopy(NameTrailer, "TestBuffer"); - StringRef NameTrailerRef = Buffer->getBuffer(); - SM.AddNewSourceBuffer(std::move(Buffer), SMLoc()); + StringRef NameTrailerRef = bufferize(SM, NameTrailer); 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 +180,14 @@ Trailer = ""; EXPECT_FALSE(Tester.parseExpect(VarName, Trailer)); + // Defined variable + VarName = "FOO"; + EXPECT_FALSE(Tester.parseExpect(VarName, Trailer)); + + // Undefined variable + VarName = "UNDEF"; + EXPECT_TRUE(Tester.parseExpect(VarName, Trailer)); + // Wrong Pseudovar. VarName = "@FOO"; EXPECT_TRUE(Tester.parseExpect(VarName, Trailer)); @@ -153,16 +220,25 @@ GlobalDefines.emplace_back(std::string("FOO=BAR")); Context.defineCmdlineVariables(GlobalDefines, SM); + // Substitution of undefined pattern variable fails. FileCheckPatternSubstitution Substitution = FileCheckPatternSubstitution(&Context, "VAR404", 42); EXPECT_FALSE(Substitution.getResult()); - 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); Substitution = FileCheckPatternSubstitution(&Context, "@LINE", &NumExpr, 12); llvm::Optional Value = Substitution.getResult(); EXPECT_TRUE(Value); EXPECT_EQ("42", *Value); + // Substitution of undefined numeric variable fails. + LineVar->clearValue(); + EXPECT_FALSE(Substitution.getResult()); + + // Substitution of defined pattern variable returns the right value. FileCheckPattern P = FileCheckPattern(Check::CheckPlain, &Context); Substitution = FileCheckPatternSubstitution(&Context, "FOO", 42); Value = Substitution.getResult(); @@ -177,19 +253,33 @@ GlobalDefines.emplace_back(std::string("FOO=BAR")); Context.defineCmdlineVariables(GlobalDefines, SM); + // Undef var in pattern variable substitution with undefined variable returns + // the variable. FileCheckPatternSubstitution Substitution = FileCheckPatternSubstitution(&Context, "VAR404", 42); StringRef UndefVar = Substitution.getUndefVarName(); EXPECT_EQ("VAR404", UndefVar); - FileCheckNumExpr NumExpr = FileCheckNumExpr(42); - Substitution = FileCheckPatternSubstitution(&Context, "@LINE", &NumExpr, 12); + // Undef var in pattern variable substitution with defined variable is empty. + Substitution = FileCheckPatternSubstitution(&Context, "FOO", 42); UndefVar = Substitution.getUndefVarName(); EXPECT_EQ("", UndefVar); - Substitution = FileCheckPatternSubstitution(&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); + Substitution = FileCheckPatternSubstitution(&Context, "@LINE", &NumExpr, 12); UndefVar = Substitution.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 = Substitution.getUndefVarName(); + EXPECT_EQ("@LINE", UndefVar); } TEST_F(FileCheckTest, FileCheckContext) { @@ -197,36 +287,71 @@ std::vector GlobalDefines; SourceMgr SM; - // Missing equal sign + // Missing equal sign. GlobalDefines.emplace_back(std::string("LocalVar")); EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); + GlobalDefines.clear(); + GlobalDefines.emplace_back(std::string("#LocalNumVar")); + EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); - // Empty variable + // Empty variable. GlobalDefines.clear(); GlobalDefines.emplace_back(std::string("=18")); EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); + GlobalDefines.clear(); + GlobalDefines.emplace_back(std::string("#=18")); + EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); - // Invalid variable + // Invalid variable. GlobalDefines.clear(); GlobalDefines.emplace_back(std::string("18LocalVar=18")); EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); + GlobalDefines.clear(); + GlobalDefines.emplace_back(std::string("#18LocalNumVar=18")); + EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); + + // Name conflict between pattern and numeric variable. + GlobalDefines.clear(); + GlobalDefines.emplace_back(std::string("LocalVar=18")); + GlobalDefines.emplace_back(std::string("#LocalVar=36")); + EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); + Cxt = FileCheckPatternContext(); + GlobalDefines.clear(); + GlobalDefines.emplace_back(std::string("#LocalNumVar=18")); + GlobalDefines.emplace_back(std::string("LocalNumVar=36")); + EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); + Cxt = FileCheckPatternContext(); + + // Invalid numeric value for numeric variable. + GlobalDefines.clear(); + GlobalDefines.emplace_back(std::string("#LocalNumVar=x")); + EXPECT_TRUE(Cxt.defineCmdlineVariables(GlobalDefines, SM)); // Define local variables from command-line. GlobalDefines.clear(); GlobalDefines.emplace_back(std::string("LocalVar=FOO")); GlobalDefines.emplace_back(std::string("EmptyVar=")); + GlobalDefines.emplace_back(std::string("#LocalNumVar=18")); bool GotError = Cxt.defineCmdlineVariables(GlobalDefines, SM); EXPECT_FALSE(GotError); // Check defined variables are present and undefined is absent. StringRef LocalVarStr = "LocalVar"; + StringRef LocalNumVarRef = bufferize(SM, "LocalNumVar"); StringRef EmptyVarStr = "EmptyVar"; StringRef UnknownVarStr = "UnknownVar"; llvm::Optional LocalVar = Cxt.getPatternVarValue(LocalVarStr); + FileCheckPattern P = FileCheckPattern(Check::CheckPlain, &Cxt); + FileCheckNumExpr *NumExpr = + P.parseNumericExpression(LocalNumVarRef, false /*IsPseudo*/, "", SM); llvm::Optional EmptyVar = Cxt.getPatternVarValue(EmptyVarStr); llvm::Optional UnknownVar = Cxt.getPatternVarValue(UnknownVarStr); EXPECT_TRUE(LocalVar); EXPECT_EQ(*LocalVar, "FOO"); + EXPECT_TRUE(NumExpr); + llvm::Optional NumExprVal = NumExpr->getAST()->eval(); + EXPECT_TRUE(NumExprVal); + EXPECT_EQ(*NumExprVal, 18U); EXPECT_TRUE(EmptyVar); EXPECT_EQ(*EmptyVar, ""); EXPECT_FALSE(UnknownVar); @@ -235,21 +360,43 @@ Cxt.clearLocalVars(); LocalVar = Cxt.getPatternVarValue(LocalVarStr); EXPECT_FALSE(LocalVar); + // Check eval fails even if we kept a pointer to the numeric expression. + EXPECT_FALSE(NumExpr->getAST()->eval()); + P = FileCheckPattern(Check::CheckPlain, &Cxt); + NumExpr = + P.parseNumericExpression(LocalNumVarRef, false /*IsPseudo*/, "", SM); + EXPECT_FALSE(NumExpr); EmptyVar = Cxt.getPatternVarValue(EmptyVarStr); EXPECT_FALSE(EmptyVar); // Redefine global variables and check variables are defined again. GlobalDefines.emplace_back(std::string("$GlobalVar=BAR")); + GlobalDefines.emplace_back(std::string("#$GlobalNumVar=36")); GotError = Cxt.defineCmdlineVariables(GlobalDefines, SM); EXPECT_FALSE(GotError); StringRef GlobalVarStr = "$GlobalVar"; + StringRef GlobalNumVarRef = bufferize(SM, "$GlobalNumVar"); llvm::Optional GlobalVar = Cxt.getPatternVarValue(GlobalVarStr); EXPECT_TRUE(GlobalVar); EXPECT_EQ(*GlobalVar, "BAR"); + P = FileCheckPattern(Check::CheckPlain, &Cxt); + NumExpr = + P.parseNumericExpression(GlobalNumVarRef, false /*IsPseudo*/, "", SM); + EXPECT_TRUE(NumExpr); + NumExprVal = NumExpr->getAST()->eval(); + EXPECT_TRUE(NumExprVal); + EXPECT_EQ(*NumExprVal, 36U); // Clear local variables and check global variables remain defined. Cxt.clearLocalVars(); GlobalVar = Cxt.getPatternVarValue(GlobalVarStr); EXPECT_TRUE(GlobalVar); + P = FileCheckPattern(Check::CheckPlain, &Cxt); + NumExpr = + P.parseNumericExpression(GlobalNumVarRef, false /*IsPseudo*/, "", SM); + EXPECT_TRUE(NumExpr); + NumExprVal = NumExpr->getAST()->eval(); + EXPECT_TRUE(NumExprVal); + EXPECT_EQ(*NumExprVal, 36U); } } // namespace