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 @@ -568,15 +568,26 @@ 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. +:program:`FileCheck` also allows to define numeric variables and check for +values satisfying a numeric expression constraint based on those variables. +This allows to capture numeric relations between two numbers in a text to +check, such as the need for consecutive registers to be used. + +The syntax to define a numeric variable is ``[[#:]]`` where ``NUMVAR`` +is the name of the numeric variable to define to the matching value. + +For example: + +.. code-block:: llvm + + ; CHECK: mov r[[#REG:]], 42 + +would match ``mov r5, 42`` and set ``REG`` to the value ``5``. The syntax to check a numeric expression constraint is ``[[#]]`` where: -*```` is the name of a numeric variable defined on the command line. +*```` is the name of a numeric variable defined on a previous line. *```` is an optional numeric operation to perform on the value of ````. Currently supported numeric operations are ``+`` and ``-``. @@ -589,7 +600,7 @@ .. code-block:: llvm - ; CHECK: add r[[#REG]], r[[#REG]], r[[#REG+1]] + ; CHECK: add r[[#REG:]], r[[#REG]], r[[#REG+1]] The above example would match the lines: @@ -609,6 +620,9 @@ global numeric variables that start with ``$`` and undefined local variables at the beginning of each CHECK-LABEL block. +Important note: In its current implementation, a numeric expression cannot use +a numeric variable defined on the same line. + FileCheck Pseudo Numeric Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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 @@ -59,7 +59,9 @@ }; /// Class representing a numeric variable with a given value in the AST of a -/// numeric expression. +/// numeric expression. Each definition of a variable gets its own instance of +/// this class, variable uses share the same instance as the respective +/// definition. class FileCheckNumVar { private: /// Name of the numeric variable. @@ -71,7 +73,15 @@ /// Value of numeric variable if defined. uint64_t Value; + /// Line number where this variable is defined. Used to determine whether a + /// variable is defined on the same line as a given use. + unsigned DefLineNumber; + public: + /// Constructor for a variable \p Name defined at line \p DefLineNumber. + FileCheckNumVar(StringRef Name, unsigned DefLineNumber) + : Name(Name), Defined(false), DefLineNumber(DefLineNumber) {} + /// 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) @@ -90,6 +100,9 @@ /// Clear value of this numeric variable. Return whether the variable was /// already undefined. bool clearValue(); + + /// Return line number where this variable is defined. + unsigned getDefLineNumber() { return DefLineNumber; } }; /// Type of functions evaluating a given binary operation. @@ -227,6 +240,18 @@ struct FileCheckDiag; +/// Structure representing the definition of a numeric variable in a pattern. +/// It holds the parenthesized capture number and the pointer to the class +/// representing the numeric variable whose value is being defined. +struct FileCheckNumExprMatch { + /// Pointer to class representing the numeric variable whose value is being + /// defined. + FileCheckNumVar *NumVarDef; + + /// Parenthesized capture number for this numeric variable definition. + unsigned CaptureParen; +}; + /// Class holding the FileCheckPattern global state, shared by all patterns: /// tables holding values of variables and whether they are defined or not at /// any given time in the matching process. @@ -248,7 +273,9 @@ /// 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. + /// for a given variable is recorded in this table. When matching a pattern + /// all definitions for that pattern are recorded in NumericVariableDefs + /// table. StringMap GlobalNumericVariableTable; /// Vector holding pointers to all numeric expressions parsed. Used to @@ -282,7 +309,7 @@ /// Make a new numeric variable and register it for destruction when the /// context is destroyed. - FileCheckNumVar *makeNumVar(StringRef Name, uint64_t Value); + template FileCheckNumVar *makeNumVar(Types... args); }; class FileCheckPattern { @@ -317,6 +344,11 @@ /// iterating over values. std::map VariableDefs; + /// Holds the capture parentheses number and pointer to corresponding + /// FileCheckNumVar class instance of all numeric variable definitions. Used + /// to set the matched value of all those variables. + std::map NumericVariableDefs; + /// Pointer to a class instance holding the global state shared by all /// patterns: /// - tables with the values of live pattern and numeric variables at @@ -327,13 +359,15 @@ Check::FileCheckType CheckTy; - /// Contains the number of line this pattern is in. + /// Line number for this CHECK pattern. Used to determine whether a variable + /// definition is made on an earlier line to the one with this CHECK. unsigned LineNumber; public: explicit FileCheckPattern(Check::FileCheckType Ty, - FileCheckPatternContext *Context) - : Context(Context), CheckTy(Ty) {} + FileCheckPatternContext *Context, + unsigned LineNumber) + : Context(Context), CheckTy(Ty), LineNumber(LineNumber) {} /// Returns the location in source code. SMLoc getLoc() const { return PatternLoc; } @@ -349,17 +383,23 @@ /// 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 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, + /// Parse \p Expr for use or definition (if \p IsDefinition is true) of a + /// numeric variable. Return whether parsing fails in which case errors are + /// reported on \p SM. Otherwise, the name of the numeric variable is set in + /// \p Name. + bool parseNumericVariable(StringRef &Expr, StringRef &Name, bool IsDefinition, + const SourceMgr &SM) const; + /// Parse \p Expr for a numeric expression. Return the class representing the + /// AST of numeric expression or nullptr if parsing fails in which case + /// errors are reported on \p SM. Set \p NumVarDef to the pointer to the + /// class representing the variable defined to this numeric expression if + /// any. + FileCheckNumExpr *parseNumericExpression(StringRef Expr, + FileCheckNumVar *&NumVarDef, const SourceMgr &SM) const; bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM, - unsigned LineNumber, const FileCheckRequest &Req); - size_t match(StringRef Buffer, size_t &MatchLen) const; + const FileCheckRequest &Req); + size_t match(StringRef Buffer, size_t &MatchLen, const SourceMgr &SM) const; /// Print value of successful substitutions or name of undefined pattern or /// numeric variables preventing such a successful substitution. void printSubsts(const SourceMgr &SM, StringRef Buffer, @@ -378,6 +418,10 @@ void AddBackrefToRegEx(unsigned BackrefNum); unsigned computeMatchDistance(StringRef Buffer) const; size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM); + + /// Numeric expression parsing helpers. + FileCheckASTBinop *parseFileCheckBinop(StringRef &Expr, + const SourceMgr &SM) const; }; //===----------------------------------------------------------------------===// 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 @@ -61,6 +61,8 @@ llvm::Optional FileCheckPatternSubst::getSubstitute() const { if (IsNumExpr) { + assert(NumExpr->getAST() != nullptr && + "Substituting empty numeric expression"); llvm::Optional EvaluatedValue = NumExpr->getAST()->eval(); if (!EvaluatedValue) return llvm::None; @@ -129,42 +131,93 @@ 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; } +bool FileCheckPattern::parseNumericVariable(StringRef &Expr, StringRef &Name, + bool IsDefinition, + const SourceMgr &SM) const { + bool IsPseudo; + unsigned TrailIdx; + + if (parseVariable(Expr, IsPseudo, TrailIdx)) { + SM.PrintMessage(SMLoc::getFromPointer(Expr.data()), SourceMgr::DK_Error, + "invalid variable name"); + return true; + } + Name = Expr.substr(0, TrailIdx); + Expr = Expr.substr(TrailIdx); -FileCheckNumExpr * -FileCheckPattern::parseNumericExpression(StringRef Name, bool IsPseudo, - StringRef Trailer, - const SourceMgr &SM) const { if (IsPseudo && !Name.equals("@LINE")) { SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, "invalid pseudo numeric variable '" + Name + "'"); - return nullptr; + return true; } - - // 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()) { + if (IsDefinition && IsPseudo) { SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, - "using undefined numeric variable '" + Name + "'"); - return nullptr; + "definition of pseudo variable '" + Name + "' unsupported"); + return true; + } + + if (IsDefinition) { + // Detect collision between pattern and numeric variable when the latter is + // created later than the former. + if (Context->DefinedVariableTable.find(Name) != + Context->DefinedVariableTable.end()) { + SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, + "pattern variable with name '" + Name + + "' already exists"); + return true; + } + + return false; + } else { + // 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 true; + } + + FileCheckNumVar *NumVar = VarTableIter->second; + if (!IsPseudo && NumVar->getDefLineNumber() == LineNumber) { + SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, + "numeric variable '" + NumVar->getName() + + "' defined on the same line"); + return true; + } + + return false; } +} + +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; } - FileCheckNumVar *Opl = VarTableIter->second; +/// Parse \p Expr for a binary operation. +/// Return the class representing that binary operation in the AST of the +/// numeric expression or nullptr if parsing fails in which case errors +/// are reported on \p SM. +FileCheckASTBinop * +FileCheckPattern::parseFileCheckBinop(StringRef &Expr, + const SourceMgr &SM) const { + StringRef Name; + if (parseNumericVariable(Expr, Name, false, SM)) + // Error reporting done in parseNumericVariable. + return nullptr; + FileCheckNumVar *Opl = Context->GlobalNumericVariableTable.find(Name)->second; // Check if this is a supported operation and selection function to perform // it. - skipWhitespace(Trailer); - if (Trailer.empty()) { - return Context->registerNumExpr(new FileCheckASTBinop(doAdd, Opl, 0)); - } - SMLoc OpLoc = SMLoc::getFromPointer(Trailer.data()); - char Operator = popFront(Trailer); + skipWhitespace(Expr); + if (Expr.empty()) + return new FileCheckASTBinop(doAdd, Opl, 0); + SMLoc OpLoc = SMLoc::getFromPointer(Expr.data()); + char Operator = popFront(Expr); binop_eval_t EvalBinop; switch (Operator) { case '+': @@ -181,27 +234,75 @@ } // Parse right operand. - skipWhitespace(Trailer); - if (Trailer.empty()) { - SM.PrintMessage(SMLoc::getFromPointer(Trailer.data()), SourceMgr::DK_Error, + skipWhitespace(Expr); + if (Expr.empty()) { + SM.PrintMessage(SMLoc::getFromPointer(Expr.data()), SourceMgr::DK_Error, "missing operand in numeric expression"); return nullptr; } uint64_t Opr; - if (Trailer.consumeInteger(10, Opr)) { - SM.PrintMessage(SMLoc::getFromPointer(Trailer.data()), SourceMgr::DK_Error, - "invalid offset in numeric expression '" + Trailer + "'"); + if (Expr.consumeInteger(10, Opr)) { + SM.PrintMessage(SMLoc::getFromPointer(Expr.data()), SourceMgr::DK_Error, + "invalid offset in numeric expression '" + Expr + "'"); return nullptr; } - skipWhitespace(Trailer); - if (!Trailer.empty()) { - SM.PrintMessage(SMLoc::getFromPointer(Trailer.data()), SourceMgr::DK_Error, + skipWhitespace(Expr); + if (!Expr.empty()) { + SM.PrintMessage(SMLoc::getFromPointer(Expr.data()), SourceMgr::DK_Error, "unexpected characters at end of numeric expression '" + - Trailer + "'"); + Expr + "'"); return nullptr; } - return Context->registerNumExpr(new FileCheckASTBinop(EvalBinop, Opl, Opr)); + return new FileCheckASTBinop(EvalBinop, Opl, Opr); +} + +/// Parse \p Expr for a numeric expression. Return the class representing the +/// AST of numeric expression or nullptr if parsing fails in which case errors +/// are reported on \p SM. Set \p NumVarDef to the pointer to the class +/// representing the variable defined to this numeric expression if any. +FileCheckNumExpr *FileCheckPattern::parseNumericExpression( + StringRef Expr, FileCheckNumVar *&NumVarDef, const SourceMgr &SM) const { + FileCheckASTBinop *NumExprAST = nullptr; + + // Parse numeric variable definition. + NumVarDef = nullptr; + size_t DefEnd = Expr.find(':'); + if (DefEnd != StringRef::npos) { + StringRef DefExpr = Expr.substr(0, DefEnd); + StringRef UseExpr = Expr = Expr.substr(DefEnd + 1); + + skipWhitespace(DefExpr); + StringRef Name; + if (parseNumericVariable(DefExpr, Name, true /*IsDefinition*/, SM)) + // Invalid variable definition. Error reporting done in parsing function. + return nullptr; + + NumVarDef = Context->makeNumVar(Name, this->LineNumber); + + skipWhitespace(DefExpr); + if (!DefExpr.empty()) { + SM.PrintMessage(SMLoc::getFromPointer(DefExpr.data()), + SourceMgr::DK_Error, + "invalid numeric variable definition"); + return nullptr; + } + skipWhitespace(UseExpr); + if (!UseExpr.empty()) { + SM.PrintMessage( + SMLoc::getFromPointer(UseExpr.data()), SourceMgr::DK_Error, + "unexpected string after variable definition: '" + UseExpr + "'"); + return nullptr; + } + } else { + // Parse numeric expression itself. + skipWhitespace(Expr); + NumExprAST = parseFileCheckBinop(Expr, SM); + if (NumExprAST == nullptr) + return nullptr; + } + + return Context->registerNumExpr(NumExprAST); } /// Parses the given string into the Pattern. @@ -211,16 +312,15 @@ /// the input file from which the pattern string was read. Returns true in /// case of an error, false otherwise. bool FileCheckPattern::ParsePattern(StringRef PatternStr, StringRef Prefix, - SourceMgr &SM, unsigned LineNumber, - const FileCheckRequest &Req) { + SourceMgr &SM, + const FileCheckRequest &Req) { bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot; - this->LineNumber = LineNumber; PatternLoc = SMLoc::getFromPointer(PatternStr.data()); // Create fake @LINE pseudo variable definition. StringRef LinePseudo = "@LINE"; - uint64_t LineNumber64 = LineNumber; + uint64_t LineNumber64 = this->LineNumber; auto LinePseudoVar = Context->makeNumVar(LinePseudo, LineNumber64); Context->GlobalNumericVariableTable[LinePseudo] = LinePseudoVar; @@ -302,9 +402,9 @@ // assigns it to the FileCheck variable 'foo'. The second form is [[foo]] // which is a substitution of foo's value. Variable names themselves must // be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject it. This is - // to catch some common errors. Numeric expressions only have the - // substitution mode. Numeric variable names have the same restriction as - // their pattern variable counterpart. + // to catch some common errors. Numeric expressions also have the + // definition and substitution modes and numeric variable names have the + // same restriction as their pattern variable counterpart. if (PatternStr.startswith("[[")) { StringRef MatchStr = PatternStr.substr(2); bool IsNumExpr = MatchStr.consume_front("#"); @@ -324,68 +424,84 @@ MatchStr = MatchStr.substr(0, End); PatternStr = PatternStr.substr(End + 4 + (int)IsNumExpr); - size_t VarEndIdx = MatchStr.find(":"); - if (IsNumExpr) - skipWhitespace(MatchStr); - else { + bool IsVarDef; + StringRef DefName; + StringRef SubstStr; + StringRef MatchRegexp; + unsigned SubstInsertIdx = RegExStr.size(); + FileCheckNumVar *NumVarDef; + FileCheckNumExpr *NumExpr; + + // Parse pattern variable or legacy numeric expression. + if (!IsNumExpr) { + size_t VarEndIdx = MatchStr.find(":"); size_t SpacePos = MatchStr.substr(0, VarEndIdx).find_first_of(" \t"); if (SpacePos != StringRef::npos) { SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data() + SpacePos), SourceMgr::DK_Error, "unexpected whitespace"); return true; } - } - - // Get the regex name (e.g. "foo") and verify it is well formed. - bool IsPseudo; - unsigned TrailIdx; - if (parseVariable(MatchStr, IsPseudo, TrailIdx)) { - SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data()), - SourceMgr::DK_Error, "invalid variable name"); - return true; - } - unsigned SubstInsertIdx = RegExStr.size(); - FileCheckNumExpr *NumExpr; - - StringRef Name = MatchStr.substr(0, TrailIdx); - StringRef Trailer = MatchStr.substr(TrailIdx); - bool IsVarDef = (VarEndIdx != StringRef::npos); - - if (IsVarDef) { - if (IsPseudo || !Trailer.consume_front(":")) { + // Get the regex name (e.g. "foo") and verify it is well formed. + bool IsPseudo; + unsigned TrailIdx; + if (parseVariable(MatchStr, IsPseudo, TrailIdx)) { SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data()), - SourceMgr::DK_Error, - "invalid name in pattern variable definition"); + SourceMgr::DK_Error, "invalid variable name"); 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; - } + StringRef Name = MatchStr.substr(0, TrailIdx); + StringRef Trailer = MatchStr.substr(TrailIdx); + IsVarDef = (VarEndIdx != StringRef::npos); + IsNumExpr = IsPseudo; + + 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; + } + DefName = Name; + MatchRegexp = Trailer; + } else + SubstStr = MatchStr; } - if (IsNumExpr || (!IsVarDef && IsPseudo)) { - NumExpr = parseNumericExpression(Name, IsPseudo, Trailer, SM); + // Parse numeric expression. + if (IsNumExpr) { + NumExpr = parseNumericExpression(MatchStr, NumVarDef, SM); if (NumExpr == nullptr) return true; IsNumExpr = true; + if (NumVarDef != nullptr) { + IsVarDef = true; + DefName = NumVarDef->getName(); + MatchRegexp = StringRef("[0-9]+"); + } else + SubstStr = MatchStr; } // Handle variable use: [[foo]] and [[#]]. if (!IsVarDef) { // Handle use of pattern variables that were defined earlier on the - // same line by emitting a backreference. - if (!IsNumExpr && VariableDefs.find(Name) != VariableDefs.end()) { - unsigned CaptureParen = VariableDefs[Name]; + // same line by emitting a backreference. Numeric expressions do not + // support using a numeric variable defined on the same line. + if (!IsNumExpr && VariableDefs.find(SubstStr) != VariableDefs.end()) { + unsigned CaptureParen = VariableDefs[SubstStr]; if (CaptureParen < 1 || CaptureParen > 9) { - SM.PrintMessage(SMLoc::getFromPointer(Name.data()), + SM.PrintMessage(SMLoc::getFromPointer(SubstStr.data()), SourceMgr::DK_Error, "Can't back-reference more than 9 variables"); return true; @@ -396,26 +512,37 @@ // CHECK pattern or use of a numeric expression. FileCheckPatternSubst Subst = IsNumExpr - ? FileCheckPatternSubst(Context, MatchStr, NumExpr, + ? FileCheckPatternSubst(Context, SubstStr, NumExpr, SubstInsertIdx) - : FileCheckPatternSubst(Context, MatchStr, SubstInsertIdx); + : FileCheckPatternSubst(Context, SubstStr, SubstInsertIdx); Substs.push_back(Subst); } continue; } - // 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; + // Handle variable definition: [[:(...)]] and [[#(...):(...)]]. + if (IsNumExpr) { + struct FileCheckNumExprMatch NumExprDef = {NumVarDef, CurParen}; + NumericVariableDefs[DefName] = NumExprDef; + // This store is done here rather than in match() to allow + // parseNumericVariable() to get the pointer to the class instance + // of the right variable definition corresponding to a given numeric + // variable use. + Context->GlobalNumericVariableTable[DefName] = NumVarDef; + } else { + VariableDefs[DefName] = CurParen; + // Mark pattern variable as defined to detect collision between pattern + // and numeric variable in parseNumericVariable and + // 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[DefName] = true; + } RegExStr += '('; ++CurParen; - if (AddRegExToRegEx(Trailer, CurParen, SM)) + if (AddRegExToRegEx(MatchRegexp, CurParen, SM)) return true; RegExStr += ')'; @@ -466,8 +593,12 @@ /// /// The GlobalVariableTable StringMap in the FileCheckPatternContext class /// instance provides the current values of FileCheck pattern variables and is -/// updated if this match defines new values. -size_t FileCheckPattern::match(StringRef Buffer, size_t &MatchLen) const { +/// updated if this match defines new values. Likewise, the +/// GlobalNumericVariableTable StringMap in the same class provides the current +/// values of FileCheck numeric variables and is updated if this match defines +/// new numeric values. +size_t FileCheckPattern::match(StringRef Buffer, size_t &MatchLen, + const SourceMgr &SM) const { // If this is the EOF pattern, match it immediately. if (CheckTy == Check::CheckEOF) { MatchLen = 0; @@ -524,6 +655,23 @@ MatchInfo[VariableDef.second]; } + // If this defines any numeric variables, remember their values. + for (const auto &NumericVariableDef : NumericVariableDefs) { + assert(NumericVariableDef.second.CaptureParen < MatchInfo.size() && + "Internal paren error"); + unsigned CaptureParen = NumericVariableDef.second.CaptureParen; + FileCheckNumVar *NumVarDef = NumericVariableDef.second.NumVarDef; + + StringRef MatchedValue = MatchInfo[CaptureParen]; + uint64_t Val; + if (MatchedValue.getAsInteger(10, Val)) { + SM.PrintMessage(SMLoc::getFromPointer(MatchedValue.data()), + SourceMgr::DK_Error, "Unable to represent numeric value"); + } + if (NumVarDef->setValue(Val)) + assert(false && "Numeric variable redefined"); + } + // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after // the required preceding newline, which is consumed by the pattern in the // case of CHECK-EMPTY but not CHECK-NEXT. @@ -671,9 +819,9 @@ return NumExprs.back().get(); } -FileCheckNumVar *FileCheckPatternContext::makeNumVar(StringRef Name, - uint64_t Value) { - NumVars.emplace_back(new FileCheckNumVar(Name, Value)); +template +FileCheckNumVar *FileCheckPatternContext::makeNumVar(Types... args) { + NumVars.emplace_back(new FileCheckNumVar(args...)); return NumVars.back().get(); } @@ -967,9 +1115,9 @@ SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc()); ImplicitNegativeChecks.push_back( - FileCheckPattern(Check::CheckNot, &PatternContext)); + FileCheckPattern(Check::CheckNot, &PatternContext, 0)); ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer, - "IMPLICIT-CHECK", SM, 0, Req); + "IMPLICIT-CHECK", SM, Req); } std::vector DagNotMatches = ImplicitNegativeChecks; @@ -1030,8 +1178,8 @@ SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data()); // Parse the pattern. - FileCheckPattern P(CheckTy, &PatternContext); - if (P.ParsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, LineNumber, Req)) + FileCheckPattern P(CheckTy, &PatternContext, LineNumber); + if (P.ParsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, Req)) return true; // Verify that CHECK-LABEL lines do not define or use variables @@ -1075,7 +1223,7 @@ // prefix as a filler for the error message. if (!DagNotMatches.empty()) { CheckStrings.emplace_back( - FileCheckPattern(Check::CheckEOF, &PatternContext), + FileCheckPattern(Check::CheckEOF, &PatternContext, LineNumber + 1), *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data())); std::swap(DagNotMatches, CheckStrings.back().DagNotStrings); } @@ -1250,7 +1398,7 @@ StringRef MatchBuffer = Buffer.substr(LastMatchEnd); size_t CurrentMatchLen; // get a match at current start point - size_t MatchPos = Pat.match(MatchBuffer, CurrentMatchLen); + size_t MatchPos = Pat.match(MatchBuffer, CurrentMatchLen, SM); if (i == 1) FirstMatchPos = LastPos + MatchPos; @@ -1374,7 +1522,7 @@ assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!"); size_t MatchLen = 0; - size_t Pos = Pat->match(Buffer, MatchLen); + size_t Pos = Pat->match(Buffer, MatchLen, SM); if (Pos == StringRef::npos) { PrintNoMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer, @@ -1435,7 +1583,7 @@ // CHECK-DAG group. for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) { StringRef MatchBuffer = Buffer.substr(MatchPos); - size_t MatchPosBuf = Pat.match(MatchBuffer, MatchLen); + size_t MatchPosBuf = Pat.match(MatchBuffer, MatchLen, SM); // With a group of CHECK-DAGs, a single mismatching means the match on // that group of CHECK-DAGs fails immediately. if (MatchPosBuf == StringRef::npos) { @@ -1598,13 +1746,17 @@ StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer(); SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc()); + // Dummy pattern to call parseNumericVariable. + FileCheckPattern P(Check::CheckPlain, this, 0); + SmallVector CmdlineDefsDiagVec; CmdlineDefsDiagRef.split(CmdlineDefsDiagVec, '\n', -1 /*MaxSplit*/, false /*KeepEmpty*/); for (StringRef CmdlineDefDiag : CmdlineDefsDiagVec) { unsigned DefStart = CmdlineDefDiag.find(Prefix2) + Prefix2.size(); StringRef CmdlineDef = CmdlineDefDiag.substr(DefStart); - if (CmdlineDef.find('=') == StringRef::npos) { + size_t EqIdx = CmdlineDef.find('='); + if (EqIdx == StringRef::npos) { SM.PrintMessage(SMLoc::getFromPointer(CmdlineDef.data()), SourceMgr::DK_Error, "Missing equal sign in global definition"); @@ -1614,16 +1766,14 @@ // 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 + "'"); + StringRef Expr = CmdlineDef.substr(1, EqIdx - 1); + StringRef CmdlineName; + SMLoc CmdlineNameLoc = SMLoc::getFromPointer(Expr.data()); + if (P.parseNumericVariable(Expr, CmdlineName, true /*IsDefinition*/, + SM) || + !Expr.empty()) { + SM.PrintMessage(CmdlineNameLoc, SourceMgr::DK_Error, + "invalid variable name"); ErrorFound = true; continue; } @@ -1649,10 +1799,11 @@ ErrorFound = true; continue; } - auto NumVarDef = makeNumVar(CmdlineName, Val); + auto NumVarDef = makeNumVar(CmdlineName, (unsigned)0); + NumVarDef->setValue(Val); // Record this variable definition. - GlobalNumericVariableTable[CmdlineName] = NumVarDef; + GlobalNumericVariableTable[NumVarDef->getName()] = NumVarDef; } else { // Pattern variable definition. std::pair CmdlineNameVal = CmdlineDef.split('='); @@ -1683,7 +1834,7 @@ // 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(). + // the ability to detect use of undefined variable in match(). DefinedVariableTable[Name] = true; } } 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 @@ -70,15 +70,15 @@ ; 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: Global defines:1:20: error: invalid variable name ; NUMERRCLIFMT-NEXT: Global define #1: #10VALUE=10 ; NUMERRCLIFMT-NEXT: {{^ \^$}} -; NUMERRCLIPSEUDO: Global defines:1:20: error: invalid name in numeric variable definition '@VALUE' +; NUMERRCLIPSEUDO: Global defines:1:20: error: invalid pseudo numeric variable ; NUMERRCLIPSEUDO-NEXT: Global define #1: #@VALUE=10 ; NUMERRCLIPSEUDO-NEXT: {{^ \^$}} -; NUMERRCLITRAIL: Global defines:1:20: error: invalid name in numeric variable definition 'VALUE + 2' +; NUMERRCLITRAIL: Global defines:1:20: error: invalid variable name ; NUMERRCLITRAIL-NEXT: Global define #1: #VALUE + 2=10 ; NUMERRCLITRAIL-NEXT: {{^ \^$}} 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 @@ -1,28 +1,48 @@ -; RUN: FileCheck -D#VAR1=11 -input-file %s %s +; RUN: FileCheck -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 +; RUN: not FileCheck -check-prefixes CHECK,INVAL-OP -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix INVAL-OP-MSG %s +; RUN: not FileCheck -check-prefixes CONFLICT,CONFLICT1,CONFLICT2 -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix INPUT-PAT-CONFLICT %s +; RUN: not FileCheck -D#NUMVAR=42 -check-prefixes CONFLICT,CONFLICT2 -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix INPUT-PAT-CONFLICT %s +; RUN: not FileCheck -D#NUMVAR=42 -DNUMVAR=foobar -check-prefix CONFLICT -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix CLI-PAT-CONFLICT %s +; RUN: not FileCheck -check-prefixes CONFLICT,CONFLICT3,CONFLICT4 -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix INPUT-NUM-CONFLICT %s +; RUN: not FileCheck -DPATVAR=foobar -check-prefixes CONFLICT,CONFLICT4 -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix INPUT-NUM-CONFLICT %s +; RUN: not FileCheck -DPATVAR=foobar -D#PATVAR=42 -check-prefix CONFLICT -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefix 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 +; Numeric variable definition without spaces +DEF NO SPC +11 +; CHECK-LABEL: DEF NO SPC +; CHECK-NEXT: [[#VAR1:]] + + +; Numeric variable definition in alternate spacing +DEF ALT SPC +11 +11 +11 +; CHECK-LABEL: DEF ALT SPC +; CHECK-NEXT: [[# VAR1a:]] +; CHECK-NEXT: [[# VAR1b :]] +; CHECK-NEXT: [[# VAR1c : ]] + + +; Numeric expressions using variables defined on other lines without spaces USE NO SPC 11 12 10 -; CHECK-LABEL: USE NO SPC +; CHECK-LABEL: USE ; CHECK-NEXT: [[#VAR1]] ; CHECK-NEXT: [[#VAR1+1]] ; CHECK-NEXT: [[#VAR1-1]] -; Numeric expressions using variables defined on the command-line in alternate +; Numeric expressions using variables defined on other lines in alternate ; spacing USE ALT SPC 11 @@ -79,17 +99,25 @@ ; Name conflict between Numeric variable definition and pattern variable -; definition +; definition whether from the command-line or input text PATVAR NUMVAR CONFLICT +redef1 42 foobar +redef2 42 ; 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: {{^ \^$}} +; CONFLICT1-NEXT: redef1 [[#NUMVAR:]] +; CONFLICT2: [[NUMVAR:foo.*]] +; CONFLICT3: [[PATVAR:foo.*]] +; CONFLICT4: redef2 [[#PATVAR:]] +; INPUT-PAT-CONFLICT: numeric-expression.txt:[[#@LINE-3]]:16: error: numeric variable with name 'NUMVAR' already exists +; INPUT-PAT-CONFLICT-NEXT: ; {{C}}ONFLICT2: {{\[\[NUMVAR:foo\.\*\]\]}} +; INPUT-PAT-CONFLICT-NEXT: {{^ \^$}} +; CLI-PAT-CONFLICT: Global defines:2:19: error: numeric variable with name 'NUMVAR' already exists +; CLI-PAT-CONFLICT-NEXT: Global define #2: NUMVAR=foobar +; CLI-PAT-CONFLICT-NEXT: {{^ \^$}} +; INPUT-NUM-CONFLICT: numeric-expression.txt:[[#@LINE-7]]:24: error: pattern variable with name 'PATVAR' already exists +; INPUT-NUM-CONFLICT-NEXT: ; CONFLICT4: redef2 {{\[\[#PATVAR:\]\]}} +; INPUT-NUM-CONFLICT-NEXT: {{^ \^$}} +; CLI-NUM-CONFLICT: Global defines:2:20: error: pattern variable with name 'PATVAR' already exists +; CLI-NUM-CONFLICT-NEXT: Global define #2: #PATVAR=42 +; CLI-NUM-CONFLICT-NEXT: {{^ \^$}} diff --git a/llvm/test/FileCheck/var-scope.txt b/llvm/test/FileCheck/var-scope.txt --- a/llvm/test/FileCheck/var-scope.txt +++ b/llvm/test/FileCheck/var-scope.txt @@ -1,15 +1,15 @@ -// 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 +// RUN: FileCheck -input-file %s %s +// RUN: FileCheck -check-prefixes CHECK,GLOBAL -input-file %s %s +// RUN: FileCheck -check-prefixes CHECK,LOCAL3 -input-file %s %s +// RUN: FileCheck -check-prefixes CHECK,GLOBAL --enable-var-scope -input-file %s %s +// RUN: not FileCheck -check-prefixes CHECK,LOCAL1 --enable-var-scope -input-file %s %s +// RUN: not FileCheck -check-prefixes CHECK,LOCAL2 --enable-var-scope -input-file %s %s +// RUN: not FileCheck -check-prefixes CHECK,LOCAL3 --enable-var-scope -input-file %s %s local1 global1 -; CHECK: [[LOCAL:loc[^[:digit:]]*]][[#LOCNUM]] -; CHECK: [[$GLOBAL:glo[^[:digit:]]*]][[#$GLOBNUM]] +; CHECK: [[LOCAL:loc[^[:digit:]]*]][[#LOCNUM:]] +; CHECK: [[$GLOBAL:glo[^[:digit:]]*]][[#$GLOBNUM:]] local2 global2 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 -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 +; 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 --strict-whitespace -check-prefix V %s +; RUN: FileCheck -vv -input-file %s %s 2>&1 | FileCheck --strict-whitespace -check-prefixes V,VV %s foo bar @@ -29,36 +29,33 @@ VV-NEXT: {{^}}bar{{$}} VV-NEXT: {{^}}^{{$}} -NUMVAR:42 +NUMVAR=42 NUMVAR - 1:41 -CHECK: NUMVAR:[[#NUMVAR]] +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: {{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: {{^}}NUMVAR=42{{$}} V-NEXT: {{^}}^~~~~~~~~{{$}} -V-NEXT: verbose.txt:[[#@LINE-12]]:13: remark: {{C}}HECK-NEXT: expected string found in input +V-NEXT: verbose.txt:[[#@LINE-9]]: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: verbose.txt:[[#@LINE-15]]: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: verbose.txt:[[#@LINE-18]]: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: verbose.txt:[[#@LINE-20]]: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: {{^ \^$}} +VV-NEXT: verbose.txt:[[#@LINE-25]]:1: note: scanning from here VV-NEXT: {{^}}NUMVAR - 1:41{{$}} VV-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 @@ -15,7 +15,7 @@ class FileCheckTest : public ::testing::Test {}; TEST_F(FileCheckTest, NumVarValueGetClearSet) { - FileCheckNumVar FooVar = FileCheckNumVar("FOO", 42); + FileCheckNumVar FooVar = FileCheckNumVar("FOO", (uint64_t)42); // Defined variable: getValue returns a value, setValue fails. llvm::Optional Value = FooVar.getValue(); @@ -39,7 +39,7 @@ uint64_t doAdd(uint64_t OpL, uint64_t OpR) { return OpL + OpR; } TEST_F(FileCheckTest, BinopEvalUndef) { - auto FooVar = new FileCheckNumVar("FOO", 42); + auto FooVar = new FileCheckNumVar("FOO", (uint64_t)42); auto Binop = new FileCheckASTBinop(doAdd, FooVar, 18); // Defined variable: eval returns right value, no undef variable returned. @@ -133,79 +133,195 @@ EXPECT_EQ(TrailIdx, VarName.size() - 1); } -class ExprTester { +class PatternTester { private: + unsigned LineNumber = 1; SourceMgr SM; struct FileCheckRequest Req; FileCheckPatternContext Context; - FileCheckPattern P = FileCheckPattern(Check::CheckPlain, &Context); + FileCheckPattern P = + FileCheckPattern(Check::CheckPlain, &Context, LineNumber++); public: - ExprTester() { + PatternTester() { std::vector GlobalDefines; GlobalDefines.emplace_back(std::string("#FOO=42")); + GlobalDefines.emplace_back(std::string("BAR=BAZ")); Context.defineCmdlineVariables(GlobalDefines, SM); // Call ParsePattern to have @LINE defined. - P.ParsePattern("N/A", "CHECK", SM, 1, Req); + P.ParsePattern("N/A", "CHECK", SM, Req); + // ParsePattern does not expect to be called twice for the same line and + // will set FixedStr and RegExStr incorrectly if it is. Therefore prepare + // a pattern for a different line. + initNextPattern(); } - bool parseExpect(std::string &VarName, std::string &Trailer) { - bool IsPseudo = VarName[0] == '@'; - StringRef NameTrailer = StringRef(VarName + Trailer); + void initNextPattern() { + P = FileCheckPattern(Check::CheckPlain, &Context, LineNumber++); + } + +private: + StringRef bufferize(StringRef Expr) { std::unique_ptr Buffer = - MemoryBuffer::getMemBufferCopy(NameTrailer, "TestBuffer"); - StringRef NameTrailerRef = Buffer->getBuffer(); + MemoryBuffer::getMemBufferCopy(Expr, "TestBuffer"); + StringRef ExprBufferRef = Buffer->getBuffer(); SM.AddNewSourceBuffer(std::move(Buffer), SMLoc()); - StringRef VarNameRef = NameTrailerRef.substr(0, VarName.size()); - StringRef TrailerRef = NameTrailerRef.substr(VarName.size()); - return P.parseNumericExpression(VarNameRef, IsPseudo, TrailerRef, SM) == - nullptr; + return ExprBufferRef; + } + +public: + bool parseNumVarExpect(StringRef Expr, bool isDefinition) { + StringRef ExprBufferRef = bufferize(Expr); + StringRef Name; + return P.parseNumericVariable(ExprBufferRef, Name, isDefinition, SM); + } + + bool parseExprExpect(StringRef Expr) { + StringRef ExprBufferRef = bufferize(Expr); + FileCheckNumVar *NumVarDef; + return P.parseNumericExpression(ExprBufferRef, NumVarDef, SM) == nullptr; + } + + bool parsePatternExpect(StringRef Pattern) { + StringRef PatBufferRef = bufferize(Pattern); + return P.ParsePattern(PatBufferRef, "CHECK", SM, Req); + } + + bool matchExpect(StringRef Buffer) { + StringRef BufferRef = bufferize(Buffer); + size_t MatchLen; + return P.match(BufferRef, MatchLen, SM); } }; +TEST_F(FileCheckTest, ParseNumVar) { + PatternTester Tester; + + // Invalid variable name. + EXPECT_TRUE(Tester.parseNumVarExpect("42INVALID", false /*isDefinition*/)); + + // Invalid pseudo variable. + EXPECT_TRUE(Tester.parseNumVarExpect("@FOO", false /*isDefinition*/)); + + // Valid pseudo variable. + EXPECT_FALSE(Tester.parseNumVarExpect("@LINE", false /*isDefinition*/)); + + // Invalid definition of pseudo. + EXPECT_TRUE(Tester.parseNumVarExpect("@LINE", true /*isDefinition*/)); + + // Conflict with pattern variable. + EXPECT_TRUE(Tester.parseNumVarExpect("BAR", true /*isDefinition*/)); + + // Undefined variable. + EXPECT_TRUE(Tester.parseNumVarExpect("UNDEF", false /*isDefinition*/)); + + // Use variable defined on same line. + EXPECT_FALSE(Tester.parseExprExpect("LINE1VAR:")); + EXPECT_TRUE(Tester.parseNumVarExpect("LINE1VAR", false /*isDefinition*/)); + + // Defined variable. + EXPECT_FALSE(Tester.parseNumVarExpect("FOO", false /*isDefinition*/)); +} + TEST_F(FileCheckTest, ParseExpr) { - ExprTester Tester; + PatternTester Tester; + + // Variable definition. + + // Definition of invalid variable. + EXPECT_TRUE(Tester.parseExprExpect("10VAR:")); + EXPECT_TRUE(Tester.parseExprExpect("@FOO:")); + EXPECT_TRUE(Tester.parseExprExpect("@LINE:")); + + // Garbage after name of variable being defined. + EXPECT_TRUE(Tester.parseExprExpect("VAR GARBAGE:")); - // @LINE with offset. - std::string VarName = "@LINE"; - std::string Trailer = "+3"; - EXPECT_FALSE(Tester.parseExpect(VarName, Trailer)); + // Variable defined to numeric expression. + EXPECT_TRUE(Tester.parseExprExpect("VAR1: FOO")); - // @LINE only. - Trailer = ""; - EXPECT_FALSE(Tester.parseExpect(VarName, Trailer)); + // Acceptable variable definition. + EXPECT_FALSE(Tester.parseExprExpect("VAR1:")); + EXPECT_FALSE(Tester.parseExprExpect(" VAR2:")); + EXPECT_FALSE(Tester.parseExprExpect("VAR3 :")); + EXPECT_FALSE(Tester.parseExprExpect("VAR3: ")); - // Defined variable - VarName = "FOO"; - EXPECT_FALSE(Tester.parseExpect(VarName, Trailer)); + // Numeric expression. - // Undefined variable - VarName = "UNDEF"; - EXPECT_TRUE(Tester.parseExpect(VarName, Trailer)); + // Unacceptable variable. + EXPECT_TRUE(Tester.parseExprExpect("10VAR")); + EXPECT_TRUE(Tester.parseExprExpect("@FOO")); + EXPECT_TRUE(Tester.parseExprExpect("UNDEF")); - // Wrong Pseudovar. - VarName = "@FOO"; - EXPECT_TRUE(Tester.parseExpect(VarName, Trailer)); + // Only valid variable. + EXPECT_FALSE(Tester.parseExprExpect("@LINE")); + EXPECT_FALSE(Tester.parseExprExpect("FOO")); // Unsupported operator. - VarName = "@LINE"; - Trailer = "/2"; - EXPECT_TRUE(Tester.parseExpect(VarName, Trailer)); + EXPECT_TRUE(Tester.parseExprExpect("@LINE/2")); // Missing offset operand. - VarName = "@LINE"; - Trailer = "+"; - EXPECT_TRUE(Tester.parseExpect(VarName, Trailer)); + EXPECT_TRUE(Tester.parseExprExpect("@LINE+")); // Cannot parse offset operand. - VarName = "@LINE"; - Trailer = "+x"; - EXPECT_TRUE(Tester.parseExpect(VarName, Trailer)); + EXPECT_TRUE(Tester.parseExprExpect("@LINE+x")); // Unexpected string at end of numeric expression. - VarName = "@LINE"; - Trailer = "+5x"; - EXPECT_TRUE(Tester.parseExpect(VarName, Trailer)); + EXPECT_TRUE(Tester.parseExprExpect("@LINE+5x")); + + // Valid expression. + EXPECT_FALSE(Tester.parseExprExpect("@LINE+5")); + EXPECT_FALSE(Tester.parseExprExpect("FOO+4")); +} + +TEST_F(FileCheckTest, ParsePattern) { + PatternTester Tester; + + // Space in pattern variable expression. + EXPECT_TRUE(Tester.parsePatternExpect("[[ BAR]]")); + + // Invalid variable name. + EXPECT_TRUE(Tester.parsePatternExpect("[[42INVALID]]")); + + // Invalid definition. + EXPECT_TRUE(Tester.parsePatternExpect("[[@PAT:]]")); + EXPECT_TRUE(Tester.parsePatternExpect("[[PAT+2:]]")); + + // Collision with numeric variable. + EXPECT_TRUE(Tester.parsePatternExpect("[[FOO:]]")); + + // Valid use. + EXPECT_FALSE(Tester.parsePatternExpect("[[BAR]]")); + + // Valid definition. + EXPECT_FALSE(Tester.parsePatternExpect("[[PAT:[0-9]+]]")); + + // Invalid numeric expressions. + EXPECT_TRUE(Tester.parsePatternExpect("[[#42INVALID]]")); + EXPECT_TRUE(Tester.parsePatternExpect("[[#@FOO]]")); + EXPECT_TRUE(Tester.parsePatternExpect("[[#@LINE/2]]")); + EXPECT_TRUE(Tester.parsePatternExpect("[[#2+@LINE]]")); + EXPECT_TRUE(Tester.parsePatternExpect("[[#YUP:@LINE]]")); + + // Valid numeric expressions. + EXPECT_FALSE(Tester.parsePatternExpect("[[#FOO]]")); + EXPECT_FALSE(Tester.parsePatternExpect("[[#@LINE+2]]")); + EXPECT_FALSE(Tester.parsePatternExpect("[[#NUMVAR:]]")); +} + +TEST_F(FileCheckTest, Match) { + PatternTester Tester; + + // Check matching a definition only matches a number. + Tester.parsePatternExpect("[[#NUMVAR:]]"); + EXPECT_TRUE(Tester.matchExpect("FAIL")); + EXPECT_FALSE(Tester.matchExpect("18")); + + // Check matching the variable defined matches the correct number only + Tester.initNextPattern(); + Tester.parsePatternExpect("[[#NUMVAR]] [[#NUMVAR+2]]"); + EXPECT_TRUE(Tester.matchExpect("19 21")); + EXPECT_TRUE(Tester.matchExpect("18 21")); + EXPECT_FALSE(Tester.matchExpect("18 20")); } TEST_F(FileCheckTest, Substitute) { @@ -220,7 +336,7 @@ EXPECT_FALSE(Subst.getSubstitute()); // Substitution of defined numeric variable returns the right value. - auto LineVar = new FileCheckNumVar("@LINE", 42); + auto LineVar = new FileCheckNumVar("@LINE", (uint64_t)42); auto Binop = new FileCheckASTBinop(doAdd, LineVar, 0); FileCheckNumExpr NumExpr = FileCheckNumExpr(Binop); Subst = FileCheckPatternSubst(&Context, "@LINE", &NumExpr, 12); @@ -233,7 +349,7 @@ EXPECT_FALSE(Subst.getSubstitute()); // Substitution of defined pattern variable returns the right value. - FileCheckPattern P = FileCheckPattern(Check::CheckPlain, &Context); + FileCheckPattern P = FileCheckPattern(Check::CheckPlain, &Context, 1); Subst = FileCheckPatternSubst(&Context, "FOO", 42); Value = Subst.getSubstitute(); EXPECT_TRUE(Value); @@ -260,7 +376,7 @@ // Undef var in numeric expression substitution with defined variable is // empty. - auto LineVar = new FileCheckNumVar("@LINE", 42); + auto LineVar = new FileCheckNumVar("@LINE", (uint64_t)42); auto Binop = new FileCheckASTBinop(doAdd, LineVar, 0); FileCheckNumExpr NumExpr = FileCheckNumExpr(Binop); Subst = FileCheckPatternSubst(&Context, "@LINE", &NumExpr, 12);