Lines Matching +full:to +full:- +full:regex +full:- +full:range
1 //===- FileCheck.cpp - Check that File's Contents match what is expected --===//
8 //===----------------------------------------------------------------------===//
10 // FileCheck does a line-by line check of a file that validates whether it
17 //===----------------------------------------------------------------------===//
26 #include "llvm/Support/Regex.h"
39 CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
42 InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
43 cl::init("-"), cl::value_desc("filename"));
46 CheckPrefixes("check-prefix",
47 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
49 "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated,
52 "Alias for -check-prefix permitting multiple comma separated values"));
55 NoCanonicalizeWhiteSpace("strict-whitespace",
59 "implicit-check-not",
60 cl::desc("Add an implicit negative check with this pattern to every\n"
61 "positive check. This can be used to ensure that no instances of\n"
66 "allow-empty", cl::init(false),
67 cl::desc("Allow the input file to be empty. This is useful when making\n"
71 "match-full-lines", cl::init(false),
72 cl::desc("Require all positive matches to cover an entire input line.\n"
73 "Allows leading and trailing whitespace if --strict-whitespace\n"
78 //===----------------------------------------------------------------------===//
80 //===----------------------------------------------------------------------===//
92 /// MatchEOF - When set, this pattern only matches the end of file. This is
93 /// used for trailing CHECK-NOTs.
95 /// CheckBadNot - Found -NOT combined with another CHECK suffix.
105 /// FixedStr - If non-empty, this pattern is a fixed string match with the
109 /// RegEx - If non-empty, this is a regex pattern.
115 /// VariableUses - Entries in this vector map to uses of a variable in the
117 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
121 /// VariableDefs - Maps definitions of variables to their parenthesized
123 /// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to 1.
131 /// getLoc - Return the location in source code.
134 /// ParsePattern - Parse the given string into the Pattern. Prefix provides
144 /// Match - Match the pattern string against the input buffer Buffer. This
153 /// PrintFailureInfo - Print additional information about a failure to match
167 /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
169 /// should correspond to a perfect match.
173 /// \brief Evaluates expression and stores the result to \p Value.
177 /// \brief Finds the closing sequence of a regex variable usage or
178 /// definition. Str has to point in the beginning of the definition
192 this->LineNumber = LineNumber; in ParsePattern()
198 PatternStr = PatternStr.substr(0, PatternStr.size()-1); in ParsePattern()
208 // Check to see if this is a fixed string, or if it has regex pieces. in ParsePattern()
226 // Otherwise, there is at least one regex piece. Build up the regex pattern in ParsePattern()
227 // by escaping scary characters in fixed strings, building up one big regex. in ParsePattern()
229 // RegEx matches. in ParsePattern()
231 // This is the start of a regex match. Scan for the }}. in ParsePattern()
236 "found start of regex string with no end '}}'"); in ParsePattern()
243 // want this to turn into: "abc(x|z)def" not "abcx|zdef". in ParsePattern()
247 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM)) in ParsePattern()
255 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .* in ParsePattern()
256 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The in ParsePattern()
257 // second form is [[foo]] which is a reference to foo. The variable name in ParsePattern()
258 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject in ParsePattern()
259 // it. This is to catch some common errors. in ParsePattern()
261 // Find the closing bracket pair ending the match. End is going to be an in ParsePattern()
262 // offset relative to the beginning of the match string. in ParsePattern()
268 "invalid named regex reference, no ]] found"); in ParsePattern()
275 // Get the regex name (e.g. "foo"). in ParsePattern()
281 "invalid name in named regex: empty name"); in ParsePattern()
286 // supports @LINE, @LINE+number, @LINE-number expressions. The check here in ParsePattern()
294 "invalid name in named regex definition"); in ParsePattern()
301 (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) { in ParsePattern()
303 SourceMgr::DK_Error, "invalid name in named regex"); in ParsePattern()
311 "invalid name in named regex"); in ParsePattern()
324 "Can't back-reference more than 9 variables"); in ParsePattern()
346 // Find the end, which is the start of the next regex. in ParsePattern()
349 RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd)); in ParsePattern()
364 Regex R(RS); in AddRegExToRegEx()
368 "invalid regex: " + Error); in AddRegExToRegEx()
385 // The only supported expression is @LINE([\+-]\d+)? in EvaluateExpression()
393 else if (Expr[0] != '-') in EvaluateExpression()
402 /// Match - Match the pattern string against the input buffer Buffer. This
419 // Regex match. in Match()
421 // If there are variable uses, we need to create a temporary string with the in Match()
442 // Look up the value and escape it so that we can put it into the regex. in Match()
443 Value += Regex::escape(it->second); in Match()
446 // Plop it into the regex at the adjusted offset. in Match()
452 // Match the newly constructed regex. in Match()
458 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo)) in Match()
461 // Successful regex match. in Match()
472 return FullMatch.data()-Buffer.data(); in Match()
478 // just compare against the regex itself and hope for the best. in ComputeMatchDistance()
480 // FIXME: One easy improvement here is have the regex lib generate a single in ComputeMatchDistance()
487 // Only compare up to the first line in the buffer, or the string size. in ComputeMatchDistance()
506 OS.write_escaped(Var) << "\" equal to \""; in PrintFailureInfo()
521 OS.write_escaped(Var) << "\" equal to \""; in PrintFailureInfo()
522 OS.write_escaped(it->second) << "\""; in PrintFailureInfo()
531 // Attempt to find the closest/best fuzzy match. Usually an error happens in PrintFailureInfo()
533 // would like to show the user a best guess at what "should have" matched, to in PrintFailureInfo()
534 // save them having to actually check the input manually. in PrintFailureInfo()
550 // match distance and the number of lines skipped to get to this match. in PrintFailureInfo()
561 // reasonable and not equal to what we showed in the "scanning from here" in PrintFailureInfo()
567 // FIXME: If we wanted to be really friendly we would show why the match in PrintFailureInfo()
568 // failed, as it can be hard to spot simple one character differences. in PrintFailureInfo()
596 "missing closing \"]\" for regex variable"); in FindRegexVarEnd()
599 BracketDepth--; in FindRegexVarEnd()
611 //===----------------------------------------------------------------------===//
613 //===----------------------------------------------------------------------===//
615 /// CheckString - This is a check that we found in the input file.
617 /// Pat - The pattern to match.
620 /// Prefix - Which prefix name this check matched.
623 /// Loc - The location in the match file that the check string was specified.
626 /// CheckTy - Specify what kind of check this is. e.g. CHECK-NEXT: directive,
627 /// as opposed to a CHECK: directive.
630 /// DagNotStrings - These are all of the strings that are disallowed from
638 /// Check - Match check string and its "not strings" and/or "dag strings".
642 /// CheckNext - Verify there is a single line in the given buffer.
645 /// CheckSame - Verify there is no newline in the given buffer.
648 /// CheckNot - Verify there's no "not strings" in the given buffer.
653 /// CheckDag - Match "dag strings" and their mixed "not strings".
660 /// with UNIX-style '\n'.
663 /// characters to a single space.
668 NewFile.reserve(MB->getBufferSize()); in CanonicalizeInputFile()
670 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); in CanonicalizeInputFile()
673 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') { in CanonicalizeInputFile()
678 // whitespace canonicalization is disabled, dump it to output as is. in CanonicalizeInputFile()
692 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier())); in CanonicalizeInputFile()
696 return (isalnum(c) || c == '-' || c == '_'); in IsPartOfWord()
707 return sizeof(":") - 1; in CheckTypeSize()
710 return sizeof("-NEXT:") - 1; in CheckTypeSize()
713 return sizeof("-SAME:") - 1; in CheckTypeSize()
716 return sizeof("-NOT:") - 1; in CheckTypeSize()
719 return sizeof("-DAG:") - 1; in CheckTypeSize()
722 return sizeof("-LABEL:") - 1; in CheckTypeSize()
738 if (NextChar != '-') in FindCheckType()
757 // You can't combine -NOT with another suffix. in FindCheckType()
758 if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") || in FindCheckType()
759 Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") || in FindCheckType()
760 Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:")) in FindCheckType()
773 // Try to find the first match in buffer for any prefix. If a valid match is
776 // string), but no valid match, return an empty string and set the position to
780 // prefixes then AA-CHECK: should match the second one.
799 // We need to only advance to the first partial match on the next attempt in FindFirstCandidateMatch()
801 // Need to skip to the end of the word, otherwise we could end up in FindFirstCandidateMatch()
806 // We only want to find the first match to avoid skipping some. in FindFirstCandidateMatch()
809 // If one matching check-prefix is a prefix of another, choose the in FindFirstCandidateMatch()
818 if (PrefixLoc != 0 && IsPartOfWord(Buffer[PrefixLoc - 1])) in FindFirstCandidateMatch()
856 // Advance to the last possible match we found and try again. in FindFirstMatchingPrefix()
863 /// ReadCheckFile - Read the check file, which specifies the sequence of
864 /// expected strings. The strings are added to the CheckStrings vector.
876 // If we want to canonicalize whitespace, strip excess whitespace from the in ReadCheckFile()
882 StringRef Buffer = F->getBuffer(); in ReadCheckFile()
888 // Create a buffer with fake command line content in order to display the in ReadCheckFile()
889 // command line option responsible for the specific implicit CHECK-NOT. in ReadCheckFile()
890 std::string Prefix = (Twine("-") + ImplicitCheckNot.ArgStr + "='").str(); in ReadCheckFile()
896 CmdLine->getBuffer().substr(Prefix.size(), PatternString.size()); in ReadCheckFile()
901 "IMPLICIT-CHECK", SM, 0); in ReadCheckFile()
925 // Location to use for error messages. in ReadCheckFile()
928 // PrefixLoc is to the start of the prefix. Skip to the end. in ReadCheckFile()
931 // Complain about useful-looking but unsupported suffixes. in ReadCheckFile()
935 "unsupported -NOT combo on prefix '" + UsedPrefix + "'"); in ReadCheckFile()
943 // Scan ahead to the end of line. in ReadCheckFile()
954 // Verify that CHECK-LABEL lines do not define or use variables in ReadCheckFile()
958 "found '" + UsedPrefix + "-LABEL:'" in ReadCheckFile()
965 // Verify that CHECK-NEXT lines have at least one CHECK line before them. in ReadCheckFile()
971 "found '" + UsedPrefix + "-" + Type + "' without previous '" in ReadCheckFile()
976 // Handle CHECK-DAG/-NOT. in ReadCheckFile()
982 // Okay, add the string we captured to the output vector and move on. in ReadCheckFile()
988 // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first in ReadCheckFile()
1023 // end of a line, advance to the start of the next line. in PrintCheckFailed()
1029 // Allow the pattern to print additional information if desired. in PrintCheckFailed()
1039 /// CountNumNewlinesBetween - Count the number of newlines in the specified
1040 /// range.
1041 static unsigned CountNumNewlinesBetween(StringRef Range, in CountNumNewlinesBetween() argument
1046 Range = Range.substr(Range.find_first_of("\n\r")); in CountNumNewlinesBetween()
1047 if (Range.empty()) return NumNewLines; in CountNumNewlinesBetween()
1052 if (Range.size() > 1 && in CountNumNewlinesBetween()
1053 (Range[1] == '\n' || Range[1] == '\r') && in CountNumNewlinesBetween()
1054 (Range[0] != Range[1])) in CountNumNewlinesBetween()
1055 Range = Range.substr(1); in CountNumNewlinesBetween()
1056 Range = Range.substr(1); in CountNumNewlinesBetween()
1059 FirstNewLine = Range.begin(); in CountNumNewlinesBetween()
1069 // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL in Check()
1071 // yet so cannot handle any final CHECK-DAG yet; this is handled when going in Check()
1072 // over the block again (including the last CHECK-LABEL) in normal mode. in Check()
1080 // Match itself from the last position after matching CHECK-DAG. in Check()
1088 // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT in Check()
1089 // or CHECK-NOT in Check()
1093 // If this check is a "CHECK-NEXT", verify that the previous match was on in Check()
1098 // If this check is a "CHECK-SAME", verify that the previous match was on in Check()
1120 SMLoc::getFromPointer(Buffer.data())))->getBufferStart() && in CheckNext()
1121 "CHECK-NEXT can't be the first check in a file"); in CheckNext()
1128 "-NEXT: is on the same line as previous match"); in CheckNext()
1138 "-NEXT: is not on the line after the previous match"); in CheckNext()
1144 "non-matching line after previous match is here"); in CheckNext()
1159 ->getBufferStart() && in CheckSame()
1160 "CHECK-SAME can't be the first check in a file"); in CheckSame()
1168 "-SAME: is not on the same line as the previous match"); in CheckSame()
1183 assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!"); in CheckNot()
1186 size_t Pos = Pat->Match(Buffer, MatchLen, VariableTable); in CheckNot()
1192 Prefix + "-NOT: string occurred!"); in CheckNot()
1193 SM.PrintMessage(Pat->getLoc(), SourceMgr::DK_Note, in CheckNot()
1194 Prefix + "-NOT: pattern specified here"); in CheckNot()
1213 "Invalid CHECK-DAG or CHECK-NOT!"); in CheckDag()
1220 assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!"); in CheckDag()
1224 // CHECK-DAG always matches from the start. in CheckDag()
1227 // With a group of CHECK-DAGs, a single mismatching means the match on in CheckDag()
1228 // that group of CHECK-DAGs fails immediately. in CheckDag()
1233 // Re-calc it as the offset relative to the start of the original string. in CheckDag()
1241 Prefix + "-DAG: found a match of CHECK-DAG" in CheckDag()
1242 " reordering across a CHECK-NOT"); in CheckDag()
1245 Prefix + "-DAG: the farthest match of CHECK-DAG" in CheckDag()
1247 SM.PrintMessage(NotStrings[0]->getLoc(), SourceMgr::DK_Note, in CheckDag()
1248 Prefix + "-NOT: the crossed pattern specified" in CheckDag()
1251 Prefix + "-DAG: the reordered pattern specified" in CheckDag()
1255 // All subsequent CHECK-DAGs should be matched from the farthest in CheckDag()
1256 // position of all precedent CHECK-DAGs (including this one.) in CheckDag()
1258 // If there's CHECK-NOTs between two CHECK-DAGs or from CHECK to in CheckDag()
1259 // CHECK-DAG, verify that there's no 'not' strings occurred in that in CheckDag()
1268 // Update the last position with CHECK-DAG matches. in CheckDag()
1277 Regex Validator("^[a-zA-Z0-9_-]*$"); in ValidateCheckPrefix()
1299 // I don't think there's a way to specify an initial value for cl::list,
1319 errs() << "Supplied check-prefix is invalid! Prefixes must be unique and " in main()
1334 // Open the file to check and add it to SourceMgr. in main()
1344 if (File->getBufferSize() == 0 && !AllowEmptyInput) { in main()
1357 StringRef Buffer = F->getBuffer(); in main()
1361 /// VariableTable - This holds all the current filecheck variables. in main()
1379 // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG in main()
1397 // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG) in main()