blob: 7d1febf3c171c88a6b09101a499e6d4ce3d866fe [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'homme7b4ecdd2019-05-14 11:58:30 +000027bool FileCheckNumericVariable::setValue(uint64_t NewValue) {
28 if (Value)
29 return true;
30 Value = NewValue;
31 return false;
32}
33
34bool FileCheckNumericVariable::clearValue() {
35 if (!Value)
36 return true;
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +000037 Value = None;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000038 return false;
39}
40
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +000041Expected<uint64_t> FileCheckExpression::eval() const {
42 assert(LeftOp && "Evaluating an empty expression");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +000043 Optional<uint64_t> LeftOpValue = LeftOp->getValue();
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000044 // Variable is undefined.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +000045 if (!LeftOpValue)
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000046 return make_error<FileCheckUndefVarError>(LeftOp->getName());
Thomas Preud'homme71d3f222019-06-06 13:21:06 +000047 return EvalBinop(*LeftOpValue, RightOp);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000048}
49
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000050Expected<std::string> FileCheckNumericSubstitution::getResult() const {
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +000051 Expected<uint64_t> EvaluatedValue = Expression->eval();
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +000052 if (!EvaluatedValue)
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000053 return EvaluatedValue.takeError();
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +000054 return utostr(*EvaluatedValue);
55}
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000056
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000057Expected<std::string> FileCheckStringSubstitution::getResult() const {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000058 // Look up the value and escape it so that we can put it into the regex.
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000059 Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000060 if (!VarVal)
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000061 return VarVal.takeError();
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +000062 return Regex::escape(*VarVal);
Thomas Preud'homme288ed912019-05-02 00:04:38 +000063}
64
Thomas Preud'homme5a330472019-04-29 13:32:36 +000065bool FileCheckPattern::isValidVarNameStart(char C) {
66 return C == '_' || isalpha(C);
67}
68
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000069Expected<StringRef> FileCheckPattern::parseVariable(StringRef &Str,
70 bool &IsPseudo,
71 const SourceMgr &SM) {
Thomas Preud'homme5a330472019-04-29 13:32:36 +000072 if (Str.empty())
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000073 return FileCheckErrorDiagnostic::get(SM, Str, "empty variable name");
Thomas Preud'homme5a330472019-04-29 13:32:36 +000074
75 bool ParsedOneChar = false;
76 unsigned I = 0;
77 IsPseudo = Str[0] == '@';
78
79 // Global vars start with '$'.
80 if (Str[0] == '$' || IsPseudo)
81 ++I;
82
83 for (unsigned E = Str.size(); I != E; ++I) {
84 if (!ParsedOneChar && !isValidVarNameStart(Str[I]))
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000085 return FileCheckErrorDiagnostic::get(SM, Str, "invalid variable name");
Thomas Preud'homme5a330472019-04-29 13:32:36 +000086
87 // Variable names are composed of alphanumeric characters and underscores.
88 if (Str[I] != '_' && !isalnum(Str[I]))
89 break;
90 ParsedOneChar = true;
91 }
92
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000093 StringRef Name = Str.take_front(I);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +000094 Str = Str.substr(I);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +000095 return Name;
Thomas Preud'homme5a330472019-04-29 13:32:36 +000096}
97
Thomas Preud'homme288ed912019-05-02 00:04:38 +000098// StringRef holding all characters considered as horizontal whitespaces by
99// FileCheck input canonicalization.
100StringRef SpaceChars = " \t";
101
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000102// Parsing helper function that strips the first character in S and returns it.
103static char popFront(StringRef &S) {
104 char C = S.front();
105 S = S.drop_front();
106 return C;
107}
108
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000109char FileCheckUndefVarError::ID = 0;
110char FileCheckErrorDiagnostic::ID = 0;
111char FileCheckNotFoundError::ID = 0;
112
113Error FileCheckPattern::parseNumericVariableDefinition(
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000114 StringRef &Expr, StringRef &Name, FileCheckPatternContext *Context,
115 const SourceMgr &SM) {
116 bool IsPseudo;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000117 Expected<StringRef> ParseVarResult = parseVariable(Expr, IsPseudo, SM);
118 if (!ParseVarResult)
119 return ParseVarResult.takeError();
120 Name = *ParseVarResult;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000121
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000122 if (IsPseudo)
123 return FileCheckErrorDiagnostic::get(
124 SM, Name, "definition of pseudo numeric variable unsupported");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000125
126 // Detect collisions between string and numeric variables when the latter
127 // is created later than the former.
128 if (Context->DefinedVariableTable.find(Name) !=
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000129 Context->DefinedVariableTable.end())
130 return FileCheckErrorDiagnostic::get(
131 SM, Name, "string variable with name '" + Name + "' already exists");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000132
Thomas Preud'homme28196a52019-07-05 12:01:06 +0000133 Expr = Expr.ltrim(SpaceChars);
134 if (!Expr.empty())
135 return FileCheckErrorDiagnostic::get(
136 SM, Expr, "unexpected characters after numeric variable name");
137
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000138 return Error::success();
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000139}
140
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000141Expected<FileCheckNumericVariable *>
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000142FileCheckPattern::parseNumericVariableUse(StringRef &Expr,
143 const SourceMgr &SM) const {
144 bool IsPseudo;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000145 Expected<StringRef> ParseVarResult = parseVariable(Expr, IsPseudo, SM);
146 if (!ParseVarResult)
147 return ParseVarResult.takeError();
148 StringRef Name = *ParseVarResult;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000149
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000150 if (IsPseudo && !Name.equals("@LINE"))
151 return FileCheckErrorDiagnostic::get(
152 SM, Name, "invalid pseudo numeric variable '" + Name + "'");
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000153
Thomas Preud'homme41f2bea2019-07-05 12:01:12 +0000154 // Numeric variable definitions and uses are parsed in the order in which
155 // they appear in the CHECK patterns. For each definition, the pointer to the
156 // class instance of the corresponding numeric variable definition is stored
157 // in GlobalNumericVariableTable in parsePattern. Therefore, the pointer we
158 // get below is for the class instance corresponding to the last definition
159 // of this variable use.
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000160 auto VarTableIter = Context->GlobalNumericVariableTable.find(Name);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000161 if (VarTableIter == Context->GlobalNumericVariableTable.end())
162 return FileCheckErrorDiagnostic::get(
163 SM, Name, "using undefined numeric variable '" + Name + "'");
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000164
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000165 FileCheckNumericVariable *NumericVariable = VarTableIter->second;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000166 if (!IsPseudo && NumericVariable->getDefLineNumber() == LineNumber)
167 return FileCheckErrorDiagnostic::get(
168 SM, Name,
169 "numeric variable '" + Name + "' defined on the same line as used");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000170
171 return NumericVariable;
172}
173
174static uint64_t add(uint64_t LeftOp, uint64_t RightOp) {
175 return LeftOp + RightOp;
176}
177
178static uint64_t sub(uint64_t LeftOp, uint64_t RightOp) {
179 return LeftOp - RightOp;
180}
181
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000182Expected<FileCheckExpression *>
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000183FileCheckPattern::parseBinop(StringRef &Expr, const SourceMgr &SM) const {
184 Expected<FileCheckNumericVariable *> LeftParseResult =
185 parseNumericVariableUse(Expr, SM);
186 if (!LeftParseResult) {
187 return LeftParseResult.takeError();
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000188 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000189 FileCheckNumericVariable *LeftOp = *LeftParseResult;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000190
191 // Check if this is a supported operation and select a function to perform
192 // it.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000193 Expr = Expr.ltrim(SpaceChars);
194 if (Expr.empty())
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000195 return Context->makeExpression(add, LeftOp, 0);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000196 SMLoc OpLoc = SMLoc::getFromPointer(Expr.data());
197 char Operator = popFront(Expr);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000198 binop_eval_t EvalBinop;
199 switch (Operator) {
200 case '+':
201 EvalBinop = add;
202 break;
203 case '-':
204 EvalBinop = sub;
205 break;
206 default:
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000207 return FileCheckErrorDiagnostic::get(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000208 SM, OpLoc, Twine("unsupported operation '") + Twine(Operator) + "'");
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000209 }
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000210
211 // Parse right operand.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000212 Expr = Expr.ltrim(SpaceChars);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000213 if (Expr.empty())
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000214 return FileCheckErrorDiagnostic::get(SM, Expr,
215 "missing operand in expression");
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000216 uint64_t RightOp;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000217 if (Expr.consumeInteger(10, RightOp))
218 return FileCheckErrorDiagnostic::get(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000219 SM, Expr, "invalid offset in expression '" + Expr + "'");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000220 Expr = Expr.ltrim(SpaceChars);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000221 if (!Expr.empty())
222 return FileCheckErrorDiagnostic::get(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000223 SM, Expr, "unexpected characters at end of expression '" + Expr + "'");
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000224
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000225 return Context->makeExpression(EvalBinop, LeftOp, RightOp);
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000226}
227
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000228Expected<FileCheckExpression *> FileCheckPattern::parseNumericSubstitutionBlock(
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000229 StringRef Expr,
230 Optional<FileCheckNumericVariable *> &DefinedNumericVariable,
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000231 const SourceMgr &SM) const {
232 // Parse the numeric variable definition.
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000233 DefinedNumericVariable = None;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000234 size_t DefEnd = Expr.find(':');
235 if (DefEnd != StringRef::npos) {
236 StringRef DefExpr = Expr.substr(0, DefEnd);
Thomas Preud'homme28196a52019-07-05 12:01:06 +0000237 StringRef UseExpr = Expr.substr(DefEnd + 1);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000238
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000239 UseExpr = UseExpr.ltrim(SpaceChars);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000240 if (!UseExpr.empty())
241 return FileCheckErrorDiagnostic::get(
242 SM, UseExpr,
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000243 "unexpected string after variable definition: '" + UseExpr + "'");
Thomas Preud'homme28196a52019-07-05 12:01:06 +0000244
245 DefExpr = DefExpr.ltrim(SpaceChars);
246 StringRef Name;
247 Error Err = parseNumericVariableDefinition(DefExpr, Name, Context, SM);
248 if (Err)
249 return std::move(Err);
250 DefinedNumericVariable = Context->makeNumericVariable(LineNumber, Name);
251
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000252 return Context->makeExpression(add, nullptr, 0);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000253 }
254
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000255 // Parse the expression itself.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000256 Expr = Expr.ltrim(SpaceChars);
257 return parseBinop(Expr, SM);
258}
259
260bool FileCheckPattern::parsePattern(StringRef PatternStr, StringRef Prefix,
261 SourceMgr &SM,
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000262 const FileCheckRequest &Req) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000263 bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot;
264
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000265 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
266
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000267 // Create fake @LINE pseudo variable definition.
268 StringRef LinePseudo = "@LINE";
269 uint64_t LineNumber64 = LineNumber;
270 FileCheckNumericVariable *LinePseudoVar =
271 Context->makeNumericVariable(LinePseudo, LineNumber64);
272 Context->GlobalNumericVariableTable[LinePseudo] = LinePseudoVar;
273
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000274 if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
275 // Ignore trailing whitespace.
276 while (!PatternStr.empty() &&
277 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
278 PatternStr = PatternStr.substr(0, PatternStr.size() - 1);
279
280 // Check that there is something on the line.
281 if (PatternStr.empty() && CheckTy != Check::CheckEmpty) {
282 SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
283 "found empty check string with prefix '" + Prefix + ":'");
284 return true;
285 }
286
287 if (!PatternStr.empty() && CheckTy == Check::CheckEmpty) {
288 SM.PrintMessage(
289 PatternLoc, SourceMgr::DK_Error,
290 "found non-empty check string for empty check with prefix '" + Prefix +
291 ":'");
292 return true;
293 }
294
295 if (CheckTy == Check::CheckEmpty) {
296 RegExStr = "(\n$)";
297 return false;
298 }
299
300 // Check to see if this is a fixed string, or if it has regex pieces.
301 if (!MatchFullLinesHere &&
302 (PatternStr.size() < 2 || (PatternStr.find("{{") == StringRef::npos &&
303 PatternStr.find("[[") == StringRef::npos))) {
304 FixedStr = PatternStr;
305 return false;
306 }
307
308 if (MatchFullLinesHere) {
309 RegExStr += '^';
310 if (!Req.NoCanonicalizeWhiteSpace)
311 RegExStr += " *";
312 }
313
314 // Paren value #0 is for the fully matched string. Any new parenthesized
315 // values add from there.
316 unsigned CurParen = 1;
317
318 // Otherwise, there is at least one regex piece. Build up the regex pattern
319 // by escaping scary characters in fixed strings, building up one big regex.
320 while (!PatternStr.empty()) {
321 // RegEx matches.
322 if (PatternStr.startswith("{{")) {
323 // This is the start of a regex match. Scan for the }}.
324 size_t End = PatternStr.find("}}");
325 if (End == StringRef::npos) {
326 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
327 SourceMgr::DK_Error,
328 "found start of regex string with no end '}}'");
329 return true;
330 }
331
332 // Enclose {{}} patterns in parens just like [[]] even though we're not
333 // capturing the result for any purpose. This is required in case the
334 // expression contains an alternation like: CHECK: abc{{x|z}}def. We
335 // want this to turn into: "abc(x|z)def" not "abcx|zdef".
336 RegExStr += '(';
337 ++CurParen;
338
339 if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
340 return true;
341 RegExStr += ')';
342
343 PatternStr = PatternStr.substr(End + 2);
344 continue;
345 }
346
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000347 // String and numeric substitution blocks. String substitution blocks come
348 // in two forms: [[foo:.*]] and [[foo]]. The former matches .* (or some
349 // other regex) and assigns it to the string variable 'foo'. The latter
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000350 // substitutes foo's value. Numeric substitution blocks work the same way
351 // as string ones, but start with a '#' sign after the double brackets.
352 // Both string and numeric variable names must satisfy the regular
353 // expression "[a-zA-Z_][0-9a-zA-Z_]*" to be valid, as this helps catch
354 // some common errors.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000355 if (PatternStr.startswith("[[")) {
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000356 StringRef UnparsedPatternStr = PatternStr.substr(2);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000357 // Find the closing bracket pair ending the match. End is going to be an
358 // offset relative to the beginning of the match string.
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000359 size_t End = FindRegexVarEnd(UnparsedPatternStr, SM);
360 StringRef MatchStr = UnparsedPatternStr.substr(0, End);
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000361 bool IsNumBlock = MatchStr.consume_front("#");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000362
363 if (End == StringRef::npos) {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000364 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
365 SourceMgr::DK_Error,
366 "Invalid substitution block, no ]] found");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000367 return true;
368 }
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000369 // Strip the substitution block we are parsing. End points to the start
370 // of the "]]" closing the expression so account for it in computing the
371 // index of the first unparsed character.
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000372 PatternStr = UnparsedPatternStr.substr(End + 2);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000373
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000374 bool IsDefinition = false;
375 StringRef DefName;
376 StringRef SubstStr;
377 StringRef MatchRegexp;
378 size_t SubstInsertIdx = RegExStr.size();
379
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000380 // Parse string variable or legacy expression.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000381 if (!IsNumBlock) {
382 size_t VarEndIdx = MatchStr.find(":");
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000383 size_t SpacePos = MatchStr.substr(0, VarEndIdx).find_first_of(" \t");
384 if (SpacePos != StringRef::npos) {
385 SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data() + SpacePos),
386 SourceMgr::DK_Error, "unexpected whitespace");
387 return true;
388 }
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000389
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000390 // Get the name (e.g. "foo") and verify it is well formed.
391 bool IsPseudo;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000392 StringRef OrigMatchStr = MatchStr;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000393 Expected<StringRef> ParseVarResult =
394 parseVariable(MatchStr, IsPseudo, SM);
395 if (!ParseVarResult) {
396 logAllUnhandledErrors(ParseVarResult.takeError(), errs());
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000397 return true;
398 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000399 StringRef Name = *ParseVarResult;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000400
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000401 IsDefinition = (VarEndIdx != StringRef::npos);
402 if (IsDefinition) {
403 if ((IsPseudo || !MatchStr.consume_front(":"))) {
404 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
405 SourceMgr::DK_Error,
406 "invalid name in string variable definition");
407 return true;
408 }
409
410 // Detect collisions between string and numeric variables when the
411 // former is created later than the latter.
412 if (Context->GlobalNumericVariableTable.find(Name) !=
413 Context->GlobalNumericVariableTable.end()) {
414 SM.PrintMessage(
415 SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
416 "numeric variable with name '" + Name + "' already exists");
417 return true;
418 }
419 DefName = Name;
420 MatchRegexp = MatchStr;
421 } else {
422 if (IsPseudo) {
423 MatchStr = OrigMatchStr;
424 IsNumBlock = true;
425 } else
426 SubstStr = Name;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000427 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000428 }
429
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000430 // Parse numeric substitution block.
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000431 FileCheckExpression *Expression;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000432 Optional<FileCheckNumericVariable *> DefinedNumericVariable;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000433 if (IsNumBlock) {
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000434 Expected<FileCheckExpression *> ParseResult =
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000435 parseNumericSubstitutionBlock(MatchStr, DefinedNumericVariable, SM);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000436 if (!ParseResult) {
437 logAllUnhandledErrors(ParseResult.takeError(), errs());
Thomas Preud'homme15cb1f12019-04-29 17:46:26 +0000438 return true;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000439 }
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000440 Expression = *ParseResult;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000441 if (DefinedNumericVariable) {
442 IsDefinition = true;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000443 DefName = (*DefinedNumericVariable)->getName();
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000444 MatchRegexp = StringRef("[0-9]+");
445 } else
446 SubstStr = MatchStr;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000447 }
448
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000449 // Handle substitutions: [[foo]] and [[#<foo expr>]].
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000450 if (!IsDefinition) {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000451 // Handle substitution of string variables that were defined earlier on
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000452 // the same line by emitting a backreference. Expressions do not
453 // support substituting a numeric variable defined on the same line.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000454 if (!IsNumBlock && VariableDefs.find(SubstStr) != VariableDefs.end()) {
455 unsigned CaptureParenGroup = VariableDefs[SubstStr];
456 if (CaptureParenGroup < 1 || CaptureParenGroup > 9) {
457 SM.PrintMessage(SMLoc::getFromPointer(SubstStr.data()),
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000458 SourceMgr::DK_Error,
459 "Can't back-reference more than 9 variables");
460 return true;
461 }
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000462 AddBackrefToRegEx(CaptureParenGroup);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000463 } else {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000464 // Handle substitution of string variables ([[<var>]]) defined in
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000465 // previous CHECK patterns, and substitution of expressions.
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000466 FileCheckSubstitution *Substitution =
467 IsNumBlock
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000468 ? Context->makeNumericSubstitution(SubstStr, Expression,
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000469 SubstInsertIdx)
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000470 : Context->makeStringSubstitution(SubstStr, SubstInsertIdx);
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000471 Substitutions.push_back(Substitution);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000472 }
473 continue;
474 }
475
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000476 // Handle variable definitions: [[<def>:(...)]] and
477 // [[#(...)<def>:(...)]].
478 if (IsNumBlock) {
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000479 FileCheckNumericVariableMatch NumericVariableDefinition = {
480 *DefinedNumericVariable, CurParen};
481 NumericVariableDefs[DefName] = NumericVariableDefinition;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000482 // This store is done here rather than in match() to allow
483 // parseNumericVariableUse() to get the pointer to the class instance
484 // of the right variable definition corresponding to a given numeric
485 // variable use.
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000486 Context->GlobalNumericVariableTable[DefName] = *DefinedNumericVariable;
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000487 } else {
488 VariableDefs[DefName] = CurParen;
489 // Mark the string variable as defined to detect collisions between
490 // string and numeric variables in parseNumericVariableUse() and
491 // DefineCmdlineVariables() when the latter is created later than the
492 // former. We cannot reuse GlobalVariableTable for this by populating
493 // it with an empty string since we would then lose the ability to
494 // detect the use of an undefined variable in match().
495 Context->DefinedVariableTable[DefName] = true;
496 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000497 RegExStr += '(';
498 ++CurParen;
499
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000500 if (AddRegExToRegEx(MatchRegexp, CurParen, SM))
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000501 return true;
502
503 RegExStr += ')';
504 }
505
506 // Handle fixed string matches.
507 // Find the end, which is the start of the next regex.
508 size_t FixedMatchEnd = PatternStr.find("{{");
509 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
510 RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
511 PatternStr = PatternStr.substr(FixedMatchEnd);
512 }
513
514 if (MatchFullLinesHere) {
515 if (!Req.NoCanonicalizeWhiteSpace)
516 RegExStr += " *";
517 RegExStr += '$';
518 }
519
520 return false;
521}
522
523bool FileCheckPattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
524 Regex R(RS);
525 std::string Error;
526 if (!R.isValid(Error)) {
527 SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error,
528 "invalid regex: " + Error);
529 return true;
530 }
531
532 RegExStr += RS.str();
533 CurParen += R.getNumMatches();
534 return false;
535}
536
537void FileCheckPattern::AddBackrefToRegEx(unsigned BackrefNum) {
538 assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
539 std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum);
540 RegExStr += Backref;
541}
542
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000543Expected<size_t> FileCheckPattern::match(StringRef Buffer, size_t &MatchLen,
544 const SourceMgr &SM) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000545 // If this is the EOF pattern, match it immediately.
546 if (CheckTy == Check::CheckEOF) {
547 MatchLen = 0;
548 return Buffer.size();
549 }
550
551 // If this is a fixed string pattern, just match it now.
552 if (!FixedStr.empty()) {
553 MatchLen = FixedStr.size();
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000554 size_t Pos = Buffer.find(FixedStr);
555 if (Pos == StringRef::npos)
556 return make_error<FileCheckNotFoundError>();
557 return Pos;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000558 }
559
560 // Regex match.
561
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000562 // If there are substitutions, we need to create a temporary string with the
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000563 // actual value.
564 StringRef RegExToMatch = RegExStr;
565 std::string TmpStr;
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000566 if (!Substitutions.empty()) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000567 TmpStr = RegExStr;
568
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000569 size_t InsertOffset = 0;
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000570 // Substitute all string variables and expressions whose values are only
571 // now known. Use of string variables defined on the same line are handled
572 // by back-references.
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000573 for (const auto &Substitution : Substitutions) {
574 // Substitute and check for failure (e.g. use of undefined variable).
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000575 Expected<std::string> Value = Substitution->getResult();
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000576 if (!Value)
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000577 return Value.takeError();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000578
579 // Plop it into the regex at the adjusted offset.
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000580 TmpStr.insert(TmpStr.begin() + Substitution->getIndex() + InsertOffset,
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000581 Value->begin(), Value->end());
582 InsertOffset += Value->size();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000583 }
584
585 // Match the newly constructed regex.
586 RegExToMatch = TmpStr;
587 }
588
589 SmallVector<StringRef, 4> MatchInfo;
590 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000591 return make_error<FileCheckNotFoundError>();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000592
593 // Successful regex match.
594 assert(!MatchInfo.empty() && "Didn't get any match");
595 StringRef FullMatch = MatchInfo[0];
596
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000597 // If this defines any string variables, remember their values.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000598 for (const auto &VariableDef : VariableDefs) {
599 assert(VariableDef.second < MatchInfo.size() && "Internal paren error");
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000600 Context->GlobalVariableTable[VariableDef.first] =
601 MatchInfo[VariableDef.second];
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000602 }
603
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000604 // If this defines any numeric variables, remember their values.
605 for (const auto &NumericVariableDef : NumericVariableDefs) {
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000606 const FileCheckNumericVariableMatch &NumericVariableMatch =
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000607 NumericVariableDef.getValue();
608 unsigned CaptureParenGroup = NumericVariableMatch.CaptureParenGroup;
609 assert(CaptureParenGroup < MatchInfo.size() && "Internal paren error");
610 FileCheckNumericVariable *DefinedNumericVariable =
611 NumericVariableMatch.DefinedNumericVariable;
612
613 StringRef MatchedValue = MatchInfo[CaptureParenGroup];
614 uint64_t Val;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000615 if (MatchedValue.getAsInteger(10, Val))
616 return FileCheckErrorDiagnostic::get(SM, MatchedValue,
617 "Unable to represent numeric value");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000618 if (DefinedNumericVariable->setValue(Val))
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000619 llvm_unreachable("Numeric variable redefined");
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000620 }
621
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000622 // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
623 // the required preceding newline, which is consumed by the pattern in the
624 // case of CHECK-EMPTY but not CHECK-NEXT.
625 size_t MatchStartSkip = CheckTy == Check::CheckEmpty;
626 MatchLen = FullMatch.size() - MatchStartSkip;
627 return FullMatch.data() - Buffer.data() + MatchStartSkip;
628}
629
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000630unsigned FileCheckPattern::computeMatchDistance(StringRef Buffer) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000631 // Just compute the number of matching characters. For regular expressions, we
632 // just compare against the regex itself and hope for the best.
633 //
634 // FIXME: One easy improvement here is have the regex lib generate a single
635 // example regular expression which matches, and use that as the example
636 // string.
637 StringRef ExampleString(FixedStr);
638 if (ExampleString.empty())
639 ExampleString = RegExStr;
640
641 // Only compare up to the first line in the buffer, or the string size.
642 StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
643 BufferPrefix = BufferPrefix.split('\n').first;
644 return BufferPrefix.edit_distance(ExampleString);
645}
646
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000647void FileCheckPattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
648 SMRange MatchRange) const {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +0000649 // Print what we know about substitutions.
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000650 if (!Substitutions.empty()) {
651 for (const auto &Substitution : Substitutions) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000652 SmallString<256> Msg;
653 raw_svector_ostream OS(Msg);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000654 Expected<std::string> MatchedValue = Substitution->getResult();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000655
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000656 // Substitution failed or is not known at match time, print the undefined
657 // variable it uses.
658 if (!MatchedValue) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000659 bool UndefSeen = false;
660 handleAllErrors(MatchedValue.takeError(),
661 [](const FileCheckNotFoundError &E) {},
Thomas Preud'hommea188ad22019-07-05 12:00:56 +0000662 // Handled in PrintNoMatch().
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000663 [](const FileCheckErrorDiagnostic &E) {},
664 [&](const FileCheckUndefVarError &E) {
665 if (!UndefSeen) {
666 OS << "uses undefined variable ";
667 UndefSeen = true;
668 }
669 E.log(OS);
670 },
671 [](const ErrorInfoBase &E) {
672 llvm_unreachable("Unexpected error");
673 });
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000674 } else {
675 // Substitution succeeded. Print substituted value.
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000676 OS << "with \"";
677 OS.write_escaped(Substitution->getFromString()) << "\" equal to \"";
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000678 OS.write_escaped(*MatchedValue) << "\"";
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000679 }
680
681 if (MatchRange.isValid())
682 SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, OS.str(),
683 {MatchRange});
684 else
685 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
686 SourceMgr::DK_Note, OS.str());
687 }
688 }
689}
690
Joel E. Denny3c5d2672018-12-18 00:01:39 +0000691static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy,
692 const SourceMgr &SM, SMLoc Loc,
693 Check::FileCheckType CheckTy,
694 StringRef Buffer, size_t Pos, size_t Len,
Joel E. Denny7df86962018-12-18 00:03:03 +0000695 std::vector<FileCheckDiag> *Diags,
696 bool AdjustPrevDiag = false) {
Joel E. Denny3c5d2672018-12-18 00:01:39 +0000697 SMLoc Start = SMLoc::getFromPointer(Buffer.data() + Pos);
698 SMLoc End = SMLoc::getFromPointer(Buffer.data() + Pos + Len);
699 SMRange Range(Start, End);
Joel E. Denny96f0e842018-12-18 00:03:36 +0000700 if (Diags) {
Joel E. Denny7df86962018-12-18 00:03:03 +0000701 if (AdjustPrevDiag)
702 Diags->rbegin()->MatchTy = MatchTy;
703 else
704 Diags->emplace_back(SM, CheckTy, Loc, MatchTy, Range);
705 }
Joel E. Denny3c5d2672018-12-18 00:01:39 +0000706 return Range;
707}
708
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000709void FileCheckPattern::printFuzzyMatch(
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000710 const SourceMgr &SM, StringRef Buffer,
Joel E. Denny2c007c82018-12-18 00:02:04 +0000711 std::vector<FileCheckDiag> *Diags) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000712 // Attempt to find the closest/best fuzzy match. Usually an error happens
713 // because some string in the output didn't exactly match. In these cases, we
714 // would like to show the user a best guess at what "should have" matched, to
715 // save them having to actually check the input manually.
716 size_t NumLinesForward = 0;
717 size_t Best = StringRef::npos;
718 double BestQuality = 0;
719
720 // Use an arbitrary 4k limit on how far we will search.
721 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
722 if (Buffer[i] == '\n')
723 ++NumLinesForward;
724
725 // Patterns have leading whitespace stripped, so skip whitespace when
726 // looking for something which looks like a pattern.
727 if (Buffer[i] == ' ' || Buffer[i] == '\t')
728 continue;
729
730 // Compute the "quality" of this match as an arbitrary combination of the
731 // match distance and the number of lines skipped to get to this match.
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000732 unsigned Distance = computeMatchDistance(Buffer.substr(i));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000733 double Quality = Distance + (NumLinesForward / 100.);
734
735 if (Quality < BestQuality || Best == StringRef::npos) {
736 Best = i;
737 BestQuality = Quality;
738 }
739 }
740
741 // Print the "possible intended match here" line if we found something
742 // reasonable and not equal to what we showed in the "scanning from here"
743 // line.
744 if (Best && Best != StringRef::npos && BestQuality < 50) {
Joel E. Denny2c007c82018-12-18 00:02:04 +0000745 SMRange MatchRange =
746 ProcessMatchResult(FileCheckDiag::MatchFuzzy, SM, getLoc(),
747 getCheckTy(), Buffer, Best, 0, Diags);
748 SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note,
749 "possible intended match here");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000750
751 // FIXME: If we wanted to be really friendly we would show why the match
752 // failed, as it can be hard to spot simple one character differences.
753 }
754}
755
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000756Expected<StringRef>
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000757FileCheckPatternContext::getPatternVarValue(StringRef VarName) {
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000758 auto VarIter = GlobalVariableTable.find(VarName);
759 if (VarIter == GlobalVariableTable.end())
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +0000760 return make_error<FileCheckUndefVarError>(VarName);
Thomas Preud'hommee038fa72019-04-15 10:10:11 +0000761
762 return VarIter->second;
763}
764
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000765FileCheckExpression *
766FileCheckPatternContext::makeExpression(binop_eval_t EvalBinop,
767 FileCheckNumericVariable *OperandLeft,
768 uint64_t OperandRight) {
769 Expressions.push_back(llvm::make_unique<FileCheckExpression>(
770 EvalBinop, OperandLeft, OperandRight));
771 return Expressions.back().get();
Thomas Preud'homme288ed912019-05-02 00:04:38 +0000772}
773
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000774template <class... Types>
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000775FileCheckNumericVariable *
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000776FileCheckPatternContext::makeNumericVariable(Types... args) {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000777 NumericVariables.push_back(
Thomas Preud'homme71d3f222019-06-06 13:21:06 +0000778 llvm::make_unique<FileCheckNumericVariable>(args...));
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +0000779 return NumericVariables.back().get();
780}
781
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000782FileCheckSubstitution *
783FileCheckPatternContext::makeStringSubstitution(StringRef VarName,
784 size_t InsertIdx) {
785 Substitutions.push_back(
786 llvm::make_unique<FileCheckStringSubstitution>(this, VarName, InsertIdx));
787 return Substitutions.back().get();
788}
789
790FileCheckSubstitution *FileCheckPatternContext::makeNumericSubstitution(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000791 StringRef ExpressionStr, FileCheckExpression *Expression,
792 size_t InsertIdx) {
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000793 Substitutions.push_back(llvm::make_unique<FileCheckNumericSubstitution>(
Thomas Preud'hommea2ef1ba2019-06-19 23:47:24 +0000794 this, ExpressionStr, Expression, InsertIdx));
Thomas Preud'hommef3b9bb32019-05-23 00:10:29 +0000795 return Substitutions.back().get();
796}
797
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000798size_t FileCheckPattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
799 // Offset keeps track of the current offset within the input Str
800 size_t Offset = 0;
801 // [...] Nesting depth
802 size_t BracketDepth = 0;
803
804 while (!Str.empty()) {
805 if (Str.startswith("]]") && BracketDepth == 0)
806 return Offset;
807 if (Str[0] == '\\') {
808 // Backslash escapes the next char within regexes, so skip them both.
809 Str = Str.substr(2);
810 Offset += 2;
811 } else {
812 switch (Str[0]) {
813 default:
814 break;
815 case '[':
816 BracketDepth++;
817 break;
818 case ']':
819 if (BracketDepth == 0) {
820 SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
821 SourceMgr::DK_Error,
822 "missing closing \"]\" for regex variable");
823 exit(1);
824 }
825 BracketDepth--;
826 break;
827 }
828 Str = Str.substr(1);
829 Offset++;
830 }
831 }
832
833 return StringRef::npos;
834}
835
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +0000836StringRef FileCheck::CanonicalizeFile(MemoryBuffer &MB,
837 SmallVectorImpl<char> &OutputBuffer) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000838 OutputBuffer.reserve(MB.getBufferSize());
839
840 for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd();
841 Ptr != End; ++Ptr) {
842 // Eliminate trailing dosish \r.
843 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
844 continue;
845 }
846
847 // If current char is not a horizontal whitespace or if horizontal
848 // whitespace canonicalization is disabled, dump it to output as is.
849 if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) {
850 OutputBuffer.push_back(*Ptr);
851 continue;
852 }
853
854 // Otherwise, add one space and advance over neighboring space.
855 OutputBuffer.push_back(' ');
856 while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
857 ++Ptr;
858 }
859
860 // Add a null byte and then return all but that byte.
861 OutputBuffer.push_back('\0');
862 return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
863}
864
Joel E. Denny3c5d2672018-12-18 00:01:39 +0000865FileCheckDiag::FileCheckDiag(const SourceMgr &SM,
866 const Check::FileCheckType &CheckTy,
867 SMLoc CheckLoc, MatchType MatchTy,
868 SMRange InputRange)
869 : CheckTy(CheckTy), MatchTy(MatchTy) {
870 auto Start = SM.getLineAndColumn(InputRange.Start);
871 auto End = SM.getLineAndColumn(InputRange.End);
872 InputStartLine = Start.first;
873 InputStartCol = Start.second;
874 InputEndLine = End.first;
875 InputEndCol = End.second;
876 Start = SM.getLineAndColumn(CheckLoc);
877 CheckLine = Start.first;
878 CheckCol = Start.second;
879}
880
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000881static bool IsPartOfWord(char c) {
882 return (isalnum(c) || c == '-' || c == '_');
883}
884
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000885Check::FileCheckType &Check::FileCheckType::setCount(int C) {
Fedor Sergeev8477a3e2018-11-13 01:09:53 +0000886 assert(Count > 0 && "zero and negative counts are not supported");
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000887 assert((C == 1 || Kind == CheckPlain) &&
888 "count supported only for plain CHECK directives");
889 Count = C;
890 return *this;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000891}
892
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000893std::string Check::FileCheckType::getDescription(StringRef Prefix) const {
894 switch (Kind) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000895 case Check::CheckNone:
896 return "invalid";
897 case Check::CheckPlain:
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000898 if (Count > 1)
899 return Prefix.str() + "-COUNT";
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000900 return Prefix;
901 case Check::CheckNext:
902 return Prefix.str() + "-NEXT";
903 case Check::CheckSame:
904 return Prefix.str() + "-SAME";
905 case Check::CheckNot:
906 return Prefix.str() + "-NOT";
907 case Check::CheckDAG:
908 return Prefix.str() + "-DAG";
909 case Check::CheckLabel:
910 return Prefix.str() + "-LABEL";
911 case Check::CheckEmpty:
912 return Prefix.str() + "-EMPTY";
913 case Check::CheckEOF:
914 return "implicit EOF";
915 case Check::CheckBadNot:
916 return "bad NOT";
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000917 case Check::CheckBadCount:
918 return "bad COUNT";
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000919 }
920 llvm_unreachable("unknown FileCheckType");
921}
922
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000923static std::pair<Check::FileCheckType, StringRef>
924FindCheckType(StringRef Buffer, StringRef Prefix) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000925 if (Buffer.size() <= Prefix.size())
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000926 return {Check::CheckNone, StringRef()};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000927
928 char NextChar = Buffer[Prefix.size()];
929
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000930 StringRef Rest = Buffer.drop_front(Prefix.size() + 1);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000931 // Verify that the : is present after the prefix.
932 if (NextChar == ':')
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000933 return {Check::CheckPlain, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000934
935 if (NextChar != '-')
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000936 return {Check::CheckNone, StringRef()};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000937
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000938 if (Rest.consume_front("COUNT-")) {
939 int64_t Count;
940 if (Rest.consumeInteger(10, Count))
941 // Error happened in parsing integer.
942 return {Check::CheckBadCount, Rest};
943 if (Count <= 0 || Count > INT32_MAX)
944 return {Check::CheckBadCount, Rest};
945 if (!Rest.consume_front(":"))
946 return {Check::CheckBadCount, Rest};
947 return {Check::FileCheckType(Check::CheckPlain).setCount(Count), Rest};
948 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000949
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000950 if (Rest.consume_front("NEXT:"))
951 return {Check::CheckNext, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000952
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000953 if (Rest.consume_front("SAME:"))
954 return {Check::CheckSame, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000955
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000956 if (Rest.consume_front("NOT:"))
957 return {Check::CheckNot, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000958
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000959 if (Rest.consume_front("DAG:"))
960 return {Check::CheckDAG, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000961
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000962 if (Rest.consume_front("LABEL:"))
963 return {Check::CheckLabel, Rest};
964
965 if (Rest.consume_front("EMPTY:"))
966 return {Check::CheckEmpty, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000967
968 // You can't combine -NOT with another suffix.
969 if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") ||
970 Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") ||
971 Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:") ||
972 Rest.startswith("EMPTY-NOT:") || Rest.startswith("NOT-EMPTY:"))
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000973 return {Check::CheckBadNot, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000974
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000975 return {Check::CheckNone, Rest};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000976}
977
978// From the given position, find the next character after the word.
979static size_t SkipWord(StringRef Str, size_t Loc) {
980 while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
981 ++Loc;
982 return Loc;
983}
984
Thomas Preud'homme4a8ef112019-05-08 21:47:31 +0000985/// Searches the buffer for the first prefix in the prefix regular expression.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +0000986///
987/// This searches the buffer using the provided regular expression, however it
988/// enforces constraints beyond that:
989/// 1) The found prefix must not be a suffix of something that looks like
990/// a valid prefix.
991/// 2) The found prefix must be followed by a valid check type suffix using \c
992/// FindCheckType above.
993///
Thomas Preud'homme4a8ef112019-05-08 21:47:31 +0000994/// \returns a pair of StringRefs into the Buffer, which combines:
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +0000995/// - the first match of the regular expression to satisfy these two is
996/// returned,
997/// otherwise an empty StringRef is returned to indicate failure.
998/// - buffer rewound to the location right after parsed suffix, for parsing
999/// to continue from
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001000///
1001/// If this routine returns a valid prefix, it will also shrink \p Buffer to
1002/// start at the beginning of the returned prefix, increment \p LineNumber for
1003/// each new line consumed from \p Buffer, and set \p CheckTy to the type of
1004/// check found by examining the suffix.
1005///
1006/// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
1007/// is unspecified.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001008static std::pair<StringRef, StringRef>
1009FindFirstMatchingPrefix(Regex &PrefixRE, StringRef &Buffer,
1010 unsigned &LineNumber, Check::FileCheckType &CheckTy) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001011 SmallVector<StringRef, 2> Matches;
1012
1013 while (!Buffer.empty()) {
1014 // Find the first (longest) match using the RE.
1015 if (!PrefixRE.match(Buffer, &Matches))
1016 // No match at all, bail.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001017 return {StringRef(), StringRef()};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001018
1019 StringRef Prefix = Matches[0];
1020 Matches.clear();
1021
1022 assert(Prefix.data() >= Buffer.data() &&
1023 Prefix.data() < Buffer.data() + Buffer.size() &&
1024 "Prefix doesn't start inside of buffer!");
1025 size_t Loc = Prefix.data() - Buffer.data();
1026 StringRef Skipped = Buffer.substr(0, Loc);
1027 Buffer = Buffer.drop_front(Loc);
1028 LineNumber += Skipped.count('\n');
1029
1030 // Check that the matched prefix isn't a suffix of some other check-like
1031 // word.
1032 // FIXME: This is a very ad-hoc check. it would be better handled in some
1033 // other way. Among other things it seems hard to distinguish between
1034 // intentional and unintentional uses of this feature.
1035 if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
1036 // Now extract the type.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001037 StringRef AfterSuffix;
1038 std::tie(CheckTy, AfterSuffix) = FindCheckType(Buffer, Prefix);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001039
1040 // If we've found a valid check type for this prefix, we're done.
1041 if (CheckTy != Check::CheckNone)
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001042 return {Prefix, AfterSuffix};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001043 }
1044
1045 // If we didn't successfully find a prefix, we need to skip this invalid
1046 // prefix and continue scanning. We directly skip the prefix that was
1047 // matched and any additional parts of that check-like word.
1048 Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size()));
1049 }
1050
1051 // We ran out of buffer while skipping partial matches so give up.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001052 return {StringRef(), StringRef()};
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001053}
1054
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +00001055bool FileCheck::ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
1056 std::vector<FileCheckString> &CheckStrings) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001057 Error DefineError =
1058 PatternContext.defineCmdlineVariables(Req.GlobalDefines, SM);
1059 if (DefineError) {
1060 logAllUnhandledErrors(std::move(DefineError), errs());
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001061 return true;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001062 }
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001063
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001064 std::vector<FileCheckPattern> ImplicitNegativeChecks;
1065 for (const auto &PatternString : Req.ImplicitCheckNot) {
1066 // Create a buffer with fake command line content in order to display the
1067 // command line option responsible for the specific implicit CHECK-NOT.
1068 std::string Prefix = "-implicit-check-not='";
1069 std::string Suffix = "'";
1070 std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
1071 Prefix + PatternString + Suffix, "command line");
1072
1073 StringRef PatternInBuffer =
1074 CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
1075 SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
1076
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001077 ImplicitNegativeChecks.push_back(
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001078 FileCheckPattern(Check::CheckNot, &PatternContext, 0));
1079 ImplicitNegativeChecks.back().parsePattern(PatternInBuffer,
1080 "IMPLICIT-CHECK", SM, Req);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001081 }
1082
1083 std::vector<FileCheckPattern> DagNotMatches = ImplicitNegativeChecks;
1084
1085 // LineNumber keeps track of the line on which CheckPrefix instances are
1086 // found.
1087 unsigned LineNumber = 1;
1088
1089 while (1) {
1090 Check::FileCheckType CheckTy;
1091
1092 // See if a prefix occurs in the memory buffer.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001093 StringRef UsedPrefix;
1094 StringRef AfterSuffix;
1095 std::tie(UsedPrefix, AfterSuffix) =
1096 FindFirstMatchingPrefix(PrefixRE, Buffer, LineNumber, CheckTy);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001097 if (UsedPrefix.empty())
1098 break;
1099 assert(UsedPrefix.data() == Buffer.data() &&
1100 "Failed to move Buffer's start forward, or pointed prefix outside "
1101 "of the buffer!");
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001102 assert(AfterSuffix.data() >= Buffer.data() &&
1103 AfterSuffix.data() < Buffer.data() + Buffer.size() &&
1104 "Parsing after suffix doesn't start inside of buffer!");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001105
1106 // Location to use for error messages.
1107 const char *UsedPrefixStart = UsedPrefix.data();
1108
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001109 // Skip the buffer to the end of parsed suffix (or just prefix, if no good
1110 // suffix was processed).
1111 Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
1112 : AfterSuffix;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001113
1114 // Complain about useful-looking but unsupported suffixes.
1115 if (CheckTy == Check::CheckBadNot) {
1116 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
1117 "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
1118 return true;
1119 }
1120
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001121 // Complain about invalid count specification.
1122 if (CheckTy == Check::CheckBadCount) {
1123 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
1124 "invalid count in -COUNT specification on prefix '" +
1125 UsedPrefix + "'");
1126 return true;
1127 }
1128
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001129 // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
1130 // leading whitespace.
1131 if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
1132 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
1133
1134 // Scan ahead to the end of line.
1135 size_t EOL = Buffer.find_first_of("\n\r");
1136
1137 // Remember the location of the start of the pattern, for diagnostics.
1138 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
1139
1140 // Parse the pattern.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001141 FileCheckPattern P(CheckTy, &PatternContext, LineNumber);
1142 if (P.parsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, Req))
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001143 return true;
1144
1145 // Verify that CHECK-LABEL lines do not define or use variables
1146 if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
1147 SM.PrintMessage(
1148 SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
1149 "found '" + UsedPrefix + "-LABEL:'"
1150 " with variable definition or use");
1151 return true;
1152 }
1153
1154 Buffer = Buffer.substr(EOL);
1155
1156 // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
1157 if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
1158 CheckTy == Check::CheckEmpty) &&
1159 CheckStrings.empty()) {
1160 StringRef Type = CheckTy == Check::CheckNext
1161 ? "NEXT"
1162 : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
1163 SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
1164 SourceMgr::DK_Error,
1165 "found '" + UsedPrefix + "-" + Type +
1166 "' without previous '" + UsedPrefix + ": line");
1167 return true;
1168 }
1169
1170 // Handle CHECK-DAG/-NOT.
1171 if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
1172 DagNotMatches.push_back(P);
1173 continue;
1174 }
1175
1176 // Okay, add the string we captured to the output vector and move on.
1177 CheckStrings.emplace_back(P, UsedPrefix, PatternLoc);
1178 std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
1179 DagNotMatches = ImplicitNegativeChecks;
1180 }
1181
1182 // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first
1183 // prefix as a filler for the error message.
1184 if (!DagNotMatches.empty()) {
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001185 CheckStrings.emplace_back(
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001186 FileCheckPattern(Check::CheckEOF, &PatternContext, LineNumber + 1),
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001187 *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001188 std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
1189 }
1190
1191 if (CheckStrings.empty()) {
1192 errs() << "error: no check strings found with prefix"
1193 << (Req.CheckPrefixes.size() > 1 ? "es " : " ");
1194 auto I = Req.CheckPrefixes.begin();
1195 auto E = Req.CheckPrefixes.end();
1196 if (I != E) {
1197 errs() << "\'" << *I << ":'";
1198 ++I;
1199 }
1200 for (; I != E; ++I)
1201 errs() << ", \'" << *I << ":'";
1202
1203 errs() << '\n';
1204 return true;
1205 }
1206
1207 return false;
1208}
1209
1210static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
1211 StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001212 int MatchedCount, StringRef Buffer, size_t MatchPos,
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001213 size_t MatchLen, const FileCheckRequest &Req,
1214 std::vector<FileCheckDiag> *Diags) {
Joel E. Denny352695c2019-01-22 21:41:42 +00001215 bool PrintDiag = true;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001216 if (ExpectedMatch) {
1217 if (!Req.Verbose)
1218 return;
1219 if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
1220 return;
Joel E. Denny352695c2019-01-22 21:41:42 +00001221 // Due to their verbosity, we don't print verbose diagnostics here if we're
1222 // gathering them for a different rendering, but we always print other
1223 // diagnostics.
1224 PrintDiag = !Diags;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001225 }
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001226 SMRange MatchRange = ProcessMatchResult(
Joel E. Dennye2afb612018-12-18 00:03:51 +00001227 ExpectedMatch ? FileCheckDiag::MatchFoundAndExpected
1228 : FileCheckDiag::MatchFoundButExcluded,
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001229 SM, Loc, Pat.getCheckTy(), Buffer, MatchPos, MatchLen, Diags);
Joel E. Denny352695c2019-01-22 21:41:42 +00001230 if (!PrintDiag)
1231 return;
1232
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001233 std::string Message = formatv("{0}: {1} string found in input",
1234 Pat.getCheckTy().getDescription(Prefix),
1235 (ExpectedMatch ? "expected" : "excluded"))
1236 .str();
1237 if (Pat.getCount() > 1)
1238 Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
1239
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001240 SM.PrintMessage(
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001241 Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message);
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001242 SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, "found here",
1243 {MatchRange});
Thomas Preud'homme288ed912019-05-02 00:04:38 +00001244 Pat.printSubstitutions(SM, Buffer, MatchRange);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001245}
1246
1247static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001248 const FileCheckString &CheckStr, int MatchedCount,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001249 StringRef Buffer, size_t MatchPos, size_t MatchLen,
1250 FileCheckRequest &Req,
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001251 std::vector<FileCheckDiag> *Diags) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001252 PrintMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001253 MatchedCount, Buffer, MatchPos, MatchLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001254}
1255
1256static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001257 StringRef Prefix, SMLoc Loc,
1258 const FileCheckPattern &Pat, int MatchedCount,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001259 StringRef Buffer, bool VerboseVerbose,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001260 std::vector<FileCheckDiag> *Diags, Error MatchErrors) {
1261 assert(MatchErrors && "Called on successful match");
Joel E. Denny352695c2019-01-22 21:41:42 +00001262 bool PrintDiag = true;
1263 if (!ExpectedMatch) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001264 if (!VerboseVerbose) {
1265 consumeError(std::move(MatchErrors));
Joel E. Denny352695c2019-01-22 21:41:42 +00001266 return;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001267 }
Joel E. Denny352695c2019-01-22 21:41:42 +00001268 // Due to their verbosity, we don't print verbose diagnostics here if we're
1269 // gathering them for a different rendering, but we always print other
1270 // diagnostics.
1271 PrintDiag = !Diags;
1272 }
1273
1274 // If the current position is at the end of a line, advance to the start of
1275 // the next line.
1276 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
1277 SMRange SearchRange = ProcessMatchResult(
1278 ExpectedMatch ? FileCheckDiag::MatchNoneButExpected
1279 : FileCheckDiag::MatchNoneAndExcluded,
1280 SM, Loc, Pat.getCheckTy(), Buffer, 0, Buffer.size(), Diags);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001281 if (!PrintDiag) {
1282 consumeError(std::move(MatchErrors));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001283 return;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001284 }
1285
1286 MatchErrors =
1287 handleErrors(std::move(MatchErrors),
1288 [](const FileCheckErrorDiagnostic &E) { E.log(errs()); });
1289
1290 // No problem matching the string per se.
1291 if (!MatchErrors)
1292 return;
1293 consumeError(std::move(MatchErrors));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001294
Joel E. Denny352695c2019-01-22 21:41:42 +00001295 // Print "not found" diagnostic.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001296 std::string Message = formatv("{0}: {1} string not found in input",
1297 Pat.getCheckTy().getDescription(Prefix),
1298 (ExpectedMatch ? "expected" : "excluded"))
1299 .str();
1300 if (Pat.getCount() > 1)
1301 Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001302 SM.PrintMessage(
1303 Loc, ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark, Message);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001304
Joel E. Denny352695c2019-01-22 21:41:42 +00001305 // Print the "scanning from here" line.
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001306 SM.PrintMessage(SearchRange.Start, SourceMgr::DK_Note, "scanning from here");
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001307
1308 // Allow the pattern to print additional information if desired.
Thomas Preud'homme288ed912019-05-02 00:04:38 +00001309 Pat.printSubstitutions(SM, Buffer);
Joel E. Denny96f0e842018-12-18 00:03:36 +00001310
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001311 if (ExpectedMatch)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001312 Pat.printFuzzyMatch(SM, Buffer, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001313}
1314
1315static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001316 const FileCheckString &CheckStr, int MatchedCount,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001317 StringRef Buffer, bool VerboseVerbose,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001318 std::vector<FileCheckDiag> *Diags, Error MatchErrors) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001319 PrintNoMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001320 MatchedCount, Buffer, VerboseVerbose, Diags,
1321 std::move(MatchErrors));
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001322}
1323
Thomas Preud'homme4a8ef112019-05-08 21:47:31 +00001324/// Counts the number of newlines in the specified range.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001325static unsigned CountNumNewlinesBetween(StringRef Range,
1326 const char *&FirstNewLine) {
1327 unsigned NumNewLines = 0;
1328 while (1) {
1329 // Scan for newline.
1330 Range = Range.substr(Range.find_first_of("\n\r"));
1331 if (Range.empty())
1332 return NumNewLines;
1333
1334 ++NumNewLines;
1335
1336 // Handle \n\r and \r\n as a single newline.
1337 if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
1338 (Range[0] != Range[1]))
1339 Range = Range.substr(1);
1340 Range = Range.substr(1);
1341
1342 if (NumNewLines == 1)
1343 FirstNewLine = Range.begin();
1344 }
1345}
1346
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001347size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001348 bool IsLabelScanMode, size_t &MatchLen,
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001349 FileCheckRequest &Req,
1350 std::vector<FileCheckDiag> *Diags) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001351 size_t LastPos = 0;
1352 std::vector<const FileCheckPattern *> NotStrings;
1353
1354 // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
1355 // bounds; we have not processed variable definitions within the bounded block
1356 // yet so cannot handle any final CHECK-DAG yet; this is handled when going
1357 // over the block again (including the last CHECK-LABEL) in normal mode.
1358 if (!IsLabelScanMode) {
1359 // Match "dag strings" (with mixed "not strings" if any).
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001360 LastPos = CheckDag(SM, Buffer, NotStrings, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001361 if (LastPos == StringRef::npos)
1362 return StringRef::npos;
1363 }
1364
1365 // Match itself from the last position after matching CHECK-DAG.
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001366 size_t LastMatchEnd = LastPos;
1367 size_t FirstMatchPos = 0;
1368 // Go match the pattern Count times. Majority of patterns only match with
1369 // count 1 though.
1370 assert(Pat.getCount() != 0 && "pattern count can not be zero");
1371 for (int i = 1; i <= Pat.getCount(); i++) {
1372 StringRef MatchBuffer = Buffer.substr(LastMatchEnd);
1373 size_t CurrentMatchLen;
1374 // get a match at current start point
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001375 Expected<size_t> MatchResult = Pat.match(MatchBuffer, CurrentMatchLen, SM);
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001376
1377 // report
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001378 if (!MatchResult) {
1379 PrintNoMatch(true, SM, *this, i, MatchBuffer, Req.VerboseVerbose, Diags,
1380 MatchResult.takeError());
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001381 return StringRef::npos;
1382 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001383 size_t MatchPos = *MatchResult;
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001384 PrintMatch(true, SM, *this, i, MatchBuffer, MatchPos, CurrentMatchLen, Req,
1385 Diags);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001386 if (i == 1)
1387 FirstMatchPos = LastPos + MatchPos;
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001388
1389 // move start point after the match
1390 LastMatchEnd += MatchPos + CurrentMatchLen;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001391 }
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001392 // Full match len counts from first match pos.
1393 MatchLen = LastMatchEnd - FirstMatchPos;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001394
1395 // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
1396 // or CHECK-NOT
1397 if (!IsLabelScanMode) {
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001398 size_t MatchPos = FirstMatchPos - LastPos;
1399 StringRef MatchBuffer = Buffer.substr(LastPos);
1400 StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001401
1402 // If this check is a "CHECK-NEXT", verify that the previous match was on
1403 // the previous line (i.e. that there is one newline between them).
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001404 if (CheckNext(SM, SkippedRegion)) {
Joel E. Dennye2afb612018-12-18 00:03:51 +00001405 ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001406 Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
Joel E. Denny7df86962018-12-18 00:03:03 +00001407 Diags, Req.Verbose);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001408 return StringRef::npos;
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001409 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001410
1411 // If this check is a "CHECK-SAME", verify that the previous match was on
1412 // the same line (i.e. that there is no newline between them).
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001413 if (CheckSame(SM, SkippedRegion)) {
Joel E. Dennye2afb612018-12-18 00:03:51 +00001414 ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001415 Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
Joel E. Denny7df86962018-12-18 00:03:03 +00001416 Diags, Req.Verbose);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001417 return StringRef::npos;
Joel E. Dennycadfcef2018-12-18 00:02:22 +00001418 }
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001419
1420 // If this match had "not strings", verify that they don't exist in the
1421 // skipped region.
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001422 if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001423 return StringRef::npos;
1424 }
1425
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001426 return FirstMatchPos;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001427}
1428
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001429bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
1430 if (Pat.getCheckTy() != Check::CheckNext &&
1431 Pat.getCheckTy() != Check::CheckEmpty)
1432 return false;
1433
1434 Twine CheckName =
1435 Prefix +
1436 Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
1437
1438 // Count the number of newlines between the previous match and this one.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001439 const char *FirstNewLine = nullptr;
1440 unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1441
1442 if (NumNewLines == 0) {
1443 SM.PrintMessage(Loc, SourceMgr::DK_Error,
1444 CheckName + ": is on the same line as previous match");
1445 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1446 "'next' match was here");
1447 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1448 "previous match ended here");
1449 return true;
1450 }
1451
1452 if (NumNewLines != 1) {
1453 SM.PrintMessage(Loc, SourceMgr::DK_Error,
1454 CheckName +
1455 ": is not on the line after the previous match");
1456 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1457 "'next' match was here");
1458 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1459 "previous match ended here");
1460 SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note,
1461 "non-matching line after previous match is here");
1462 return true;
1463 }
1464
1465 return false;
1466}
1467
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001468bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
1469 if (Pat.getCheckTy() != Check::CheckSame)
1470 return false;
1471
1472 // Count the number of newlines between the previous match and this one.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001473 const char *FirstNewLine = nullptr;
1474 unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1475
1476 if (NumNewLines != 0) {
1477 SM.PrintMessage(Loc, SourceMgr::DK_Error,
1478 Prefix +
1479 "-SAME: is not on the same line as the previous match");
1480 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1481 "'next' match was here");
1482 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1483 "previous match ended here");
1484 return true;
1485 }
1486
1487 return false;
1488}
1489
Joel E. Denny0e7e3fa2018-12-18 00:02:47 +00001490bool FileCheckString::CheckNot(
1491 const SourceMgr &SM, StringRef Buffer,
1492 const std::vector<const FileCheckPattern *> &NotStrings,
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001493 const FileCheckRequest &Req, std::vector<FileCheckDiag> *Diags) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001494 for (const FileCheckPattern *Pat : NotStrings) {
1495 assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
1496
1497 size_t MatchLen = 0;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001498 Expected<size_t> MatchResult = Pat->match(Buffer, MatchLen, SM);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001499
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001500 if (!MatchResult) {
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001501 PrintNoMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001502 Req.VerboseVerbose, Diags, MatchResult.takeError());
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001503 continue;
1504 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001505 size_t Pos = *MatchResult;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001506
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001507 PrintMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer, Pos, MatchLen,
1508 Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001509
1510 return true;
1511 }
1512
1513 return false;
1514}
1515
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001516size_t
1517FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
1518 std::vector<const FileCheckPattern *> &NotStrings,
Joel E. Denny3c5d2672018-12-18 00:01:39 +00001519 const FileCheckRequest &Req,
1520 std::vector<FileCheckDiag> *Diags) const {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001521 if (DagNotStrings.empty())
1522 return 0;
1523
1524 // The start of the search range.
1525 size_t StartPos = 0;
1526
1527 struct MatchRange {
1528 size_t Pos;
1529 size_t End;
1530 };
1531 // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
1532 // ranges are erased from this list once they are no longer in the search
1533 // range.
1534 std::list<MatchRange> MatchRanges;
1535
1536 // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
1537 // group, so we don't use a range-based for loop here.
1538 for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
1539 PatItr != PatEnd; ++PatItr) {
1540 const FileCheckPattern &Pat = *PatItr;
1541 assert((Pat.getCheckTy() == Check::CheckDAG ||
1542 Pat.getCheckTy() == Check::CheckNot) &&
1543 "Invalid CHECK-DAG or CHECK-NOT!");
1544
1545 if (Pat.getCheckTy() == Check::CheckNot) {
1546 NotStrings.push_back(&Pat);
1547 continue;
1548 }
1549
1550 assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
1551
1552 // CHECK-DAG always matches from the start.
1553 size_t MatchLen = 0, MatchPos = StartPos;
1554
1555 // Search for a match that doesn't overlap a previous match in this
1556 // CHECK-DAG group.
1557 for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
1558 StringRef MatchBuffer = Buffer.substr(MatchPos);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001559 Expected<size_t> MatchResult = Pat.match(MatchBuffer, MatchLen, SM);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001560 // With a group of CHECK-DAGs, a single mismatching means the match on
1561 // that group of CHECK-DAGs fails immediately.
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001562 if (!MatchResult) {
Fedor Sergeev6c9e19b2018-11-13 00:46:13 +00001563 PrintNoMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, MatchBuffer,
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001564 Req.VerboseVerbose, Diags, MatchResult.takeError());
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001565 return StringRef::npos;
1566 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001567 size_t MatchPosBuf = *MatchResult;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001568 // Re-calc it as the offset relative to the start of the original string.
1569 MatchPos += MatchPosBuf;
1570 if (Req.VerboseVerbose)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001571 PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, MatchPos,
1572 MatchLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001573 MatchRange M{MatchPos, MatchPos + MatchLen};
1574 if (Req.AllowDeprecatedDagOverlap) {
1575 // We don't need to track all matches in this mode, so we just maintain
1576 // one match range that encompasses the current CHECK-DAG group's
1577 // matches.
1578 if (MatchRanges.empty())
1579 MatchRanges.insert(MatchRanges.end(), M);
1580 else {
1581 auto Block = MatchRanges.begin();
1582 Block->Pos = std::min(Block->Pos, M.Pos);
1583 Block->End = std::max(Block->End, M.End);
1584 }
1585 break;
1586 }
1587 // Iterate previous matches until overlapping match or insertion point.
1588 bool Overlap = false;
1589 for (; MI != ME; ++MI) {
1590 if (M.Pos < MI->End) {
1591 // !Overlap => New match has no overlap and is before this old match.
1592 // Overlap => New match overlaps this old match.
1593 Overlap = MI->Pos < M.End;
1594 break;
1595 }
1596 }
1597 if (!Overlap) {
1598 // Insert non-overlapping match into list.
1599 MatchRanges.insert(MI, M);
1600 break;
1601 }
1602 if (Req.VerboseVerbose) {
Joel E. Denny352695c2019-01-22 21:41:42 +00001603 // Due to their verbosity, we don't print verbose diagnostics here if
1604 // we're gathering them for a different rendering, but we always print
1605 // other diagnostics.
1606 if (!Diags) {
1607 SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
1608 SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
1609 SMRange OldRange(OldStart, OldEnd);
1610 SM.PrintMessage(OldStart, SourceMgr::DK_Note,
1611 "match discarded, overlaps earlier DAG match here",
1612 {OldRange});
1613 } else
Joel E. Dennye2afb612018-12-18 00:03:51 +00001614 Diags->rbegin()->MatchTy = FileCheckDiag::MatchFoundButDiscarded;
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001615 }
1616 MatchPos = MI->End;
1617 }
1618 if (!Req.VerboseVerbose)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001619 PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, MatchPos,
1620 MatchLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001621
1622 // Handle the end of a CHECK-DAG group.
1623 if (std::next(PatItr) == PatEnd ||
1624 std::next(PatItr)->getCheckTy() == Check::CheckNot) {
1625 if (!NotStrings.empty()) {
1626 // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
1627 // CHECK-DAG, verify that there are no 'not' strings occurred in that
1628 // region.
1629 StringRef SkippedRegion =
1630 Buffer.slice(StartPos, MatchRanges.begin()->Pos);
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001631 if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001632 return StringRef::npos;
1633 // Clear "not strings".
1634 NotStrings.clear();
1635 }
1636 // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
1637 // end of this CHECK-DAG group's match range.
1638 StartPos = MatchRanges.rbegin()->End;
1639 // Don't waste time checking for (impossible) overlaps before that.
1640 MatchRanges.clear();
1641 }
1642 }
1643
1644 return StartPos;
1645}
1646
1647// A check prefix must contain only alphanumeric, hyphens and underscores.
1648static bool ValidateCheckPrefix(StringRef CheckPrefix) {
1649 Regex Validator("^[a-zA-Z0-9_-]*$");
1650 return Validator.match(CheckPrefix);
1651}
1652
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +00001653bool FileCheck::ValidateCheckPrefixes() {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001654 StringSet<> PrefixSet;
1655
1656 for (StringRef Prefix : Req.CheckPrefixes) {
1657 // Reject empty prefixes.
1658 if (Prefix == "")
1659 return false;
1660
1661 if (!PrefixSet.insert(Prefix).second)
1662 return false;
1663
1664 if (!ValidateCheckPrefix(Prefix))
1665 return false;
1666 }
1667
1668 return true;
1669}
1670
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +00001671Regex FileCheck::buildCheckPrefixRegex() {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001672 // I don't think there's a way to specify an initial value for cl::list,
1673 // so if nothing was specified, add the default
1674 if (Req.CheckPrefixes.empty())
1675 Req.CheckPrefixes.push_back("CHECK");
1676
1677 // We already validated the contents of CheckPrefixes so just concatenate
1678 // them as alternatives.
1679 SmallString<32> PrefixRegexStr;
1680 for (StringRef Prefix : Req.CheckPrefixes) {
1681 if (Prefix != Req.CheckPrefixes.front())
1682 PrefixRegexStr.push_back('|');
1683
1684 PrefixRegexStr.append(Prefix);
1685 }
1686
1687 return Regex(PrefixRegexStr);
1688}
1689
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001690Error FileCheckPatternContext::defineCmdlineVariables(
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001691 std::vector<std::string> &CmdlineDefines, SourceMgr &SM) {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001692 assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() &&
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001693 "Overriding defined variable with command-line variable definitions");
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001694
1695 if (CmdlineDefines.empty())
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001696 return Error::success();
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001697
1698 // Create a string representing the vector of command-line definitions. Each
1699 // definition is on its own line and prefixed with a definition number to
1700 // clarify which definition a given diagnostic corresponds to.
1701 unsigned I = 0;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001702 Error Errs = Error::success();
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001703 std::string CmdlineDefsDiag;
1704 StringRef Prefix1 = "Global define #";
1705 StringRef Prefix2 = ": ";
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001706 for (StringRef CmdlineDef : CmdlineDefines)
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001707 CmdlineDefsDiag +=
1708 (Prefix1 + Twine(++I) + Prefix2 + CmdlineDef + "\n").str();
1709
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001710 // Create a buffer with fake command line content in order to display
1711 // parsing diagnostic with location information and point to the
1712 // global definition with invalid syntax.
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001713 std::unique_ptr<MemoryBuffer> CmdLineDefsDiagBuffer =
1714 MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines");
1715 StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer();
1716 SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc());
1717
1718 SmallVector<StringRef, 4> CmdlineDefsDiagVec;
1719 CmdlineDefsDiagRef.split(CmdlineDefsDiagVec, '\n', -1 /*MaxSplit*/,
1720 false /*KeepEmpty*/);
1721 for (StringRef CmdlineDefDiag : CmdlineDefsDiagVec) {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001722 unsigned DefStart = CmdlineDefDiag.find(Prefix2) + Prefix2.size();
1723 StringRef CmdlineDef = CmdlineDefDiag.substr(DefStart);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001724 size_t EqIdx = CmdlineDef.find('=');
1725 if (EqIdx == StringRef::npos) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001726 Errs = joinErrors(
1727 std::move(Errs),
1728 FileCheckErrorDiagnostic::get(
1729 SM, CmdlineDef, "missing equal sign in global definition"));
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001730 continue;
1731 }
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001732
1733 // Numeric variable definition.
1734 if (CmdlineDef[0] == '#') {
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001735 StringRef CmdlineName = CmdlineDef.substr(1, EqIdx - 1);
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001736 StringRef VarName;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001737 Error ErrorDiagnostic = FileCheckPattern::parseNumericVariableDefinition(
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001738 CmdlineName, VarName, this, SM);
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001739 if (ErrorDiagnostic) {
1740 Errs = joinErrors(std::move(Errs), std::move(ErrorDiagnostic));
1741 continue;
1742 }
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001743
1744 StringRef CmdlineVal = CmdlineDef.substr(EqIdx + 1);
1745 uint64_t Val;
1746 if (CmdlineVal.getAsInteger(10, Val)) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001747 Errs = joinErrors(std::move(Errs),
1748 FileCheckErrorDiagnostic::get(
1749 SM, CmdlineVal,
1750 "invalid value in numeric variable definition '" +
1751 CmdlineVal + "'"));
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001752 continue;
1753 }
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001754 auto DefinedNumericVariable = makeNumericVariable(0, VarName);
1755 DefinedNumericVariable->setValue(Val);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001756
1757 // Record this variable definition.
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001758 GlobalNumericVariableTable[DefinedNumericVariable->getName()] =
1759 DefinedNumericVariable;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001760 } else {
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001761 // String variable definition.
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001762 std::pair<StringRef, StringRef> CmdlineNameVal = CmdlineDef.split('=');
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001763 StringRef CmdlineName = CmdlineNameVal.first;
1764 StringRef OrigCmdlineName = CmdlineName;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001765 bool IsPseudo;
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001766 Expected<StringRef> ParseVarResult =
1767 FileCheckPattern::parseVariable(CmdlineName, IsPseudo, SM);
1768 if (!ParseVarResult) {
1769 Errs = joinErrors(std::move(Errs), ParseVarResult.takeError());
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001770 continue;
1771 }
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001772 // Check that CmdlineName does not denote a pseudo variable is only
1773 // composed of the parsed numeric variable. This catches cases like
1774 // "FOO+2" in a "FOO+2=10" definition.
1775 if (IsPseudo || !CmdlineName.empty()) {
1776 Errs = joinErrors(std::move(Errs),
1777 FileCheckErrorDiagnostic::get(
1778 SM, OrigCmdlineName,
1779 "invalid name in string variable definition '" +
1780 OrigCmdlineName + "'"));
1781 continue;
1782 }
1783 StringRef Name = *ParseVarResult;
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001784
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001785 // Detect collisions between string and numeric variables when the former
1786 // is created later than the latter.
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001787 if (GlobalNumericVariableTable.find(Name) !=
1788 GlobalNumericVariableTable.end()) {
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001789 Errs = joinErrors(std::move(Errs), FileCheckErrorDiagnostic::get(
1790 SM, Name,
1791 "numeric variable with name '" +
1792 Name + "' already exists"));
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001793 continue;
1794 }
1795 GlobalVariableTable.insert(CmdlineNameVal);
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001796 // Mark the string variable as defined to detect collisions between
1797 // string and numeric variables in DefineCmdlineVariables when the latter
1798 // is created later than the former. We cannot reuse GlobalVariableTable
Thomas Preud'homme71d3f222019-06-06 13:21:06 +00001799 // for this by populating it with an empty string since we would then
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001800 // lose the ability to detect the use of an undefined variable in
1801 // match().
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001802 DefinedVariableTable[Name] = true;
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001803 }
Thomas Preud'homme5a330472019-04-29 13:32:36 +00001804 }
1805
Thomas Preud'hommebaae41f2019-06-19 23:47:10 +00001806 return Errs;
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001807}
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001808
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001809void FileCheckPatternContext::clearLocalVars() {
1810 SmallVector<StringRef, 16> LocalPatternVars, LocalNumericVars;
1811 for (const StringMapEntry<StringRef> &Var : GlobalVariableTable)
1812 if (Var.first()[0] != '$')
1813 LocalPatternVars.push_back(Var.first());
1814
Thomas Preud'homme1a944d22019-05-23 00:10:14 +00001815 // Numeric substitution reads the value of a variable directly, not via
1816 // GlobalNumericVariableTable. Therefore, we clear local variables by
1817 // clearing their value which will lead to a numeric substitution failure. We
1818 // also mark the variable for removal from GlobalNumericVariableTable since
1819 // this is what defineCmdlineVariables checks to decide that no global
1820 // variable has been defined.
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001821 for (const auto &Var : GlobalNumericVariableTable)
1822 if (Var.first()[0] != '$') {
1823 Var.getValue()->clearValue();
1824 LocalNumericVars.push_back(Var.first());
1825 }
1826
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001827 for (const auto &Var : LocalPatternVars)
1828 GlobalVariableTable.erase(Var);
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001829 for (const auto &Var : LocalNumericVars)
1830 GlobalNumericVariableTable.erase(Var);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001831}
1832
Thomas Preud'homme7b7683d2019-05-23 17:19:36 +00001833bool FileCheck::CheckInput(SourceMgr &SM, StringRef Buffer,
1834 ArrayRef<FileCheckString> CheckStrings,
1835 std::vector<FileCheckDiag> *Diags) {
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001836 bool ChecksFailed = false;
1837
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001838 unsigned i = 0, j = 0, e = CheckStrings.size();
1839 while (true) {
1840 StringRef CheckRegion;
1841 if (j == e) {
1842 CheckRegion = Buffer;
1843 } else {
1844 const FileCheckString &CheckLabelStr = CheckStrings[j];
1845 if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
1846 ++j;
1847 continue;
1848 }
1849
1850 // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
1851 size_t MatchLabelLen = 0;
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001852 size_t MatchLabelPos =
1853 CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001854 if (MatchLabelPos == StringRef::npos)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001855 // Immediately bail if CHECK-LABEL fails, nothing else we can do.
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001856 return false;
1857
1858 CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
1859 Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
1860 ++j;
1861 }
1862
Thomas Preud'homme7b4ecdd2019-05-14 11:58:30 +00001863 // Do not clear the first region as it's the one before the first
1864 // CHECK-LABEL and it would clear variables defined on the command-line
1865 // before they get used.
1866 if (i != 0 && Req.EnableVarScope)
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001867 PatternContext.clearLocalVars();
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001868
1869 for (; i != j; ++i) {
1870 const FileCheckString &CheckStr = CheckStrings[i];
1871
1872 // Check each string within the scanned region, including a second check
1873 // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
1874 size_t MatchLen = 0;
Thomas Preud'hommee038fa72019-04-15 10:10:11 +00001875 size_t MatchPos =
1876 CheckStr.Check(SM, CheckRegion, false, MatchLen, Req, Diags);
Aditya Nandakumarffa9d2e2018-08-07 21:58:49 +00001877
1878 if (MatchPos == StringRef::npos) {
1879 ChecksFailed = true;
1880 i = j;
1881 break;
1882 }
1883
1884 CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
1885 }
1886
1887 if (j == e)
1888 break;
1889 }
1890
1891 // Success if no checks failed.
1892 return !ChecksFailed;
1893}