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 @@ -132,14 +132,14 @@ SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange); }; -class FileCheckPatternContext; +class PatternContext; struct FileCheckString; /// FileCheck class takes the request and exposes various methods that /// use information from the request. class FileCheck { FileCheckRequest Req; - std::unique_ptr PatternContext; + std::unique_ptr PatternContextPtr; // C++17 TODO: make this a plain std::vector. std::unique_ptr> CheckStrings; 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 @@ -25,15 +25,15 @@ using namespace llvm; -Expected FileCheckNumericVariableUse::eval() const { - Optional Value = NumericVariable->getValue(); +Expected NumericVariableUse::eval() const { + Optional Value = Variable->getValue(); if (Value) return *Value; - return make_error(Name); + return make_error(Name); } -Expected FileCheckASTBinop::eval() const { +Expected BinaryOperation::eval() const { Expected LeftOp = LeftOperand->eval(); Expected RightOp = RightOperand->eval(); @@ -51,14 +51,14 @@ return EvalBinop(*LeftOp, *RightOp); } -Expected FileCheckNumericSubstitution::getResult() const { - Expected EvaluatedValue = ExpressionAST->eval(); +Expected NumericSubstitution::getResult() const { + Expected EvaluatedValue = ExpressionASTPointer->eval(); if (!EvaluatedValue) return EvaluatedValue.takeError(); return utostr(*EvaluatedValue); } -Expected FileCheckStringSubstitution::getResult() const { +Expected StringSubstitution::getResult() const { // Look up the value and escape it so that we can put it into the regex. Expected VarVal = Context->getPatternVarValue(FromStr); if (!VarVal) @@ -66,14 +66,12 @@ return Regex::escape(*VarVal); } -bool FileCheckPattern::isValidVarNameStart(char C) { - return C == '_' || isalpha(C); -} +bool Pattern::isValidVarNameStart(char C) { return C == '_' || isalpha(C); } -Expected -FileCheckPattern::parseVariable(StringRef &Str, const SourceMgr &SM) { +Expected +Pattern::parseVariable(StringRef &Str, const SourceMgr &SM) { if (Str.empty()) - return FileCheckErrorDiagnostic::get(SM, Str, "empty variable name"); + return ErrorDiagnostic::get(SM, Str, "empty variable name"); bool ParsedOneChar = false; unsigned I = 0; @@ -85,7 +83,7 @@ for (unsigned E = Str.size(); I != E; ++I) { if (!ParsedOneChar && !isValidVarNameStart(Str[I])) - return FileCheckErrorDiagnostic::get(SM, Str, "invalid variable name"); + return ErrorDiagnostic::get(SM, Str, "invalid variable name"); // Variable names are composed of alphanumeric characters and underscores. if (Str[I] != '_' && !isalnum(Str[I])) @@ -109,36 +107,35 @@ return C; } -char FileCheckUndefVarError::ID = 0; -char FileCheckErrorDiagnostic::ID = 0; -char FileCheckNotFoundError::ID = 0; +char UndefVarError::ID = 0; +char ErrorDiagnostic::ID = 0; +char NotFoundError::ID = 0; -Expected -FileCheckPattern::parseNumericVariableDefinition( - StringRef &Expr, FileCheckPatternContext *Context, - Optional LineNumber, const SourceMgr &SM) { +Expected Pattern::parseNumericVariableDefinition( + StringRef &Expr, PatternContext *Context, Optional LineNumber, + const SourceMgr &SM) { Expected ParseVarResult = parseVariable(Expr, SM); if (!ParseVarResult) return ParseVarResult.takeError(); StringRef Name = ParseVarResult->Name; if (ParseVarResult->IsPseudo) - return FileCheckErrorDiagnostic::get( + return ErrorDiagnostic::get( SM, Name, "definition of pseudo numeric variable unsupported"); // Detect collisions between string and numeric variables when the latter // is created later than the former. if (Context->DefinedVariableTable.find(Name) != Context->DefinedVariableTable.end()) - return FileCheckErrorDiagnostic::get( + return ErrorDiagnostic::get( SM, Name, "string variable with name '" + Name + "' already exists"); Expr = Expr.ltrim(SpaceChars); if (!Expr.empty()) - return FileCheckErrorDiagnostic::get( + return ErrorDiagnostic::get( SM, Expr, "unexpected characters after numeric variable name"); - FileCheckNumericVariable *DefinedNumericVariable; + NumericVariable *DefinedNumericVariable; auto VarTableIter = Context->GlobalNumericVariableTable.find(Name); if (VarTableIter != Context->GlobalNumericVariableTable.end()) DefinedNumericVariable = VarTableIter->second; @@ -148,13 +145,12 @@ return DefinedNumericVariable; } -Expected> -FileCheckPattern::parseNumericVariableUse(StringRef Name, bool IsPseudo, - Optional LineNumber, - FileCheckPatternContext *Context, - const SourceMgr &SM) { +Expected> +Pattern::parseNumericVariableUse(StringRef Name, bool IsPseudo, + Optional LineNumber, + PatternContext *Context, const SourceMgr &SM) { if (IsPseudo && !Name.equals("@LINE")) - return FileCheckErrorDiagnostic::get( + return ErrorDiagnostic::get( SM, Name, "invalid pseudo numeric variable '" + Name + "'"); // Numeric variable definitions and uses are parsed in the order in which @@ -166,7 +162,7 @@ // uses of undefined variables, whether string or numeric, are then diagnosed // in printSubstitutions() after failing to match. auto VarTableIter = Context->GlobalNumericVariableTable.find(Name); - FileCheckNumericVariable *NumericVariable; + NumericVariable *NumericVariable; if (VarTableIter != Context->GlobalNumericVariableTable.end()) NumericVariable = VarTableIter->second; else { @@ -176,22 +172,21 @@ Optional DefLineNumber = NumericVariable->getDefLineNumber(); if (DefLineNumber && LineNumber && *DefLineNumber == *LineNumber) - return FileCheckErrorDiagnostic::get( + return ErrorDiagnostic::get( SM, Name, "numeric variable '" + Name + "' defined earlier in the same CHECK directive"); - return std::make_unique(Name, NumericVariable); + return std::make_unique(Name, NumericVariable); } -Expected> -FileCheckPattern::parseNumericOperand(StringRef &Expr, AllowedOperand AO, - Optional LineNumber, - FileCheckPatternContext *Context, - const SourceMgr &SM) { +Expected> +Pattern::parseNumericOperand(StringRef &Expr, AllowedOperand AO, + Optional LineNumber, + PatternContext *Context, const SourceMgr &SM) { if (AO == AllowedOperand::LineVar || AO == AllowedOperand::Any) { // Try to parse as a numeric variable use. - Expected ParseVarResult = + Expected ParseVarResult = parseVariable(Expr, SM); if (ParseVarResult) return parseNumericVariableUse(ParseVarResult->Name, @@ -206,10 +201,10 @@ // Otherwise, parse it as a literal. uint64_t LiteralValue; if (!Expr.consumeInteger(/*Radix=*/10, LiteralValue)) - return std::make_unique(LiteralValue); + return std::make_unique(LiteralValue); - return FileCheckErrorDiagnostic::get(SM, Expr, - "invalid operand format '" + Expr + "'"); + return ErrorDiagnostic::get(SM, Expr, + "invalid operand format '" + Expr + "'"); } static uint64_t add(uint64_t LeftOp, uint64_t RightOp) { @@ -220,10 +215,10 @@ return LeftOp - RightOp; } -Expected> FileCheckPattern::parseBinop( - StringRef &Expr, std::unique_ptr LeftOp, - bool IsLegacyLineExpr, Optional LineNumber, - FileCheckPatternContext *Context, const SourceMgr &SM) { +Expected> +Pattern::parseBinop(StringRef &Expr, std::unique_ptr LeftOp, + bool IsLegacyLineExpr, Optional LineNumber, + PatternContext *Context, const SourceMgr &SM) { Expr = Expr.ltrim(SpaceChars); if (Expr.empty()) return std::move(LeftOp); @@ -241,35 +236,32 @@ EvalBinop = sub; break; default: - return FileCheckErrorDiagnostic::get( + return ErrorDiagnostic::get( SM, OpLoc, Twine("unsupported operation '") + Twine(Operator) + "'"); } // Parse right operand. Expr = Expr.ltrim(SpaceChars); if (Expr.empty()) - return FileCheckErrorDiagnostic::get(SM, Expr, - "missing operand in expression"); + return ErrorDiagnostic::get(SM, Expr, "missing operand in expression"); // The second operand in a legacy @LINE expression is always a literal. AllowedOperand AO = IsLegacyLineExpr ? AllowedOperand::Literal : AllowedOperand::Any; - Expected> RightOpResult = + Expected> RightOpResult = parseNumericOperand(Expr, AO, LineNumber, Context, SM); if (!RightOpResult) return RightOpResult; Expr = Expr.ltrim(SpaceChars); - return std::make_unique(EvalBinop, std::move(LeftOp), - std::move(*RightOpResult)); + return std::make_unique(EvalBinop, std::move(LeftOp), + std::move(*RightOpResult)); } -Expected> -FileCheckPattern::parseNumericSubstitutionBlock( - StringRef Expr, - Optional &DefinedNumericVariable, - bool IsLegacyLineExpr, Optional LineNumber, - FileCheckPatternContext *Context, const SourceMgr &SM) { - std::unique_ptr ExpressionAST = nullptr; +Expected> Pattern::parseNumericSubstitutionBlock( + StringRef Expr, Optional &DefinedNumericVariable, + bool IsLegacyLineExpr, Optional LineNumber, PatternContext *Context, + const SourceMgr &SM) { + std::unique_ptr ExpressionASTPointer = nullptr; StringRef DefExpr = StringRef(); DefinedNumericVariable = None; // Save variable definition expression if any. @@ -286,26 +278,26 @@ // pseudo variable. AllowedOperand AO = IsLegacyLineExpr ? AllowedOperand::LineVar : AllowedOperand::Any; - Expected> ParseResult = + Expected> ParseResult = parseNumericOperand(Expr, AO, LineNumber, Context, SM); while (ParseResult && !Expr.empty()) { ParseResult = parseBinop(Expr, std::move(*ParseResult), IsLegacyLineExpr, LineNumber, Context, SM); // Legacy @LINE expressions only allow 2 operands. if (ParseResult && IsLegacyLineExpr && !Expr.empty()) - return FileCheckErrorDiagnostic::get( + return ErrorDiagnostic::get( SM, Expr, "unexpected characters at end of expression '" + Expr + "'"); } if (!ParseResult) return ParseResult; - ExpressionAST = std::move(*ParseResult); + ExpressionASTPointer = std::move(*ParseResult); } // Parse the numeric variable definition. if (DefEnd != StringRef::npos) { DefExpr = DefExpr.ltrim(SpaceChars); - Expected ParseResult = + Expected ParseResult = parseNumericVariableDefinition(DefExpr, Context, LineNumber, SM); if (!ParseResult) @@ -313,12 +305,11 @@ DefinedNumericVariable = *ParseResult; } - return std::move(ExpressionAST); + return std::move(ExpressionASTPointer); } -bool FileCheckPattern::parsePattern(StringRef PatternStr, StringRef Prefix, - SourceMgr &SM, - const FileCheckRequest &Req) { +bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix, + SourceMgr &SM, const FileCheckRequest &Req) { bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot; IgnoreCase = Req.IgnoreCase; @@ -447,7 +438,7 @@ // Get the name (e.g. "foo") and verify it is well formed. StringRef OrigMatchStr = MatchStr; - Expected ParseVarResult = + Expected ParseVarResult = parseVariable(MatchStr, SM); if (!ParseVarResult) { logAllUnhandledErrors(ParseVarResult.takeError(), errs()); @@ -487,10 +478,10 @@ } // Parse numeric substitution block. - std::unique_ptr ExpressionAST; - Optional DefinedNumericVariable; + std::unique_ptr ExpressionASTPointer; + Optional DefinedNumericVariable; if (IsNumBlock) { - Expected> ParseResult = + Expected> ParseResult = parseNumericSubstitutionBlock(MatchStr, DefinedNumericVariable, IsLegacyLineExpr, LineNumber, Context, SM); @@ -498,8 +489,8 @@ logAllUnhandledErrors(ParseResult.takeError(), errs()); return true; } - ExpressionAST = std::move(*ParseResult); - SubstNeeded = ExpressionAST != nullptr; + ExpressionASTPointer = std::move(*ParseResult); + SubstNeeded = ExpressionASTPointer != nullptr; if (DefinedNumericVariable) { IsDefinition = true; DefName = (*DefinedNumericVariable)->getName(); @@ -516,7 +507,7 @@ ++SubstInsertIdx; if (IsNumBlock) { - FileCheckNumericVariableMatch NumericVariableDefinition = { + NumericVariableMatch NumericVariableDefinition = { *DefinedNumericVariable, CurParen}; NumericVariableDefs[DefName] = NumericVariableDefinition; // This store is done here rather than in match() to allow @@ -562,10 +553,11 @@ } else { // Handle substitution of string variables ([[]]) defined in // previous CHECK patterns, and substitution of expressions. - FileCheckSubstitution *Substitution = + Substitution *Substitution = IsNumBlock ? Context->makeNumericSubstitution( - SubstStr, std::move(ExpressionAST), SubstInsertIdx) + SubstStr, std::move(ExpressionASTPointer), + SubstInsertIdx) : Context->makeStringSubstitution(SubstStr, SubstInsertIdx); Substitutions.push_back(Substitution); } @@ -589,7 +581,7 @@ return false; } -bool FileCheckPattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) { +bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) { Regex R(RS); std::string Error; if (!R.isValid(Error)) { @@ -603,14 +595,14 @@ return false; } -void FileCheckPattern::AddBackrefToRegEx(unsigned BackrefNum) { +void Pattern::AddBackrefToRegEx(unsigned BackrefNum) { assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number"); std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum); RegExStr += Backref; } -Expected FileCheckPattern::match(StringRef Buffer, size_t &MatchLen, - const SourceMgr &SM) const { +Expected Pattern::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; @@ -620,10 +612,10 @@ // If this is a fixed string pattern, just match it now. if (!FixedStr.empty()) { MatchLen = FixedStr.size(); - size_t Pos = IgnoreCase ? Buffer.find_lower(FixedStr) - : Buffer.find(FixedStr); + size_t Pos = + IgnoreCase ? Buffer.find_lower(FixedStr) : Buffer.find(FixedStr); if (Pos == StringRef::npos) - return make_error(); + return make_error(); return Pos; } @@ -663,7 +655,7 @@ if (IgnoreCase) Flags |= Regex::IgnoreCase; if (!Regex(RegExToMatch, Flags).match(Buffer, &MatchInfo)) - return make_error(); + return make_error(); // Successful regex match. assert(!MatchInfo.empty() && "Didn't get any match"); @@ -678,18 +670,18 @@ // If this defines any numeric variables, remember their values. for (const auto &NumericVariableDef : NumericVariableDefs) { - const FileCheckNumericVariableMatch &NumericVariableMatch = + const NumericVariableMatch &NumericVariableMatch = NumericVariableDef.getValue(); unsigned CaptureParenGroup = NumericVariableMatch.CaptureParenGroup; assert(CaptureParenGroup < MatchInfo.size() && "Internal paren error"); - FileCheckNumericVariable *DefinedNumericVariable = + NumericVariable *DefinedNumericVariable = NumericVariableMatch.DefinedNumericVariable; StringRef MatchedValue = MatchInfo[CaptureParenGroup]; uint64_t Val; if (MatchedValue.getAsInteger(10, Val)) - return FileCheckErrorDiagnostic::get(SM, MatchedValue, - "Unable to represent numeric value"); + return ErrorDiagnostic::get(SM, MatchedValue, + "Unable to represent numeric value"); DefinedNumericVariable->setValue(Val); } @@ -701,7 +693,7 @@ return FullMatch.data() - Buffer.data() + MatchStartSkip; } -unsigned FileCheckPattern::computeMatchDistance(StringRef Buffer) const { +unsigned Pattern::computeMatchDistance(StringRef Buffer) const { // Just compute the number of matching characters. For regular expressions, we // just compare against the regex itself and hope for the best. // @@ -718,8 +710,8 @@ return BufferPrefix.edit_distance(ExampleString); } -void FileCheckPattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer, - SMRange MatchRange) const { +void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer, + SMRange MatchRange) const { // Print what we know about substitutions. if (!Substitutions.empty()) { for (const auto &Substitution : Substitutions) { @@ -731,11 +723,10 @@ // variables it uses. if (!MatchedValue) { bool UndefSeen = false; - handleAllErrors(MatchedValue.takeError(), - [](const FileCheckNotFoundError &E) {}, + handleAllErrors(MatchedValue.takeError(), [](const NotFoundError &E) {}, // Handled in PrintNoMatch(). - [](const FileCheckErrorDiagnostic &E) {}, - [&](const FileCheckUndefVarError &E) { + [](const ErrorDiagnostic &E) {}, + [&](const UndefVarError &E) { if (!UndefSeen) { OS << "uses undefined variable(s):"; UndefSeen = true; @@ -778,9 +769,8 @@ return Range; } -void FileCheckPattern::printFuzzyMatch( - const SourceMgr &SM, StringRef Buffer, - std::vector *Diags) const { +void Pattern::printFuzzyMatch(const SourceMgr &SM, StringRef Buffer, + std::vector *Diags) const { // Attempt to find the closest/best fuzzy match. Usually an error happens // because some string in the output didn't exactly match. In these cases, we // would like to show the user a best guess at what "should have" matched, to @@ -825,40 +815,36 @@ } } -Expected -FileCheckPatternContext::getPatternVarValue(StringRef VarName) { +Expected PatternContext::getPatternVarValue(StringRef VarName) { auto VarIter = GlobalVariableTable.find(VarName); if (VarIter == GlobalVariableTable.end()) - return make_error(VarName); + return make_error(VarName); return VarIter->second; } template -FileCheckNumericVariable * -FileCheckPatternContext::makeNumericVariable(Types... args) { - NumericVariables.push_back( - std::make_unique(args...)); +NumericVariable *PatternContext::makeNumericVariable(Types... args) { + NumericVariables.push_back(std::make_unique(args...)); return NumericVariables.back().get(); } -FileCheckSubstitution * -FileCheckPatternContext::makeStringSubstitution(StringRef VarName, - size_t InsertIdx) { +Substitution *PatternContext::makeStringSubstitution(StringRef VarName, + size_t InsertIdx) { Substitutions.push_back( - std::make_unique(this, VarName, InsertIdx)); + std::make_unique(this, VarName, InsertIdx)); return Substitutions.back().get(); } -FileCheckSubstitution *FileCheckPatternContext::makeNumericSubstitution( +Substitution *PatternContext::makeNumericSubstitution( StringRef ExpressionStr, - std::unique_ptr ExpressionAST, size_t InsertIdx) { - Substitutions.push_back(std::make_unique( - this, ExpressionStr, std::move(ExpressionAST), InsertIdx)); + std::unique_ptr ExpressionASTPointer, size_t InsertIdx) { + Substitutions.push_back(std::make_unique( + this, ExpressionStr, std::move(ExpressionASTPointer), InsertIdx)); return Substitutions.back().get(); } -size_t FileCheckPattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) { +size_t Pattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) { // Offset keeps track of the current offset within the input Str size_t Offset = 0; // [...] Nesting depth @@ -1115,7 +1101,7 @@ return {StringRef(), StringRef()}; } -void FileCheckPatternContext::createLineVariable() { +void PatternContext::createLineVariable() { assert(!LineVariable && "@LINE pseudo numeric variable already created"); StringRef LineName = "@LINE"; LineVariable = makeNumericVariable(LineName); @@ -1123,7 +1109,7 @@ } FileCheck::FileCheck(FileCheckRequest Req) - : Req(Req), PatternContext(std::make_unique()), + : Req(Req), PatternContextPtr(std::make_unique()), CheckStrings(std::make_unique>()) {} FileCheck::~FileCheck() = default; @@ -1131,15 +1117,15 @@ bool FileCheck::readCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE) { Error DefineError = - PatternContext->defineCmdlineVariables(Req.GlobalDefines, SM); + PatternContextPtr->defineCmdlineVariables(Req.GlobalDefines, SM); if (DefineError) { logAllUnhandledErrors(std::move(DefineError), errs()); return true; } - PatternContext->createLineVariable(); + PatternContextPtr->createLineVariable(); - std::vector ImplicitNegativeChecks; + std::vector ImplicitNegativeChecks; for (const auto &PatternString : Req.ImplicitCheckNot) { // Create a buffer with fake command line content in order to display the // command line option responsible for the specific implicit CHECK-NOT. @@ -1153,12 +1139,12 @@ SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc()); ImplicitNegativeChecks.push_back( - FileCheckPattern(Check::CheckNot, PatternContext.get())); + Pattern(Check::CheckNot, PatternContextPtr.get())); ImplicitNegativeChecks.back().parsePattern(PatternInBuffer, "IMPLICIT-CHECK", SM, Req); } - std::vector DagNotMatches = ImplicitNegativeChecks; + std::vector DagNotMatches = ImplicitNegativeChecks; // LineNumber keeps track of the line on which CheckPrefix instances are // found. @@ -1216,7 +1202,7 @@ SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data()); // Parse the pattern. - FileCheckPattern P(CheckTy, PatternContext.get(), LineNumber); + Pattern P(CheckTy, PatternContextPtr.get(), LineNumber); if (P.parsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, Req)) return true; @@ -1261,7 +1247,7 @@ // prefix as a filler for the error message. if (!DagNotMatches.empty()) { CheckStrings->emplace_back( - FileCheckPattern(Check::CheckEOF, PatternContext.get(), LineNumber + 1), + Pattern(Check::CheckEOF, PatternContextPtr.get(), LineNumber + 1), *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data())); std::swap(DagNotMatches, CheckStrings->back().DagNotStrings); } @@ -1286,7 +1272,7 @@ } static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM, - StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat, + StringRef Prefix, SMLoc Loc, const Pattern &Pat, int MatchedCount, StringRef Buffer, size_t MatchPos, size_t MatchLen, const FileCheckRequest &Req, std::vector *Diags) { @@ -1332,10 +1318,10 @@ } static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM, - StringRef Prefix, SMLoc Loc, - const FileCheckPattern &Pat, int MatchedCount, - StringRef Buffer, bool VerboseVerbose, - std::vector *Diags, Error MatchErrors) { + StringRef Prefix, SMLoc Loc, const Pattern &Pat, + int MatchedCount, StringRef Buffer, + bool VerboseVerbose, std::vector *Diags, + Error MatchErrors) { assert(MatchErrors && "Called on successful match"); bool PrintDiag = true; if (!ExpectedMatch) { @@ -1361,9 +1347,8 @@ return; } - MatchErrors = - handleErrors(std::move(MatchErrors), - [](const FileCheckErrorDiagnostic &E) { E.log(errs()); }); + MatchErrors = handleErrors(std::move(MatchErrors), + [](const ErrorDiagnostic &E) { E.log(errs()); }); // No problem matching the string per se. if (!MatchErrors) @@ -1427,7 +1412,7 @@ FileCheckRequest &Req, std::vector *Diags) const { size_t LastPos = 0; - std::vector NotStrings; + std::vector NotStrings; // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL // bounds; we have not processed variable definitions within the bounded block @@ -1565,11 +1550,11 @@ return false; } -bool FileCheckString::CheckNot( - const SourceMgr &SM, StringRef Buffer, - const std::vector &NotStrings, - const FileCheckRequest &Req, std::vector *Diags) const { - for (const FileCheckPattern *Pat : NotStrings) { +bool FileCheckString::CheckNot(const SourceMgr &SM, StringRef Buffer, + const std::vector &NotStrings, + const FileCheckRequest &Req, + std::vector *Diags) const { + for (const Pattern *Pat : NotStrings) { assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!"); size_t MatchLen = 0; @@ -1591,11 +1576,10 @@ return false; } -size_t -FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer, - std::vector &NotStrings, - const FileCheckRequest &Req, - std::vector *Diags) const { +size_t FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer, + std::vector &NotStrings, + const FileCheckRequest &Req, + std::vector *Diags) const { if (DagNotStrings.empty()) return 0; @@ -1615,7 +1599,7 @@ // group, so we don't use a range-based for loop here. for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end(); PatItr != PatEnd; ++PatItr) { - const FileCheckPattern &Pat = *PatItr; + const Pattern &Pat = *PatItr; assert((Pat.getCheckTy() == Check::CheckDAG || Pat.getCheckTy() == Check::CheckNot) && "Invalid CHECK-DAG or CHECK-NOT!"); @@ -1765,7 +1749,7 @@ return Regex(PrefixRegexStr); } -Error FileCheckPatternContext::defineCmdlineVariables( +Error PatternContext::defineCmdlineVariables( std::vector &CmdlineDefines, SourceMgr &SM) { assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() && "Overriding defined variable with command-line variable definitions"); @@ -1820,8 +1804,8 @@ if (CmdlineDef.empty()) { Errs = joinErrors( std::move(Errs), - FileCheckErrorDiagnostic::get( - SM, CmdlineDef, "missing equal sign in global definition")); + ErrorDiagnostic::get(SM, CmdlineDef, + "missing equal sign in global definition")); continue; } @@ -1830,21 +1814,21 @@ // Now parse the definition both to check that the syntax is correct and // to create the necessary class instance. StringRef CmdlineDefExpr = CmdlineDef.substr(1); - Optional DefinedNumericVariable; - Expected> ExpressionASTResult = - FileCheckPattern::parseNumericSubstitutionBlock( + Optional DefinedNumericVariable; + Expected> ExpressionASTResult = + Pattern::parseNumericSubstitutionBlock( CmdlineDefExpr, DefinedNumericVariable, false, None, this, SM); if (!ExpressionASTResult) { Errs = joinErrors(std::move(Errs), ExpressionASTResult.takeError()); continue; } - std::unique_ptr ExpressionAST = + std::unique_ptr ExpressionASTPointer = std::move(*ExpressionASTResult); // Now evaluate the expression whose value this variable should be set // to, since the expression of a command-line variable definition should // only use variables defined earlier on the command-line. If not, this // is an error and we report it. - Expected Value = ExpressionAST->eval(); + Expected Value = ExpressionASTPointer->eval(); if (!Value) { Errs = joinErrors(std::move(Errs), Value.takeError()); continue; @@ -1861,8 +1845,8 @@ std::pair CmdlineNameVal = CmdlineDef.split('='); StringRef CmdlineName = CmdlineNameVal.first; StringRef OrigCmdlineName = CmdlineName; - Expected ParseVarResult = - FileCheckPattern::parseVariable(CmdlineName, SM); + Expected ParseVarResult = + Pattern::parseVariable(CmdlineName, SM); if (!ParseVarResult) { Errs = joinErrors(std::move(Errs), ParseVarResult.takeError()); continue; @@ -1872,7 +1856,7 @@ // "FOO+2" in a "FOO+2=10" definition. if (ParseVarResult->IsPseudo || !CmdlineName.empty()) { Errs = joinErrors(std::move(Errs), - FileCheckErrorDiagnostic::get( + ErrorDiagnostic::get( SM, OrigCmdlineName, "invalid name in string variable definition '" + OrigCmdlineName + "'")); @@ -1884,8 +1868,8 @@ // is created later than the latter. if (GlobalNumericVariableTable.find(Name) != GlobalNumericVariableTable.end()) { - Errs = joinErrors(std::move(Errs), FileCheckErrorDiagnostic::get( - SM, Name, + Errs = joinErrors(std::move(Errs), + ErrorDiagnostic::get(SM, Name, "numeric variable with name '" + Name + "' already exists")); continue; @@ -1904,7 +1888,7 @@ return Errs; } -void FileCheckPatternContext::clearLocalVars() { +void PatternContext::clearLocalVars() { SmallVector LocalPatternVars, LocalNumericVars; for (const StringMapEntry &Var : GlobalVariableTable) if (Var.first()[0] != '$') @@ -1961,7 +1945,7 @@ // CHECK-LABEL and it would clear variables defined on the command-line // before they get used. if (i != 0 && Req.EnableVarScope) - PatternContext->clearLocalVars(); + PatternContextPtr->clearLocalVars(); for (; i != j; ++i) { const FileCheckString &CheckStr = (*CheckStrings)[i]; diff --git a/llvm/lib/Support/FileCheckImpl.h b/llvm/lib/Support/FileCheckImpl.h --- a/llvm/lib/Support/FileCheckImpl.h +++ b/llvm/lib/Support/FileCheckImpl.h @@ -31,9 +31,9 @@ //===----------------------------------------------------------------------===// /// Base class representing the AST of a given expression. -class FileCheckExpressionAST { +class ExpressionAST { public: - virtual ~FileCheckExpressionAST() = default; + virtual ~ExpressionAST() = default; /// Evaluates and \returns the value of the expression represented by this /// AST or an error if evaluation fails. @@ -41,14 +41,14 @@ }; /// Class representing an unsigned literal in the AST of an expression. -class FileCheckExpressionLiteral : public FileCheckExpressionAST { +class ExpressionLiteral : public ExpressionAST { private: /// Actual value of the literal. uint64_t Value; public: /// Constructs a literal with the specified value. - FileCheckExpressionLiteral(uint64_t Val) : Value(Val) {} + ExpressionLiteral(uint64_t Val) : Value(Val) {} /// \returns the literal's value. Expected eval() const override { return Value; } @@ -56,14 +56,14 @@ /// Class to represent an undefined variable error, which quotes that /// variable's name when printed. -class FileCheckUndefVarError : public ErrorInfo { +class UndefVarError : public ErrorInfo { private: StringRef VarName; public: static char ID; - FileCheckUndefVarError(StringRef VarName) : VarName(VarName) {} + UndefVarError(StringRef VarName) : VarName(VarName) {} StringRef getVarName() const { return VarName; } @@ -79,7 +79,7 @@ }; /// Class representing a numeric variable and its associated current value. -class FileCheckNumericVariable { +class NumericVariable { private: /// Name of the numeric variable. StringRef Name; @@ -95,8 +95,8 @@ public: /// Constructor for a variable \p Name defined at line \p DefLineNumber or /// defined before input is parsed if \p DefLineNumber is None. - explicit FileCheckNumericVariable(StringRef Name, - Optional DefLineNumber = None) + explicit NumericVariable(StringRef Name, + Optional DefLineNumber = None) : Name(Name), DefLineNumber(DefLineNumber) {} /// \returns name of this numeric variable. @@ -119,18 +119,17 @@ /// Class representing the use of a numeric variable in the AST of an /// expression. -class FileCheckNumericVariableUse : public FileCheckExpressionAST { +class NumericVariableUse : public ExpressionAST { private: /// Name of the numeric variable. StringRef Name; /// Pointer to the class instance for the variable this use is about. - FileCheckNumericVariable *NumericVariable; + NumericVariable *Variable; public: - FileCheckNumericVariableUse(StringRef Name, - FileCheckNumericVariable *NumericVariable) - : Name(Name), NumericVariable(NumericVariable) {} + NumericVariableUse(StringRef Name, NumericVariable *Variable) + : Name(Name), Variable(Variable) {} /// \returns the value of the variable referenced by this instance. Expected eval() const override; @@ -140,21 +139,20 @@ using binop_eval_t = uint64_t (*)(uint64_t, uint64_t); /// Class representing a single binary operation in the AST of an expression. -class FileCheckASTBinop : public FileCheckExpressionAST { +class BinaryOperation : public ExpressionAST { private: /// Left operand. - std::unique_ptr LeftOperand; + std::unique_ptr LeftOperand; /// Right operand. - std::unique_ptr RightOperand; + std::unique_ptr RightOperand; /// Pointer to function that can evaluate this binary operation. binop_eval_t EvalBinop; public: - FileCheckASTBinop(binop_eval_t EvalBinop, - std::unique_ptr LeftOp, - std::unique_ptr RightOp) + BinaryOperation(binop_eval_t EvalBinop, std::unique_ptr LeftOp, + std::unique_ptr RightOp) : EvalBinop(EvalBinop) { LeftOperand = std::move(LeftOp); RightOperand = std::move(RightOp); @@ -167,10 +165,10 @@ Expected eval() const override; }; -class FileCheckPatternContext; +class PatternContext; /// Class representing a substitution to perform in the RegExStr string. -class FileCheckSubstitution { +class Substitution { protected: /// Pointer to a class instance holding, among other things, the table with /// the values of live string variables at the start of any given CHECK line. @@ -178,7 +176,7 @@ /// as. Expressions are linked to the numeric variables they use at /// parse time and directly access the value of the numeric variable to /// evaluate their value. - FileCheckPatternContext *Context; + PatternContext *Context; /// The string that needs to be substituted for something else. For a /// string variable this is its name, otherwise this is the whole expression. @@ -188,11 +186,10 @@ size_t InsertIdx; public: - FileCheckSubstitution(FileCheckPatternContext *Context, StringRef VarName, - size_t InsertIdx) + Substitution(PatternContext *Context, StringRef VarName, size_t InsertIdx) : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {} - virtual ~FileCheckSubstitution() = default; + virtual ~Substitution() = default; /// \returns the string to be substituted for something else. StringRef getFromString() const { return FromStr; } @@ -205,29 +202,28 @@ virtual Expected getResult() const = 0; }; -class FileCheckStringSubstitution : public FileCheckSubstitution { +class StringSubstitution : public Substitution { public: - FileCheckStringSubstitution(FileCheckPatternContext *Context, - StringRef VarName, size_t InsertIdx) - : FileCheckSubstitution(Context, VarName, InsertIdx) {} + StringSubstitution(PatternContext *Context, StringRef VarName, + size_t InsertIdx) + : Substitution(Context, VarName, InsertIdx) {} /// \returns the text that the string variable in this substitution matched /// when defined, or an error if the variable is undefined. Expected getResult() const override; }; -class FileCheckNumericSubstitution : public FileCheckSubstitution { +class NumericSubstitution : public Substitution { private: /// Pointer to the class representing the expression whose value is to be /// substituted. - std::unique_ptr ExpressionAST; + std::unique_ptr ExpressionASTPointer; public: - FileCheckNumericSubstitution(FileCheckPatternContext *Context, StringRef Expr, - std::unique_ptr ExprAST, - size_t InsertIdx) - : FileCheckSubstitution(Context, Expr, InsertIdx) { - ExpressionAST = std::move(ExprAST); + NumericSubstitution(PatternContext *Context, StringRef Expr, + std::unique_ptr ExprAST, size_t InsertIdx) + : Substitution(Context, Expr, InsertIdx) { + ExpressionASTPointer = std::move(ExprAST); } /// \returns a string containing the result of evaluating the expression in @@ -241,11 +237,11 @@ struct FileCheckDiag; -/// 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. -class FileCheckPatternContext { - friend class FileCheckPattern; +/// Class holding the Pattern 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. +class PatternContext { + friend class Pattern; private: /// When matching a given pattern, this holds the value of all the string @@ -262,21 +258,20 @@ /// When matching a given pattern, this holds the pointers to the classes /// representing the numeric variables defined in previous patterns. When /// matching a pattern all definitions for that pattern are recorded in the - /// NumericVariableDefs table in the FileCheckPattern instance of that - /// pattern. - StringMap GlobalNumericVariableTable; + /// NumericVariableDefs table in the Pattern instance of that pattern. + StringMap GlobalNumericVariableTable; /// Pointer to the class instance representing the @LINE pseudo variable for /// easily updating its value. - FileCheckNumericVariable *LineVariable = nullptr; + NumericVariable *LineVariable = nullptr; /// Vector holding pointers to all parsed numeric variables. Used to /// automatically free them once they are guaranteed to no longer be used. - std::vector> NumericVariables; + std::vector> NumericVariables; /// Vector holding pointers to all substitutions. Used to automatically free /// them once they are guaranteed to no longer be used. - std::vector> Substitutions; + std::vector> Substitutions; public: /// \returns the value of string variable \p VarName or an error if no such @@ -303,32 +298,30 @@ private: /// Makes a new numeric variable and registers it for destruction when the /// context is destroyed. - template - FileCheckNumericVariable *makeNumericVariable(Types... args); + template NumericVariable *makeNumericVariable(Types... args); /// Makes a new string substitution and registers it for destruction when the /// context is destroyed. - FileCheckSubstitution *makeStringSubstitution(StringRef VarName, - size_t InsertIdx); + Substitution *makeStringSubstitution(StringRef VarName, size_t InsertIdx); /// Makes a new numeric substitution and registers it for destruction when /// the context is destroyed. - FileCheckSubstitution * + Substitution * makeNumericSubstitution(StringRef ExpressionStr, - std::unique_ptr ExpressionAST, + std::unique_ptr ExpressionAST, size_t InsertIdx); }; /// Class to represent an error holding a diagnostic with location information /// used when printing it. -class FileCheckErrorDiagnostic : public ErrorInfo { +class ErrorDiagnostic : public ErrorInfo { private: SMDiagnostic Diagnostic; public: static char ID; - FileCheckErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {} + ErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {} std::error_code convertToErrorCode() const override { return inconvertibleErrorCode(); @@ -338,7 +331,7 @@ void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); } static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg) { - return make_error( + return make_error( SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg)); } @@ -347,7 +340,7 @@ } }; -class FileCheckNotFoundError : public ErrorInfo { +class NotFoundError : public ErrorInfo { public: static char ID; @@ -361,7 +354,7 @@ } }; -class FileCheckPattern { +class Pattern { SMLoc PatternLoc; /// A fixed string to match as the pattern or empty if this pattern requires @@ -378,7 +371,7 @@ /// RegExStr will contain "foobaz" and we'll get two entries in this vector /// that tells us to insert the value of string variable "bar" at offset 3 /// and the value of expression "N+1" at offset 6. - std::vector Substitutions; + std::vector Substitutions; /// Maps names of string variables defined in a pattern to the number of /// their parenthesis group in RegExStr capturing their last definition. @@ -397,10 +390,10 @@ /// It holds the pointer to the class representing the numeric variable whose /// value is being defined and the number of the parenthesis group in /// RegExStr to capture that value. - struct FileCheckNumericVariableMatch { + struct NumericVariableMatch { /// Pointer to class representing the numeric variable whose value is being /// defined. - FileCheckNumericVariable *DefinedNumericVariable; + NumericVariable *DefinedNumericVariable; /// Number of the parenthesis group in RegExStr that captures the value of /// this numeric variable definition. @@ -408,10 +401,9 @@ }; /// Holds the number of the parenthesis group in RegExStr and pointer to the - /// corresponding FileCheckNumericVariable class instance of all numeric - /// variable definitions. Used to set the matched value of all those - /// variables. - StringMap NumericVariableDefs; + /// corresponding NumericVariable class instance of all numeric variable + /// definitions. Used to set the matched value of all those variables. + StringMap NumericVariableDefs; /// Pointer to a class instance holding the global state shared by all /// patterns: @@ -419,7 +411,7 @@ /// respectively at the start of any given CHECK line; /// - table holding whether a string variable has been defined at any given /// point during the parsing phase. - FileCheckPatternContext *Context; + PatternContext *Context; Check::FileCheckType CheckTy; @@ -432,8 +424,8 @@ bool IgnoreCase = false; public: - FileCheckPattern(Check::FileCheckType Ty, FileCheckPatternContext *Context, - Optional Line = None) + Pattern(Check::FileCheckType Ty, PatternContext *Context, + Optional Line = None) : Context(Context), CheckTy(Ty), LineNumber(Line) {} /// \returns the location in source code. @@ -441,7 +433,7 @@ /// \returns the pointer to the global state for all patterns in this /// FileCheck instance. - FileCheckPatternContext *getContext() const { return Context; } + PatternContext *getContext() const { return Context; } /// \returns whether \p C is a valid first character for a variable name. static bool isValidVarNameStart(char C); @@ -469,14 +461,12 @@ /// substitution was successful, sets \p DefinedNumericVariable to point to /// the class representing the numeric variable defined in this numeric /// substitution block, or None if this block does not define any variable. - static Expected> - parseNumericSubstitutionBlock( - StringRef Expr, - Optional &DefinedNumericVariable, + static Expected> parseNumericSubstitutionBlock( + StringRef Expr, Optional &DefinedNumericVariable, bool IsLegacyLineExpr, Optional LineNumber, - FileCheckPatternContext *Context, const SourceMgr &SM); - /// Parses the pattern in \p PatternStr and initializes this FileCheckPattern - /// instance accordingly. + PatternContext *Context, const SourceMgr &SM); + /// Parses the pattern in \p PatternStr and initializes this Pattern instance + /// accordingly. /// /// \p Prefix provides which prefix is being matched, \p Req describes the /// global options that influence the parsing such as whitespace @@ -490,9 +480,9 @@ /// failed. If there is a match, updates \p MatchLen with the size of the /// matched string. /// - /// The GlobalVariableTable StringMap in the FileCheckPatternContext class - /// instance provides the current values of FileCheck string variables and - /// is updated if this match defines new values. Likewise, the + /// The GlobalVariableTable StringMap in the PatternContext class instance + /// provides the current values of FileCheck string variables and is 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. @@ -533,19 +523,19 @@ /// \returns a pointer to the class instance representing that variable, /// creating it if needed, or an error holding a diagnostic against \p SM /// should defining such a variable be invalid. - static Expected parseNumericVariableDefinition( - StringRef &Expr, FileCheckPatternContext *Context, - Optional LineNumber, const SourceMgr &SM); + static Expected + parseNumericVariableDefinition(StringRef &Expr, PatternContext *Context, + Optional LineNumber, + const SourceMgr &SM); /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use /// at line \p LineNumber, or before input is parsed if \p LineNumber is /// None. Parameter \p Context points to the class instance holding the live /// string and numeric variables. \returns the pointer to the class instance /// representing that variable if successful, or an error holding a /// diagnostic against \p SM otherwise. - static Expected> + static Expected> parseNumericVariableUse(StringRef Name, bool IsPseudo, - Optional LineNumber, - FileCheckPatternContext *Context, + Optional LineNumber, PatternContext *Context, const SourceMgr &SM); enum class AllowedOperand { LineVar, Literal, Any }; /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or @@ -555,10 +545,10 @@ /// numeric variables. \returns the class representing that operand in the /// AST of the expression or an error holding a diagnostic against \p SM /// otherwise. - static Expected> + static Expected> parseNumericOperand(StringRef &Expr, AllowedOperand AO, - Optional LineNumber, - FileCheckPatternContext *Context, const SourceMgr &SM); + Optional LineNumber, PatternContext *Context, + const SourceMgr &SM); /// Parses \p Expr for a binary operation at line \p LineNumber, or before /// input is parsed if \p LineNumber is None. The left operand of this binary /// operation is given in \p LeftOp and \p IsLegacyLineExpr indicates whether @@ -566,10 +556,10 @@ /// the class instance holding the live string and numeric variables. /// \returns the class representing the binary operation in the AST of the /// expression, or an error holding a diagnostic against \p SM otherwise. - static Expected> - parseBinop(StringRef &Expr, std::unique_ptr LeftOp, + static Expected> + parseBinop(StringRef &Expr, std::unique_ptr LeftOp, bool IsLegacyLineExpr, Optional LineNumber, - FileCheckPatternContext *Context, const SourceMgr &SM); + PatternContext *Context, const SourceMgr &SM); }; //===----------------------------------------------------------------------===// @@ -579,7 +569,7 @@ /// A check that we found in the input file. struct FileCheckString { /// The pattern to match. - FileCheckPattern Pat; + Pattern Pat; /// Which prefix name this check matched. StringRef Prefix; @@ -589,9 +579,9 @@ /// All of the strings that are disallowed from occurring between this match /// string and the previous one (or start of file). - std::vector DagNotStrings; + std::vector DagNotStrings; - FileCheckString(const FileCheckPattern &P, StringRef S, SMLoc L) + FileCheckString(const Pattern &P, StringRef S, SMLoc L) : Pat(P), Prefix(S), Loc(L) {} /// Matches check string and its "not strings" and/or "dag strings". @@ -609,12 +599,12 @@ /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in /// \p Diags according to the verbosity level set in \p Req. bool CheckNot(const SourceMgr &SM, StringRef Buffer, - const std::vector &NotStrings, + const std::vector &NotStrings, const FileCheckRequest &Req, std::vector *Diags) const; /// Matches "dag strings" and their mixed "not strings". size_t CheckDag(const SourceMgr &SM, StringRef Buffer, - std::vector &NotStrings, + std::vector &NotStrings, const FileCheckRequest &Req, std::vector *Diags) const; }; 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 @@ -19,13 +19,13 @@ TEST_F(FileCheckTest, Literal) { // Eval returns the literal's value. - FileCheckExpressionLiteral Ten(10); + ExpressionLiteral Ten(10); Expected Value = Ten.eval(); ASSERT_TRUE(bool(Value)); EXPECT_EQ(10U, *Value); // Max value can be correctly represented. - FileCheckExpressionLiteral Max(std::numeric_limits::max()); + ExpressionLiteral Max(std::numeric_limits::max()); Value = Max.eval(); ASSERT_TRUE(bool(Value)); EXPECT_EQ(std::numeric_limits::max(), *Value); @@ -45,14 +45,14 @@ static void expectUndefErrors(std::unordered_set ExpectedUndefVarNames, Error Err) { - handleAllErrors(std::move(Err), [&](const FileCheckUndefVarError &E) { + handleAllErrors(std::move(Err), [&](const UndefVarError &E) { ExpectedUndefVarNames.erase(E.getVarName()); }); EXPECT_TRUE(ExpectedUndefVarNames.empty()) << toString(ExpectedUndefVarNames); } -// Return whether Err contains any FileCheckUndefVarError whose associated name -// is not ExpectedUndefVarName. +// Return whether Err contains any UndefVarError whose associated name is not +// ExpectedUndefVarName. static void expectUndefError(const Twine &ExpectedUndefVarName, Error Err) { expectUndefErrors({ExpectedUndefVarName.str()}, std::move(Err)); } @@ -62,9 +62,9 @@ TEST_F(FileCheckTest, NumericVariable) { // Undefined variable: getValue and eval fail, error returned by eval holds // the name of the undefined variable. - FileCheckNumericVariable FooVar("FOO", 1); + NumericVariable FooVar("FOO", 1); EXPECT_EQ("FOO", FooVar.getName()); - FileCheckNumericVariableUse FooVarUse("FOO", &FooVar); + NumericVariableUse FooVarUse("FOO", &FooVar); EXPECT_FALSE(FooVar.getValue()); Expected EvalResult = FooVarUse.eval(); ASSERT_FALSE(EvalResult); @@ -90,15 +90,15 @@ } TEST_F(FileCheckTest, Binop) { - FileCheckNumericVariable FooVar("FOO", 1); + NumericVariable FooVar("FOO", 1); FooVar.setValue(42); - std::unique_ptr FooVarUse = - std::make_unique("FOO", &FooVar); - FileCheckNumericVariable BarVar("BAR", 2); + std::unique_ptr FooVarUse = + std::make_unique("FOO", &FooVar); + NumericVariable BarVar("BAR", 2); BarVar.setValue(18); - std::unique_ptr BarVarUse = - std::make_unique("BAR", &BarVar); - FileCheckASTBinop Binop(doAdd, std::move(FooVarUse), std::move(BarVarUse)); + std::unique_ptr BarVarUse = + std::make_unique("BAR", &BarVar); + BinaryOperation Binop(doAdd, std::move(FooVarUse), std::move(BarVarUse)); // Defined variable: eval returns right value. Expected Value = Binop.eval(); @@ -121,15 +121,15 @@ } TEST_F(FileCheckTest, ValidVarNameStart) { - EXPECT_TRUE(FileCheckPattern::isValidVarNameStart('a')); - EXPECT_TRUE(FileCheckPattern::isValidVarNameStart('G')); - EXPECT_TRUE(FileCheckPattern::isValidVarNameStart('_')); - EXPECT_FALSE(FileCheckPattern::isValidVarNameStart('2')); - EXPECT_FALSE(FileCheckPattern::isValidVarNameStart('$')); - EXPECT_FALSE(FileCheckPattern::isValidVarNameStart('@')); - EXPECT_FALSE(FileCheckPattern::isValidVarNameStart('+')); - EXPECT_FALSE(FileCheckPattern::isValidVarNameStart('-')); - EXPECT_FALSE(FileCheckPattern::isValidVarNameStart(':')); + EXPECT_TRUE(Pattern::isValidVarNameStart('a')); + EXPECT_TRUE(Pattern::isValidVarNameStart('G')); + EXPECT_TRUE(Pattern::isValidVarNameStart('_')); + EXPECT_FALSE(Pattern::isValidVarNameStart('2')); + EXPECT_FALSE(Pattern::isValidVarNameStart('$')); + EXPECT_FALSE(Pattern::isValidVarNameStart('@')); + EXPECT_FALSE(Pattern::isValidVarNameStart('+')); + EXPECT_FALSE(Pattern::isValidVarNameStart('-')); + EXPECT_FALSE(Pattern::isValidVarNameStart(':')); } static StringRef bufferize(SourceMgr &SM, StringRef Str) { @@ -144,65 +144,65 @@ SourceMgr SM; StringRef OrigVarName = bufferize(SM, "GoodVar42"); StringRef VarName = OrigVarName; - Expected ParsedVarResult = - FileCheckPattern::parseVariable(VarName, SM); + Expected ParsedVarResult = + Pattern::parseVariable(VarName, SM); ASSERT_TRUE(bool(ParsedVarResult)); EXPECT_EQ(ParsedVarResult->Name, OrigVarName); EXPECT_TRUE(VarName.empty()); EXPECT_FALSE(ParsedVarResult->IsPseudo); VarName = OrigVarName = bufferize(SM, "$GoodGlobalVar"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); ASSERT_TRUE(bool(ParsedVarResult)); EXPECT_EQ(ParsedVarResult->Name, OrigVarName); EXPECT_TRUE(VarName.empty()); EXPECT_FALSE(ParsedVarResult->IsPseudo); VarName = OrigVarName = bufferize(SM, "@GoodPseudoVar"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); ASSERT_TRUE(bool(ParsedVarResult)); EXPECT_EQ(ParsedVarResult->Name, OrigVarName); EXPECT_TRUE(VarName.empty()); EXPECT_TRUE(ParsedVarResult->IsPseudo); VarName = bufferize(SM, "42BadVar"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); EXPECT_TRUE(errorToBool(ParsedVarResult.takeError())); VarName = bufferize(SM, "$@"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); EXPECT_TRUE(errorToBool(ParsedVarResult.takeError())); VarName = OrigVarName = bufferize(SM, "B@dVar"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); ASSERT_TRUE(bool(ParsedVarResult)); EXPECT_EQ(VarName, OrigVarName.substr(1)); EXPECT_EQ(ParsedVarResult->Name, "B"); EXPECT_FALSE(ParsedVarResult->IsPseudo); VarName = OrigVarName = bufferize(SM, "B$dVar"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); ASSERT_TRUE(bool(ParsedVarResult)); EXPECT_EQ(VarName, OrigVarName.substr(1)); EXPECT_EQ(ParsedVarResult->Name, "B"); EXPECT_FALSE(ParsedVarResult->IsPseudo); VarName = bufferize(SM, "BadVar+"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); ASSERT_TRUE(bool(ParsedVarResult)); EXPECT_EQ(VarName, "+"); EXPECT_EQ(ParsedVarResult->Name, "BadVar"); EXPECT_FALSE(ParsedVarResult->IsPseudo); VarName = bufferize(SM, "BadVar-"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); ASSERT_TRUE(bool(ParsedVarResult)); EXPECT_EQ(VarName, "-"); EXPECT_EQ(ParsedVarResult->Name, "BadVar"); EXPECT_FALSE(ParsedVarResult->IsPseudo); VarName = bufferize(SM, "BadVar:"); - ParsedVarResult = FileCheckPattern::parseVariable(VarName, SM); + ParsedVarResult = Pattern::parseVariable(VarName, SM); ASSERT_TRUE(bool(ParsedVarResult)); EXPECT_EQ(VarName, ":"); EXPECT_EQ(ParsedVarResult->Name, "BadVar"); @@ -214,8 +214,8 @@ size_t LineNumber = 1; SourceMgr SM; FileCheckRequest Req; - FileCheckPatternContext Context; - FileCheckPattern P{Check::CheckPlain, &Context, LineNumber++}; + PatternContext Context; + Pattern P{Check::CheckPlain, &Context, LineNumber++}; public: PatternTester() { @@ -236,12 +236,12 @@ } void initNextPattern() { - P = FileCheckPattern(Check::CheckPlain, &Context, LineNumber++); + P = Pattern(Check::CheckPlain, &Context, LineNumber++); } bool parseSubstExpect(StringRef Expr) { StringRef ExprBufferRef = bufferize(SM, Expr); - Optional DefinedNumericVariable; + Optional DefinedNumericVariable; return errorToBool( P.parseNumericSubstitutionBlock(ExprBufferRef, DefinedNumericVariable, false, LineNumber - 1, &Context, SM) @@ -399,31 +399,29 @@ TEST_F(FileCheckTest, Substitution) { SourceMgr SM; - FileCheckPatternContext Context; + PatternContext Context; std::vector GlobalDefines; GlobalDefines.emplace_back(std::string("FOO=BAR")); EXPECT_FALSE(errorToBool(Context.defineCmdlineVariables(GlobalDefines, SM))); // Substitution of an undefined string variable fails and error holds that // variable's name. - FileCheckStringSubstitution StringSubstitution(&Context, "VAR404", 42); + StringSubstitution StringSubstitution(&Context, "VAR404", 42); Expected SubstValue = StringSubstitution.getResult(); ASSERT_FALSE(bool(SubstValue)); expectUndefError("VAR404", SubstValue.takeError()); // Substitutions of defined pseudo and non-pseudo numeric variables return // the right value. - FileCheckNumericVariable LineVar("@LINE", 1); - FileCheckNumericVariable NVar("N", 1); + NumericVariable LineVar("@LINE", 1); + NumericVariable NVar("N", 1); LineVar.setValue(42); NVar.setValue(10); - auto LineVarUse = - std::make_unique("@LINE", &LineVar); - auto NVarUse = std::make_unique("N", &NVar); - FileCheckNumericSubstitution SubstitutionLine(&Context, "@LINE", - std::move(LineVarUse), 12); - FileCheckNumericSubstitution SubstitutionN(&Context, "N", std::move(NVarUse), - 30); + auto LineVarUse = std::make_unique("@LINE", &LineVar); + auto NVarUse = std::make_unique("N", &NVar); + NumericSubstitution SubstitutionLine(&Context, "@LINE", std::move(LineVarUse), + 12); + NumericSubstitution SubstitutionN(&Context, "N", std::move(NVarUse), 30); SubstValue = SubstitutionLine.getResult(); ASSERT_TRUE(bool(SubstValue)); EXPECT_EQ("42", *SubstValue); @@ -443,15 +441,15 @@ expectUndefError("N", SubstValue.takeError()); // Substitution of a defined string variable returns the right value. - FileCheckPattern P(Check::CheckPlain, &Context, 1); - StringSubstitution = FileCheckStringSubstitution(&Context, "FOO", 42); + Pattern P(Check::CheckPlain, &Context, 1); + StringSubstitution = llvm::StringSubstitution(&Context, "FOO", 42); SubstValue = StringSubstitution.getResult(); ASSERT_TRUE(bool(SubstValue)); EXPECT_EQ("BAR", *SubstValue); } TEST_F(FileCheckTest, FileCheckContext) { - FileCheckPatternContext Cxt; + PatternContext Cxt; std::vector GlobalDefines; SourceMgr SM; @@ -483,12 +481,12 @@ GlobalDefines.emplace_back(std::string("LocalVar=18")); GlobalDefines.emplace_back(std::string("#LocalVar=36")); EXPECT_TRUE(errorToBool(Cxt.defineCmdlineVariables(GlobalDefines, SM))); - Cxt = FileCheckPatternContext(); + Cxt = PatternContext(); GlobalDefines.clear(); GlobalDefines.emplace_back(std::string("#LocalNumVar=18")); GlobalDefines.emplace_back(std::string("LocalNumVar=36")); EXPECT_TRUE(errorToBool(Cxt.defineCmdlineVariables(GlobalDefines, SM))); - Cxt = FileCheckPatternContext(); + Cxt = PatternContext(); // Invalid numeric value for numeric variable. GlobalDefines.clear(); @@ -514,9 +512,9 @@ StringRef EmptyVarStr = "EmptyVar"; StringRef UnknownVarStr = "UnknownVar"; Expected LocalVar = Cxt.getPatternVarValue(LocalVarStr); - FileCheckPattern P(Check::CheckPlain, &Cxt, 1); - Optional DefinedNumericVariable; - Expected> ExpressionAST = + Pattern P(Check::CheckPlain, &Cxt, 1); + Optional DefinedNumericVariable; + Expected> ExpressionASTPointer = P.parseNumericSubstitutionBlock(LocalNumVar1Ref, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, /*LineNumber=*/1, &Cxt, SM); @@ -524,16 +522,16 @@ EXPECT_EQ(*LocalVar, "FOO"); Expected EmptyVar = Cxt.getPatternVarValue(EmptyVarStr); Expected UnknownVar = Cxt.getPatternVarValue(UnknownVarStr); - ASSERT_TRUE(bool(ExpressionAST)); - Expected ExpressionVal = (*ExpressionAST)->eval(); + ASSERT_TRUE(bool(ExpressionASTPointer)); + Expected ExpressionVal = (*ExpressionASTPointer)->eval(); ASSERT_TRUE(bool(ExpressionVal)); EXPECT_EQ(*ExpressionVal, 18U); - ExpressionAST = + ExpressionASTPointer = P.parseNumericSubstitutionBlock(LocalNumVar2Ref, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, /*LineNumber=*/1, &Cxt, SM); - ASSERT_TRUE(bool(ExpressionAST)); - ExpressionVal = (*ExpressionAST)->eval(); + ASSERT_TRUE(bool(ExpressionASTPointer)); + ExpressionVal = (*ExpressionASTPointer)->eval(); ASSERT_TRUE(bool(ExpressionVal)); EXPECT_EQ(*ExpressionVal, 20U); ASSERT_TRUE(bool(EmptyVar)); @@ -548,19 +546,19 @@ // local variables, if it was created before. This is important because local // variable clearing due to --enable-var-scope happens after numeric // expressions are linked to the numeric variables they use. - EXPECT_TRUE(errorToBool((*ExpressionAST)->eval().takeError())); - P = FileCheckPattern(Check::CheckPlain, &Cxt, 2); - ExpressionAST = P.parseNumericSubstitutionBlock( + EXPECT_TRUE(errorToBool((*ExpressionASTPointer)->eval().takeError())); + P = Pattern(Check::CheckPlain, &Cxt, 2); + ExpressionASTPointer = P.parseNumericSubstitutionBlock( LocalNumVar1Ref, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, /*LineNumber=*/2, &Cxt, SM); - ASSERT_TRUE(bool(ExpressionAST)); - ExpressionVal = (*ExpressionAST)->eval(); + ASSERT_TRUE(bool(ExpressionASTPointer)); + ExpressionVal = (*ExpressionASTPointer)->eval(); EXPECT_TRUE(errorToBool(ExpressionVal.takeError())); - ExpressionAST = P.parseNumericSubstitutionBlock( + ExpressionASTPointer = P.parseNumericSubstitutionBlock( LocalNumVar2Ref, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, /*LineNumber=*/2, &Cxt, SM); - ASSERT_TRUE(bool(ExpressionAST)); - ExpressionVal = (*ExpressionAST)->eval(); + ASSERT_TRUE(bool(ExpressionASTPointer)); + ExpressionVal = (*ExpressionASTPointer)->eval(); EXPECT_TRUE(errorToBool(ExpressionVal.takeError())); EmptyVar = Cxt.getPatternVarValue(EmptyVarStr); EXPECT_TRUE(errorToBool(EmptyVar.takeError())); @@ -577,24 +575,24 @@ Expected GlobalVar = Cxt.getPatternVarValue(GlobalVarStr); ASSERT_TRUE(bool(GlobalVar)); EXPECT_EQ(*GlobalVar, "BAR"); - P = FileCheckPattern(Check::CheckPlain, &Cxt, 3); - ExpressionAST = P.parseNumericSubstitutionBlock( + P = Pattern(Check::CheckPlain, &Cxt, 3); + ExpressionASTPointer = P.parseNumericSubstitutionBlock( GlobalNumVarRef, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, /*LineNumber=*/3, &Cxt, SM); - ASSERT_TRUE(bool(ExpressionAST)); - ExpressionVal = (*ExpressionAST)->eval(); + ASSERT_TRUE(bool(ExpressionASTPointer)); + ExpressionVal = (*ExpressionASTPointer)->eval(); ASSERT_TRUE(bool(ExpressionVal)); EXPECT_EQ(*ExpressionVal, 36U); // Clear local variables and check global variables remain defined. Cxt.clearLocalVars(); EXPECT_FALSE(errorToBool(Cxt.getPatternVarValue(GlobalVarStr).takeError())); - P = FileCheckPattern(Check::CheckPlain, &Cxt, 4); - ExpressionAST = P.parseNumericSubstitutionBlock( + P = Pattern(Check::CheckPlain, &Cxt, 4); + ExpressionASTPointer = P.parseNumericSubstitutionBlock( GlobalNumVarRef, DefinedNumericVariable, /*IsLegacyLineExpr=*/false, /*LineNumber=*/4, &Cxt, SM); - ASSERT_TRUE(bool(ExpressionAST)); - ExpressionVal = (*ExpressionAST)->eval(); + ASSERT_TRUE(bool(ExpressionASTPointer)); + ExpressionVal = (*ExpressionASTPointer)->eval(); ASSERT_TRUE(bool(ExpressionVal)); EXPECT_EQ(*ExpressionVal, 36U); }