blob: 5ec126f934e656f4da14064c5d0d8e54baf185b5 [file] [log] [blame]
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001//===- FileCheck.cpp - Check that File's Contents match what is expected --===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00006//
7//===----------------------------------------------------------------------===//
8//
9// FileCheck does a line-by line check of a file that validates whether it
10// contains the expected content. This is useful for regression tests etc.
11//
12// This file implements most of the API that will be used by the FileCheck utility
13// as well as various unittests.
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Support/FileCheck.h"
17#include "llvm/ADT/StringSet.h"
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +000018#include "llvm/Support/FormatVariadic.h"
19#include <cstdint>
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +000020#include <list>
21#include <map>
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +000022#include <tuple>
23#include <utility>
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +000024
25using namespace llvm;
26
Thomas Preud'homme2bf04f22019-07-10 12:49:28 +000027void FileCheckNumericVariable::setValue(uint64_t NewValue) {
28 assert(!Value && "Overwriting numeric variable's value is not allowed");
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000029 Value = NewValue;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000030}
31
Thomas Preud'homme2bf04f22019-07-10 12:49:28 +000032void FileCheckNumericVariable::clearValue() {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000033 if (!Value)
Thomas Preud'homme2bf04f22019-07-10 12:49:28 +000034 return;
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +000035 Value = None;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000036}
37
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +000038Expected<uint64_t> FileCheckExpression::eval() const {
39 assert(LeftOp && "Evaluating an empty expression");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +000040 Optional<uint64_t> LeftOpValue = LeftOp->getValue();
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000041 // Variable is undefined.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +000042 if (!LeftOpValue)
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000043 return make_error<FileCheckUndefVarError>(LeftOp->getName());
Thomas Preud'homme71d3f222019-06-06 13:21:06 +000044 return EvalBinop(*LeftOpValue, RightOp);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000045}
46
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000047Expected<std::string> FileCheckNumericSubstitution::getResult() const {
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +000048 Expected<uint64_t> EvaluatedValue = Expression->eval();
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +000049 if (!EvaluatedValue)
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000050 return EvaluatedValue.takeError();
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +000051 return utostr(*EvaluatedValue);
52}
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000053
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000054Expected<std::string> FileCheckStringSubstitution::getResult() const {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000055 // Look up the value and escape it so that we can put it into the regex.
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000056 Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000057 if (!VarVal)
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000058 return VarVal.takeError();
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000059 return Regex::escape(*VarVal);
Thomas Preud'homme288ed912019-05-02 00:04:38 +000060}
61
Thomas Preud'homme5a330472019-04-29 13:32:36 +000062bool FileCheckPattern::isValidVarNameStart(char C) {
63 return C == '_' || isalpha(C);
64}
65
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000066Expected<StringRef> FileCheckPattern::parseVariable(StringRef &Str,
67 bool &IsPseudo,
68 const SourceMgr &SM) {
Thomas Preud'homme5a330472019-04-29 13:32:36 +000069 if (Str.empty())
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000070 return FileCheckErrorDiagnostic::get(SM, Str, "empty variable name");
Thomas Preud'homme5a330472019-04-29 13:32:36 +000071
72 bool ParsedOneChar = false;
73 unsigned I = 0;
74 IsPseudo = Str[0] == '@';
75
76 // Global vars start with '$'.
77 if (Str[0] == '$' || IsPseudo)
78 ++I;
79
80 for (unsigned E = Str.size(); I != E; ++I) {
81 if (!ParsedOneChar && !isValidVarNameStart(Str[I]))
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000082 return FileCheckErrorDiagnostic::get(SM, Str, "invalid variable name");
Thomas Preud'homme5a330472019-04-29 13:32:36 +000083
84 // Variable names are composed of alphanumeric characters and underscores.
85 if (Str[I] != '_' && !isalnum(Str[I]))
86 break;
87 ParsedOneChar = true;
88 }
89
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000090 StringRef Name = Str.take_front(I);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +000091 Str = Str.substr(I);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000092 return Name;
Thomas Preud'homme5a330472019-04-29 13:32:36 +000093}
94
Thomas Preud'homme288ed912019-05-02 00:04:38 +000095// StringRef holding all characters considered as horizontal whitespaces by
96// FileCheck input canonicalization.
97StringRef SpaceChars = " \t";
98
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +000099// Parsing helper function that strips the first character in S and returns it.
100static char popFront(StringRef &S) {
101 char C = S.front();
102 S = S.drop_front();
103 return C;
104}
105
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000106char FileCheckUndefVarError::ID = 0;
107char FileCheckErrorDiagnostic::ID = 0;
108char FileCheckNotFoundError::ID = 0;
109
Thomas Preud'homme56f63082019-07-05 16:25:46 +0000110Expected<FileCheckNumericVariable *>
111FileCheckPattern::parseNumericVariableDefinition(
112 StringRef &Expr, FileCheckPatternContext *Context, size_t LineNumber,
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000113 const SourceMgr &SM) {
114 bool IsPseudo;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000115 Expected<StringRef> ParseVarResult = parseVariable(Expr, IsPseudo, SM);
116 if (!ParseVarResult)
117 return ParseVarResult.takeError();
Thomas Preud'homme56f63082019-07-05 16:25:46 +0000118 StringRef Name = *ParseVarResult;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000119
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000120 if (IsPseudo)
121 return FileCheckErrorDiagnostic::get(
122 SM, Name, "definition of pseudo numeric variable unsupported");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000123
124 // Detect collisions between string and numeric variables when the latter
125 // is created later than the former.
126 if (Context->DefinedVariableTable.find(Name) !=
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000127 Context->DefinedVariableTable.end())
128 return FileCheckErrorDiagnostic::get(
129 SM, Name, "string variable with name '" + Name + "' already exists");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000130
Thomas Preud'homme28196a52019-07-05 12:01:06 +0000131 Expr = Expr.ltrim(SpaceChars);
132 if (!Expr.empty())
133 return FileCheckErrorDiagnostic::get(
134 SM, Expr, "unexpected characters after numeric variable name");
135
Thomas Preud'homme56f63082019-07-05 16:25:46 +0000136 FileCheckNumericVariable *DefinedNumericVariable;
137 auto VarTableIter = Context->GlobalNumericVariableTable.find(Name);
138 if (VarTableIter != Context->GlobalNumericVariableTable.end())
139 DefinedNumericVariable = VarTableIter->second;
140 else
141 DefinedNumericVariable = Context->makeNumericVariable(LineNumber, Name);
142
143 return DefinedNumericVariable;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000144}
145
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000146Expected<FileCheckNumericVariable *>
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000147FileCheckPattern::parseNumericVariableUse(StringRef &Expr,
148 const SourceMgr &SM) const {
149 bool IsPseudo;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000150 Expected<StringRef> ParseVarResult = parseVariable(Expr, IsPseudo, SM);
151 if (!ParseVarResult)
152 return ParseVarResult.takeError();
153 StringRef Name = *ParseVarResult;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000154
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000155 if (IsPseudo && !Name.equals("@LINE"))
156 return FileCheckErrorDiagnostic::get(
157 SM, Name, "invalid pseudo numeric variable '" + Name + "'");
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000158
Thomas Preud'homme41f2bea2019-07-05 12:01:12 +0000159 // Numeric variable definitions and uses are parsed in the order in which
160 // they appear in the CHECK patterns. For each definition, the pointer to the
161 // class instance of the corresponding numeric variable definition is stored
Thomas Preud'homme56f63082019-07-05 16:25:46 +0000162 // in GlobalNumericVariableTable in parsePattern. Therefore, if the pointer
163 // we get below is null, it means no such variable was defined before. When
164 // that happens, we create a dummy variable so that parsing can continue. All
165 // uses of undefined variables, whether string or numeric, are then diagnosed
166 // in printSubstitutions() after failing to match.
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000167 auto VarTableIter = Context->GlobalNumericVariableTable.find(Name);
Thomas Preud'hommefe7ac172019-07-05 16:25:33 +0000168 FileCheckNumericVariable *NumericVariable;
169 if (VarTableIter != Context->GlobalNumericVariableTable.end())
170 NumericVariable = VarTableIter->second;
171 else {
172 NumericVariable = Context->makeNumericVariable(0, Name);
173 Context->GlobalNumericVariableTable[Name] = NumericVariable;
174 }
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000175
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000176 if (!IsPseudo && NumericVariable->getDefLineNumber() == LineNumber)
177 return FileCheckErrorDiagnostic::get(
178 SM, Name,
179 "numeric variable '" + Name + "' defined on the same line as used");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000180
181 return NumericVariable;
182}
183
184static uint64_t add(uint64_t LeftOp, uint64_t RightOp) {
185 return LeftOp + RightOp;
186}
187
188static uint64_t sub(uint64_t LeftOp, uint64_t RightOp) {
189 return LeftOp - RightOp;
190}
191
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000192Expected<FileCheckExpression *>
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000193FileCheckPattern::parseBinop(StringRef &Expr, const SourceMgr &SM) const {
194 Expected<FileCheckNumericVariable *> LeftParseResult =
195 parseNumericVariableUse(Expr, SM);
196 if (!LeftParseResult) {
197 return LeftParseResult.takeError();
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000198 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000199 FileCheckNumericVariable *LeftOp = *LeftParseResult;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000200
201 // Check if this is a supported operation and select a function to perform
202 // it.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000203 Expr = Expr.ltrim(SpaceChars);
204 if (Expr.empty())
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000205 return Context->makeExpression(add, LeftOp, 0);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000206 SMLoc OpLoc = SMLoc::getFromPointer(Expr.data());
207 char Operator = popFront(Expr);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000208 binop_eval_t EvalBinop;
209 switch (Operator) {
210 case '+':
211 EvalBinop = add;
212 break;
213 case '-':
214 EvalBinop = sub;
215 break;
216 default:
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000217 return FileCheckErrorDiagnostic::get(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000218 SM, OpLoc, Twine("unsupported operation '") + Twine(Operator) + "'");
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000219 }
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000220
221 // Parse right operand.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000222 Expr = Expr.ltrim(SpaceChars);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000223 if (Expr.empty())
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000224 return FileCheckErrorDiagnostic::get(SM, Expr,
225 "missing operand in expression");
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000226 uint64_t RightOp;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000227 if (Expr.consumeInteger(10, RightOp))
228 return FileCheckErrorDiagnostic::get(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000229 SM, Expr, "invalid offset in expression '" + Expr + "'");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000230 Expr = Expr.ltrim(SpaceChars);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000231 if (!Expr.empty())
232 return FileCheckErrorDiagnostic::get(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000233 SM, Expr, "unexpected characters at end of expression '" + Expr + "'");
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000234
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000235 return Context->makeExpression(EvalBinop, LeftOp, RightOp);
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000236}
237
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000238Expected<FileCheckExpression *> FileCheckPattern::parseNumericSubstitutionBlock(
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000239 StringRef Expr,
240 Optional<FileCheckNumericVariable *> &DefinedNumericVariable,
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000241 const SourceMgr &SM) const {
242 // Parse the numeric variable definition.
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000243 DefinedNumericVariable = None;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000244 size_t DefEnd = Expr.find(':');
245 if (DefEnd != StringRef::npos) {
246 StringRef DefExpr = Expr.substr(0, DefEnd);
Thomas Preud'homme28196a52019-07-05 12:01:06 +0000247 StringRef UseExpr = Expr.substr(DefEnd + 1);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000248
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000249 UseExpr = UseExpr.ltrim(SpaceChars);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000250 if (!UseExpr.empty())
251 return FileCheckErrorDiagnostic::get(
252 SM, UseExpr,
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000253 "unexpected string after variable definition: '" + UseExpr + "'");
Thomas Preud'homme28196a52019-07-05 12:01:06 +0000254
255 DefExpr = DefExpr.ltrim(SpaceChars);
Thomas Preud'homme56f63082019-07-05 16:25:46 +0000256 Expected<FileCheckNumericVariable *> ParseResult =
257 parseNumericVariableDefinition(DefExpr, Context, LineNumber, SM);
258 if (!ParseResult)
259 return ParseResult.takeError();
260 DefinedNumericVariable = *ParseResult;
Thomas Preud'homme28196a52019-07-05 12:01:06 +0000261
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000262 return Context->makeExpression(add, nullptr, 0);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000263 }
264
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000265 // Parse the expression itself.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000266 Expr = Expr.ltrim(SpaceChars);
267 return parseBinop(Expr, SM);
268}
269
270bool FileCheckPattern::parsePattern(StringRef PatternStr, StringRef Prefix,
271 SourceMgr &SM,
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000272 const FileCheckRequest &Req) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000273 bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot;
274
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000275 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
276
277 if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
278 // Ignore trailing whitespace.
279 while (!PatternStr.empty() &&
280 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
281 PatternStr = PatternStr.substr(0, PatternStr.size() - 1);
282
283 // Check that there is something on the line.
284 if (PatternStr.empty() && CheckTy != Check::CheckEmpty) {
285 SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
286 "found empty check string with prefix '" + Prefix + ":'");
287 return true;
288 }
289
290 if (!PatternStr.empty() && CheckTy == Check::CheckEmpty) {
291 SM.PrintMessage(
292 PatternLoc, SourceMgr::DK_Error,
293 "found non-empty check string for empty check with prefix '" + Prefix +
294 ":'");
295 return true;
296 }
297
298 if (CheckTy == Check::CheckEmpty) {
299 RegExStr = "(\n$)";
300 return false;
301 }
302
303 // Check to see if this is a fixed string, or if it has regex pieces.
304 if (!MatchFullLinesHere &&
305 (PatternStr.size() < 2 || (PatternStr.find("{{") == StringRef::npos &&
306 PatternStr.find("[[") == StringRef::npos))) {
307 FixedStr = PatternStr;
308 return false;
309 }
310
311 if (MatchFullLinesHere) {
312 RegExStr += '^';
313 if (!Req.NoCanonicalizeWhiteSpace)
314 RegExStr += " *";
315 }
316
317 // Paren value #0 is for the fully matched string. Any new parenthesized
318 // values add from there.
319 unsigned CurParen = 1;
320
321 // Otherwise, there is at least one regex piece. Build up the regex pattern
322 // by escaping scary characters in fixed strings, building up one big regex.
323 while (!PatternStr.empty()) {
324 // RegEx matches.
325 if (PatternStr.startswith("{{")) {
326 // This is the start of a regex match. Scan for the }}.
327 size_t End = PatternStr.find("}}");
328 if (End == StringRef::npos) {
329 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
330 SourceMgr::DK_Error,
331 "found start of regex string with no end '}}'");
332 return true;
333 }
334
335 // Enclose {{}} patterns in parens just like [[]] even though we're not
336 // capturing the result for any purpose. This is required in case the
337 // expression contains an alternation like: CHECK: abc{{x|z}}def. We
338 // want this to turn into: "abc(x|z)def" not "abcx|zdef".
339 RegExStr += '(';
340 ++CurParen;
341
342 if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
343 return true;
344 RegExStr += ')';
345
346 PatternStr = PatternStr.substr(End + 2);
347 continue;
348 }
349
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000350 // String and numeric substitution blocks. String substitution blocks come
351 // in two forms: [[foo:.*]] and [[foo]]. The former matches .* (or some
352 // other regex) and assigns it to the string variable 'foo'. The latter
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000353 // substitutes foo's value. Numeric substitution blocks work the same way
354 // as string ones, but start with a '#' sign after the double brackets.
355 // Both string and numeric variable names must satisfy the regular
356 // expression "[a-zA-Z_][0-9a-zA-Z_]*" to be valid, as this helps catch
357 // some common errors.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000358 if (PatternStr.startswith("[[")) {
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000359 StringRef UnparsedPatternStr = PatternStr.substr(2);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000360 // Find the closing bracket pair ending the match. End is going to be an
361 // offset relative to the beginning of the match string.
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000362 size_t End = FindRegexVarEnd(UnparsedPatternStr, SM);
363 StringRef MatchStr = UnparsedPatternStr.substr(0, End);
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000364 bool IsNumBlock = MatchStr.consume_front("#");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000365
366 if (End == StringRef::npos) {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000367 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
368 SourceMgr::DK_Error,
369 "Invalid substitution block, no ]] found");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000370 return true;
371 }
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000372 // Strip the substitution block we are parsing. End points to the start
373 // of the "]]" closing the expression so account for it in computing the
374 // index of the first unparsed character.
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000375 PatternStr = UnparsedPatternStr.substr(End + 2);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000376
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000377 bool IsDefinition = false;
378 StringRef DefName;
379 StringRef SubstStr;
380 StringRef MatchRegexp;
381 size_t SubstInsertIdx = RegExStr.size();
382
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000383 // Parse string variable or legacy expression.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000384 if (!IsNumBlock) {
385 size_t VarEndIdx = MatchStr.find(":");
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000386 size_t SpacePos = MatchStr.substr(0, VarEndIdx).find_first_of(" \t");
387 if (SpacePos != StringRef::npos) {
388 SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data() + SpacePos),
389 SourceMgr::DK_Error, "unexpected whitespace");
390 return true;
391 }
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000392
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000393 // Get the name (e.g. "foo") and verify it is well formed.
394 bool IsPseudo;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000395 StringRef OrigMatchStr = MatchStr;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000396 Expected<StringRef> ParseVarResult =
397 parseVariable(MatchStr, IsPseudo, SM);
398 if (!ParseVarResult) {
399 logAllUnhandledErrors(ParseVarResult.takeError(), errs());
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000400 return true;
401 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000402 StringRef Name = *ParseVarResult;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000403
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000404 IsDefinition = (VarEndIdx != StringRef::npos);
405 if (IsDefinition) {
406 if ((IsPseudo || !MatchStr.consume_front(":"))) {
407 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
408 SourceMgr::DK_Error,
409 "invalid name in string variable definition");
410 return true;
411 }
412
413 // Detect collisions between string and numeric variables when the
414 // former is created later than the latter.
415 if (Context->GlobalNumericVariableTable.find(Name) !=
416 Context->GlobalNumericVariableTable.end()) {
417 SM.PrintMessage(
418 SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
419 "numeric variable with name '" + Name + "' already exists");
420 return true;
421 }
422 DefName = Name;
423 MatchRegexp = MatchStr;
424 } else {
425 if (IsPseudo) {
426 MatchStr = OrigMatchStr;
427 IsNumBlock = true;
428 } else
429 SubstStr = Name;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000430 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000431 }
432
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000433 // Parse numeric substitution block.
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000434 FileCheckExpression *Expression;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000435 Optional<FileCheckNumericVariable *> DefinedNumericVariable;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000436 if (IsNumBlock) {
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000437 Expected<FileCheckExpression *> ParseResult =
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000438 parseNumericSubstitutionBlock(MatchStr, DefinedNumericVariable, SM);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000439 if (!ParseResult) {
440 logAllUnhandledErrors(ParseResult.takeError(), errs());
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000441 return true;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000442 }
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000443 Expression = *ParseResult;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000444 if (DefinedNumericVariable) {
445 IsDefinition = true;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000446 DefName = (*DefinedNumericVariable)->getName();
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000447 MatchRegexp = StringRef("[0-9]+");
448 } else
449 SubstStr = MatchStr;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000450 }
451
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000452 // Handle substitutions: [[foo]] and [[#<foo expr>]].
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000453 if (!IsDefinition) {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000454 // Handle substitution of string variables that were defined earlier on
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000455 // the same line by emitting a backreference. Expressions do not
456 // support substituting a numeric variable defined on the same line.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000457 if (!IsNumBlock && VariableDefs.find(SubstStr) != VariableDefs.end()) {
458 unsigned CaptureParenGroup = VariableDefs[SubstStr];
459 if (CaptureParenGroup < 1 || CaptureParenGroup > 9) {
460 SM.PrintMessage(SMLoc::getFromPointer(SubstStr.data()),
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000461 SourceMgr::DK_Error,
462 "Can't back-reference more than 9 variables");
463 return true;
464 }
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000465 AddBackrefToRegEx(CaptureParenGroup);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000466 } else {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000467 // Handle substitution of string variables ([[<var>]]) defined in
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000468 // previous CHECK patterns, and substitution of expressions.
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000469 FileCheckSubstitution *Substitution =
470 IsNumBlock
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000471 ? Context->makeNumericSubstitution(SubstStr, Expression,
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000472 SubstInsertIdx)
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000473 : Context->makeStringSubstitution(SubstStr, SubstInsertIdx);
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000474 Substitutions.push_back(Substitution);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000475 }
476 continue;
477 }
478
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000479 // Handle variable definitions: [[<def>:(...)]] and
480 // [[#(...)<def>:(...)]].
481 if (IsNumBlock) {
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000482 FileCheckNumericVariableMatch NumericVariableDefinition = {
483 *DefinedNumericVariable, CurParen};
484 NumericVariableDefs[DefName] = NumericVariableDefinition;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000485 // This store is done here rather than in match() to allow
486 // parseNumericVariableUse() to get the pointer to the class instance
487 // of the right variable definition corresponding to a given numeric
488 // variable use.
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000489 Context->GlobalNumericVariableTable[DefName] = *DefinedNumericVariable;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000490 } else {
491 VariableDefs[DefName] = CurParen;
492 // Mark the string variable as defined to detect collisions between
493 // string and numeric variables in parseNumericVariableUse() and
494 // DefineCmdlineVariables() when the latter is created later than the
495 // former. We cannot reuse GlobalVariableTable for this by populating
496 // it with an empty string since we would then lose the ability to
497 // detect the use of an undefined variable in match().
498 Context->DefinedVariableTable[DefName] = true;
499 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000500 RegExStr += '(';
501 ++CurParen;
502
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000503 if (AddRegExToRegEx(MatchRegexp, CurParen, SM))
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000504 return true;
505
506 RegExStr += ')';
507 }
508
509 // Handle fixed string matches.
510 // Find the end, which is the start of the next regex.
511 size_t FixedMatchEnd = PatternStr.find("{{");
512 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
513 RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
514 PatternStr = PatternStr.substr(FixedMatchEnd);
515 }
516
517 if (MatchFullLinesHere) {
518 if (!Req.NoCanonicalizeWhiteSpace)
519 RegExStr += " *";
520 RegExStr += '$';
521 }
522
523 return false;
524}
525
526bool FileCheckPattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
527 Regex R(RS);
528 std::string Error;
529 if (!R.isValid(Error)) {
530 SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error,
531 "invalid regex: " + Error);
532 return true;
533 }
534
535 RegExStr += RS.str();
536 CurParen += R.getNumMatches();
537 return false;
538}
539
540void FileCheckPattern::AddBackrefToRegEx(unsigned BackrefNum) {
541 assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
542 std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum);
543 RegExStr += Backref;
544}
545
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000546Expected<size_t> FileCheckPattern::match(StringRef Buffer, size_t &MatchLen,
547 const SourceMgr &SM) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000548 // If this is the EOF pattern, match it immediately.
549 if (CheckTy == Check::CheckEOF) {
550 MatchLen = 0;
551 return Buffer.size();
552 }
553
554 // If this is a fixed string pattern, just match it now.
555 if (!FixedStr.empty()) {
556 MatchLen = FixedStr.size();
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000557 size_t Pos = Buffer.find(FixedStr);
558 if (Pos == StringRef::npos)
559 return make_error<FileCheckNotFoundError>();
560 return Pos;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000561 }
562
563 // Regex match.
564
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000565 // If there are substitutions, we need to create a temporary string with the
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000566 // actual value.
567 StringRef RegExToMatch = RegExStr;
568 std::string TmpStr;
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000569 if (!Substitutions.empty()) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000570 TmpStr = RegExStr;
Thomas Preud'homme56f63082019-07-05 16:25:46 +0000571 Context->LineVariable->setValue(LineNumber);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000572
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000573 size_t InsertOffset = 0;
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000574 // Substitute all string variables and expressions whose values are only
575 // now known. Use of string variables defined on the same line are handled
576 // by back-references.
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000577 for (const auto &Substitution : Substitutions) {
578 // Substitute and check for failure (e.g. use of undefined variable).
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000579 Expected<std::string> Value = Substitution->getResult();
Thomas Preud'hommef6ea43b2019-07-10 12:49:17 +0000580 if (!Value) {
581 Context->LineVariable->clearValue();
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000582 return Value.takeError();
Thomas Preud'hommef6ea43b2019-07-10 12:49:17 +0000583 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000584
585 // Plop it into the regex at the adjusted offset.
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000586 TmpStr.insert(TmpStr.begin() + Substitution->getIndex() + InsertOffset,
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000587 Value->begin(), Value->end());
588 InsertOffset += Value->size();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000589 }
590
591 // Match the newly constructed regex.
592 RegExToMatch = TmpStr;
Thomas Preud'homme56f63082019-07-05 16:25:46 +0000593 Context->LineVariable->clearValue();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000594 }
595
596 SmallVector<StringRef, 4> MatchInfo;
597 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000598 return make_error<FileCheckNotFoundError>();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000599
600 // Successful regex match.
601 assert(!MatchInfo.empty() && "Didn't get any match");
602 StringRef FullMatch = MatchInfo[0];
603
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000604 // If this defines any string variables, remember their values.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000605 for (const auto &VariableDef : VariableDefs) {
606 assert(VariableDef.second < MatchInfo.size() && "Internal paren error");
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000607 Context->GlobalVariableTable[VariableDef.first] =
608 MatchInfo[VariableDef.second];
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000609 }
610
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000611 // If this defines any numeric variables, remember their values.
612 for (const auto &NumericVariableDef : NumericVariableDefs) {
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000613 const FileCheckNumericVariableMatch &NumericVariableMatch =
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000614 NumericVariableDef.getValue();
615 unsigned CaptureParenGroup = NumericVariableMatch.CaptureParenGroup;
616 assert(CaptureParenGroup < MatchInfo.size() && "Internal paren error");
617 FileCheckNumericVariable *DefinedNumericVariable =
618 NumericVariableMatch.DefinedNumericVariable;
619
620 StringRef MatchedValue = MatchInfo[CaptureParenGroup];
621 uint64_t Val;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000622 if (MatchedValue.getAsInteger(10, Val))
623 return FileCheckErrorDiagnostic::get(SM, MatchedValue,
624 "Unable to represent numeric value");
Thomas Preud'homme2bf04f22019-07-10 12:49:28 +0000625 DefinedNumericVariable->setValue(Val);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000626 }
627
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000628 // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
629 // the required preceding newline, which is consumed by the pattern in the
630 // case of CHECK-EMPTY but not CHECK-NEXT.
631 size_t MatchStartSkip = CheckTy == Check::CheckEmpty;
632 MatchLen = FullMatch.size() - MatchStartSkip;
633 return FullMatch.data() - Buffer.data() + MatchStartSkip;
634}
635
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000636unsigned FileCheckPattern::computeMatchDistance(StringRef Buffer) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000637 // Just compute the number of matching characters. For regular expressions, we
638 // just compare against the regex itself and hope for the best.
639 //
640 // FIXME: One easy improvement here is have the regex lib generate a single
641 // example regular expression which matches, and use that as the example
642 // string.
643 StringRef ExampleString(FixedStr);
644 if (ExampleString.empty())
645 ExampleString = RegExStr;
646
647 // Only compare up to the first line in the buffer, or the string size.
648 StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
649 BufferPrefix = BufferPrefix.split('\n').first;
650 return BufferPrefix.edit_distance(ExampleString);
651}
652
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000653void FileCheckPattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
654 SMRange MatchRange) const {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000655 // Print what we know about substitutions.
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000656 if (!Substitutions.empty()) {
657 for (const auto &Substitution : Substitutions) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000658 SmallString<256> Msg;
659 raw_svector_ostream OS(Msg);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000660 Expected<std::string> MatchedValue = Substitution->getResult();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000661
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000662 // Substitution failed or is not known at match time, print the undefined
663 // variable it uses.
664 if (!MatchedValue) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000665 bool UndefSeen = false;
666 handleAllErrors(MatchedValue.takeError(),
667 [](const FileCheckNotFoundError &E) {},
Thomas Preud'hommea188ad22019-07-05 12:00:56 +0000668 // Handled in PrintNoMatch().
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000669 [](const FileCheckErrorDiagnostic &E) {},
670 [&](const FileCheckUndefVarError &E) {
671 if (!UndefSeen) {
672 OS << "uses undefined variable ";
673 UndefSeen = true;
674 }
675 E.log(OS);
676 },
677 [](const ErrorInfoBase &E) {
678 llvm_unreachable("Unexpected error");
679 });
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000680 } else {
681 // Substitution succeeded. Print substituted value.
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000682 OS << "with \"";
683 OS.write_escaped(Substitution->getFromString()) << "\" equal to \"";
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000684 OS.write_escaped(*MatchedValue) << "\"";
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000685 }
686
687 if (MatchRange.isValid())
688 SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, OS.str(),
689 {MatchRange});
690 else
691 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
692 SourceMgr::DK_Note, OS.str());
693 }
694 }
695}
696
Joel E. Denny3c5d2672018-12-18 00:01:39 +0000697static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy,
698 const SourceMgr &SM, SMLoc Loc,
699 Check::FileCheckType CheckTy,
700 StringRef Buffer, size_t Pos, size_t Len,
Joel E. Denny7df86962018-12-18 00:03:03 +0000701 std::vector<FileCheckDiag> *Diags,
702 bool AdjustPrevDiag = false) {
Joel E. Denny3c5d2672018-12-18 00:01:39 +0000703 SMLoc Start = SMLoc::getFromPointer(Buffer.data() + Pos);
704 SMLoc End = SMLoc::getFromPointer(Buffer.data() + Pos + Len);
705 SMRange Range(Start, End);
Joel E. Denny96f0e842018-12-18 00:03:36 +0000706 if (Diags) {
Joel E. Denny7df86962018-12-18 00:03:03 +0000707 if (AdjustPrevDiag)
708 Diags->rbegin()->MatchTy = MatchTy;
709 else
710 Diags->emplace_back(SM, CheckTy, Loc, MatchTy, Range);
711 }
Joel E. Denny3c5d2672018-12-18 00:01:39 +0000712 return Range;
713}
714
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000715void FileCheckPattern::printFuzzyMatch(
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000716 const SourceMgr &SM, StringRef Buffer,
Joel E. Denny2c007c82018-12-18 00:02:04 +0000717 std::vector<FileCheckDiag> *Diags) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000718 // Attempt to find the closest/best fuzzy match. Usually an error happens
719 // because some string in the output didn't exactly match. In these cases, we
720 // would like to show the user a best guess at what "should have" matched, to
721 // save them having to actually check the input manually.
722 size_t NumLinesForward = 0;
723 size_t Best = StringRef::npos;
724 double BestQuality = 0;
725
726 // Use an arbitrary 4k limit on how far we will search.
727 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
728 if (Buffer[i] == '\n')
729 ++NumLinesForward;
730
731 // Patterns have leading whitespace stripped, so skip whitespace when
732 // looking for something which looks like a pattern.
733 if (Buffer[i] == ' ' || Buffer[i] == '\t')
734 continue;
735
736 // Compute the "quality" of this match as an arbitrary combination of the
737 // match distance and the number of lines skipped to get to this match.
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000738 unsigned Distance = computeMatchDistance(Buffer.substr(i));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000739 double Quality = Distance + (NumLinesForward / 100.);
740
741 if (Quality < BestQuality || Best == StringRef::npos) {
742 Best = i;
743 BestQuality = Quality;
744 }
745 }
746
747 // Print the "possible intended match here" line if we found something
748 // reasonable and not equal to what we showed in the "scanning from here"
749 // line.
750 if (Best && Best != StringRef::npos && BestQuality < 50) {
Joel E. Denny2c007c82018-12-18 00:02:04 +0000751 SMRange MatchRange =
752 ProcessMatchResult(FileCheckDiag::MatchFuzzy, SM, getLoc(),
753 getCheckTy(), Buffer, Best, 0, Diags);
754 SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note,
755 "possible intended match here");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000756
757 // FIXME: If we wanted to be really friendly we would show why the match
758 // failed, as it can be hard to spot simple one character differences.
759 }
760}
761
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000762Expected<StringRef>
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000763FileCheckPatternContext::getPatternVarValue(StringRef VarName) {
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000764 auto VarIter = GlobalVariableTable.find(VarName);
765 if (VarIter == GlobalVariableTable.end())
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000766 return make_error<FileCheckUndefVarError>(VarName);
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000767
768 return VarIter->second;
769}
770
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000771FileCheckExpression *
772FileCheckPatternContext::makeExpression(binop_eval_t EvalBinop,
773 FileCheckNumericVariable *OperandLeft,
774 uint64_t OperandRight) {
775 Expressions.push_back(llvm::make_unique<FileCheckExpression>(
776 EvalBinop, OperandLeft, OperandRight));
777 return Expressions.back().get();
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000778}
779
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000780template <class... Types>
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000781FileCheckNumericVariable *
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000782FileCheckPatternContext::makeNumericVariable(Types... args) {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000783 NumericVariables.push_back(
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000784 llvm::make_unique<FileCheckNumericVariable>(args...));
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000785 return NumericVariables.back().get();
786}
787
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000788FileCheckSubstitution *
789FileCheckPatternContext::makeStringSubstitution(StringRef VarName,
790 size_t InsertIdx) {
791 Substitutions.push_back(
792 llvm::make_unique<FileCheckStringSubstitution>(this, VarName, InsertIdx));
793 return Substitutions.back().get();
794}
795
796FileCheckSubstitution *FileCheckPatternContext::makeNumericSubstitution(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000797 StringRef ExpressionStr, FileCheckExpression *Expression,
798 size_t InsertIdx) {
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000799 Substitutions.push_back(llvm::make_unique<FileCheckNumericSubstitution>(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000800 this, ExpressionStr, Expression, InsertIdx));
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000801 return Substitutions.back().get();
802}
803
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000804size_t FileCheckPattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
805 // Offset keeps track of the current offset within the input Str
806 size_t Offset = 0;
807 // [...] Nesting depth
808 size_t BracketDepth = 0;
809
810 while (!Str.empty()) {
811 if (Str.startswith("]]") && BracketDepth == 0)
812 return Offset;
813 if (Str[0] == '\\') {
814 // Backslash escapes the next char within regexes, so skip them both.
815 Str = Str.substr(2);
816 Offset += 2;
817 } else {
818 switch (Str[0]) {
819 default:
820 break;
821 case '[':
822 BracketDepth++;
823 break;
824 case ']':
825 if (BracketDepth == 0) {
826 SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
827 SourceMgr::DK_Error,
828 "missing closing \"]\" for regex variable");
829 exit(1);
830 }
831 BracketDepth--;
832 break;
833 }
834 Str = Str.substr(1);
835 Offset++;
836 }
837 }
838
839 return StringRef::npos;
840}
841
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +0000842StringRef FileCheck::CanonicalizeFile(MemoryBuffer &MB,
843 SmallVectorImpl<char> &OutputBuffer) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000844 OutputBuffer.reserve(MB.getBufferSize());
845
846 for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd();
847 Ptr != End; ++Ptr) {
848 // Eliminate trailing dosish \r.
849 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
850 continue;
851 }
852
853 // If current char is not a horizontal whitespace or if horizontal
854 // whitespace canonicalization is disabled, dump it to output as is.
855 if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) {
856 OutputBuffer.push_back(*Ptr);
857 continue;
858 }
859
860 // Otherwise, add one space and advance over neighboring space.
861 OutputBuffer.push_back(' ');
862 while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
863 ++Ptr;
864 }
865
866 // Add a null byte and then return all but that byte.
867 OutputBuffer.push_back('\0');
868 return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
869}
870
Joel E. Denny3c5d2672018-12-18 00:01:39 +0000871FileCheckDiag::FileCheckDiag(const SourceMgr &SM,
872 const Check::FileCheckType &CheckTy,
873 SMLoc CheckLoc, MatchType MatchTy,
874 SMRange InputRange)
875 : CheckTy(CheckTy), MatchTy(MatchTy) {
876 auto Start = SM.getLineAndColumn(InputRange.Start);
877 auto End = SM.getLineAndColumn(InputRange.End);
878 InputStartLine = Start.first;
879 InputStartCol = Start.second;
880 InputEndLine = End.first;
881 InputEndCol = End.second;
882 Start = SM.getLineAndColumn(CheckLoc);
883 CheckLine = Start.first;
884 CheckCol = Start.second;
885}
886
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000887static bool IsPartOfWord(char c) {
888 return (isalnum(c) || c == '-' || c == '_');
889}
890
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000891Check::FileCheckType &Check::FileCheckType::setCount(int C) {
Fedor Sergeev8477a3e2018-11-13 01:09:53 +0000892 assert(Count > 0 && "zero and negative counts are not supported");
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000893 assert((C == 1 || Kind == CheckPlain) &&
894 "count supported only for plain CHECK directives");
895 Count = C;
896 return *this;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000897}
898
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000899std::string Check::FileCheckType::getDescription(StringRef Prefix) const {
900 switch (Kind) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000901 case Check::CheckNone:
902 return "invalid";
903 case Check::CheckPlain:
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000904 if (Count > 1)
905 return Prefix.str() + "-COUNT";
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000906 return Prefix;
907 case Check::CheckNext:
908 return Prefix.str() + "-NEXT";
909 case Check::CheckSame:
910 return Prefix.str() + "-SAME";
911 case Check::CheckNot:
912 return Prefix.str() + "-NOT";
913 case Check::CheckDAG:
914 return Prefix.str() + "-DAG";
915 case Check::CheckLabel:
916 return Prefix.str() + "-LABEL";
917 case Check::CheckEmpty:
918 return Prefix.str() + "-EMPTY";
919 case Check::CheckEOF:
920 return "implicit EOF";
921 case Check::CheckBadNot:
922 return "bad NOT";
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000923 case Check::CheckBadCount:
924 return "bad COUNT";
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000925 }
926 llvm_unreachable("unknown FileCheckType");
927}
928
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000929static std::pair<Check::FileCheckType, StringRef>
930FindCheckType(StringRef Buffer, StringRef Prefix) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000931 if (Buffer.size() <= Prefix.size())
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000932 return {Check::CheckNone, StringRef()};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000933
934 char NextChar = Buffer[Prefix.size()];
935
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000936 StringRef Rest = Buffer.drop_front(Prefix.size() + 1);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000937 // Verify that the : is present after the prefix.
938 if (NextChar == ':')
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000939 return {Check::CheckPlain, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000940
941 if (NextChar != '-')
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000942 return {Check::CheckNone, StringRef()};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000943
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000944 if (Rest.consume_front("COUNT-")) {
945 int64_t Count;
946 if (Rest.consumeInteger(10, Count))
947 // Error happened in parsing integer.
948 return {Check::CheckBadCount, Rest};
949 if (Count <= 0 || Count > INT32_MAX)
950 return {Check::CheckBadCount, Rest};
951 if (!Rest.consume_front(":"))
952 return {Check::CheckBadCount, Rest};
953 return {Check::FileCheckType(Check::CheckPlain).setCount(Count), Rest};
954 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000955
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000956 if (Rest.consume_front("NEXT:"))
957 return {Check::CheckNext, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000958
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000959 if (Rest.consume_front("SAME:"))
960 return {Check::CheckSame, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000961
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000962 if (Rest.consume_front("NOT:"))
963 return {Check::CheckNot, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000964
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000965 if (Rest.consume_front("DAG:"))
966 return {Check::CheckDAG, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000967
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000968 if (Rest.consume_front("LABEL:"))
969 return {Check::CheckLabel, Rest};
970
971 if (Rest.consume_front("EMPTY:"))
972 return {Check::CheckEmpty, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000973
974 // You can't combine -NOT with another suffix.
975 if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") ||
976 Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") ||
977 Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:") ||
978 Rest.startswith("EMPTY-NOT:") || Rest.startswith("NOT-EMPTY:"))
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000979 return {Check::CheckBadNot, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000980
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000981 return {Check::CheckNone, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000982}
983
984// From the given position, find the next character after the word.
985static size_t SkipWord(StringRef Str, size_t Loc) {
986 while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
987 ++Loc;
988 return Loc;
989}
990
Thomas Preud'homme4a8ef112019-05-08 21:47:31 +0000991/// Searches the buffer for the first prefix in the prefix regular expression.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000992///
993/// This searches the buffer using the provided regular expression, however it
994/// enforces constraints beyond that:
995/// 1) The found prefix must not be a suffix of something that looks like
996/// a valid prefix.
997/// 2) The found prefix must be followed by a valid check type suffix using \c
998/// FindCheckType above.
999///
Thomas Preud'homme4a8ef112019-05-08 21:47:31 +00001000/// \returns a pair of StringRefs into the Buffer, which combines:
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001001/// - the first match of the regular expression to satisfy these two is
1002/// returned,
1003/// otherwise an empty StringRef is returned to indicate failure.
1004/// - buffer rewound to the location right after parsed suffix, for parsing
1005/// to continue from
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001006///
1007/// If this routine returns a valid prefix, it will also shrink \p Buffer to
1008/// start at the beginning of the returned prefix, increment \p LineNumber for
1009/// each new line consumed from \p Buffer, and set \p CheckTy to the type of
1010/// check found by examining the suffix.
1011///
1012/// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
1013/// is unspecified.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001014static std::pair<StringRef, StringRef>
1015FindFirstMatchingPrefix(Regex &PrefixRE, StringRef &Buffer,
1016 unsigned &LineNumber, Check::FileCheckType &CheckTy) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001017 SmallVector<StringRef, 2> Matches;
1018
1019 while (!Buffer.empty()) {
1020 // Find the first (longest) match using the RE.
1021 if (!PrefixRE.match(Buffer, &Matches))
1022 // No match at all, bail.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001023 return {StringRef(), StringRef()};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001024
1025 StringRef Prefix = Matches[0];
1026 Matches.clear();
1027
1028 assert(Prefix.data() >= Buffer.data() &&
1029 Prefix.data() < Buffer.data() + Buffer.size() &&
1030 "Prefix doesn't start inside of buffer!");
1031 size_t Loc = Prefix.data() - Buffer.data();
1032 StringRef Skipped = Buffer.substr(0, Loc);
1033 Buffer = Buffer.drop_front(Loc);
1034 LineNumber += Skipped.count('\n');
1035
1036 // Check that the matched prefix isn't a suffix of some other check-like
1037 // word.
1038 // FIXME: This is a very ad-hoc check. it would be better handled in some
1039 // other way. Among other things it seems hard to distinguish between
1040 // intentional and unintentional uses of this feature.
1041 if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
1042 // Now extract the type.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001043 StringRef AfterSuffix;
1044 std::tie(CheckTy, AfterSuffix) = FindCheckType(Buffer, Prefix);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001045
1046 // If we've found a valid check type for this prefix, we're done.
1047 if (CheckTy != Check::CheckNone)
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001048 return {Prefix, AfterSuffix};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001049 }
1050
1051 // If we didn't successfully find a prefix, we need to skip this invalid
1052 // prefix and continue scanning. We directly skip the prefix that was
1053 // matched and any additional parts of that check-like word.
1054 Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size()));
1055 }
1056
1057 // We ran out of buffer while skipping partial matches so give up.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001058 return {StringRef(), StringRef()};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001059}
1060
Thomas Preud'homme56f63082019-07-05 16:25:46 +00001061void FileCheckPatternContext::createLineVariable() {
1062 assert(!LineVariable && "@LINE pseudo numeric variable already created");
1063 StringRef LineName = "@LINE";
1064 LineVariable = makeNumericVariable(0, LineName);
1065 GlobalNumericVariableTable[LineName] = LineVariable;
1066}
1067
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +00001068bool FileCheck::ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
1069 std::vector<FileCheckString> &CheckStrings) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001070 Error DefineError =
1071 PatternContext.defineCmdlineVariables(Req.GlobalDefines, SM);
1072 if (DefineError) {
1073 logAllUnhandledErrors(std::move(DefineError), errs());
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001074 return true;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001075 }
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001076
Thomas Preud'homme56f63082019-07-05 16:25:46 +00001077 PatternContext.createLineVariable();
1078
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001079 std::vector<FileCheckPattern> ImplicitNegativeChecks;
1080 for (const auto &PatternString : Req.ImplicitCheckNot) {
1081 // Create a buffer with fake command line content in order to display the
1082 // command line option responsible for the specific implicit CHECK-NOT.
1083 std::string Prefix = "-implicit-check-not='";
1084 std::string Suffix = "'";
1085 std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
1086 Prefix + PatternString + Suffix, "command line");
1087
1088 StringRef PatternInBuffer =
1089 CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
1090 SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
1091
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001092 ImplicitNegativeChecks.push_back(
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001093 FileCheckPattern(Check::CheckNot, &PatternContext, 0));
1094 ImplicitNegativeChecks.back().parsePattern(PatternInBuffer,
1095 "IMPLICIT-CHECK", SM, Req);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001096 }
1097
1098 std::vector<FileCheckPattern> DagNotMatches = ImplicitNegativeChecks;
1099
1100 // LineNumber keeps track of the line on which CheckPrefix instances are
1101 // found.
1102 unsigned LineNumber = 1;
1103
1104 while (1) {
1105 Check::FileCheckType CheckTy;
1106
1107 // See if a prefix occurs in the memory buffer.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001108 StringRef UsedPrefix;
1109 StringRef AfterSuffix;
1110 std::tie(UsedPrefix, AfterSuffix) =
1111 FindFirstMatchingPrefix(PrefixRE, Buffer, LineNumber, CheckTy);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001112 if (UsedPrefix.empty())
1113 break;
1114 assert(UsedPrefix.data() == Buffer.data() &&
1115 "Failed to move Buffer's start forward, or pointed prefix outside "
1116 "of the buffer!");
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001117 assert(AfterSuffix.data() >= Buffer.data() &&
1118 AfterSuffix.data() < Buffer.data() + Buffer.size() &&
1119 "Parsing after suffix doesn't start inside of buffer!");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001120
1121 // Location to use for error messages.
1122 const char *UsedPrefixStart = UsedPrefix.data();
1123
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001124 // Skip the buffer to the end of parsed suffix (or just prefix, if no good
1125 // suffix was processed).
1126 Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
1127 : AfterSuffix;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001128
1129 // Complain about useful-looking but unsupported suffixes.
1130 if (CheckTy == Check::CheckBadNot) {
1131 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
1132 "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
1133 return true;
1134 }
1135
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001136 // Complain about invalid count specification.
1137 if (CheckTy == Check::CheckBadCount) {
1138 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
1139 "invalid count in -COUNT specification on prefix '" +
1140 UsedPrefix + "'");
1141 return true;
1142 }
1143
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001144 // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
1145 // leading whitespace.
1146 if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
1147 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
1148
1149 // Scan ahead to the end of line.
1150 size_t EOL = Buffer.find_first_of("\n\r");
1151
1152 // Remember the location of the start of the pattern, for diagnostics.
1153 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
1154
1155 // Parse the pattern.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001156 FileCheckPattern P(CheckTy, &PatternContext, LineNumber);
1157 if (P.parsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, Req))
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001158 return true;
1159
1160 // Verify that CHECK-LABEL lines do not define or use variables
1161 if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
1162 SM.PrintMessage(
1163 SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
1164 "found '" + UsedPrefix + "-LABEL:'"
1165 " with variable definition or use");
1166 return true;
1167 }
1168
1169 Buffer = Buffer.substr(EOL);
1170
1171 // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
1172 if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
1173 CheckTy == Check::CheckEmpty) &&
1174 CheckStrings.empty()) {
1175 StringRef Type = CheckTy == Check::CheckNext
1176 ? "NEXT"
1177 : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
1178 SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
1179 SourceMgr::DK_Error,
1180 "found '" + UsedPrefix + "-" + Type +
1181 "' without previous '" + UsedPrefix + ": line");
1182 return true;
1183 }
1184
1185 // Handle CHECK-DAG/-NOT.
1186 if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
1187 DagNotMatches.push_back(P);
1188 continue;
1189 }
1190
1191 // Okay, add the string we captured to the output vector and move on.
1192 CheckStrings.emplace_back(P, UsedPrefix, PatternLoc);
1193 std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
1194 DagNotMatches = ImplicitNegativeChecks;
1195 }
1196
1197 // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first
1198 // prefix as a filler for the error message.
1199 if (!DagNotMatches.empty()) {
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001200 CheckStrings.emplace_back(
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001201 FileCheckPattern(Check::CheckEOF, &PatternContext, LineNumber + 1),
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001202 *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001203 std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
1204 }
1205
1206 if (CheckStrings.empty()) {
1207 errs() << "error: no check strings found with prefix"
1208 << (Req.CheckPrefixes.size() > 1 ? "es " : " ");
1209 auto I = Req.CheckPrefixes.begin();
1210 auto E = Req.CheckPrefixes.end();
1211 if (I != E) {
1212 errs() << "\'" << *I << ":'";
1213 ++I;
1214 }
1215 for (; I != E; ++I)
1216 errs() << ", \'" << *I << ":'";
1217
1218 errs() << '\n';
1219 return true;
1220 }
1221
1222 return false;
1223}
1224
1225static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
1226 StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001227 int MatchedCount, StringRef Buffer, size_t MatchPos,
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001228 size_t MatchLen, const FileCheckRequest &Req,
1229 std::vector<FileCheckDiag> *Diags) {
Joel E. Denny352695c2019-01-22 21:41:42 +00001230 bool PrintDiag = true;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001231 if (ExpectedMatch) {
1232 if (!Req.Verbose)
1233 return;
1234 if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
1235 return;
Joel E. Denny352695c2019-01-22 21:41:42 +00001236 // Due to their verbosity, we don't print verbose diagnostics here if we're
1237 // gathering them for a different rendering, but we always print other
1238 // diagnostics.
1239 PrintDiag = !Diags;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001240 }
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001241 SMRange MatchRange = ProcessMatchResult(
Joel E. Dennye2afb612018-12-18 00:03:51 +00001242 ExpectedMatch ? FileCheckDiag::MatchFoundAndExpected
1243 : FileCheckDiag::MatchFoundButExcluded,
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001244 SM, Loc, Pat.getCheckTy(), Buffer, MatchPos, MatchLen, Diags);
Joel E. Denny352695c2019-01-22 21:41:42 +00001245 if (!PrintDiag)
1246 return;
1247
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001248 std::string Message = formatv("{0}: {1} string found in input",
1249 Pat.getCheckTy().getDescription(Prefix),
1250 (ExpectedMatch ? "expected" : "excluded"))
1251 .str();
1252 if (Pat.getCount() > 1)
1253 Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
1254
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001255 SM.PrintMessage(
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001256 Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message);
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001257 SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, "found here",
1258 {MatchRange});
Thomas Preud'homme288ed912019-05-02 00:04:38 +00001259 Pat.printSubstitutions(SM, Buffer, MatchRange);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001260}
1261
1262static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001263 const FileCheckString &CheckStr, int MatchedCount,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001264 StringRef Buffer, size_t MatchPos, size_t MatchLen,
1265 FileCheckRequest &Req,
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001266 std::vector<FileCheckDiag> *Diags) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001267 PrintMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001268 MatchedCount, Buffer, MatchPos, MatchLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001269}
1270
1271static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001272 StringRef Prefix, SMLoc Loc,
1273 const FileCheckPattern &Pat, int MatchedCount,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001274 StringRef Buffer, bool VerboseVerbose,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001275 std::vector<FileCheckDiag> *Diags, Error MatchErrors) {
1276 assert(MatchErrors && "Called on successful match");
Joel E. Denny352695c2019-01-22 21:41:42 +00001277 bool PrintDiag = true;
1278 if (!ExpectedMatch) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001279 if (!VerboseVerbose) {
1280 consumeError(std::move(MatchErrors));
Joel E. Denny352695c2019-01-22 21:41:42 +00001281 return;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001282 }
Joel E. Denny352695c2019-01-22 21:41:42 +00001283 // Due to their verbosity, we don't print verbose diagnostics here if we're
1284 // gathering them for a different rendering, but we always print other
1285 // diagnostics.
1286 PrintDiag = !Diags;
1287 }
1288
1289 // If the current position is at the end of a line, advance to the start of
1290 // the next line.
1291 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
1292 SMRange SearchRange = ProcessMatchResult(
1293 ExpectedMatch ? FileCheckDiag::MatchNoneButExpected
1294 : FileCheckDiag::MatchNoneAndExcluded,
1295 SM, Loc, Pat.getCheckTy(), Buffer, 0, Buffer.size(), Diags);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001296 if (!PrintDiag) {
1297 consumeError(std::move(MatchErrors));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001298 return;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001299 }
1300
1301 MatchErrors =
1302 handleErrors(std::move(MatchErrors),
1303 [](const FileCheckErrorDiagnostic &E) { E.log(errs()); });
1304
1305 // No problem matching the string per se.
1306 if (!MatchErrors)
1307 return;
1308 consumeError(std::move(MatchErrors));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001309
Joel E. Denny352695c2019-01-22 21:41:42 +00001310 // Print "not found" diagnostic.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001311 std::string Message = formatv("{0}: {1} string not found in input",
1312 Pat.getCheckTy().getDescription(Prefix),
1313 (ExpectedMatch ? "expected" : "excluded"))
1314 .str();
1315 if (Pat.getCount() > 1)
1316 Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001317 SM.PrintMessage(
1318 Loc, ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark, Message);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001319
Joel E. Denny352695c2019-01-22 21:41:42 +00001320 // Print the "scanning from here" line.
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001321 SM.PrintMessage(SearchRange.Start, SourceMgr::DK_Note, "scanning from here");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001322
1323 // Allow the pattern to print additional information if desired.
Thomas Preud'homme288ed912019-05-02 00:04:38 +00001324 Pat.printSubstitutions(SM, Buffer);
Joel E. Denny96f0e842018-12-18 00:03:36 +00001325
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001326 if (ExpectedMatch)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001327 Pat.printFuzzyMatch(SM, Buffer, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001328}
1329
1330static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001331 const FileCheckString &CheckStr, int MatchedCount,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001332 StringRef Buffer, bool VerboseVerbose,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001333 std::vector<FileCheckDiag> *Diags, Error MatchErrors) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001334 PrintNoMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001335 MatchedCount, Buffer, VerboseVerbose, Diags,
1336 std::move(MatchErrors));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001337}
1338
Thomas Preud'homme4a8ef112019-05-08 21:47:31 +00001339/// Counts the number of newlines in the specified range.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001340static unsigned CountNumNewlinesBetween(StringRef Range,
1341 const char *&FirstNewLine) {
1342 unsigned NumNewLines = 0;
1343 while (1) {
1344 // Scan for newline.
1345 Range = Range.substr(Range.find_first_of("\n\r"));
1346 if (Range.empty())
1347 return NumNewLines;
1348
1349 ++NumNewLines;
1350
1351 // Handle \n\r and \r\n as a single newline.
1352 if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
1353 (Range[0] != Range[1]))
1354 Range = Range.substr(1);
1355 Range = Range.substr(1);
1356
1357 if (NumNewLines == 1)
1358 FirstNewLine = Range.begin();
1359 }
1360}
1361
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001362size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001363 bool IsLabelScanMode, size_t &MatchLen,
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001364 FileCheckRequest &Req,
1365 std::vector<FileCheckDiag> *Diags) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001366 size_t LastPos = 0;
1367 std::vector<const FileCheckPattern *> NotStrings;
1368
1369 // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
1370 // bounds; we have not processed variable definitions within the bounded block
1371 // yet so cannot handle any final CHECK-DAG yet; this is handled when going
1372 // over the block again (including the last CHECK-LABEL) in normal mode.
1373 if (!IsLabelScanMode) {
1374 // Match "dag strings" (with mixed "not strings" if any).
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001375 LastPos = CheckDag(SM, Buffer, NotStrings, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001376 if (LastPos == StringRef::npos)
1377 return StringRef::npos;
1378 }
1379
1380 // Match itself from the last position after matching CHECK-DAG.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001381 size_t LastMatchEnd = LastPos;
1382 size_t FirstMatchPos = 0;
1383 // Go match the pattern Count times. Majority of patterns only match with
1384 // count 1 though.
1385 assert(Pat.getCount() != 0 && "pattern count can not be zero");
1386 for (int i = 1; i <= Pat.getCount(); i++) {
1387 StringRef MatchBuffer = Buffer.substr(LastMatchEnd);
1388 size_t CurrentMatchLen;
1389 // get a match at current start point
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001390 Expected<size_t> MatchResult = Pat.match(MatchBuffer, CurrentMatchLen, SM);
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001391
1392 // report
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001393 if (!MatchResult) {
1394 PrintNoMatch(true, SM, *this, i, MatchBuffer, Req.VerboseVerbose, Diags,
1395 MatchResult.takeError());
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001396 return StringRef::npos;
1397 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001398 size_t MatchPos = *MatchResult;
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001399 PrintMatch(true, SM, *this, i, MatchBuffer, MatchPos, CurrentMatchLen, Req,
1400 Diags);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001401 if (i == 1)
1402 FirstMatchPos = LastPos + MatchPos;
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001403
1404 // move start point after the match
1405 LastMatchEnd += MatchPos + CurrentMatchLen;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001406 }
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001407 // Full match len counts from first match pos.
1408 MatchLen = LastMatchEnd - FirstMatchPos;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001409
1410 // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
1411 // or CHECK-NOT
1412 if (!IsLabelScanMode) {
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001413 size_t MatchPos = FirstMatchPos - LastPos;
1414 StringRef MatchBuffer = Buffer.substr(LastPos);
1415 StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001416
1417 // If this check is a "CHECK-NEXT", verify that the previous match was on
1418 // the previous line (i.e. that there is one newline between them).
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001419 if (CheckNext(SM, SkippedRegion)) {
Joel E. Dennye2afb612018-12-18 00:03:51 +00001420 ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001421 Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
Joel E. Denny7df86962018-12-18 00:03:03 +00001422 Diags, Req.Verbose);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001423 return StringRef::npos;
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001424 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001425
1426 // If this check is a "CHECK-SAME", verify that the previous match was on
1427 // the same line (i.e. that there is no newline between them).
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001428 if (CheckSame(SM, SkippedRegion)) {
Joel E. Dennye2afb612018-12-18 00:03:51 +00001429 ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001430 Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
Joel E. Denny7df86962018-12-18 00:03:03 +00001431 Diags, Req.Verbose);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001432 return StringRef::npos;
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001433 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001434
1435 // If this match had "not strings", verify that they don't exist in the
1436 // skipped region.
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001437 if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001438 return StringRef::npos;
1439 }
1440
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001441 return FirstMatchPos;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001442}
1443
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001444bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
1445 if (Pat.getCheckTy() != Check::CheckNext &&
1446 Pat.getCheckTy() != Check::CheckEmpty)
1447 return false;
1448
1449 Twine CheckName =
1450 Prefix +
1451 Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
1452
1453 // Count the number of newlines between the previous match and this one.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001454 const char *FirstNewLine = nullptr;
1455 unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1456
1457 if (NumNewLines == 0) {
1458 SM.PrintMessage(Loc, SourceMgr::DK_Error,
1459 CheckName + ": is on the same line as previous match");
1460 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1461 "'next' match was here");
1462 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1463 "previous match ended here");
1464 return true;
1465 }
1466
1467 if (NumNewLines != 1) {
1468 SM.PrintMessage(Loc, SourceMgr::DK_Error,
1469 CheckName +
1470 ": is not on the line after the previous match");
1471 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1472 "'next' match was here");
1473 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1474 "previous match ended here");
1475 SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note,
1476 "non-matching line after previous match is here");
1477 return true;
1478 }
1479
1480 return false;
1481}
1482
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001483bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
1484 if (Pat.getCheckTy() != Check::CheckSame)
1485 return false;
1486
1487 // Count the number of newlines between the previous match and this one.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001488 const char *FirstNewLine = nullptr;
1489 unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1490
1491 if (NumNewLines != 0) {
1492 SM.PrintMessage(Loc, SourceMgr::DK_Error,
1493 Prefix +
1494 "-SAME: is not on the same line as the previous match");
1495 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1496 "'next' match was here");
1497 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1498 "previous match ended here");
1499 return true;
1500 }
1501
1502 return false;
1503}
1504
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001505bool FileCheckString::CheckNot(
1506 const SourceMgr &SM, StringRef Buffer,
1507 const std::vector<const FileCheckPattern *> &NotStrings,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001508 const FileCheckRequest &Req, std::vector<FileCheckDiag> *Diags) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001509 for (const FileCheckPattern *Pat : NotStrings) {
1510 assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
1511
1512 size_t MatchLen = 0;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001513 Expected<size_t> MatchResult = Pat->match(Buffer, MatchLen, SM);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001514
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001515 if (!MatchResult) {
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001516 PrintNoMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001517 Req.VerboseVerbose, Diags, MatchResult.takeError());
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001518 continue;
1519 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001520 size_t Pos = *MatchResult;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001521
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001522 PrintMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer, Pos, MatchLen,
1523 Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001524
1525 return true;
1526 }
1527
1528 return false;
1529}
1530
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001531size_t
1532FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
1533 std::vector<const FileCheckPattern *> &NotStrings,
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001534 const FileCheckRequest &Req,
1535 std::vector<FileCheckDiag> *Diags) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001536 if (DagNotStrings.empty())
1537 return 0;
1538
1539 // The start of the search range.
1540 size_t StartPos = 0;
1541
1542 struct MatchRange {
1543 size_t Pos;
1544 size_t End;
1545 };
1546 // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
1547 // ranges are erased from this list once they are no longer in the search
1548 // range.
1549 std::list<MatchRange> MatchRanges;
1550
1551 // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
1552 // group, so we don't use a range-based for loop here.
1553 for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
1554 PatItr != PatEnd; ++PatItr) {
1555 const FileCheckPattern &Pat = *PatItr;
1556 assert((Pat.getCheckTy() == Check::CheckDAG ||
1557 Pat.getCheckTy() == Check::CheckNot) &&
1558 "Invalid CHECK-DAG or CHECK-NOT!");
1559
1560 if (Pat.getCheckTy() == Check::CheckNot) {
1561 NotStrings.push_back(&Pat);
1562 continue;
1563 }
1564
1565 assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
1566
1567 // CHECK-DAG always matches from the start.
1568 size_t MatchLen = 0, MatchPos = StartPos;
1569
1570 // Search for a match that doesn't overlap a previous match in this
1571 // CHECK-DAG group.
1572 for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
1573 StringRef MatchBuffer = Buffer.substr(MatchPos);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001574 Expected<size_t> MatchResult = Pat.match(MatchBuffer, MatchLen, SM);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001575 // With a group of CHECK-DAGs, a single mismatching means the match on
1576 // that group of CHECK-DAGs fails immediately.
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001577 if (!MatchResult) {
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001578 PrintNoMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, MatchBuffer,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001579 Req.VerboseVerbose, Diags, MatchResult.takeError());
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001580 return StringRef::npos;
1581 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001582 size_t MatchPosBuf = *MatchResult;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001583 // Re-calc it as the offset relative to the start of the original string.
1584 MatchPos += MatchPosBuf;
1585 if (Req.VerboseVerbose)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001586 PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, MatchPos,
1587 MatchLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001588 MatchRange M{MatchPos, MatchPos + MatchLen};
1589 if (Req.AllowDeprecatedDagOverlap) {
1590 // We don't need to track all matches in this mode, so we just maintain
1591 // one match range that encompasses the current CHECK-DAG group's
1592 // matches.
1593 if (MatchRanges.empty())
1594 MatchRanges.insert(MatchRanges.end(), M);
1595 else {
1596 auto Block = MatchRanges.begin();
1597 Block->Pos = std::min(Block->Pos, M.Pos);
1598 Block->End = std::max(Block->End, M.End);
1599 }
1600 break;
1601 }
1602 // Iterate previous matches until overlapping match or insertion point.
1603 bool Overlap = false;
1604 for (; MI != ME; ++MI) {
1605 if (M.Pos < MI->End) {
1606 // !Overlap => New match has no overlap and is before this old match.
1607 // Overlap => New match overlaps this old match.
1608 Overlap = MI->Pos < M.End;
1609 break;
1610 }
1611 }
1612 if (!Overlap) {
1613 // Insert non-overlapping match into list.
1614 MatchRanges.insert(MI, M);
1615 break;
1616 }
1617 if (Req.VerboseVerbose) {
Joel E. Denny352695c2019-01-22 21:41:42 +00001618 // Due to their verbosity, we don't print verbose diagnostics here if
1619 // we're gathering them for a different rendering, but we always print
1620 // other diagnostics.
1621 if (!Diags) {
1622 SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
1623 SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
1624 SMRange OldRange(OldStart, OldEnd);
1625 SM.PrintMessage(OldStart, SourceMgr::DK_Note,
1626 "match discarded, overlaps earlier DAG match here",
1627 {OldRange});
1628 } else
Joel E. Dennye2afb612018-12-18 00:03:51 +00001629 Diags->rbegin()->MatchTy = FileCheckDiag::MatchFoundButDiscarded;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001630 }
1631 MatchPos = MI->End;
1632 }
1633 if (!Req.VerboseVerbose)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001634 PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, MatchPos,
1635 MatchLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001636
1637 // Handle the end of a CHECK-DAG group.
1638 if (std::next(PatItr) == PatEnd ||
1639 std::next(PatItr)->getCheckTy() == Check::CheckNot) {
1640 if (!NotStrings.empty()) {
1641 // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
1642 // CHECK-DAG, verify that there are no 'not' strings occurred in that
1643 // region.
1644 StringRef SkippedRegion =
1645 Buffer.slice(StartPos, MatchRanges.begin()->Pos);
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001646 if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001647 return StringRef::npos;
1648 // Clear "not strings".
1649 NotStrings.clear();
1650 }
1651 // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
1652 // end of this CHECK-DAG group's match range.
1653 StartPos = MatchRanges.rbegin()->End;
1654 // Don't waste time checking for (impossible) overlaps before that.
1655 MatchRanges.clear();
1656 }
1657 }
1658
1659 return StartPos;
1660}
1661
1662// A check prefix must contain only alphanumeric, hyphens and underscores.
1663static bool ValidateCheckPrefix(StringRef CheckPrefix) {
1664 Regex Validator("^[a-zA-Z0-9_-]*$");
1665 return Validator.match(CheckPrefix);
1666}
1667
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +00001668bool FileCheck::ValidateCheckPrefixes() {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001669 StringSet<> PrefixSet;
1670
1671 for (StringRef Prefix : Req.CheckPrefixes) {
1672 // Reject empty prefixes.
1673 if (Prefix == "")
1674 return false;
1675
1676 if (!PrefixSet.insert(Prefix).second)
1677 return false;
1678
1679 if (!ValidateCheckPrefix(Prefix))
1680 return false;
1681 }
1682
1683 return true;
1684}
1685
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +00001686Regex FileCheck::buildCheckPrefixRegex() {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001687 // I don't think there's a way to specify an initial value for cl::list,
1688 // so if nothing was specified, add the default
1689 if (Req.CheckPrefixes.empty())
1690 Req.CheckPrefixes.push_back("CHECK");
1691
1692 // We already validated the contents of CheckPrefixes so just concatenate
1693 // them as alternatives.
1694 SmallString<32> PrefixRegexStr;
1695 for (StringRef Prefix : Req.CheckPrefixes) {
1696 if (Prefix != Req.CheckPrefixes.front())
1697 PrefixRegexStr.push_back('|');
1698
1699 PrefixRegexStr.append(Prefix);
1700 }
1701
1702 return Regex(PrefixRegexStr);
1703}
1704
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001705Error FileCheckPatternContext::defineCmdlineVariables(
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001706 std::vector<std::string> &CmdlineDefines, SourceMgr &SM) {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001707 assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() &&
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001708 "Overriding defined variable with command-line variable definitions");
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001709
1710 if (CmdlineDefines.empty())
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001711 return Error::success();
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001712
1713 // Create a string representing the vector of command-line definitions. Each
1714 // definition is on its own line and prefixed with a definition number to
1715 // clarify which definition a given diagnostic corresponds to.
1716 unsigned I = 0;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001717 Error Errs = Error::success();
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001718 std::string CmdlineDefsDiag;
1719 StringRef Prefix1 = "Global define #";
1720 StringRef Prefix2 = ": ";
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001721 for (StringRef CmdlineDef : CmdlineDefines)
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001722 CmdlineDefsDiag +=
1723 (Prefix1 + Twine(++I) + Prefix2 + CmdlineDef + "\n").str();
1724
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001725 // Create a buffer with fake command line content in order to display
1726 // parsing diagnostic with location information and point to the
1727 // global definition with invalid syntax.
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001728 std::unique_ptr<MemoryBuffer> CmdLineDefsDiagBuffer =
1729 MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines");
1730 StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer();
1731 SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc());
1732
1733 SmallVector<StringRef, 4> CmdlineDefsDiagVec;
1734 CmdlineDefsDiagRef.split(CmdlineDefsDiagVec, '\n', -1 /*MaxSplit*/,
1735 false /*KeepEmpty*/);
1736 for (StringRef CmdlineDefDiag : CmdlineDefsDiagVec) {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001737 unsigned DefStart = CmdlineDefDiag.find(Prefix2) + Prefix2.size();
1738 StringRef CmdlineDef = CmdlineDefDiag.substr(DefStart);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001739 size_t EqIdx = CmdlineDef.find('=');
1740 if (EqIdx == StringRef::npos) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001741 Errs = joinErrors(
1742 std::move(Errs),
1743 FileCheckErrorDiagnostic::get(
1744 SM, CmdlineDef, "missing equal sign in global definition"));
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001745 continue;
1746 }
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001747
1748 // Numeric variable definition.
1749 if (CmdlineDef[0] == '#') {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001750 StringRef CmdlineName = CmdlineDef.substr(1, EqIdx - 1);
Thomas Preud'homme56f63082019-07-05 16:25:46 +00001751 Expected<FileCheckNumericVariable *> ParseResult =
1752 FileCheckPattern::parseNumericVariableDefinition(CmdlineName, this, 0,
1753 SM);
1754 if (!ParseResult) {
1755 Errs = joinErrors(std::move(Errs), ParseResult.takeError());
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001756 continue;
1757 }
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001758
1759 StringRef CmdlineVal = CmdlineDef.substr(EqIdx + 1);
1760 uint64_t Val;
1761 if (CmdlineVal.getAsInteger(10, Val)) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001762 Errs = joinErrors(std::move(Errs),
1763 FileCheckErrorDiagnostic::get(
1764 SM, CmdlineVal,
1765 "invalid value in numeric variable definition '" +
1766 CmdlineVal + "'"));
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001767 continue;
1768 }
Thomas Preud'homme56f63082019-07-05 16:25:46 +00001769 FileCheckNumericVariable *DefinedNumericVariable = *ParseResult;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001770 DefinedNumericVariable->setValue(Val);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001771
1772 // Record this variable definition.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001773 GlobalNumericVariableTable[DefinedNumericVariable->getName()] =
1774 DefinedNumericVariable;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001775 } else {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001776 // String variable definition.
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001777 std::pair<StringRef, StringRef> CmdlineNameVal = CmdlineDef.split('=');
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001778 StringRef CmdlineName = CmdlineNameVal.first;
1779 StringRef OrigCmdlineName = CmdlineName;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001780 bool IsPseudo;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001781 Expected<StringRef> ParseVarResult =
1782 FileCheckPattern::parseVariable(CmdlineName, IsPseudo, SM);
1783 if (!ParseVarResult) {
1784 Errs = joinErrors(std::move(Errs), ParseVarResult.takeError());
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001785 continue;
1786 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001787 // Check that CmdlineName does not denote a pseudo variable is only
1788 // composed of the parsed numeric variable. This catches cases like
1789 // "FOO+2" in a "FOO+2=10" definition.
1790 if (IsPseudo || !CmdlineName.empty()) {
1791 Errs = joinErrors(std::move(Errs),
1792 FileCheckErrorDiagnostic::get(
1793 SM, OrigCmdlineName,
1794 "invalid name in string variable definition '" +
1795 OrigCmdlineName + "'"));
1796 continue;
1797 }
1798 StringRef Name = *ParseVarResult;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001799
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001800 // Detect collisions between string and numeric variables when the former
1801 // is created later than the latter.
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001802 if (GlobalNumericVariableTable.find(Name) !=
1803 GlobalNumericVariableTable.end()) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001804 Errs = joinErrors(std::move(Errs), FileCheckErrorDiagnostic::get(
1805 SM, Name,
1806 "numeric variable with name '" +
1807 Name + "' already exists"));
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001808 continue;
1809 }
1810 GlobalVariableTable.insert(CmdlineNameVal);
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001811 // Mark the string variable as defined to detect collisions between
1812 // string and numeric variables in DefineCmdlineVariables when the latter
1813 // is created later than the former. We cannot reuse GlobalVariableTable
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001814 // for this by populating it with an empty string since we would then
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001815 // lose the ability to detect the use of an undefined variable in
1816 // match().
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001817 DefinedVariableTable[Name] = true;
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001818 }
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001819 }
1820
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001821 return Errs;
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001822}
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001823
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001824void FileCheckPatternContext::clearLocalVars() {
1825 SmallVector<StringRef, 16> LocalPatternVars, LocalNumericVars;
1826 for (const StringMapEntry<StringRef> &Var : GlobalVariableTable)
1827 if (Var.first()[0] != '$')
1828 LocalPatternVars.push_back(Var.first());
1829
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001830 // Numeric substitution reads the value of a variable directly, not via
1831 // GlobalNumericVariableTable. Therefore, we clear local variables by
1832 // clearing their value which will lead to a numeric substitution failure. We
1833 // also mark the variable for removal from GlobalNumericVariableTable since
1834 // this is what defineCmdlineVariables checks to decide that no global
1835 // variable has been defined.
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001836 for (const auto &Var : GlobalNumericVariableTable)
1837 if (Var.first()[0] != '$') {
1838 Var.getValue()->clearValue();
1839 LocalNumericVars.push_back(Var.first());
1840 }
1841
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001842 for (const auto &Var : LocalPatternVars)
1843 GlobalVariableTable.erase(Var);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001844 for (const auto &Var : LocalNumericVars)
1845 GlobalNumericVariableTable.erase(Var);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001846}
1847
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +00001848bool FileCheck::CheckInput(SourceMgr &SM, StringRef Buffer,
1849 ArrayRef<FileCheckString> CheckStrings,
1850 std::vector<FileCheckDiag> *Diags) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001851 bool ChecksFailed = false;
1852
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001853 unsigned i = 0, j = 0, e = CheckStrings.size();
1854 while (true) {
1855 StringRef CheckRegion;
1856 if (j == e) {
1857 CheckRegion = Buffer;
1858 } else {
1859 const FileCheckString &CheckLabelStr = CheckStrings[j];
1860 if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
1861 ++j;
1862 continue;
1863 }
1864
1865 // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
1866 size_t MatchLabelLen = 0;
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001867 size_t MatchLabelPos =
1868 CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001869 if (MatchLabelPos == StringRef::npos)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001870 // Immediately bail if CHECK-LABEL fails, nothing else we can do.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001871 return false;
1872
1873 CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
1874 Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
1875 ++j;
1876 }
1877
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001878 // Do not clear the first region as it's the one before the first
1879 // CHECK-LABEL and it would clear variables defined on the command-line
1880 // before they get used.
1881 if (i != 0 && Req.EnableVarScope)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001882 PatternContext.clearLocalVars();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001883
1884 for (; i != j; ++i) {
1885 const FileCheckString &CheckStr = CheckStrings[i];
1886
1887 // Check each string within the scanned region, including a second check
1888 // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
1889 size_t MatchLen = 0;
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001890 size_t MatchPos =
1891 CheckStr.Check(SM, CheckRegion, false, MatchLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001892
1893 if (MatchPos == StringRef::npos) {
1894 ChecksFailed = true;
1895 i = j;
1896 break;
1897 }
1898
1899 CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
1900 }
1901
1902 if (j == e)
1903 break;
1904 }
1905
1906 // Success if no checks failed.
1907 return !ChecksFailed;
1908}