llvm.org GIT mirror llvm / 7112c86
Trailing whitespace. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111656 91177308-0d34-0410-b5e6-96231b3b80d8 Mikhail Glushenkov 9 years ago
3 changed file(s) with 92 addition(s) and 92 deletion(s). Raw diff Collapse all Expand all
2323 /// handlers which conflict with the ones installed by this module.
2424 /// Defaults to false.
2525 extern bool DisablePrettyStackTrace;
26
26
2727 /// PrettyStackTraceEntry - This class is used to represent a frame of the
2828 /// "pretty" stack trace that is dumped when a program crashes. You can define
29 /// subclasses of this and declare them on the program stack: when they are
29 /// subclasses of this and declare them on the program stack: when they are
3030 /// constructed and destructed, they will add their symbolic frames to a
3131 /// virtual stack trace. This gets dumped out if the program crashes.
3232 class PrettyStackTraceEntry {
3636 public:
3737 PrettyStackTraceEntry();
3838 virtual ~PrettyStackTraceEntry();
39
39
4040 /// print - Emit information about this stack frame to OS.
4141 virtual void print(raw_ostream &OS) const = 0;
42
42
4343 /// getNextEntry - Return the next entry in the list of frames.
4444 const PrettyStackTraceEntry *getNextEntry() const { return NextEntry; }
4545 };
46
46
4747 /// PrettyStackTraceString - This object prints a specified string (which
4848 /// should not contain newlines) to the stream as the stack trace when a crash
4949 /// occurs.
5353 PrettyStackTraceString(const char *str) : Str(str) {}
5454 virtual void print(raw_ostream &OS) const;
5555 };
56
56
5757 /// PrettyStackTraceProgram - This object prints a specified program arguments
5858 /// to the stream as the stack trace when a crash occurs.
5959 class PrettyStackTraceProgram : public PrettyStackTraceEntry {
6464 : ArgC(argc), ArgV(argv) {}
6565 virtual void print(raw_ostream &OS) const;
6666 };
67
67
6868 } // end namespace llvm
6969
7070 #endif
1717 namespace llvm {
1818 class StringRef;
1919 template class SmallVectorImpl;
20
20
2121 class Regex {
2222 public:
2323 enum {
2525 /// Compile for matching that ignores upper/lower case distinctions.
2626 IgnoreCase=1,
2727 /// Compile for newline-sensitive matching. With this flag '[^' bracket
28 /// expressions and '.' never match newline. A ^ anchor matches the
29 /// null string after any newline in the string in addition to its normal
30 /// function, and the $ anchor matches the null string before any
28 /// expressions and '.' never match newline. A ^ anchor matches the
29 /// null string after any newline in the string in addition to its normal
30 /// function, and the $ anchor matches the null string before any
3131 /// newline in the string in addition to its normal function.
3232 Newline=2
3333 };
4646 /// matches it contains. The number filled in by match will include this
4747 /// many entries plus one for the whole regex (as element 0).
4848 unsigned getNumMatches() const;
49
49
5050 /// matches - Match the regex against a given \arg String.
5151 ///
5252 /// \param Matches - If given, on a succesful match this will be filled in
4848
4949 class Pattern {
5050 SMLoc PatternLoc;
51
51
5252 /// FixedStr - If non-empty, this pattern is a fixed string match with the
5353 /// specified fixed string.
5454 StringRef FixedStr;
55
55
5656 /// RegEx - If non-empty, this is a regex pattern.
5757 std::string RegExStr;
58
58
5959 /// VariableUses - Entries in this vector map to uses of a variable in the
6060 /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain
6161 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
6262 /// value of bar at offset 3.
6363 std::vector > VariableUses;
64
64
6565 /// VariableDefs - Entries in this vector map to definitions of a variable in
6666 /// the pattern, e.g. "foo[[bar:.*]]baz". In this case, the RegExStr will
6767 /// contain "foo(.*)baz" and VariableDefs will contain the pair "bar",1. The
6868 /// index indicates what parenthesized value captures the variable value.
6969 std::vector > VariableDefs;
70
70
7171 public:
72
72
7373 Pattern() { }
74
74
7575 bool ParsePattern(StringRef PatternStr, SourceMgr &SM);
76
76
7777 /// Match - Match the pattern string against the input buffer Buffer. This
7878 /// returns the position that is matched or npos if there is no match. If
7979 /// there is a match, the size of the matched string is returned in MatchLen.
102102
103103 bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) {
104104 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
105
105
106106 // Ignore trailing whitespace.
107107 while (!PatternStr.empty() &&
108108 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
109109 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
110
110
111111 // Check that there is something on the line.
112112 if (PatternStr.empty()) {
113113 SM.PrintMessage(PatternLoc, "found empty check string with prefix '" +
114114 CheckPrefix+":'", "error");
115115 return true;
116116 }
117
117
118118 // Check to see if this is a fixed string, or if it has regex pieces.
119119 if (PatternStr.size() < 2 ||
120120 (PatternStr.find("{{") == StringRef::npos &&
122122 FixedStr = PatternStr;
123123 return false;
124124 }
125
125
126126 // Paren value #0 is for the fully matched string. Any new parenthesized
127127 // values add from their.
128128 unsigned CurParen = 1;
129
129
130130 // Otherwise, there is at least one regex piece. Build up the regex pattern
131131 // by escaping scary characters in fixed strings, building up one big regex.
132132 while (!PatternStr.empty()) {
133133 // RegEx matches.
134134 if (PatternStr.size() >= 2 &&
135135 PatternStr[0] == '{' && PatternStr[1] == '{') {
136
136
137137 // Otherwise, this is the start of a regex match. Scan for the }}.
138138 size_t End = PatternStr.find("}}");
139139 if (End == StringRef::npos) {
141141 "found start of regex string with no end '}}'", "error");
142142 return true;
143143 }
144
144
145145 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
146146 return true;
147147 PatternStr = PatternStr.substr(End+2);
148148 continue;
149149 }
150
150
151151 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
152152 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
153153 // second form is [[foo]] which is a reference to foo. The variable name
162162 "invalid named regex reference, no ]] found", "error");
163163 return true;
164164 }
165
165
166166 StringRef MatchStr = PatternStr.substr(2, End-2);
167167 PatternStr = PatternStr.substr(End+2);
168
168
169169 // Get the regex name (e.g. "foo").
170170 size_t NameEnd = MatchStr.find(':');
171171 StringRef Name = MatchStr.substr(0, NameEnd);
172
172
173173 if (Name.empty()) {
174174 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
175175 "invalid name in named regex: empty name", "error");
186186 "invalid name in named regex", "error");
187187 return true;
188188 }
189
189
190190 // Name can't start with a digit.
191191 if (isdigit(Name[0])) {
192192 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
193193 "invalid name in named regex", "error");
194194 return true;
195195 }
196
196
197197 // Handle [[foo]].
198198 if (NameEnd == StringRef::npos) {
199199 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
200200 continue;
201201 }
202
202
203203 // Handle [[foo:.*]].
204204 VariableDefs.push_back(std::make_pair(Name, CurParen));
205205 RegExStr += '(';
206206 ++CurParen;
207
207
208208 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
209209 return true;
210210
211211 RegExStr += ')';
212212 }
213
213
214214 // Handle fixed string matches.
215215 // Find the end, which is the start of the next regex.
216216 size_t FixedMatchEnd = PatternStr.find("{{");
259259 "invalid regex: " + Error, "error");
260260 return true;
261261 }
262
262
263263 RegExStr += RegexStr.str();
264264 CurParen += R.getNumMatches();
265265 return false;
277277 }
278278
279279 // Regex match.
280
280
281281 // If there are variable uses, we need to create a temporary string with the
282282 // actual value.
283283 StringRef RegExToMatch = RegExStr;
284284 std::string TmpStr;
285285 if (!VariableUses.empty()) {
286286 TmpStr = RegExStr;
287
287
288288 unsigned InsertOffset = 0;
289289 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
290290 StringMap::iterator it =
296296 // Look up the value and escape it so that we can plop it into the regex.
297297 std::string Value;
298298 AddFixedStringToRegEx(it->second, Value);
299
299
300300 // Plop it into the regex at the adjusted offset.
301301 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
302302 Value.begin(), Value.end());
303303 InsertOffset += Value.size();
304304 }
305
305
306306 // Match the newly constructed regex.
307307 RegExToMatch = TmpStr;
308308 }
309
310
309
310
311311 SmallVector MatchInfo;
312312 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
313313 return StringRef::npos;
314
314
315315 // Successful regex match.
316316 assert(!MatchInfo.empty() && "Didn't get any match");
317317 StringRef FullMatch = MatchInfo[0];
318
318
319319 // If this defines any variables, remember their values.
320320 for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) {
321321 assert(VariableDefs[i].second < MatchInfo.size() &&
322322 "Internal paren error");
323323 VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second];
324324 }
325
325
326326 MatchLen = FullMatch.size();
327327 return FullMatch.data()-Buffer.data();
328328 }
420420 struct CheckString {
421421 /// Pat - The pattern to match.
422422 Pattern Pat;
423
423
424424 /// Loc - The location in the match file that the check string was specified.
425425 SMLoc Loc;
426
426
427427 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
428428 /// to a CHECK: directive.
429429 bool IsCheckNext;
430
430
431431 /// NotStrings - These are all of the strings that are disallowed from
432432 /// occurring between this match string and the previous one (or start of
433433 /// file).
434434 std::vector > NotStrings;
435
435
436436 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
437437 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
438438 };
442442 static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
443443 SmallString<128> NewFile;
444444 NewFile.reserve(MB->getBufferSize());
445
445
446446 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
447447 Ptr != End; ++Ptr) {
448448 // If C is not a horizontal whitespace, skip it.
450450 NewFile.push_back(*Ptr);
451451 continue;
452452 }
453
453
454454 // Otherwise, add one space and advance over neighboring space.
455455 NewFile.push_back(' ');
456456 while (Ptr+1 != End &&
457457 (Ptr[1] == ' ' || Ptr[1] == '\t'))
458458 ++Ptr;
459459 }
460
460
461461 // Free the old buffer and return a new one.
462462 MemoryBuffer *MB2 =
463463 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier());
464
464
465465 delete MB;
466466 return MB2;
467467 }
476476 MemoryBuffer *F =
477477 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
478478 if (F == 0) {
479 errs() << "Could not open check file '" << CheckFilename << "': "
479 errs() << "Could not open check file '" << CheckFilename << "': "
480480 << ErrorStr << '\n';
481481 return true;
482482 }
483
483
484484 // If we want to canonicalize whitespace, strip excess whitespace from the
485485 // buffer containing the CHECK lines.
486486 if (!NoCanonicalizeWhiteSpace)
487487 F = CanonicalizeInputFile(F);
488
488
489489 SM.AddNewSourceBuffer(F, SMLoc());
490490
491491 // Find all instances of CheckPrefix followed by : in the file.
492492 StringRef Buffer = F->getBuffer();
493493
494494 std::vector > NotMatches;
495
495
496496 while (1) {
497497 // See if Prefix occurs in the memory buffer.
498498 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
499
499
500500 // If we didn't find a match, we're done.
501501 if (Buffer.empty())
502502 break;
503
503
504504 const char *CheckPrefixStart = Buffer.data();
505
505
506506 // When we find a check prefix, keep track of whether we find CHECK: or
507507 // CHECK-NEXT:
508508 bool IsCheckNext = false, IsCheckNot = false;
509
509
510510 // Verify that the : is present after the prefix.
511511 if (Buffer[CheckPrefix.size()] == ':') {
512512 Buffer = Buffer.substr(CheckPrefix.size()+1);
522522 Buffer = Buffer.substr(1);
523523 continue;
524524 }
525
525
526526 // Okay, we found the prefix, yay. Remember the rest of the line, but
527527 // ignore leading and trailing whitespace.
528528 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
529
529
530530 // Scan ahead to the end of line.
531531 size_t EOL = Buffer.find_first_of("\n\r");
532532
537537 Pattern P;
538538 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
539539 return true;
540
540
541541 Buffer = Buffer.substr(EOL);
542542
543
543
544544 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
545545 if (IsCheckNext && CheckStrings.empty()) {
546546 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
548548 CheckPrefix+ ": line", "error");
549549 return true;
550550 }
551
551
552552 // Handle CHECK-NOT.
553553 if (IsCheckNot) {
554554 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
555555 P));
556556 continue;
557557 }
558
559
558
559
560560 // Okay, add the string we captured to the output vector and move on.
561561 CheckStrings.push_back(CheckString(P,
562562 PatternLoc,
563563 IsCheckNext));
564564 std::swap(NotMatches, CheckStrings.back().NotStrings);
565565 }
566
566
567567 if (CheckStrings.empty()) {
568568 errs() << "error: no check strings found with prefix '" << CheckPrefix
569569 << ":'\n";
570570 return true;
571571 }
572
572
573573 if (!NotMatches.empty()) {
574574 errs() << "error: '" << CheckPrefix
575575 << "-NOT:' not supported after last check line.\n";
576576 return true;
577577 }
578
578
579579 return false;
580580 }
581581
585585 // Otherwise, we have an error, emit an error message.
586586 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
587587 "error");
588
588
589589 // Print the "scanning from here" line. If the current position is at the
590590 // end of a line, advance to the start of the next line.
591591 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
592
592
593593 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
594594 "note");
595595
605605 // Scan for newline.
606606 Range = Range.substr(Range.find_first_of("\n\r"));
607607 if (Range.empty()) return NumNewLines;
608
608
609609 ++NumNewLines;
610
610
611611 // Handle \n\r and \r\n as a single newline.
612612 if (Range.size() > 1 &&
613613 (Range[1] == '\n' || Range[1] == '\r') &&
623623 cl::ParseCommandLineOptions(argc, argv);
624624
625625 SourceMgr SM;
626
626
627627 // Read the expected strings from the check file.
628628 std::vector CheckStrings;
629629 if (ReadCheckFile(SM, CheckStrings))
634634 MemoryBuffer *F =
635635 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
636636 if (F == 0) {
637 errs() << "Could not open input file '" << InputFilename << "': "
637 errs() << "Could not open input file '" << InputFilename << "': "
638638 << ErrorStr << '\n';
639639 return true;
640640 }
641
641
642642 // Remove duplicate spaces in the input file if requested.
643643 if (!NoCanonicalizeWhiteSpace)
644644 F = CanonicalizeInputFile(F);
645
645
646646 SM.AddNewSourceBuffer(F, SMLoc());
647
647
648648 /// VariableTable - This holds all the current filecheck variables.
649649 StringMap VariableTable;
650
650
651651 // Check that we have all of the expected strings, in order, in the input
652652 // file.
653653 StringRef Buffer = F->getBuffer();
654
654
655655 const char *LastMatch = Buffer.data();
656
656
657657 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
658658 const CheckString &CheckStr = CheckStrings[StrNo];
659
659
660660 StringRef SearchFrom = Buffer;
661
661
662662 // Find StrNo in the file.
663663 size_t MatchLen = 0;
664664 Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen, VariableTable));
665
665
666666 // If we didn't find a match, reject the input.
667667 if (Buffer.empty()) {
668668 PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
689689 "previous match was here", "note");
690690 return 1;
691691 }
692
692
693693 if (NumNewLines != 1) {
694694 SM.PrintMessage(CheckStr.Loc,
695695 CheckPrefix+
702702 return 1;
703703 }
704704 }
705
705
706706 // If this match had "not strings", verify that they don't exist in the
707707 // skipped region.
708708 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
712712 MatchLen,
713713 VariableTable);
714714 if (Pos == StringRef::npos) continue;
715
715
716716 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
717717 CheckPrefix+"-NOT: string occurred!", "error");
718718 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first,
719719 CheckPrefix+"-NOT: pattern specified here", "note");
720720 return 1;
721721 }
722
722
723723
724724 // Otherwise, everything is good. Step over the matched text and remember
725725 // the position after the match as the end of the last match.
726726 Buffer = Buffer.substr(MatchLen);
727727 LastMatch = Buffer.data();
728728 }
729
729
730730 return 0;
731731 }