llvm.org GIT mirror llvm / b541b81
[FileCheck] Run clang-format over this code. NFC. This fixes one formatting goof I left in my previous commit and *many* other inconsistencies. I'm planning to make substantial changes here and so wanted to get to a clean baseline. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@289379 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 3 years ago
1 changed file(s) with 112 addition(s) and 122 deletion(s). Raw diff Collapse all Expand all
3535 using namespace llvm;
3636
3737 static cl::opt
38 CheckFilename(cl::Positional, cl::desc(""), cl::Required);
38 CheckFilename(cl::Positional, cl::desc(""), cl::Required);
3939
4040 static cl::opt
41 InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
42 cl::init("-"), cl::value_desc("filename"));
43
44 static cl::list
45 CheckPrefixes("check-prefix",
46 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
41 InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
42 cl::init("-"), cl::value_desc("filename"));
43
44 static cl::list CheckPrefixes(
45 "check-prefix",
46 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
4747 static cl::alias CheckPrefixesAlias(
4848 "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated,
4949 cl::NotHidden,
5050 cl::desc(
5151 "Alias for -check-prefix permitting multiple comma separated values"));
5252
53 static cl::opt
54 NoCanonicalizeWhiteSpace("strict-whitespace",
55 cl::desc("Do not treat all horizontal whitespace as equivalent"));
53 static cl::opt NoCanonicalizeWhiteSpace(
54 "strict-whitespace",
55 cl::desc("Do not treat all horizontal whitespace as equivalent"));
5656
5757 static cl::list ImplicitCheckNot(
5858 "implicit-check-not",
7979 //===----------------------------------------------------------------------===//
8080
8181 namespace Check {
82 enum CheckType {
83 CheckNone = 0,
84 CheckPlain,
85 CheckNext,
86 CheckSame,
87 CheckNot,
88 CheckDAG,
89 CheckLabel,
90
91 /// MatchEOF - When set, this pattern only matches the end of file. This is
92 /// used for trailing CHECK-NOTs.
93 CheckEOF,
94 /// CheckBadNot - Found -NOT combined with another CHECK suffix.
95 CheckBadNot
96 };
82 enum CheckType {
83 CheckNone = 0,
84 CheckPlain,
85 CheckNext,
86 CheckSame,
87 CheckNot,
88 CheckDAG,
89 CheckLabel,
90
91 /// MatchEOF - When set, this pattern only matches the end of file. This is
92 /// used for trailing CHECK-NOTs.
93 CheckEOF,
94 /// CheckBadNot - Found -NOT combined with another CHECK suffix.
95 CheckBadNot
96 };
9797 }
9898
9999 class Pattern {
110110 /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain
111111 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
112112 /// value of bar at offset 3.
113 std::vector > VariableUses;
113 std::vector> VariableUses;
114114
115115 /// VariableDefs - Maps definitions of variables to their parenthesized
116116 /// capture numbers.
133133 /// reports, and LineNumber is the line number in the input file from which
134134 /// the pattern string was read. Returns true in case of an error, false
135135 /// otherwise.
136 bool ParsePattern(StringRef PatternStr,
137 StringRef Prefix,
138 SourceMgr &SM,
136 bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
139137 unsigned LineNumber);
140138
141139 /// Match - Match the pattern string against the input buffer Buffer. This
152150 void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
153151 const StringMap &VariableTable) const;
154152
155 bool hasVariable() const { return !(VariableUses.empty() &&
156 VariableDefs.empty()); }
153 bool hasVariable() const {
154 return !(VariableUses.empty() && VariableDefs.empty());
155 }
157156
158157 Check::CheckType getCheckTy() const { return CheckTy; }
159158
164163 /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
165164 /// matching this pattern at the start of \arg Buffer; a distance of zero
166165 /// should correspond to a perfect match.
167 unsigned ComputeMatchDistance(StringRef Buffer,
168 const StringMap &VariableTable) const;
166 unsigned
167 ComputeMatchDistance(StringRef Buffer,
168 const StringMap &VariableTable) const;
169169
170170 /// \brief Evaluates expression and stores the result to \p Value.
171171 /// \return true on success. false when the expression has invalid syntax.
179179 size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
180180 };
181181
182
183 bool Pattern::ParsePattern(StringRef PatternStr,
184 StringRef Prefix,
185 SourceMgr &SM,
186 unsigned LineNumber) {
182 bool Pattern::ParsePattern(StringRef PatternStr, StringRef Prefix,
183 SourceMgr &SM, unsigned LineNumber) {
187184 bool MatchFullLinesHere = MatchFullLines && CheckTy != Check::CheckNot;
188185
189186 this->LineNumber = LineNumber;
192189 // Ignore trailing whitespace.
193190 while (!PatternStr.empty() &&
194191 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
195 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
192 PatternStr = PatternStr.substr(0, PatternStr.size() - 1);
196193
197194 // Check that there is something on the line.
198195 if (PatternStr.empty()) {
199196 SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
200 "found empty check string with prefix '" +
201 Prefix + ":'");
197 "found empty check string with prefix '" + Prefix + ":'");
202198 return true;
203199 }
204200
241237 RegExStr += '(';
242238 ++CurParen;
243239
244 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
240 if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
245241 return true;
246242 RegExStr += ')';
247243
248 PatternStr = PatternStr.substr(End+2);
244 PatternStr = PatternStr.substr(End + 2);
249245 continue;
250246 }
251247
267263 }
268264
269265 StringRef MatchStr = PatternStr.substr(2, End);
270 PatternStr = PatternStr.substr(End+4);
266 PatternStr = PatternStr.substr(End + 4);
271267
272268 // Get the regex name (e.g. "foo").
273269 size_t NameEnd = MatchStr.find(':');
296292 }
297293 if (Name[i] != '_' && !isalnum(Name[i]) &&
298294 (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) {
299 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
295 SM.PrintMessage(SMLoc::getFromPointer(Name.data() + i),
300296 SourceMgr::DK_Error, "invalid name in named regex");
301297 return true;
302298 }
333329 RegExStr += '(';
334330 ++CurParen;
335331
336 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
332 if (AddRegExToRegEx(MatchStr.substr(NameEnd + 1), CurParen, SM))
337333 return true;
338334
339335 RegExStr += ')';
356352 return false;
357353 }
358354
359 bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen,
360 SourceMgr &SM) {
355 bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
361356 Regex R(RS);
362357 std::string Error;
363358 if (!R.isValid(Error)) {
373368
374369 void Pattern::AddBackrefToRegEx(unsigned BackrefNum) {
375370 assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
376 std::string Backref = std::string("\\") +
377 std::string(1, '0' + BackrefNum);
371 std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum);
378372 RegExStr += Backref;
379373 }
380374
450444 RegExToMatch = TmpStr;
451445 }
452446
453
454447 SmallVector MatchInfo;
455448 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
456449 return StringRef::npos;
466459 }
467460
468461 MatchLen = FullMatch.size();
469 return FullMatch.data()-Buffer.data();
470 }
471
472 unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
462 return FullMatch.data() - Buffer.data();
463 }
464
465 unsigned
466 Pattern::ComputeMatchDistance(StringRef Buffer,
473467 const StringMap &VariableTable) const {
474468 // Just compute the number of matching characters. For regular expressions, we
475469 // just compare against the regex itself and hope for the best.
487481 return BufferPrefix.edit_distance(ExampleString);
488482 }
489483
490 void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
491 const StringMap &VariableTable) const{
484 void Pattern::PrintFailureInfo(
485 const SourceMgr &SM, StringRef Buffer,
486 const StringMap &VariableTable) const {
492487 // If this was a regular expression using variables, print the current
493488 // variable values.
494489 if (!VariableUses.empty()) {
558553 // reasonable and not equal to what we showed in the "scanning from here"
559554 // line.
560555 if (Best && Best != StringRef::npos && BestQuality < 50) {
561 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
562 SourceMgr::DK_Note, "possible intended match here");
556 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
557 SourceMgr::DK_Note, "possible intended match here");
563558
564559 // FIXME: If we wanted to be really friendly we would show why the match
565560 // failed, as it can be hard to spot simple one character differences.
581576 Offset += 2;
582577 } else {
583578 switch (Str[0]) {
584 default:
585 break;
586 case '[':
587 BracketDepth++;
588 break;
589 case ']':
590 if (BracketDepth == 0) {
591 SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
592 SourceMgr::DK_Error,
593 "missing closing \"]\" for regex variable");
594 exit(1);
595 }
596 BracketDepth--;
597 break;
579 default:
580 break;
581 case '[':
582 BracketDepth++;
583 break;
584 case ']':
585 if (BracketDepth == 0) {
586 SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
587 SourceMgr::DK_Error,
588 "missing closing \"]\" for regex variable");
589 exit(1);
590 }
591 BracketDepth--;
592 break;
598593 }
599594 Str = Str.substr(1);
600595 Offset++;
603598
604599 return StringRef::npos;
605600 }
606
607601
608602 //===----------------------------------------------------------------------===//
609603 // Check Strings.
678672
679673 // Otherwise, add one space and advance over neighboring space.
680674 OutputBuffer.push_back(' ');
681 while (Ptr+1 != End &&
682 (Ptr[1] == ' ' || Ptr[1] == '\t'))
675 while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
683676 ++Ptr;
684677 }
685678
879872 "IMPLICIT-CHECK", SM, 0);
880873 }
881874
882
883875 std::vector DagNotMatches = ImplicitNegativeChecks;
884876
885877 // LineNumber keeps track of the line on which CheckPrefix instances are
891883 size_t PrefixLoc;
892884
893885 // See if a prefix occurs in the memory buffer.
894 StringRef UsedPrefix = FindFirstMatchingPrefix(Buffer,
895 LineNumber,
896 CheckTy,
897 PrefixLoc);
886 StringRef UsedPrefix =
887 FindFirstMatchingPrefix(Buffer, LineNumber, CheckTy, PrefixLoc);
898888 if (UsedPrefix.empty())
899889 break;
900890
908898
909899 // Complain about useful-looking but unsupported suffixes.
910900 if (CheckTy == Check::CheckBadNot) {
911 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
912 SourceMgr::DK_Error,
901 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
913902 "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
914903 return true;
915904 }
931920
932921 // Verify that CHECK-LABEL lines do not define or use variables
933922 if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
934 SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
935 SourceMgr::DK_Error,
936 "found '" + UsedPrefix + "-LABEL:'"
937 " with variable definition or use");
923 SM.PrintMessage(
924 SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
925 "found '" + UsedPrefix + "-LABEL:'"
926 " with variable definition or use");
938927 return true;
939928 }
940929
946935 StringRef Type = CheckTy == Check::CheckNext ? "NEXT" : "SAME";
947936 SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
948937 SourceMgr::DK_Error,
949 "found '" + UsedPrefix + "-" + Type + "' without previous '"
950 + UsedPrefix + ": line");
938 "found '" + UsedPrefix + "-" + Type +
939 "' without previous '" + UsedPrefix + ": line");
951940 return true;
952941 }
953942
990979 return false;
991980 }
992981
993 static void PrintCheckFailed(const SourceMgr &SM, SMLoc Loc,
994 const Pattern &Pat, StringRef Buffer,
982 static void PrintCheckFailed(const SourceMgr &SM, SMLoc Loc, const Pattern &Pat,
983 StringRef Buffer,
995984 StringMap &VariableTable) {
996985 // Otherwise, we have an error, emit an error message.
997986 SM.PrintMessage(Loc, SourceMgr::DK_Error,
10221011 while (1) {
10231012 // Scan for newline.
10241013 Range = Range.substr(Range.find_first_of("\n\r"));
1025 if (Range.empty()) return NumNewLines;
1014 if (Range.empty())
1015 return NumNewLines;
10261016
10271017 ++NumNewLines;
10281018
10291019 // Handle \n\r and \r\n as a single newline.
1030 if (Range.size() > 1 &&
1031 (Range[1] == '\n' || Range[1] == '\r') &&
1020 if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
10321021 (Range[0] != Range[1]))
10331022 Range = Range.substr(1);
10341023 Range = Range.substr(1);
10931082
10941083 // Count the number of newlines between the previous match and this one.
10951084 assert(Buffer.data() !=
1096 SM.getMemoryBuffer(
1097 SM.FindBufferContainingLoc(
1098 SMLoc::getFromPointer(Buffer.data())))->getBufferStart() &&
1085 SM.getMemoryBuffer(SM.FindBufferContainingLoc(
1086 SMLoc::getFromPointer(Buffer.data())))
1087 ->getBufferStart() &&
10991088 "CHECK-NEXT can't be the first check in a file");
11001089
11011090 const char *FirstNewLine = nullptr;
11021091 unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
11031092
11041093 if (NumNewLines == 0) {
1105 SM.PrintMessage(Loc, SourceMgr::DK_Error, Prefix +
1106 "-NEXT: is on the same line as previous match");
1107 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()),
1108 SourceMgr::DK_Note, "'next' match was here");
1094 SM.PrintMessage(Loc, SourceMgr::DK_Error,
1095 Prefix + "-NEXT: is on the same line as previous match");
1096 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1097 "'next' match was here");
11091098 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
11101099 "previous match ended here");
11111100 return true;
11121101 }
11131102
11141103 if (NumNewLines != 1) {
1115 SM.PrintMessage(Loc, SourceMgr::DK_Error, Prefix +
1116 "-NEXT: is not on the line after the previous match");
1117 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()),
1118 SourceMgr::DK_Note, "'next' match was here");
1104 SM.PrintMessage(Loc, SourceMgr::DK_Error,
1105 Prefix +
1106 "-NEXT: is not on the line after the previous match");
1107 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1108 "'next' match was here");
11191109 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
11201110 "previous match ended here");
11211111 SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note,
11631153 size_t MatchLen = 0;
11641154 size_t Pos = Pat->Match(Buffer, MatchLen, VariableTable);
11651155
1166 if (Pos == StringRef::npos) continue;
1167
1168 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()+Pos),
1169 SourceMgr::DK_Error,
1170 Prefix + "-NOT: string occurred!");
1156 if (Pos == StringRef::npos)
1157 continue;
1158
1159 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Pos),
1160 SourceMgr::DK_Error, Prefix + "-NOT: string occurred!");
11711161 SM.PrintMessage(Pat->getLoc(), SourceMgr::DK_Note,
11721162 Prefix + "-NOT: pattern specified here");
11731163 return true;
12171207 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + MatchPos),
12181208 SourceMgr::DK_Error,
12191209 Prefix + "-DAG: found a match of CHECK-DAG"
1220 " reordering across a CHECK-NOT");
1210 " reordering across a CHECK-NOT");
12211211 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + LastPos),
12221212 SourceMgr::DK_Note,
12231213 Prefix + "-DAG: the farthest match of CHECK-DAG"
1224 " is found here");
1214 " is found here");
12251215 SM.PrintMessage(NotStrings[0]->getLoc(), SourceMgr::DK_Note,
12261216 Prefix + "-NOT: the crossed pattern specified"
1227 " here");
1217 " here");
12281218 SM.PrintMessage(Pat.getLoc(), SourceMgr::DK_Note,
12291219 Prefix + "-DAG: the reordered pattern specified"
1230 " here");
1220 " here");
12311221 return StringRef::npos;
12321222 }
12331223 // All subsequent CHECK-DAGs should be matched from the farthest
13131303
13141304 // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
13151305 size_t MatchLabelLen = 0;
1316 size_t MatchLabelPos = CheckLabelStr.Check(SM, Buffer, true,
1317 MatchLabelLen, VariableTable);
1306 size_t MatchLabelPos =
1307 CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, VariableTable);
13181308 if (MatchLabelPos == StringRef::npos)
13191309 // Immediately bail of CHECK-LABEL fails, nothing else we can do.
13201310 return false;
13241314 ++j;
13251315 }
13261316
1327 for ( ; i != j; ++i) {
1317 for (; i != j; ++i) {
13281318 const CheckString &CheckStr = CheckStrings[i];
13291319
13301320 // Check each string within the scanned region, including a second check
13311321 // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
13321322 size_t MatchLen = 0;
1333 size_t MatchPos = CheckStr.Check(SM, CheckRegion, false, MatchLen,
1334 VariableTable);
1323 size_t MatchPos =
1324 CheckStr.Check(SM, CheckRegion, false, MatchLen, VariableTable);
13351325
13361326 if (MatchPos == StringRef::npos) {
13371327 ChecksFailed = true;
13881378 if (ReadCheckFile(SM, CheckFileText, CheckStrings))
13891379 return 2;
13901380
1391
13921381 // Open the file to check and add it to SourceMgr.
13931382 ErrorOr> InputFileOrErr =
13941383 MemoryBuffer::getFileOrSTDIN(InputFilename);
14091398 StringRef InputFileText =
14101399 CanonicalizeFile(InputFile, NoCanonicalizeWhiteSpace, InputFileBuffer);
14111400
1412 SM.AddNewSourceBuffer(
1413 MemoryBuffer::getMemBuffer(InputFileText, InputFile.getBufferIdentifier()), SMLoc());
1401 SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer(
1402 InputFileText, InputFile.getBufferIdentifier()),
1403 SMLoc());
14141404
14151405 return CheckInput(SM, InputFileText, CheckStrings) ? EXIT_SUCCESS : 1;
14161406 }