xref: /aosp_15_r20/external/llvm/utils/FileCheck/FileCheck.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- FileCheck.cpp - Check that File's Contents match what is expected --===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // FileCheck does a line-by line check of a file that validates whether it
11*9880d681SAndroid Build Coastguard Worker // contains the expected content.  This is useful for regression tests etc.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // This program exits with an error status of 2 on error, exit status of 0 if
14*9880d681SAndroid Build Coastguard Worker // the file matched the expected contents, and exit status of 1 if it did not
15*9880d681SAndroid Build Coastguard Worker // contain the expected contents.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallString.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringExtras.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringMap.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringSet.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MemoryBuffer.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/PrettyStackTrace.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Regex.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Signals.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/SourceMgr.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
30*9880d681SAndroid Build Coastguard Worker #include <algorithm>
31*9880d681SAndroid Build Coastguard Worker #include <cctype>
32*9880d681SAndroid Build Coastguard Worker #include <map>
33*9880d681SAndroid Build Coastguard Worker #include <string>
34*9880d681SAndroid Build Coastguard Worker #include <system_error>
35*9880d681SAndroid Build Coastguard Worker #include <vector>
36*9880d681SAndroid Build Coastguard Worker using namespace llvm;
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker static cl::opt<std::string>
39*9880d681SAndroid Build Coastguard Worker CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
40*9880d681SAndroid Build Coastguard Worker 
41*9880d681SAndroid Build Coastguard Worker static cl::opt<std::string>
42*9880d681SAndroid Build Coastguard Worker InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
43*9880d681SAndroid Build Coastguard Worker               cl::init("-"), cl::value_desc("filename"));
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker static cl::list<std::string>
46*9880d681SAndroid Build Coastguard Worker CheckPrefixes("check-prefix",
47*9880d681SAndroid Build Coastguard Worker               cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
48*9880d681SAndroid Build Coastguard Worker static cl::alias CheckPrefixesAlias(
49*9880d681SAndroid Build Coastguard Worker     "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated,
50*9880d681SAndroid Build Coastguard Worker     cl::NotHidden,
51*9880d681SAndroid Build Coastguard Worker     cl::desc(
52*9880d681SAndroid Build Coastguard Worker         "Alias for -check-prefix permitting multiple comma separated values"));
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
55*9880d681SAndroid Build Coastguard Worker NoCanonicalizeWhiteSpace("strict-whitespace",
56*9880d681SAndroid Build Coastguard Worker               cl::desc("Do not treat all horizontal whitespace as equivalent"));
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker static cl::list<std::string> ImplicitCheckNot(
59*9880d681SAndroid Build Coastguard Worker     "implicit-check-not",
60*9880d681SAndroid Build Coastguard Worker     cl::desc("Add an implicit negative check with this pattern to every\n"
61*9880d681SAndroid Build Coastguard Worker              "positive check. This can be used to ensure that no instances of\n"
62*9880d681SAndroid Build Coastguard Worker              "this pattern occur which are not matched by a positive pattern"),
63*9880d681SAndroid Build Coastguard Worker     cl::value_desc("pattern"));
64*9880d681SAndroid Build Coastguard Worker 
65*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> AllowEmptyInput(
66*9880d681SAndroid Build Coastguard Worker     "allow-empty", cl::init(false),
67*9880d681SAndroid Build Coastguard Worker     cl::desc("Allow the input file to be empty. This is useful when making\n"
68*9880d681SAndroid Build Coastguard Worker              "checks that some error message does not occur, for example."));
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> MatchFullLines(
71*9880d681SAndroid Build Coastguard Worker     "match-full-lines", cl::init(false),
72*9880d681SAndroid Build Coastguard Worker     cl::desc("Require all positive matches to cover an entire input line.\n"
73*9880d681SAndroid Build Coastguard Worker              "Allows leading and trailing whitespace if --strict-whitespace\n"
74*9880d681SAndroid Build Coastguard Worker              "is not also passed."));
75*9880d681SAndroid Build Coastguard Worker 
76*9880d681SAndroid Build Coastguard Worker typedef cl::list<std::string>::const_iterator prefix_iterator;
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
79*9880d681SAndroid Build Coastguard Worker // Pattern Handling Code.
80*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
81*9880d681SAndroid Build Coastguard Worker 
82*9880d681SAndroid Build Coastguard Worker namespace Check {
83*9880d681SAndroid Build Coastguard Worker   enum CheckType {
84*9880d681SAndroid Build Coastguard Worker     CheckNone = 0,
85*9880d681SAndroid Build Coastguard Worker     CheckPlain,
86*9880d681SAndroid Build Coastguard Worker     CheckNext,
87*9880d681SAndroid Build Coastguard Worker     CheckSame,
88*9880d681SAndroid Build Coastguard Worker     CheckNot,
89*9880d681SAndroid Build Coastguard Worker     CheckDAG,
90*9880d681SAndroid Build Coastguard Worker     CheckLabel,
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker     /// MatchEOF - When set, this pattern only matches the end of file. This is
93*9880d681SAndroid Build Coastguard Worker     /// used for trailing CHECK-NOTs.
94*9880d681SAndroid Build Coastguard Worker     CheckEOF,
95*9880d681SAndroid Build Coastguard Worker     /// CheckBadNot - Found -NOT combined with another CHECK suffix.
96*9880d681SAndroid Build Coastguard Worker     CheckBadNot
97*9880d681SAndroid Build Coastguard Worker   };
98*9880d681SAndroid Build Coastguard Worker }
99*9880d681SAndroid Build Coastguard Worker 
100*9880d681SAndroid Build Coastguard Worker class Pattern {
101*9880d681SAndroid Build Coastguard Worker   SMLoc PatternLoc;
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker   Check::CheckType CheckTy;
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker   /// FixedStr - If non-empty, this pattern is a fixed string match with the
106*9880d681SAndroid Build Coastguard Worker   /// specified fixed string.
107*9880d681SAndroid Build Coastguard Worker   StringRef FixedStr;
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker   /// RegEx - If non-empty, this is a regex pattern.
110*9880d681SAndroid Build Coastguard Worker   std::string RegExStr;
111*9880d681SAndroid Build Coastguard Worker 
112*9880d681SAndroid Build Coastguard Worker   /// \brief Contains the number of line this pattern is in.
113*9880d681SAndroid Build Coastguard Worker   unsigned LineNumber;
114*9880d681SAndroid Build Coastguard Worker 
115*9880d681SAndroid Build Coastguard Worker   /// VariableUses - Entries in this vector map to uses of a variable in the
116*9880d681SAndroid Build Coastguard Worker   /// pattern, e.g. "foo[[bar]]baz".  In this case, the RegExStr will contain
117*9880d681SAndroid Build Coastguard Worker   /// "foobaz" and we'll get an entry in this vector that tells us to insert the
118*9880d681SAndroid Build Coastguard Worker   /// value of bar at offset 3.
119*9880d681SAndroid Build Coastguard Worker   std::vector<std::pair<StringRef, unsigned> > VariableUses;
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker   /// VariableDefs - Maps definitions of variables to their parenthesized
122*9880d681SAndroid Build Coastguard Worker   /// capture numbers.
123*9880d681SAndroid Build Coastguard Worker   /// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to 1.
124*9880d681SAndroid Build Coastguard Worker   std::map<StringRef, unsigned> VariableDefs;
125*9880d681SAndroid Build Coastguard Worker 
126*9880d681SAndroid Build Coastguard Worker public:
127*9880d681SAndroid Build Coastguard Worker 
Pattern(Check::CheckType Ty)128*9880d681SAndroid Build Coastguard Worker   Pattern(Check::CheckType Ty)
129*9880d681SAndroid Build Coastguard Worker     : CheckTy(Ty) { }
130*9880d681SAndroid Build Coastguard Worker 
131*9880d681SAndroid Build Coastguard Worker   /// getLoc - Return the location in source code.
getLoc() const132*9880d681SAndroid Build Coastguard Worker   SMLoc getLoc() const { return PatternLoc; }
133*9880d681SAndroid Build Coastguard Worker 
134*9880d681SAndroid Build Coastguard Worker   /// ParsePattern - Parse the given string into the Pattern. Prefix provides
135*9880d681SAndroid Build Coastguard Worker   /// which prefix is being matched, SM provides the SourceMgr used for error
136*9880d681SAndroid Build Coastguard Worker   /// reports, and LineNumber is the line number in the input file from which
137*9880d681SAndroid Build Coastguard Worker   /// the pattern string was read.  Returns true in case of an error, false
138*9880d681SAndroid Build Coastguard Worker   /// otherwise.
139*9880d681SAndroid Build Coastguard Worker   bool ParsePattern(StringRef PatternStr,
140*9880d681SAndroid Build Coastguard Worker                     StringRef Prefix,
141*9880d681SAndroid Build Coastguard Worker                     SourceMgr &SM,
142*9880d681SAndroid Build Coastguard Worker                     unsigned LineNumber);
143*9880d681SAndroid Build Coastguard Worker 
144*9880d681SAndroid Build Coastguard Worker   /// Match - Match the pattern string against the input buffer Buffer.  This
145*9880d681SAndroid Build Coastguard Worker   /// returns the position that is matched or npos if there is no match.  If
146*9880d681SAndroid Build Coastguard Worker   /// there is a match, the size of the matched string is returned in MatchLen.
147*9880d681SAndroid Build Coastguard Worker   ///
148*9880d681SAndroid Build Coastguard Worker   /// The VariableTable StringMap provides the current values of filecheck
149*9880d681SAndroid Build Coastguard Worker   /// variables and is updated if this match defines new values.
150*9880d681SAndroid Build Coastguard Worker   size_t Match(StringRef Buffer, size_t &MatchLen,
151*9880d681SAndroid Build Coastguard Worker                StringMap<StringRef> &VariableTable) const;
152*9880d681SAndroid Build Coastguard Worker 
153*9880d681SAndroid Build Coastguard Worker   /// PrintFailureInfo - Print additional information about a failure to match
154*9880d681SAndroid Build Coastguard Worker   /// involving this pattern.
155*9880d681SAndroid Build Coastguard Worker   void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
156*9880d681SAndroid Build Coastguard Worker                         const StringMap<StringRef> &VariableTable) const;
157*9880d681SAndroid Build Coastguard Worker 
hasVariable() const158*9880d681SAndroid Build Coastguard Worker   bool hasVariable() const { return !(VariableUses.empty() &&
159*9880d681SAndroid Build Coastguard Worker                                       VariableDefs.empty()); }
160*9880d681SAndroid Build Coastguard Worker 
getCheckTy() const161*9880d681SAndroid Build Coastguard Worker   Check::CheckType getCheckTy() const { return CheckTy; }
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker private:
164*9880d681SAndroid Build Coastguard Worker   bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
165*9880d681SAndroid Build Coastguard Worker   void AddBackrefToRegEx(unsigned BackrefNum);
166*9880d681SAndroid Build Coastguard Worker 
167*9880d681SAndroid Build Coastguard Worker   /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
168*9880d681SAndroid Build Coastguard Worker   /// matching this pattern at the start of \arg Buffer; a distance of zero
169*9880d681SAndroid Build Coastguard Worker   /// should correspond to a perfect match.
170*9880d681SAndroid Build Coastguard Worker   unsigned ComputeMatchDistance(StringRef Buffer,
171*9880d681SAndroid Build Coastguard Worker                                const StringMap<StringRef> &VariableTable) const;
172*9880d681SAndroid Build Coastguard Worker 
173*9880d681SAndroid Build Coastguard Worker   /// \brief Evaluates expression and stores the result to \p Value.
174*9880d681SAndroid Build Coastguard Worker   /// \return true on success. false when the expression has invalid syntax.
175*9880d681SAndroid Build Coastguard Worker   bool EvaluateExpression(StringRef Expr, std::string &Value) const;
176*9880d681SAndroid Build Coastguard Worker 
177*9880d681SAndroid Build Coastguard Worker   /// \brief Finds the closing sequence of a regex variable usage or
178*9880d681SAndroid Build Coastguard Worker   /// definition. Str has to point in the beginning of the definition
179*9880d681SAndroid Build Coastguard Worker   /// (right after the opening sequence).
180*9880d681SAndroid Build Coastguard Worker   /// \return offset of the closing sequence within Str, or npos if it was not
181*9880d681SAndroid Build Coastguard Worker   /// found.
182*9880d681SAndroid Build Coastguard Worker   size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
183*9880d681SAndroid Build Coastguard Worker };
184*9880d681SAndroid Build Coastguard Worker 
185*9880d681SAndroid Build Coastguard Worker 
ParsePattern(StringRef PatternStr,StringRef Prefix,SourceMgr & SM,unsigned LineNumber)186*9880d681SAndroid Build Coastguard Worker bool Pattern::ParsePattern(StringRef PatternStr,
187*9880d681SAndroid Build Coastguard Worker                            StringRef Prefix,
188*9880d681SAndroid Build Coastguard Worker                            SourceMgr &SM,
189*9880d681SAndroid Build Coastguard Worker                            unsigned LineNumber) {
190*9880d681SAndroid Build Coastguard Worker   bool MatchFullLinesHere = MatchFullLines && CheckTy != Check::CheckNot;
191*9880d681SAndroid Build Coastguard Worker 
192*9880d681SAndroid Build Coastguard Worker   this->LineNumber = LineNumber;
193*9880d681SAndroid Build Coastguard Worker   PatternLoc = SMLoc::getFromPointer(PatternStr.data());
194*9880d681SAndroid Build Coastguard Worker 
195*9880d681SAndroid Build Coastguard Worker   // Ignore trailing whitespace.
196*9880d681SAndroid Build Coastguard Worker   while (!PatternStr.empty() &&
197*9880d681SAndroid Build Coastguard Worker          (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
198*9880d681SAndroid Build Coastguard Worker     PatternStr = PatternStr.substr(0, PatternStr.size()-1);
199*9880d681SAndroid Build Coastguard Worker 
200*9880d681SAndroid Build Coastguard Worker   // Check that there is something on the line.
201*9880d681SAndroid Build Coastguard Worker   if (PatternStr.empty()) {
202*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
203*9880d681SAndroid Build Coastguard Worker                     "found empty check string with prefix '" +
204*9880d681SAndroid Build Coastguard Worker                     Prefix + ":'");
205*9880d681SAndroid Build Coastguard Worker     return true;
206*9880d681SAndroid Build Coastguard Worker   }
207*9880d681SAndroid Build Coastguard Worker 
208*9880d681SAndroid Build Coastguard Worker   // Check to see if this is a fixed string, or if it has regex pieces.
209*9880d681SAndroid Build Coastguard Worker   if (!MatchFullLinesHere &&
210*9880d681SAndroid Build Coastguard Worker       (PatternStr.size() < 2 || (PatternStr.find("{{") == StringRef::npos &&
211*9880d681SAndroid Build Coastguard Worker                                  PatternStr.find("[[") == StringRef::npos))) {
212*9880d681SAndroid Build Coastguard Worker     FixedStr = PatternStr;
213*9880d681SAndroid Build Coastguard Worker     return false;
214*9880d681SAndroid Build Coastguard Worker   }
215*9880d681SAndroid Build Coastguard Worker 
216*9880d681SAndroid Build Coastguard Worker   if (MatchFullLinesHere) {
217*9880d681SAndroid Build Coastguard Worker     RegExStr += '^';
218*9880d681SAndroid Build Coastguard Worker     if (!NoCanonicalizeWhiteSpace)
219*9880d681SAndroid Build Coastguard Worker       RegExStr += " *";
220*9880d681SAndroid Build Coastguard Worker   }
221*9880d681SAndroid Build Coastguard Worker 
222*9880d681SAndroid Build Coastguard Worker   // Paren value #0 is for the fully matched string.  Any new parenthesized
223*9880d681SAndroid Build Coastguard Worker   // values add from there.
224*9880d681SAndroid Build Coastguard Worker   unsigned CurParen = 1;
225*9880d681SAndroid Build Coastguard Worker 
226*9880d681SAndroid Build Coastguard Worker   // Otherwise, there is at least one regex piece.  Build up the regex pattern
227*9880d681SAndroid Build Coastguard Worker   // by escaping scary characters in fixed strings, building up one big regex.
228*9880d681SAndroid Build Coastguard Worker   while (!PatternStr.empty()) {
229*9880d681SAndroid Build Coastguard Worker     // RegEx matches.
230*9880d681SAndroid Build Coastguard Worker     if (PatternStr.startswith("{{")) {
231*9880d681SAndroid Build Coastguard Worker       // This is the start of a regex match.  Scan for the }}.
232*9880d681SAndroid Build Coastguard Worker       size_t End = PatternStr.find("}}");
233*9880d681SAndroid Build Coastguard Worker       if (End == StringRef::npos) {
234*9880d681SAndroid Build Coastguard Worker         SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
235*9880d681SAndroid Build Coastguard Worker                         SourceMgr::DK_Error,
236*9880d681SAndroid Build Coastguard Worker                         "found start of regex string with no end '}}'");
237*9880d681SAndroid Build Coastguard Worker         return true;
238*9880d681SAndroid Build Coastguard Worker       }
239*9880d681SAndroid Build Coastguard Worker 
240*9880d681SAndroid Build Coastguard Worker       // Enclose {{}} patterns in parens just like [[]] even though we're not
241*9880d681SAndroid Build Coastguard Worker       // capturing the result for any purpose.  This is required in case the
242*9880d681SAndroid Build Coastguard Worker       // expression contains an alternation like: CHECK:  abc{{x|z}}def.  We
243*9880d681SAndroid Build Coastguard Worker       // want this to turn into: "abc(x|z)def" not "abcx|zdef".
244*9880d681SAndroid Build Coastguard Worker       RegExStr += '(';
245*9880d681SAndroid Build Coastguard Worker       ++CurParen;
246*9880d681SAndroid Build Coastguard Worker 
247*9880d681SAndroid Build Coastguard Worker       if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
248*9880d681SAndroid Build Coastguard Worker         return true;
249*9880d681SAndroid Build Coastguard Worker       RegExStr += ')';
250*9880d681SAndroid Build Coastguard Worker 
251*9880d681SAndroid Build Coastguard Worker       PatternStr = PatternStr.substr(End+2);
252*9880d681SAndroid Build Coastguard Worker       continue;
253*9880d681SAndroid Build Coastguard Worker     }
254*9880d681SAndroid Build Coastguard Worker 
255*9880d681SAndroid Build Coastguard Worker     // Named RegEx matches.  These are of two forms: [[foo:.*]] which matches .*
256*9880d681SAndroid Build Coastguard Worker     // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
257*9880d681SAndroid Build Coastguard Worker     // second form is [[foo]] which is a reference to foo.  The variable name
258*9880d681SAndroid Build Coastguard Worker     // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
259*9880d681SAndroid Build Coastguard Worker     // it.  This is to catch some common errors.
260*9880d681SAndroid Build Coastguard Worker     if (PatternStr.startswith("[[")) {
261*9880d681SAndroid Build Coastguard Worker       // Find the closing bracket pair ending the match.  End is going to be an
262*9880d681SAndroid Build Coastguard Worker       // offset relative to the beginning of the match string.
263*9880d681SAndroid Build Coastguard Worker       size_t End = FindRegexVarEnd(PatternStr.substr(2), SM);
264*9880d681SAndroid Build Coastguard Worker 
265*9880d681SAndroid Build Coastguard Worker       if (End == StringRef::npos) {
266*9880d681SAndroid Build Coastguard Worker         SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
267*9880d681SAndroid Build Coastguard Worker                         SourceMgr::DK_Error,
268*9880d681SAndroid Build Coastguard Worker                         "invalid named regex reference, no ]] found");
269*9880d681SAndroid Build Coastguard Worker         return true;
270*9880d681SAndroid Build Coastguard Worker       }
271*9880d681SAndroid Build Coastguard Worker 
272*9880d681SAndroid Build Coastguard Worker       StringRef MatchStr = PatternStr.substr(2, End);
273*9880d681SAndroid Build Coastguard Worker       PatternStr = PatternStr.substr(End+4);
274*9880d681SAndroid Build Coastguard Worker 
275*9880d681SAndroid Build Coastguard Worker       // Get the regex name (e.g. "foo").
276*9880d681SAndroid Build Coastguard Worker       size_t NameEnd = MatchStr.find(':');
277*9880d681SAndroid Build Coastguard Worker       StringRef Name = MatchStr.substr(0, NameEnd);
278*9880d681SAndroid Build Coastguard Worker 
279*9880d681SAndroid Build Coastguard Worker       if (Name.empty()) {
280*9880d681SAndroid Build Coastguard Worker         SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
281*9880d681SAndroid Build Coastguard Worker                         "invalid name in named regex: empty name");
282*9880d681SAndroid Build Coastguard Worker         return true;
283*9880d681SAndroid Build Coastguard Worker       }
284*9880d681SAndroid Build Coastguard Worker 
285*9880d681SAndroid Build Coastguard Worker       // Verify that the name/expression is well formed. FileCheck currently
286*9880d681SAndroid Build Coastguard Worker       // supports @LINE, @LINE+number, @LINE-number expressions. The check here
287*9880d681SAndroid Build Coastguard Worker       // is relaxed, more strict check is performed in \c EvaluateExpression.
288*9880d681SAndroid Build Coastguard Worker       bool IsExpression = false;
289*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0, e = Name.size(); i != e; ++i) {
290*9880d681SAndroid Build Coastguard Worker         if (i == 0 && Name[i] == '@') {
291*9880d681SAndroid Build Coastguard Worker           if (NameEnd != StringRef::npos) {
292*9880d681SAndroid Build Coastguard Worker             SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
293*9880d681SAndroid Build Coastguard Worker                             SourceMgr::DK_Error,
294*9880d681SAndroid Build Coastguard Worker                             "invalid name in named regex definition");
295*9880d681SAndroid Build Coastguard Worker             return true;
296*9880d681SAndroid Build Coastguard Worker           }
297*9880d681SAndroid Build Coastguard Worker           IsExpression = true;
298*9880d681SAndroid Build Coastguard Worker           continue;
299*9880d681SAndroid Build Coastguard Worker         }
300*9880d681SAndroid Build Coastguard Worker         if (Name[i] != '_' && !isalnum(Name[i]) &&
301*9880d681SAndroid Build Coastguard Worker             (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) {
302*9880d681SAndroid Build Coastguard Worker           SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
303*9880d681SAndroid Build Coastguard Worker                           SourceMgr::DK_Error, "invalid name in named regex");
304*9880d681SAndroid Build Coastguard Worker           return true;
305*9880d681SAndroid Build Coastguard Worker         }
306*9880d681SAndroid Build Coastguard Worker       }
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker       // Name can't start with a digit.
309*9880d681SAndroid Build Coastguard Worker       if (isdigit(static_cast<unsigned char>(Name[0]))) {
310*9880d681SAndroid Build Coastguard Worker         SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
311*9880d681SAndroid Build Coastguard Worker                         "invalid name in named regex");
312*9880d681SAndroid Build Coastguard Worker         return true;
313*9880d681SAndroid Build Coastguard Worker       }
314*9880d681SAndroid Build Coastguard Worker 
315*9880d681SAndroid Build Coastguard Worker       // Handle [[foo]].
316*9880d681SAndroid Build Coastguard Worker       if (NameEnd == StringRef::npos) {
317*9880d681SAndroid Build Coastguard Worker         // Handle variables that were defined earlier on the same line by
318*9880d681SAndroid Build Coastguard Worker         // emitting a backreference.
319*9880d681SAndroid Build Coastguard Worker         if (VariableDefs.find(Name) != VariableDefs.end()) {
320*9880d681SAndroid Build Coastguard Worker           unsigned VarParenNum = VariableDefs[Name];
321*9880d681SAndroid Build Coastguard Worker           if (VarParenNum < 1 || VarParenNum > 9) {
322*9880d681SAndroid Build Coastguard Worker             SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
323*9880d681SAndroid Build Coastguard Worker                             SourceMgr::DK_Error,
324*9880d681SAndroid Build Coastguard Worker                             "Can't back-reference more than 9 variables");
325*9880d681SAndroid Build Coastguard Worker             return true;
326*9880d681SAndroid Build Coastguard Worker           }
327*9880d681SAndroid Build Coastguard Worker           AddBackrefToRegEx(VarParenNum);
328*9880d681SAndroid Build Coastguard Worker         } else {
329*9880d681SAndroid Build Coastguard Worker           VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
330*9880d681SAndroid Build Coastguard Worker         }
331*9880d681SAndroid Build Coastguard Worker         continue;
332*9880d681SAndroid Build Coastguard Worker       }
333*9880d681SAndroid Build Coastguard Worker 
334*9880d681SAndroid Build Coastguard Worker       // Handle [[foo:.*]].
335*9880d681SAndroid Build Coastguard Worker       VariableDefs[Name] = CurParen;
336*9880d681SAndroid Build Coastguard Worker       RegExStr += '(';
337*9880d681SAndroid Build Coastguard Worker       ++CurParen;
338*9880d681SAndroid Build Coastguard Worker 
339*9880d681SAndroid Build Coastguard Worker       if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
340*9880d681SAndroid Build Coastguard Worker         return true;
341*9880d681SAndroid Build Coastguard Worker 
342*9880d681SAndroid Build Coastguard Worker       RegExStr += ')';
343*9880d681SAndroid Build Coastguard Worker     }
344*9880d681SAndroid Build Coastguard Worker 
345*9880d681SAndroid Build Coastguard Worker     // Handle fixed string matches.
346*9880d681SAndroid Build Coastguard Worker     // Find the end, which is the start of the next regex.
347*9880d681SAndroid Build Coastguard Worker     size_t FixedMatchEnd = PatternStr.find("{{");
348*9880d681SAndroid Build Coastguard Worker     FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
349*9880d681SAndroid Build Coastguard Worker     RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
350*9880d681SAndroid Build Coastguard Worker     PatternStr = PatternStr.substr(FixedMatchEnd);
351*9880d681SAndroid Build Coastguard Worker   }
352*9880d681SAndroid Build Coastguard Worker 
353*9880d681SAndroid Build Coastguard Worker   if (MatchFullLinesHere) {
354*9880d681SAndroid Build Coastguard Worker     if (!NoCanonicalizeWhiteSpace)
355*9880d681SAndroid Build Coastguard Worker       RegExStr += " *";
356*9880d681SAndroid Build Coastguard Worker     RegExStr += '$';
357*9880d681SAndroid Build Coastguard Worker   }
358*9880d681SAndroid Build Coastguard Worker 
359*9880d681SAndroid Build Coastguard Worker   return false;
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker 
AddRegExToRegEx(StringRef RS,unsigned & CurParen,SourceMgr & SM)362*9880d681SAndroid Build Coastguard Worker bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen,
363*9880d681SAndroid Build Coastguard Worker                               SourceMgr &SM) {
364*9880d681SAndroid Build Coastguard Worker   Regex R(RS);
365*9880d681SAndroid Build Coastguard Worker   std::string Error;
366*9880d681SAndroid Build Coastguard Worker   if (!R.isValid(Error)) {
367*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error,
368*9880d681SAndroid Build Coastguard Worker                     "invalid regex: " + Error);
369*9880d681SAndroid Build Coastguard Worker     return true;
370*9880d681SAndroid Build Coastguard Worker   }
371*9880d681SAndroid Build Coastguard Worker 
372*9880d681SAndroid Build Coastguard Worker   RegExStr += RS.str();
373*9880d681SAndroid Build Coastguard Worker   CurParen += R.getNumMatches();
374*9880d681SAndroid Build Coastguard Worker   return false;
375*9880d681SAndroid Build Coastguard Worker }
376*9880d681SAndroid Build Coastguard Worker 
AddBackrefToRegEx(unsigned BackrefNum)377*9880d681SAndroid Build Coastguard Worker void Pattern::AddBackrefToRegEx(unsigned BackrefNum) {
378*9880d681SAndroid Build Coastguard Worker   assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
379*9880d681SAndroid Build Coastguard Worker   std::string Backref = std::string("\\") +
380*9880d681SAndroid Build Coastguard Worker                         std::string(1, '0' + BackrefNum);
381*9880d681SAndroid Build Coastguard Worker   RegExStr += Backref;
382*9880d681SAndroid Build Coastguard Worker }
383*9880d681SAndroid Build Coastguard Worker 
EvaluateExpression(StringRef Expr,std::string & Value) const384*9880d681SAndroid Build Coastguard Worker bool Pattern::EvaluateExpression(StringRef Expr, std::string &Value) const {
385*9880d681SAndroid Build Coastguard Worker   // The only supported expression is @LINE([\+-]\d+)?
386*9880d681SAndroid Build Coastguard Worker   if (!Expr.startswith("@LINE"))
387*9880d681SAndroid Build Coastguard Worker     return false;
388*9880d681SAndroid Build Coastguard Worker   Expr = Expr.substr(StringRef("@LINE").size());
389*9880d681SAndroid Build Coastguard Worker   int Offset = 0;
390*9880d681SAndroid Build Coastguard Worker   if (!Expr.empty()) {
391*9880d681SAndroid Build Coastguard Worker     if (Expr[0] == '+')
392*9880d681SAndroid Build Coastguard Worker       Expr = Expr.substr(1);
393*9880d681SAndroid Build Coastguard Worker     else if (Expr[0] != '-')
394*9880d681SAndroid Build Coastguard Worker       return false;
395*9880d681SAndroid Build Coastguard Worker     if (Expr.getAsInteger(10, Offset))
396*9880d681SAndroid Build Coastguard Worker       return false;
397*9880d681SAndroid Build Coastguard Worker   }
398*9880d681SAndroid Build Coastguard Worker   Value = llvm::itostr(LineNumber + Offset);
399*9880d681SAndroid Build Coastguard Worker   return true;
400*9880d681SAndroid Build Coastguard Worker }
401*9880d681SAndroid Build Coastguard Worker 
402*9880d681SAndroid Build Coastguard Worker /// Match - Match the pattern string against the input buffer Buffer.  This
403*9880d681SAndroid Build Coastguard Worker /// returns the position that is matched or npos if there is no match.  If
404*9880d681SAndroid Build Coastguard Worker /// there is a match, the size of the matched string is returned in MatchLen.
Match(StringRef Buffer,size_t & MatchLen,StringMap<StringRef> & VariableTable) const405*9880d681SAndroid Build Coastguard Worker size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
406*9880d681SAndroid Build Coastguard Worker                       StringMap<StringRef> &VariableTable) const {
407*9880d681SAndroid Build Coastguard Worker   // If this is the EOF pattern, match it immediately.
408*9880d681SAndroid Build Coastguard Worker   if (CheckTy == Check::CheckEOF) {
409*9880d681SAndroid Build Coastguard Worker     MatchLen = 0;
410*9880d681SAndroid Build Coastguard Worker     return Buffer.size();
411*9880d681SAndroid Build Coastguard Worker   }
412*9880d681SAndroid Build Coastguard Worker 
413*9880d681SAndroid Build Coastguard Worker   // If this is a fixed string pattern, just match it now.
414*9880d681SAndroid Build Coastguard Worker   if (!FixedStr.empty()) {
415*9880d681SAndroid Build Coastguard Worker     MatchLen = FixedStr.size();
416*9880d681SAndroid Build Coastguard Worker     return Buffer.find(FixedStr);
417*9880d681SAndroid Build Coastguard Worker   }
418*9880d681SAndroid Build Coastguard Worker 
419*9880d681SAndroid Build Coastguard Worker   // Regex match.
420*9880d681SAndroid Build Coastguard Worker 
421*9880d681SAndroid Build Coastguard Worker   // If there are variable uses, we need to create a temporary string with the
422*9880d681SAndroid Build Coastguard Worker   // actual value.
423*9880d681SAndroid Build Coastguard Worker   StringRef RegExToMatch = RegExStr;
424*9880d681SAndroid Build Coastguard Worker   std::string TmpStr;
425*9880d681SAndroid Build Coastguard Worker   if (!VariableUses.empty()) {
426*9880d681SAndroid Build Coastguard Worker     TmpStr = RegExStr;
427*9880d681SAndroid Build Coastguard Worker 
428*9880d681SAndroid Build Coastguard Worker     unsigned InsertOffset = 0;
429*9880d681SAndroid Build Coastguard Worker     for (const auto &VariableUse : VariableUses) {
430*9880d681SAndroid Build Coastguard Worker       std::string Value;
431*9880d681SAndroid Build Coastguard Worker 
432*9880d681SAndroid Build Coastguard Worker       if (VariableUse.first[0] == '@') {
433*9880d681SAndroid Build Coastguard Worker         if (!EvaluateExpression(VariableUse.first, Value))
434*9880d681SAndroid Build Coastguard Worker           return StringRef::npos;
435*9880d681SAndroid Build Coastguard Worker       } else {
436*9880d681SAndroid Build Coastguard Worker         StringMap<StringRef>::iterator it =
437*9880d681SAndroid Build Coastguard Worker             VariableTable.find(VariableUse.first);
438*9880d681SAndroid Build Coastguard Worker         // If the variable is undefined, return an error.
439*9880d681SAndroid Build Coastguard Worker         if (it == VariableTable.end())
440*9880d681SAndroid Build Coastguard Worker           return StringRef::npos;
441*9880d681SAndroid Build Coastguard Worker 
442*9880d681SAndroid Build Coastguard Worker         // Look up the value and escape it so that we can put it into the regex.
443*9880d681SAndroid Build Coastguard Worker         Value += Regex::escape(it->second);
444*9880d681SAndroid Build Coastguard Worker       }
445*9880d681SAndroid Build Coastguard Worker 
446*9880d681SAndroid Build Coastguard Worker       // Plop it into the regex at the adjusted offset.
447*9880d681SAndroid Build Coastguard Worker       TmpStr.insert(TmpStr.begin() + VariableUse.second + InsertOffset,
448*9880d681SAndroid Build Coastguard Worker                     Value.begin(), Value.end());
449*9880d681SAndroid Build Coastguard Worker       InsertOffset += Value.size();
450*9880d681SAndroid Build Coastguard Worker     }
451*9880d681SAndroid Build Coastguard Worker 
452*9880d681SAndroid Build Coastguard Worker     // Match the newly constructed regex.
453*9880d681SAndroid Build Coastguard Worker     RegExToMatch = TmpStr;
454*9880d681SAndroid Build Coastguard Worker   }
455*9880d681SAndroid Build Coastguard Worker 
456*9880d681SAndroid Build Coastguard Worker 
457*9880d681SAndroid Build Coastguard Worker   SmallVector<StringRef, 4> MatchInfo;
458*9880d681SAndroid Build Coastguard Worker   if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
459*9880d681SAndroid Build Coastguard Worker     return StringRef::npos;
460*9880d681SAndroid Build Coastguard Worker 
461*9880d681SAndroid Build Coastguard Worker   // Successful regex match.
462*9880d681SAndroid Build Coastguard Worker   assert(!MatchInfo.empty() && "Didn't get any match");
463*9880d681SAndroid Build Coastguard Worker   StringRef FullMatch = MatchInfo[0];
464*9880d681SAndroid Build Coastguard Worker 
465*9880d681SAndroid Build Coastguard Worker   // If this defines any variables, remember their values.
466*9880d681SAndroid Build Coastguard Worker   for (const auto &VariableDef : VariableDefs) {
467*9880d681SAndroid Build Coastguard Worker     assert(VariableDef.second < MatchInfo.size() && "Internal paren error");
468*9880d681SAndroid Build Coastguard Worker     VariableTable[VariableDef.first] = MatchInfo[VariableDef.second];
469*9880d681SAndroid Build Coastguard Worker   }
470*9880d681SAndroid Build Coastguard Worker 
471*9880d681SAndroid Build Coastguard Worker   MatchLen = FullMatch.size();
472*9880d681SAndroid Build Coastguard Worker   return FullMatch.data()-Buffer.data();
473*9880d681SAndroid Build Coastguard Worker }
474*9880d681SAndroid Build Coastguard Worker 
ComputeMatchDistance(StringRef Buffer,const StringMap<StringRef> & VariableTable) const475*9880d681SAndroid Build Coastguard Worker unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
476*9880d681SAndroid Build Coastguard Worker                               const StringMap<StringRef> &VariableTable) const {
477*9880d681SAndroid Build Coastguard Worker   // Just compute the number of matching characters. For regular expressions, we
478*9880d681SAndroid Build Coastguard Worker   // just compare against the regex itself and hope for the best.
479*9880d681SAndroid Build Coastguard Worker   //
480*9880d681SAndroid Build Coastguard Worker   // FIXME: One easy improvement here is have the regex lib generate a single
481*9880d681SAndroid Build Coastguard Worker   // example regular expression which matches, and use that as the example
482*9880d681SAndroid Build Coastguard Worker   // string.
483*9880d681SAndroid Build Coastguard Worker   StringRef ExampleString(FixedStr);
484*9880d681SAndroid Build Coastguard Worker   if (ExampleString.empty())
485*9880d681SAndroid Build Coastguard Worker     ExampleString = RegExStr;
486*9880d681SAndroid Build Coastguard Worker 
487*9880d681SAndroid Build Coastguard Worker   // Only compare up to the first line in the buffer, or the string size.
488*9880d681SAndroid Build Coastguard Worker   StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
489*9880d681SAndroid Build Coastguard Worker   BufferPrefix = BufferPrefix.split('\n').first;
490*9880d681SAndroid Build Coastguard Worker   return BufferPrefix.edit_distance(ExampleString);
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker 
PrintFailureInfo(const SourceMgr & SM,StringRef Buffer,const StringMap<StringRef> & VariableTable) const493*9880d681SAndroid Build Coastguard Worker void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
494*9880d681SAndroid Build Coastguard Worker                                const StringMap<StringRef> &VariableTable) const{
495*9880d681SAndroid Build Coastguard Worker   // If this was a regular expression using variables, print the current
496*9880d681SAndroid Build Coastguard Worker   // variable values.
497*9880d681SAndroid Build Coastguard Worker   if (!VariableUses.empty()) {
498*9880d681SAndroid Build Coastguard Worker     for (const auto &VariableUse : VariableUses) {
499*9880d681SAndroid Build Coastguard Worker       SmallString<256> Msg;
500*9880d681SAndroid Build Coastguard Worker       raw_svector_ostream OS(Msg);
501*9880d681SAndroid Build Coastguard Worker       StringRef Var = VariableUse.first;
502*9880d681SAndroid Build Coastguard Worker       if (Var[0] == '@') {
503*9880d681SAndroid Build Coastguard Worker         std::string Value;
504*9880d681SAndroid Build Coastguard Worker         if (EvaluateExpression(Var, Value)) {
505*9880d681SAndroid Build Coastguard Worker           OS << "with expression \"";
506*9880d681SAndroid Build Coastguard Worker           OS.write_escaped(Var) << "\" equal to \"";
507*9880d681SAndroid Build Coastguard Worker           OS.write_escaped(Value) << "\"";
508*9880d681SAndroid Build Coastguard Worker         } else {
509*9880d681SAndroid Build Coastguard Worker           OS << "uses incorrect expression \"";
510*9880d681SAndroid Build Coastguard Worker           OS.write_escaped(Var) << "\"";
511*9880d681SAndroid Build Coastguard Worker         }
512*9880d681SAndroid Build Coastguard Worker       } else {
513*9880d681SAndroid Build Coastguard Worker         StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
514*9880d681SAndroid Build Coastguard Worker 
515*9880d681SAndroid Build Coastguard Worker         // Check for undefined variable references.
516*9880d681SAndroid Build Coastguard Worker         if (it == VariableTable.end()) {
517*9880d681SAndroid Build Coastguard Worker           OS << "uses undefined variable \"";
518*9880d681SAndroid Build Coastguard Worker           OS.write_escaped(Var) << "\"";
519*9880d681SAndroid Build Coastguard Worker         } else {
520*9880d681SAndroid Build Coastguard Worker           OS << "with variable \"";
521*9880d681SAndroid Build Coastguard Worker           OS.write_escaped(Var) << "\" equal to \"";
522*9880d681SAndroid Build Coastguard Worker           OS.write_escaped(it->second) << "\"";
523*9880d681SAndroid Build Coastguard Worker         }
524*9880d681SAndroid Build Coastguard Worker       }
525*9880d681SAndroid Build Coastguard Worker 
526*9880d681SAndroid Build Coastguard Worker       SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
527*9880d681SAndroid Build Coastguard Worker                       OS.str());
528*9880d681SAndroid Build Coastguard Worker     }
529*9880d681SAndroid Build Coastguard Worker   }
530*9880d681SAndroid Build Coastguard Worker 
531*9880d681SAndroid Build Coastguard Worker   // Attempt to find the closest/best fuzzy match.  Usually an error happens
532*9880d681SAndroid Build Coastguard Worker   // because some string in the output didn't exactly match. In these cases, we
533*9880d681SAndroid Build Coastguard Worker   // would like to show the user a best guess at what "should have" matched, to
534*9880d681SAndroid Build Coastguard Worker   // save them having to actually check the input manually.
535*9880d681SAndroid Build Coastguard Worker   size_t NumLinesForward = 0;
536*9880d681SAndroid Build Coastguard Worker   size_t Best = StringRef::npos;
537*9880d681SAndroid Build Coastguard Worker   double BestQuality = 0;
538*9880d681SAndroid Build Coastguard Worker 
539*9880d681SAndroid Build Coastguard Worker   // Use an arbitrary 4k limit on how far we will search.
540*9880d681SAndroid Build Coastguard Worker   for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
541*9880d681SAndroid Build Coastguard Worker     if (Buffer[i] == '\n')
542*9880d681SAndroid Build Coastguard Worker       ++NumLinesForward;
543*9880d681SAndroid Build Coastguard Worker 
544*9880d681SAndroid Build Coastguard Worker     // Patterns have leading whitespace stripped, so skip whitespace when
545*9880d681SAndroid Build Coastguard Worker     // looking for something which looks like a pattern.
546*9880d681SAndroid Build Coastguard Worker     if (Buffer[i] == ' ' || Buffer[i] == '\t')
547*9880d681SAndroid Build Coastguard Worker       continue;
548*9880d681SAndroid Build Coastguard Worker 
549*9880d681SAndroid Build Coastguard Worker     // Compute the "quality" of this match as an arbitrary combination of the
550*9880d681SAndroid Build Coastguard Worker     // match distance and the number of lines skipped to get to this match.
551*9880d681SAndroid Build Coastguard Worker     unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
552*9880d681SAndroid Build Coastguard Worker     double Quality = Distance + (NumLinesForward / 100.);
553*9880d681SAndroid Build Coastguard Worker 
554*9880d681SAndroid Build Coastguard Worker     if (Quality < BestQuality || Best == StringRef::npos) {
555*9880d681SAndroid Build Coastguard Worker       Best = i;
556*9880d681SAndroid Build Coastguard Worker       BestQuality = Quality;
557*9880d681SAndroid Build Coastguard Worker     }
558*9880d681SAndroid Build Coastguard Worker   }
559*9880d681SAndroid Build Coastguard Worker 
560*9880d681SAndroid Build Coastguard Worker   // Print the "possible intended match here" line if we found something
561*9880d681SAndroid Build Coastguard Worker   // reasonable and not equal to what we showed in the "scanning from here"
562*9880d681SAndroid Build Coastguard Worker   // line.
563*9880d681SAndroid Build Coastguard Worker   if (Best && Best != StringRef::npos && BestQuality < 50) {
564*9880d681SAndroid Build Coastguard Worker       SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
565*9880d681SAndroid Build Coastguard Worker                       SourceMgr::DK_Note, "possible intended match here");
566*9880d681SAndroid Build Coastguard Worker 
567*9880d681SAndroid Build Coastguard Worker     // FIXME: If we wanted to be really friendly we would show why the match
568*9880d681SAndroid Build Coastguard Worker     // failed, as it can be hard to spot simple one character differences.
569*9880d681SAndroid Build Coastguard Worker   }
570*9880d681SAndroid Build Coastguard Worker }
571*9880d681SAndroid Build Coastguard Worker 
FindRegexVarEnd(StringRef Str,SourceMgr & SM)572*9880d681SAndroid Build Coastguard Worker size_t Pattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
573*9880d681SAndroid Build Coastguard Worker   // Offset keeps track of the current offset within the input Str
574*9880d681SAndroid Build Coastguard Worker   size_t Offset = 0;
575*9880d681SAndroid Build Coastguard Worker   // [...] Nesting depth
576*9880d681SAndroid Build Coastguard Worker   size_t BracketDepth = 0;
577*9880d681SAndroid Build Coastguard Worker 
578*9880d681SAndroid Build Coastguard Worker   while (!Str.empty()) {
579*9880d681SAndroid Build Coastguard Worker     if (Str.startswith("]]") && BracketDepth == 0)
580*9880d681SAndroid Build Coastguard Worker       return Offset;
581*9880d681SAndroid Build Coastguard Worker     if (Str[0] == '\\') {
582*9880d681SAndroid Build Coastguard Worker       // Backslash escapes the next char within regexes, so skip them both.
583*9880d681SAndroid Build Coastguard Worker       Str = Str.substr(2);
584*9880d681SAndroid Build Coastguard Worker       Offset += 2;
585*9880d681SAndroid Build Coastguard Worker     } else {
586*9880d681SAndroid Build Coastguard Worker       switch (Str[0]) {
587*9880d681SAndroid Build Coastguard Worker         default:
588*9880d681SAndroid Build Coastguard Worker           break;
589*9880d681SAndroid Build Coastguard Worker         case '[':
590*9880d681SAndroid Build Coastguard Worker           BracketDepth++;
591*9880d681SAndroid Build Coastguard Worker           break;
592*9880d681SAndroid Build Coastguard Worker         case ']':
593*9880d681SAndroid Build Coastguard Worker           if (BracketDepth == 0) {
594*9880d681SAndroid Build Coastguard Worker             SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
595*9880d681SAndroid Build Coastguard Worker                             SourceMgr::DK_Error,
596*9880d681SAndroid Build Coastguard Worker                             "missing closing \"]\" for regex variable");
597*9880d681SAndroid Build Coastguard Worker             exit(1);
598*9880d681SAndroid Build Coastguard Worker           }
599*9880d681SAndroid Build Coastguard Worker           BracketDepth--;
600*9880d681SAndroid Build Coastguard Worker           break;
601*9880d681SAndroid Build Coastguard Worker       }
602*9880d681SAndroid Build Coastguard Worker       Str = Str.substr(1);
603*9880d681SAndroid Build Coastguard Worker       Offset++;
604*9880d681SAndroid Build Coastguard Worker     }
605*9880d681SAndroid Build Coastguard Worker   }
606*9880d681SAndroid Build Coastguard Worker 
607*9880d681SAndroid Build Coastguard Worker   return StringRef::npos;
608*9880d681SAndroid Build Coastguard Worker }
609*9880d681SAndroid Build Coastguard Worker 
610*9880d681SAndroid Build Coastguard Worker 
611*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
612*9880d681SAndroid Build Coastguard Worker // Check Strings.
613*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
614*9880d681SAndroid Build Coastguard Worker 
615*9880d681SAndroid Build Coastguard Worker /// CheckString - This is a check that we found in the input file.
616*9880d681SAndroid Build Coastguard Worker struct CheckString {
617*9880d681SAndroid Build Coastguard Worker   /// Pat - The pattern to match.
618*9880d681SAndroid Build Coastguard Worker   Pattern Pat;
619*9880d681SAndroid Build Coastguard Worker 
620*9880d681SAndroid Build Coastguard Worker   /// Prefix - Which prefix name this check matched.
621*9880d681SAndroid Build Coastguard Worker   StringRef Prefix;
622*9880d681SAndroid Build Coastguard Worker 
623*9880d681SAndroid Build Coastguard Worker   /// Loc - The location in the match file that the check string was specified.
624*9880d681SAndroid Build Coastguard Worker   SMLoc Loc;
625*9880d681SAndroid Build Coastguard Worker 
626*9880d681SAndroid Build Coastguard Worker   /// CheckTy - Specify what kind of check this is. e.g. CHECK-NEXT: directive,
627*9880d681SAndroid Build Coastguard Worker   /// as opposed to a CHECK: directive.
628*9880d681SAndroid Build Coastguard Worker   //  Check::CheckType CheckTy;
629*9880d681SAndroid Build Coastguard Worker 
630*9880d681SAndroid Build Coastguard Worker   /// DagNotStrings - These are all of the strings that are disallowed from
631*9880d681SAndroid Build Coastguard Worker   /// occurring between this match string and the previous one (or start of
632*9880d681SAndroid Build Coastguard Worker   /// file).
633*9880d681SAndroid Build Coastguard Worker   std::vector<Pattern> DagNotStrings;
634*9880d681SAndroid Build Coastguard Worker 
CheckStringCheckString635*9880d681SAndroid Build Coastguard Worker   CheckString(const Pattern &P, StringRef S, SMLoc L)
636*9880d681SAndroid Build Coastguard Worker       : Pat(P), Prefix(S), Loc(L) {}
637*9880d681SAndroid Build Coastguard Worker 
638*9880d681SAndroid Build Coastguard Worker   /// Check - Match check string and its "not strings" and/or "dag strings".
639*9880d681SAndroid Build Coastguard Worker   size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
640*9880d681SAndroid Build Coastguard Worker                size_t &MatchLen, StringMap<StringRef> &VariableTable) const;
641*9880d681SAndroid Build Coastguard Worker 
642*9880d681SAndroid Build Coastguard Worker   /// CheckNext - Verify there is a single line in the given buffer.
643*9880d681SAndroid Build Coastguard Worker   bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
644*9880d681SAndroid Build Coastguard Worker 
645*9880d681SAndroid Build Coastguard Worker   /// CheckSame - Verify there is no newline in the given buffer.
646*9880d681SAndroid Build Coastguard Worker   bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
647*9880d681SAndroid Build Coastguard Worker 
648*9880d681SAndroid Build Coastguard Worker   /// CheckNot - Verify there's no "not strings" in the given buffer.
649*9880d681SAndroid Build Coastguard Worker   bool CheckNot(const SourceMgr &SM, StringRef Buffer,
650*9880d681SAndroid Build Coastguard Worker                 const std::vector<const Pattern *> &NotStrings,
651*9880d681SAndroid Build Coastguard Worker                 StringMap<StringRef> &VariableTable) const;
652*9880d681SAndroid Build Coastguard Worker 
653*9880d681SAndroid Build Coastguard Worker   /// CheckDag - Match "dag strings" and their mixed "not strings".
654*9880d681SAndroid Build Coastguard Worker   size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
655*9880d681SAndroid Build Coastguard Worker                   std::vector<const Pattern *> &NotStrings,
656*9880d681SAndroid Build Coastguard Worker                   StringMap<StringRef> &VariableTable) const;
657*9880d681SAndroid Build Coastguard Worker };
658*9880d681SAndroid Build Coastguard Worker 
659*9880d681SAndroid Build Coastguard Worker /// Canonicalize whitespaces in the input file. Line endings are replaced
660*9880d681SAndroid Build Coastguard Worker /// with UNIX-style '\n'.
661*9880d681SAndroid Build Coastguard Worker ///
662*9880d681SAndroid Build Coastguard Worker /// \param PreserveHorizontal Don't squash consecutive horizontal whitespace
663*9880d681SAndroid Build Coastguard Worker /// characters to a single space.
664*9880d681SAndroid Build Coastguard Worker static std::unique_ptr<MemoryBuffer>
CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB,bool PreserveHorizontal)665*9880d681SAndroid Build Coastguard Worker CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB,
666*9880d681SAndroid Build Coastguard Worker                       bool PreserveHorizontal) {
667*9880d681SAndroid Build Coastguard Worker   SmallString<128> NewFile;
668*9880d681SAndroid Build Coastguard Worker   NewFile.reserve(MB->getBufferSize());
669*9880d681SAndroid Build Coastguard Worker 
670*9880d681SAndroid Build Coastguard Worker   for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
671*9880d681SAndroid Build Coastguard Worker        Ptr != End; ++Ptr) {
672*9880d681SAndroid Build Coastguard Worker     // Eliminate trailing dosish \r.
673*9880d681SAndroid Build Coastguard Worker     if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
674*9880d681SAndroid Build Coastguard Worker       continue;
675*9880d681SAndroid Build Coastguard Worker     }
676*9880d681SAndroid Build Coastguard Worker 
677*9880d681SAndroid Build Coastguard Worker     // If current char is not a horizontal whitespace or if horizontal
678*9880d681SAndroid Build Coastguard Worker     // whitespace canonicalization is disabled, dump it to output as is.
679*9880d681SAndroid Build Coastguard Worker     if (PreserveHorizontal || (*Ptr != ' ' && *Ptr != '\t')) {
680*9880d681SAndroid Build Coastguard Worker       NewFile.push_back(*Ptr);
681*9880d681SAndroid Build Coastguard Worker       continue;
682*9880d681SAndroid Build Coastguard Worker     }
683*9880d681SAndroid Build Coastguard Worker 
684*9880d681SAndroid Build Coastguard Worker     // Otherwise, add one space and advance over neighboring space.
685*9880d681SAndroid Build Coastguard Worker     NewFile.push_back(' ');
686*9880d681SAndroid Build Coastguard Worker     while (Ptr+1 != End &&
687*9880d681SAndroid Build Coastguard Worker            (Ptr[1] == ' ' || Ptr[1] == '\t'))
688*9880d681SAndroid Build Coastguard Worker       ++Ptr;
689*9880d681SAndroid Build Coastguard Worker   }
690*9880d681SAndroid Build Coastguard Worker 
691*9880d681SAndroid Build Coastguard Worker   return std::unique_ptr<MemoryBuffer>(
692*9880d681SAndroid Build Coastguard Worker       MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier()));
693*9880d681SAndroid Build Coastguard Worker }
694*9880d681SAndroid Build Coastguard Worker 
IsPartOfWord(char c)695*9880d681SAndroid Build Coastguard Worker static bool IsPartOfWord(char c) {
696*9880d681SAndroid Build Coastguard Worker   return (isalnum(c) || c == '-' || c == '_');
697*9880d681SAndroid Build Coastguard Worker }
698*9880d681SAndroid Build Coastguard Worker 
699*9880d681SAndroid Build Coastguard Worker // Get the size of the prefix extension.
CheckTypeSize(Check::CheckType Ty)700*9880d681SAndroid Build Coastguard Worker static size_t CheckTypeSize(Check::CheckType Ty) {
701*9880d681SAndroid Build Coastguard Worker   switch (Ty) {
702*9880d681SAndroid Build Coastguard Worker   case Check::CheckNone:
703*9880d681SAndroid Build Coastguard Worker   case Check::CheckBadNot:
704*9880d681SAndroid Build Coastguard Worker     return 0;
705*9880d681SAndroid Build Coastguard Worker 
706*9880d681SAndroid Build Coastguard Worker   case Check::CheckPlain:
707*9880d681SAndroid Build Coastguard Worker     return sizeof(":") - 1;
708*9880d681SAndroid Build Coastguard Worker 
709*9880d681SAndroid Build Coastguard Worker   case Check::CheckNext:
710*9880d681SAndroid Build Coastguard Worker     return sizeof("-NEXT:") - 1;
711*9880d681SAndroid Build Coastguard Worker 
712*9880d681SAndroid Build Coastguard Worker   case Check::CheckSame:
713*9880d681SAndroid Build Coastguard Worker     return sizeof("-SAME:") - 1;
714*9880d681SAndroid Build Coastguard Worker 
715*9880d681SAndroid Build Coastguard Worker   case Check::CheckNot:
716*9880d681SAndroid Build Coastguard Worker     return sizeof("-NOT:") - 1;
717*9880d681SAndroid Build Coastguard Worker 
718*9880d681SAndroid Build Coastguard Worker   case Check::CheckDAG:
719*9880d681SAndroid Build Coastguard Worker     return sizeof("-DAG:") - 1;
720*9880d681SAndroid Build Coastguard Worker 
721*9880d681SAndroid Build Coastguard Worker   case Check::CheckLabel:
722*9880d681SAndroid Build Coastguard Worker     return sizeof("-LABEL:") - 1;
723*9880d681SAndroid Build Coastguard Worker 
724*9880d681SAndroid Build Coastguard Worker   case Check::CheckEOF:
725*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Should not be using EOF size");
726*9880d681SAndroid Build Coastguard Worker   }
727*9880d681SAndroid Build Coastguard Worker 
728*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("Bad check type");
729*9880d681SAndroid Build Coastguard Worker }
730*9880d681SAndroid Build Coastguard Worker 
FindCheckType(StringRef Buffer,StringRef Prefix)731*9880d681SAndroid Build Coastguard Worker static Check::CheckType FindCheckType(StringRef Buffer, StringRef Prefix) {
732*9880d681SAndroid Build Coastguard Worker   char NextChar = Buffer[Prefix.size()];
733*9880d681SAndroid Build Coastguard Worker 
734*9880d681SAndroid Build Coastguard Worker   // Verify that the : is present after the prefix.
735*9880d681SAndroid Build Coastguard Worker   if (NextChar == ':')
736*9880d681SAndroid Build Coastguard Worker     return Check::CheckPlain;
737*9880d681SAndroid Build Coastguard Worker 
738*9880d681SAndroid Build Coastguard Worker   if (NextChar != '-')
739*9880d681SAndroid Build Coastguard Worker     return Check::CheckNone;
740*9880d681SAndroid Build Coastguard Worker 
741*9880d681SAndroid Build Coastguard Worker   StringRef Rest = Buffer.drop_front(Prefix.size() + 1);
742*9880d681SAndroid Build Coastguard Worker   if (Rest.startswith("NEXT:"))
743*9880d681SAndroid Build Coastguard Worker     return Check::CheckNext;
744*9880d681SAndroid Build Coastguard Worker 
745*9880d681SAndroid Build Coastguard Worker   if (Rest.startswith("SAME:"))
746*9880d681SAndroid Build Coastguard Worker     return Check::CheckSame;
747*9880d681SAndroid Build Coastguard Worker 
748*9880d681SAndroid Build Coastguard Worker   if (Rest.startswith("NOT:"))
749*9880d681SAndroid Build Coastguard Worker     return Check::CheckNot;
750*9880d681SAndroid Build Coastguard Worker 
751*9880d681SAndroid Build Coastguard Worker   if (Rest.startswith("DAG:"))
752*9880d681SAndroid Build Coastguard Worker     return Check::CheckDAG;
753*9880d681SAndroid Build Coastguard Worker 
754*9880d681SAndroid Build Coastguard Worker   if (Rest.startswith("LABEL:"))
755*9880d681SAndroid Build Coastguard Worker     return Check::CheckLabel;
756*9880d681SAndroid Build Coastguard Worker 
757*9880d681SAndroid Build Coastguard Worker   // You can't combine -NOT with another suffix.
758*9880d681SAndroid Build Coastguard Worker   if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") ||
759*9880d681SAndroid Build Coastguard Worker       Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") ||
760*9880d681SAndroid Build Coastguard Worker       Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:"))
761*9880d681SAndroid Build Coastguard Worker     return Check::CheckBadNot;
762*9880d681SAndroid Build Coastguard Worker 
763*9880d681SAndroid Build Coastguard Worker   return Check::CheckNone;
764*9880d681SAndroid Build Coastguard Worker }
765*9880d681SAndroid Build Coastguard Worker 
766*9880d681SAndroid Build Coastguard Worker // From the given position, find the next character after the word.
SkipWord(StringRef Str,size_t Loc)767*9880d681SAndroid Build Coastguard Worker static size_t SkipWord(StringRef Str, size_t Loc) {
768*9880d681SAndroid Build Coastguard Worker   while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
769*9880d681SAndroid Build Coastguard Worker     ++Loc;
770*9880d681SAndroid Build Coastguard Worker   return Loc;
771*9880d681SAndroid Build Coastguard Worker }
772*9880d681SAndroid Build Coastguard Worker 
773*9880d681SAndroid Build Coastguard Worker // Try to find the first match in buffer for any prefix. If a valid match is
774*9880d681SAndroid Build Coastguard Worker // found, return that prefix and set its type and location.  If there are almost
775*9880d681SAndroid Build Coastguard Worker // matches (e.g. the actual prefix string is found, but is not an actual check
776*9880d681SAndroid Build Coastguard Worker // string), but no valid match, return an empty string and set the position to
777*9880d681SAndroid Build Coastguard Worker // resume searching from. If no partial matches are found, return an empty
778*9880d681SAndroid Build Coastguard Worker // string and the location will be StringRef::npos. If one prefix is a substring
779*9880d681SAndroid Build Coastguard Worker // of another, the maximal match should be found. e.g. if "A" and "AA" are
780*9880d681SAndroid Build Coastguard Worker // prefixes then AA-CHECK: should match the second one.
FindFirstCandidateMatch(StringRef & Buffer,Check::CheckType & CheckTy,size_t & CheckLoc)781*9880d681SAndroid Build Coastguard Worker static StringRef FindFirstCandidateMatch(StringRef &Buffer,
782*9880d681SAndroid Build Coastguard Worker                                          Check::CheckType &CheckTy,
783*9880d681SAndroid Build Coastguard Worker                                          size_t &CheckLoc) {
784*9880d681SAndroid Build Coastguard Worker   StringRef FirstPrefix;
785*9880d681SAndroid Build Coastguard Worker   size_t FirstLoc = StringRef::npos;
786*9880d681SAndroid Build Coastguard Worker   size_t SearchLoc = StringRef::npos;
787*9880d681SAndroid Build Coastguard Worker   Check::CheckType FirstTy = Check::CheckNone;
788*9880d681SAndroid Build Coastguard Worker 
789*9880d681SAndroid Build Coastguard Worker   CheckTy = Check::CheckNone;
790*9880d681SAndroid Build Coastguard Worker   CheckLoc = StringRef::npos;
791*9880d681SAndroid Build Coastguard Worker 
792*9880d681SAndroid Build Coastguard Worker   for (StringRef Prefix : CheckPrefixes) {
793*9880d681SAndroid Build Coastguard Worker     size_t PrefixLoc = Buffer.find(Prefix);
794*9880d681SAndroid Build Coastguard Worker 
795*9880d681SAndroid Build Coastguard Worker     if (PrefixLoc == StringRef::npos)
796*9880d681SAndroid Build Coastguard Worker       continue;
797*9880d681SAndroid Build Coastguard Worker 
798*9880d681SAndroid Build Coastguard Worker     // Track where we are searching for invalid prefixes that look almost right.
799*9880d681SAndroid Build Coastguard Worker     // We need to only advance to the first partial match on the next attempt
800*9880d681SAndroid Build Coastguard Worker     // since a partial match could be a substring of a later, valid prefix.
801*9880d681SAndroid Build Coastguard Worker     // Need to skip to the end of the word, otherwise we could end up
802*9880d681SAndroid Build Coastguard Worker     // matching a prefix in a substring later.
803*9880d681SAndroid Build Coastguard Worker     if (PrefixLoc < SearchLoc)
804*9880d681SAndroid Build Coastguard Worker       SearchLoc = SkipWord(Buffer, PrefixLoc);
805*9880d681SAndroid Build Coastguard Worker 
806*9880d681SAndroid Build Coastguard Worker     // We only want to find the first match to avoid skipping some.
807*9880d681SAndroid Build Coastguard Worker     if (PrefixLoc > FirstLoc)
808*9880d681SAndroid Build Coastguard Worker       continue;
809*9880d681SAndroid Build Coastguard Worker     // If one matching check-prefix is a prefix of another, choose the
810*9880d681SAndroid Build Coastguard Worker     // longer one.
811*9880d681SAndroid Build Coastguard Worker     if (PrefixLoc == FirstLoc && Prefix.size() < FirstPrefix.size())
812*9880d681SAndroid Build Coastguard Worker       continue;
813*9880d681SAndroid Build Coastguard Worker 
814*9880d681SAndroid Build Coastguard Worker     StringRef Rest = Buffer.drop_front(PrefixLoc);
815*9880d681SAndroid Build Coastguard Worker     // Make sure we have actually found the prefix, and not a word containing
816*9880d681SAndroid Build Coastguard Worker     // it. This should also prevent matching the wrong prefix when one is a
817*9880d681SAndroid Build Coastguard Worker     // substring of another.
818*9880d681SAndroid Build Coastguard Worker     if (PrefixLoc != 0 && IsPartOfWord(Buffer[PrefixLoc - 1]))
819*9880d681SAndroid Build Coastguard Worker       FirstTy = Check::CheckNone;
820*9880d681SAndroid Build Coastguard Worker     else
821*9880d681SAndroid Build Coastguard Worker       FirstTy = FindCheckType(Rest, Prefix);
822*9880d681SAndroid Build Coastguard Worker 
823*9880d681SAndroid Build Coastguard Worker     FirstLoc = PrefixLoc;
824*9880d681SAndroid Build Coastguard Worker     FirstPrefix = Prefix;
825*9880d681SAndroid Build Coastguard Worker   }
826*9880d681SAndroid Build Coastguard Worker 
827*9880d681SAndroid Build Coastguard Worker   // If the first prefix is invalid, we should continue the search after it.
828*9880d681SAndroid Build Coastguard Worker   if (FirstTy == Check::CheckNone) {
829*9880d681SAndroid Build Coastguard Worker     CheckLoc = SearchLoc;
830*9880d681SAndroid Build Coastguard Worker     return "";
831*9880d681SAndroid Build Coastguard Worker   }
832*9880d681SAndroid Build Coastguard Worker 
833*9880d681SAndroid Build Coastguard Worker   CheckTy = FirstTy;
834*9880d681SAndroid Build Coastguard Worker   CheckLoc = FirstLoc;
835*9880d681SAndroid Build Coastguard Worker   return FirstPrefix;
836*9880d681SAndroid Build Coastguard Worker }
837*9880d681SAndroid Build Coastguard Worker 
FindFirstMatchingPrefix(StringRef & Buffer,unsigned & LineNumber,Check::CheckType & CheckTy,size_t & CheckLoc)838*9880d681SAndroid Build Coastguard Worker static StringRef FindFirstMatchingPrefix(StringRef &Buffer,
839*9880d681SAndroid Build Coastguard Worker                                          unsigned &LineNumber,
840*9880d681SAndroid Build Coastguard Worker                                          Check::CheckType &CheckTy,
841*9880d681SAndroid Build Coastguard Worker                                          size_t &CheckLoc) {
842*9880d681SAndroid Build Coastguard Worker   while (!Buffer.empty()) {
843*9880d681SAndroid Build Coastguard Worker     StringRef Prefix = FindFirstCandidateMatch(Buffer, CheckTy, CheckLoc);
844*9880d681SAndroid Build Coastguard Worker     // If we found a real match, we are done.
845*9880d681SAndroid Build Coastguard Worker     if (!Prefix.empty()) {
846*9880d681SAndroid Build Coastguard Worker       LineNumber += Buffer.substr(0, CheckLoc).count('\n');
847*9880d681SAndroid Build Coastguard Worker       return Prefix;
848*9880d681SAndroid Build Coastguard Worker     }
849*9880d681SAndroid Build Coastguard Worker 
850*9880d681SAndroid Build Coastguard Worker     // We didn't find any almost matches either, we are also done.
851*9880d681SAndroid Build Coastguard Worker     if (CheckLoc == StringRef::npos)
852*9880d681SAndroid Build Coastguard Worker       return StringRef();
853*9880d681SAndroid Build Coastguard Worker 
854*9880d681SAndroid Build Coastguard Worker     LineNumber += Buffer.substr(0, CheckLoc + 1).count('\n');
855*9880d681SAndroid Build Coastguard Worker 
856*9880d681SAndroid Build Coastguard Worker     // Advance to the last possible match we found and try again.
857*9880d681SAndroid Build Coastguard Worker     Buffer = Buffer.drop_front(CheckLoc + 1);
858*9880d681SAndroid Build Coastguard Worker   }
859*9880d681SAndroid Build Coastguard Worker 
860*9880d681SAndroid Build Coastguard Worker   return StringRef();
861*9880d681SAndroid Build Coastguard Worker }
862*9880d681SAndroid Build Coastguard Worker 
863*9880d681SAndroid Build Coastguard Worker /// ReadCheckFile - Read the check file, which specifies the sequence of
864*9880d681SAndroid Build Coastguard Worker /// expected strings.  The strings are added to the CheckStrings vector.
865*9880d681SAndroid Build Coastguard Worker /// Returns true in case of an error, false otherwise.
ReadCheckFile(SourceMgr & SM,std::vector<CheckString> & CheckStrings)866*9880d681SAndroid Build Coastguard Worker static bool ReadCheckFile(SourceMgr &SM,
867*9880d681SAndroid Build Coastguard Worker                           std::vector<CheckString> &CheckStrings) {
868*9880d681SAndroid Build Coastguard Worker   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
869*9880d681SAndroid Build Coastguard Worker       MemoryBuffer::getFileOrSTDIN(CheckFilename);
870*9880d681SAndroid Build Coastguard Worker   if (std::error_code EC = FileOrErr.getError()) {
871*9880d681SAndroid Build Coastguard Worker     errs() << "Could not open check file '" << CheckFilename
872*9880d681SAndroid Build Coastguard Worker            << "': " << EC.message() << '\n';
873*9880d681SAndroid Build Coastguard Worker     return true;
874*9880d681SAndroid Build Coastguard Worker   }
875*9880d681SAndroid Build Coastguard Worker 
876*9880d681SAndroid Build Coastguard Worker   // If we want to canonicalize whitespace, strip excess whitespace from the
877*9880d681SAndroid Build Coastguard Worker   // buffer containing the CHECK lines. Remove DOS style line endings.
878*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<MemoryBuffer> F = CanonicalizeInputFile(
879*9880d681SAndroid Build Coastguard Worker       std::move(FileOrErr.get()), NoCanonicalizeWhiteSpace);
880*9880d681SAndroid Build Coastguard Worker 
881*9880d681SAndroid Build Coastguard Worker   // Find all instances of CheckPrefix followed by : in the file.
882*9880d681SAndroid Build Coastguard Worker   StringRef Buffer = F->getBuffer();
883*9880d681SAndroid Build Coastguard Worker 
884*9880d681SAndroid Build Coastguard Worker   SM.AddNewSourceBuffer(std::move(F), SMLoc());
885*9880d681SAndroid Build Coastguard Worker 
886*9880d681SAndroid Build Coastguard Worker   std::vector<Pattern> ImplicitNegativeChecks;
887*9880d681SAndroid Build Coastguard Worker   for (const auto &PatternString : ImplicitCheckNot) {
888*9880d681SAndroid Build Coastguard Worker     // Create a buffer with fake command line content in order to display the
889*9880d681SAndroid Build Coastguard Worker     // command line option responsible for the specific implicit CHECK-NOT.
890*9880d681SAndroid Build Coastguard Worker     std::string Prefix = (Twine("-") + ImplicitCheckNot.ArgStr + "='").str();
891*9880d681SAndroid Build Coastguard Worker     std::string Suffix = "'";
892*9880d681SAndroid Build Coastguard Worker     std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
893*9880d681SAndroid Build Coastguard Worker         Prefix + PatternString + Suffix, "command line");
894*9880d681SAndroid Build Coastguard Worker 
895*9880d681SAndroid Build Coastguard Worker     StringRef PatternInBuffer =
896*9880d681SAndroid Build Coastguard Worker         CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
897*9880d681SAndroid Build Coastguard Worker     SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
898*9880d681SAndroid Build Coastguard Worker 
899*9880d681SAndroid Build Coastguard Worker     ImplicitNegativeChecks.push_back(Pattern(Check::CheckNot));
900*9880d681SAndroid Build Coastguard Worker     ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer,
901*9880d681SAndroid Build Coastguard Worker                                                "IMPLICIT-CHECK", SM, 0);
902*9880d681SAndroid Build Coastguard Worker   }
903*9880d681SAndroid Build Coastguard Worker 
904*9880d681SAndroid Build Coastguard Worker 
905*9880d681SAndroid Build Coastguard Worker   std::vector<Pattern> DagNotMatches = ImplicitNegativeChecks;
906*9880d681SAndroid Build Coastguard Worker 
907*9880d681SAndroid Build Coastguard Worker   // LineNumber keeps track of the line on which CheckPrefix instances are
908*9880d681SAndroid Build Coastguard Worker   // found.
909*9880d681SAndroid Build Coastguard Worker   unsigned LineNumber = 1;
910*9880d681SAndroid Build Coastguard Worker 
911*9880d681SAndroid Build Coastguard Worker   while (1) {
912*9880d681SAndroid Build Coastguard Worker     Check::CheckType CheckTy;
913*9880d681SAndroid Build Coastguard Worker     size_t PrefixLoc;
914*9880d681SAndroid Build Coastguard Worker 
915*9880d681SAndroid Build Coastguard Worker     // See if a prefix occurs in the memory buffer.
916*9880d681SAndroid Build Coastguard Worker     StringRef UsedPrefix = FindFirstMatchingPrefix(Buffer,
917*9880d681SAndroid Build Coastguard Worker                                                    LineNumber,
918*9880d681SAndroid Build Coastguard Worker                                                    CheckTy,
919*9880d681SAndroid Build Coastguard Worker                                                    PrefixLoc);
920*9880d681SAndroid Build Coastguard Worker     if (UsedPrefix.empty())
921*9880d681SAndroid Build Coastguard Worker       break;
922*9880d681SAndroid Build Coastguard Worker 
923*9880d681SAndroid Build Coastguard Worker     Buffer = Buffer.drop_front(PrefixLoc);
924*9880d681SAndroid Build Coastguard Worker 
925*9880d681SAndroid Build Coastguard Worker     // Location to use for error messages.
926*9880d681SAndroid Build Coastguard Worker     const char *UsedPrefixStart = Buffer.data() + (PrefixLoc == 0 ? 0 : 1);
927*9880d681SAndroid Build Coastguard Worker 
928*9880d681SAndroid Build Coastguard Worker     // PrefixLoc is to the start of the prefix. Skip to the end.
929*9880d681SAndroid Build Coastguard Worker     Buffer = Buffer.drop_front(UsedPrefix.size() + CheckTypeSize(CheckTy));
930*9880d681SAndroid Build Coastguard Worker 
931*9880d681SAndroid Build Coastguard Worker     // Complain about useful-looking but unsupported suffixes.
932*9880d681SAndroid Build Coastguard Worker     if (CheckTy == Check::CheckBadNot) {
933*9880d681SAndroid Build Coastguard Worker       SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
934*9880d681SAndroid Build Coastguard Worker                       SourceMgr::DK_Error,
935*9880d681SAndroid Build Coastguard Worker                       "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
936*9880d681SAndroid Build Coastguard Worker       return true;
937*9880d681SAndroid Build Coastguard Worker     }
938*9880d681SAndroid Build Coastguard Worker 
939*9880d681SAndroid Build Coastguard Worker     // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
940*9880d681SAndroid Build Coastguard Worker     // leading and trailing whitespace.
941*9880d681SAndroid Build Coastguard Worker     Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
942*9880d681SAndroid Build Coastguard Worker 
943*9880d681SAndroid Build Coastguard Worker     // Scan ahead to the end of line.
944*9880d681SAndroid Build Coastguard Worker     size_t EOL = Buffer.find_first_of("\n\r");
945*9880d681SAndroid Build Coastguard Worker 
946*9880d681SAndroid Build Coastguard Worker     // Remember the location of the start of the pattern, for diagnostics.
947*9880d681SAndroid Build Coastguard Worker     SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
948*9880d681SAndroid Build Coastguard Worker 
949*9880d681SAndroid Build Coastguard Worker     // Parse the pattern.
950*9880d681SAndroid Build Coastguard Worker     Pattern P(CheckTy);
951*9880d681SAndroid Build Coastguard Worker     if (P.ParsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, LineNumber))
952*9880d681SAndroid Build Coastguard Worker       return true;
953*9880d681SAndroid Build Coastguard Worker 
954*9880d681SAndroid Build Coastguard Worker     // Verify that CHECK-LABEL lines do not define or use variables
955*9880d681SAndroid Build Coastguard Worker     if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
956*9880d681SAndroid Build Coastguard Worker       SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
957*9880d681SAndroid Build Coastguard Worker                       SourceMgr::DK_Error,
958*9880d681SAndroid Build Coastguard Worker                       "found '" + UsedPrefix + "-LABEL:'"
959*9880d681SAndroid Build Coastguard Worker                       " with variable definition or use");
960*9880d681SAndroid Build Coastguard Worker       return true;
961*9880d681SAndroid Build Coastguard Worker     }
962*9880d681SAndroid Build Coastguard Worker 
963*9880d681SAndroid Build Coastguard Worker     Buffer = Buffer.substr(EOL);
964*9880d681SAndroid Build Coastguard Worker 
965*9880d681SAndroid Build Coastguard Worker     // Verify that CHECK-NEXT lines have at least one CHECK line before them.
966*9880d681SAndroid Build Coastguard Worker     if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame) &&
967*9880d681SAndroid Build Coastguard Worker         CheckStrings.empty()) {
968*9880d681SAndroid Build Coastguard Worker       StringRef Type = CheckTy == Check::CheckNext ? "NEXT" : "SAME";
969*9880d681SAndroid Build Coastguard Worker       SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
970*9880d681SAndroid Build Coastguard Worker                       SourceMgr::DK_Error,
971*9880d681SAndroid Build Coastguard Worker                       "found '" + UsedPrefix + "-" + Type + "' without previous '"
972*9880d681SAndroid Build Coastguard Worker                       + UsedPrefix + ": line");
973*9880d681SAndroid Build Coastguard Worker       return true;
974*9880d681SAndroid Build Coastguard Worker     }
975*9880d681SAndroid Build Coastguard Worker 
976*9880d681SAndroid Build Coastguard Worker     // Handle CHECK-DAG/-NOT.
977*9880d681SAndroid Build Coastguard Worker     if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
978*9880d681SAndroid Build Coastguard Worker       DagNotMatches.push_back(P);
979*9880d681SAndroid Build Coastguard Worker       continue;
980*9880d681SAndroid Build Coastguard Worker     }
981*9880d681SAndroid Build Coastguard Worker 
982*9880d681SAndroid Build Coastguard Worker     // Okay, add the string we captured to the output vector and move on.
983*9880d681SAndroid Build Coastguard Worker     CheckStrings.emplace_back(P, UsedPrefix, PatternLoc);
984*9880d681SAndroid Build Coastguard Worker     std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
985*9880d681SAndroid Build Coastguard Worker     DagNotMatches = ImplicitNegativeChecks;
986*9880d681SAndroid Build Coastguard Worker   }
987*9880d681SAndroid Build Coastguard Worker 
988*9880d681SAndroid Build Coastguard Worker   // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first
989*9880d681SAndroid Build Coastguard Worker   // prefix as a filler for the error message.
990*9880d681SAndroid Build Coastguard Worker   if (!DagNotMatches.empty()) {
991*9880d681SAndroid Build Coastguard Worker     CheckStrings.emplace_back(Pattern(Check::CheckEOF), *CheckPrefixes.begin(),
992*9880d681SAndroid Build Coastguard Worker                               SMLoc::getFromPointer(Buffer.data()));
993*9880d681SAndroid Build Coastguard Worker     std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
994*9880d681SAndroid Build Coastguard Worker   }
995*9880d681SAndroid Build Coastguard Worker 
996*9880d681SAndroid Build Coastguard Worker   if (CheckStrings.empty()) {
997*9880d681SAndroid Build Coastguard Worker     errs() << "error: no check strings found with prefix"
998*9880d681SAndroid Build Coastguard Worker            << (CheckPrefixes.size() > 1 ? "es " : " ");
999*9880d681SAndroid Build Coastguard Worker     prefix_iterator I = CheckPrefixes.begin();
1000*9880d681SAndroid Build Coastguard Worker     prefix_iterator E = CheckPrefixes.end();
1001*9880d681SAndroid Build Coastguard Worker     if (I != E) {
1002*9880d681SAndroid Build Coastguard Worker       errs() << "\'" << *I << ":'";
1003*9880d681SAndroid Build Coastguard Worker       ++I;
1004*9880d681SAndroid Build Coastguard Worker     }
1005*9880d681SAndroid Build Coastguard Worker     for (; I != E; ++I)
1006*9880d681SAndroid Build Coastguard Worker       errs() << ", \'" << *I << ":'";
1007*9880d681SAndroid Build Coastguard Worker 
1008*9880d681SAndroid Build Coastguard Worker     errs() << '\n';
1009*9880d681SAndroid Build Coastguard Worker     return true;
1010*9880d681SAndroid Build Coastguard Worker   }
1011*9880d681SAndroid Build Coastguard Worker 
1012*9880d681SAndroid Build Coastguard Worker   return false;
1013*9880d681SAndroid Build Coastguard Worker }
1014*9880d681SAndroid Build Coastguard Worker 
PrintCheckFailed(const SourceMgr & SM,SMLoc Loc,const Pattern & Pat,StringRef Buffer,StringMap<StringRef> & VariableTable)1015*9880d681SAndroid Build Coastguard Worker static void PrintCheckFailed(const SourceMgr &SM, SMLoc Loc,
1016*9880d681SAndroid Build Coastguard Worker                              const Pattern &Pat, StringRef Buffer,
1017*9880d681SAndroid Build Coastguard Worker                              StringMap<StringRef> &VariableTable) {
1018*9880d681SAndroid Build Coastguard Worker   // Otherwise, we have an error, emit an error message.
1019*9880d681SAndroid Build Coastguard Worker   SM.PrintMessage(Loc, SourceMgr::DK_Error,
1020*9880d681SAndroid Build Coastguard Worker                   "expected string not found in input");
1021*9880d681SAndroid Build Coastguard Worker 
1022*9880d681SAndroid Build Coastguard Worker   // Print the "scanning from here" line.  If the current position is at the
1023*9880d681SAndroid Build Coastguard Worker   // end of a line, advance to the start of the next line.
1024*9880d681SAndroid Build Coastguard Worker   Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
1025*9880d681SAndroid Build Coastguard Worker 
1026*9880d681SAndroid Build Coastguard Worker   SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1027*9880d681SAndroid Build Coastguard Worker                   "scanning from here");
1028*9880d681SAndroid Build Coastguard Worker 
1029*9880d681SAndroid Build Coastguard Worker   // Allow the pattern to print additional information if desired.
1030*9880d681SAndroid Build Coastguard Worker   Pat.PrintFailureInfo(SM, Buffer, VariableTable);
1031*9880d681SAndroid Build Coastguard Worker }
1032*9880d681SAndroid Build Coastguard Worker 
PrintCheckFailed(const SourceMgr & SM,const CheckString & CheckStr,StringRef Buffer,StringMap<StringRef> & VariableTable)1033*9880d681SAndroid Build Coastguard Worker static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
1034*9880d681SAndroid Build Coastguard Worker                              StringRef Buffer,
1035*9880d681SAndroid Build Coastguard Worker                              StringMap<StringRef> &VariableTable) {
1036*9880d681SAndroid Build Coastguard Worker   PrintCheckFailed(SM, CheckStr.Loc, CheckStr.Pat, Buffer, VariableTable);
1037*9880d681SAndroid Build Coastguard Worker }
1038*9880d681SAndroid Build Coastguard Worker 
1039*9880d681SAndroid Build Coastguard Worker /// CountNumNewlinesBetween - Count the number of newlines in the specified
1040*9880d681SAndroid Build Coastguard Worker /// range.
CountNumNewlinesBetween(StringRef Range,const char * & FirstNewLine)1041*9880d681SAndroid Build Coastguard Worker static unsigned CountNumNewlinesBetween(StringRef Range,
1042*9880d681SAndroid Build Coastguard Worker                                         const char *&FirstNewLine) {
1043*9880d681SAndroid Build Coastguard Worker   unsigned NumNewLines = 0;
1044*9880d681SAndroid Build Coastguard Worker   while (1) {
1045*9880d681SAndroid Build Coastguard Worker     // Scan for newline.
1046*9880d681SAndroid Build Coastguard Worker     Range = Range.substr(Range.find_first_of("\n\r"));
1047*9880d681SAndroid Build Coastguard Worker     if (Range.empty()) return NumNewLines;
1048*9880d681SAndroid Build Coastguard Worker 
1049*9880d681SAndroid Build Coastguard Worker     ++NumNewLines;
1050*9880d681SAndroid Build Coastguard Worker 
1051*9880d681SAndroid Build Coastguard Worker     // Handle \n\r and \r\n as a single newline.
1052*9880d681SAndroid Build Coastguard Worker     if (Range.size() > 1 &&
1053*9880d681SAndroid Build Coastguard Worker         (Range[1] == '\n' || Range[1] == '\r') &&
1054*9880d681SAndroid Build Coastguard Worker         (Range[0] != Range[1]))
1055*9880d681SAndroid Build Coastguard Worker       Range = Range.substr(1);
1056*9880d681SAndroid Build Coastguard Worker     Range = Range.substr(1);
1057*9880d681SAndroid Build Coastguard Worker 
1058*9880d681SAndroid Build Coastguard Worker     if (NumNewLines == 1)
1059*9880d681SAndroid Build Coastguard Worker       FirstNewLine = Range.begin();
1060*9880d681SAndroid Build Coastguard Worker   }
1061*9880d681SAndroid Build Coastguard Worker }
1062*9880d681SAndroid Build Coastguard Worker 
Check(const SourceMgr & SM,StringRef Buffer,bool IsLabelScanMode,size_t & MatchLen,StringMap<StringRef> & VariableTable) const1063*9880d681SAndroid Build Coastguard Worker size_t CheckString::Check(const SourceMgr &SM, StringRef Buffer,
1064*9880d681SAndroid Build Coastguard Worker                           bool IsLabelScanMode, size_t &MatchLen,
1065*9880d681SAndroid Build Coastguard Worker                           StringMap<StringRef> &VariableTable) const {
1066*9880d681SAndroid Build Coastguard Worker   size_t LastPos = 0;
1067*9880d681SAndroid Build Coastguard Worker   std::vector<const Pattern *> NotStrings;
1068*9880d681SAndroid Build Coastguard Worker 
1069*9880d681SAndroid Build Coastguard Worker   // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
1070*9880d681SAndroid Build Coastguard Worker   // bounds; we have not processed variable definitions within the bounded block
1071*9880d681SAndroid Build Coastguard Worker   // yet so cannot handle any final CHECK-DAG yet; this is handled when going
1072*9880d681SAndroid Build Coastguard Worker   // over the block again (including the last CHECK-LABEL) in normal mode.
1073*9880d681SAndroid Build Coastguard Worker   if (!IsLabelScanMode) {
1074*9880d681SAndroid Build Coastguard Worker     // Match "dag strings" (with mixed "not strings" if any).
1075*9880d681SAndroid Build Coastguard Worker     LastPos = CheckDag(SM, Buffer, NotStrings, VariableTable);
1076*9880d681SAndroid Build Coastguard Worker     if (LastPos == StringRef::npos)
1077*9880d681SAndroid Build Coastguard Worker       return StringRef::npos;
1078*9880d681SAndroid Build Coastguard Worker   }
1079*9880d681SAndroid Build Coastguard Worker 
1080*9880d681SAndroid Build Coastguard Worker   // Match itself from the last position after matching CHECK-DAG.
1081*9880d681SAndroid Build Coastguard Worker   StringRef MatchBuffer = Buffer.substr(LastPos);
1082*9880d681SAndroid Build Coastguard Worker   size_t MatchPos = Pat.Match(MatchBuffer, MatchLen, VariableTable);
1083*9880d681SAndroid Build Coastguard Worker   if (MatchPos == StringRef::npos) {
1084*9880d681SAndroid Build Coastguard Worker     PrintCheckFailed(SM, *this, MatchBuffer, VariableTable);
1085*9880d681SAndroid Build Coastguard Worker     return StringRef::npos;
1086*9880d681SAndroid Build Coastguard Worker   }
1087*9880d681SAndroid Build Coastguard Worker 
1088*9880d681SAndroid Build Coastguard Worker   // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
1089*9880d681SAndroid Build Coastguard Worker   // or CHECK-NOT
1090*9880d681SAndroid Build Coastguard Worker   if (!IsLabelScanMode) {
1091*9880d681SAndroid Build Coastguard Worker     StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
1092*9880d681SAndroid Build Coastguard Worker 
1093*9880d681SAndroid Build Coastguard Worker     // If this check is a "CHECK-NEXT", verify that the previous match was on
1094*9880d681SAndroid Build Coastguard Worker     // the previous line (i.e. that there is one newline between them).
1095*9880d681SAndroid Build Coastguard Worker     if (CheckNext(SM, SkippedRegion))
1096*9880d681SAndroid Build Coastguard Worker       return StringRef::npos;
1097*9880d681SAndroid Build Coastguard Worker 
1098*9880d681SAndroid Build Coastguard Worker     // If this check is a "CHECK-SAME", verify that the previous match was on
1099*9880d681SAndroid Build Coastguard Worker     // the same line (i.e. that there is no newline between them).
1100*9880d681SAndroid Build Coastguard Worker     if (CheckSame(SM, SkippedRegion))
1101*9880d681SAndroid Build Coastguard Worker       return StringRef::npos;
1102*9880d681SAndroid Build Coastguard Worker 
1103*9880d681SAndroid Build Coastguard Worker     // If this match had "not strings", verify that they don't exist in the
1104*9880d681SAndroid Build Coastguard Worker     // skipped region.
1105*9880d681SAndroid Build Coastguard Worker     if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable))
1106*9880d681SAndroid Build Coastguard Worker       return StringRef::npos;
1107*9880d681SAndroid Build Coastguard Worker   }
1108*9880d681SAndroid Build Coastguard Worker 
1109*9880d681SAndroid Build Coastguard Worker   return LastPos + MatchPos;
1110*9880d681SAndroid Build Coastguard Worker }
1111*9880d681SAndroid Build Coastguard Worker 
CheckNext(const SourceMgr & SM,StringRef Buffer) const1112*9880d681SAndroid Build Coastguard Worker bool CheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
1113*9880d681SAndroid Build Coastguard Worker   if (Pat.getCheckTy() != Check::CheckNext)
1114*9880d681SAndroid Build Coastguard Worker     return false;
1115*9880d681SAndroid Build Coastguard Worker 
1116*9880d681SAndroid Build Coastguard Worker   // Count the number of newlines between the previous match and this one.
1117*9880d681SAndroid Build Coastguard Worker   assert(Buffer.data() !=
1118*9880d681SAndroid Build Coastguard Worker          SM.getMemoryBuffer(
1119*9880d681SAndroid Build Coastguard Worker            SM.FindBufferContainingLoc(
1120*9880d681SAndroid Build Coastguard Worker              SMLoc::getFromPointer(Buffer.data())))->getBufferStart() &&
1121*9880d681SAndroid Build Coastguard Worker          "CHECK-NEXT can't be the first check in a file");
1122*9880d681SAndroid Build Coastguard Worker 
1123*9880d681SAndroid Build Coastguard Worker   const char *FirstNewLine = nullptr;
1124*9880d681SAndroid Build Coastguard Worker   unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1125*9880d681SAndroid Build Coastguard Worker 
1126*9880d681SAndroid Build Coastguard Worker   if (NumNewLines == 0) {
1127*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(Loc, SourceMgr::DK_Error, Prefix +
1128*9880d681SAndroid Build Coastguard Worker                     "-NEXT: is on the same line as previous match");
1129*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()),
1130*9880d681SAndroid Build Coastguard Worker                     SourceMgr::DK_Note, "'next' match was here");
1131*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1132*9880d681SAndroid Build Coastguard Worker                     "previous match ended here");
1133*9880d681SAndroid Build Coastguard Worker     return true;
1134*9880d681SAndroid Build Coastguard Worker   }
1135*9880d681SAndroid Build Coastguard Worker 
1136*9880d681SAndroid Build Coastguard Worker   if (NumNewLines != 1) {
1137*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(Loc, SourceMgr::DK_Error, Prefix +
1138*9880d681SAndroid Build Coastguard Worker                     "-NEXT: is not on the line after the previous match");
1139*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()),
1140*9880d681SAndroid Build Coastguard Worker                     SourceMgr::DK_Note, "'next' match was here");
1141*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1142*9880d681SAndroid Build Coastguard Worker                     "previous match ended here");
1143*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note,
1144*9880d681SAndroid Build Coastguard Worker                     "non-matching line after previous match is here");
1145*9880d681SAndroid Build Coastguard Worker     return true;
1146*9880d681SAndroid Build Coastguard Worker   }
1147*9880d681SAndroid Build Coastguard Worker 
1148*9880d681SAndroid Build Coastguard Worker   return false;
1149*9880d681SAndroid Build Coastguard Worker }
1150*9880d681SAndroid Build Coastguard Worker 
CheckSame(const SourceMgr & SM,StringRef Buffer) const1151*9880d681SAndroid Build Coastguard Worker bool CheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
1152*9880d681SAndroid Build Coastguard Worker   if (Pat.getCheckTy() != Check::CheckSame)
1153*9880d681SAndroid Build Coastguard Worker     return false;
1154*9880d681SAndroid Build Coastguard Worker 
1155*9880d681SAndroid Build Coastguard Worker   // Count the number of newlines between the previous match and this one.
1156*9880d681SAndroid Build Coastguard Worker   assert(Buffer.data() !=
1157*9880d681SAndroid Build Coastguard Worker              SM.getMemoryBuffer(SM.FindBufferContainingLoc(
1158*9880d681SAndroid Build Coastguard Worker                                     SMLoc::getFromPointer(Buffer.data())))
1159*9880d681SAndroid Build Coastguard Worker                  ->getBufferStart() &&
1160*9880d681SAndroid Build Coastguard Worker          "CHECK-SAME can't be the first check in a file");
1161*9880d681SAndroid Build Coastguard Worker 
1162*9880d681SAndroid Build Coastguard Worker   const char *FirstNewLine = nullptr;
1163*9880d681SAndroid Build Coastguard Worker   unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1164*9880d681SAndroid Build Coastguard Worker 
1165*9880d681SAndroid Build Coastguard Worker   if (NumNewLines != 0) {
1166*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(Loc, SourceMgr::DK_Error,
1167*9880d681SAndroid Build Coastguard Worker                     Prefix +
1168*9880d681SAndroid Build Coastguard Worker                         "-SAME: is not on the same line as the previous match");
1169*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
1170*9880d681SAndroid Build Coastguard Worker                     "'next' match was here");
1171*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
1172*9880d681SAndroid Build Coastguard Worker                     "previous match ended here");
1173*9880d681SAndroid Build Coastguard Worker     return true;
1174*9880d681SAndroid Build Coastguard Worker   }
1175*9880d681SAndroid Build Coastguard Worker 
1176*9880d681SAndroid Build Coastguard Worker   return false;
1177*9880d681SAndroid Build Coastguard Worker }
1178*9880d681SAndroid Build Coastguard Worker 
CheckNot(const SourceMgr & SM,StringRef Buffer,const std::vector<const Pattern * > & NotStrings,StringMap<StringRef> & VariableTable) const1179*9880d681SAndroid Build Coastguard Worker bool CheckString::CheckNot(const SourceMgr &SM, StringRef Buffer,
1180*9880d681SAndroid Build Coastguard Worker                            const std::vector<const Pattern *> &NotStrings,
1181*9880d681SAndroid Build Coastguard Worker                            StringMap<StringRef> &VariableTable) const {
1182*9880d681SAndroid Build Coastguard Worker   for (const Pattern *Pat : NotStrings) {
1183*9880d681SAndroid Build Coastguard Worker     assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
1184*9880d681SAndroid Build Coastguard Worker 
1185*9880d681SAndroid Build Coastguard Worker     size_t MatchLen = 0;
1186*9880d681SAndroid Build Coastguard Worker     size_t Pos = Pat->Match(Buffer, MatchLen, VariableTable);
1187*9880d681SAndroid Build Coastguard Worker 
1188*9880d681SAndroid Build Coastguard Worker     if (Pos == StringRef::npos) continue;
1189*9880d681SAndroid Build Coastguard Worker 
1190*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()+Pos),
1191*9880d681SAndroid Build Coastguard Worker                     SourceMgr::DK_Error,
1192*9880d681SAndroid Build Coastguard Worker                     Prefix + "-NOT: string occurred!");
1193*9880d681SAndroid Build Coastguard Worker     SM.PrintMessage(Pat->getLoc(), SourceMgr::DK_Note,
1194*9880d681SAndroid Build Coastguard Worker                     Prefix + "-NOT: pattern specified here");
1195*9880d681SAndroid Build Coastguard Worker     return true;
1196*9880d681SAndroid Build Coastguard Worker   }
1197*9880d681SAndroid Build Coastguard Worker 
1198*9880d681SAndroid Build Coastguard Worker   return false;
1199*9880d681SAndroid Build Coastguard Worker }
1200*9880d681SAndroid Build Coastguard Worker 
CheckDag(const SourceMgr & SM,StringRef Buffer,std::vector<const Pattern * > & NotStrings,StringMap<StringRef> & VariableTable) const1201*9880d681SAndroid Build Coastguard Worker size_t CheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
1202*9880d681SAndroid Build Coastguard Worker                              std::vector<const Pattern *> &NotStrings,
1203*9880d681SAndroid Build Coastguard Worker                              StringMap<StringRef> &VariableTable) const {
1204*9880d681SAndroid Build Coastguard Worker   if (DagNotStrings.empty())
1205*9880d681SAndroid Build Coastguard Worker     return 0;
1206*9880d681SAndroid Build Coastguard Worker 
1207*9880d681SAndroid Build Coastguard Worker   size_t LastPos = 0;
1208*9880d681SAndroid Build Coastguard Worker   size_t StartPos = LastPos;
1209*9880d681SAndroid Build Coastguard Worker 
1210*9880d681SAndroid Build Coastguard Worker   for (const Pattern &Pat : DagNotStrings) {
1211*9880d681SAndroid Build Coastguard Worker     assert((Pat.getCheckTy() == Check::CheckDAG ||
1212*9880d681SAndroid Build Coastguard Worker             Pat.getCheckTy() == Check::CheckNot) &&
1213*9880d681SAndroid Build Coastguard Worker            "Invalid CHECK-DAG or CHECK-NOT!");
1214*9880d681SAndroid Build Coastguard Worker 
1215*9880d681SAndroid Build Coastguard Worker     if (Pat.getCheckTy() == Check::CheckNot) {
1216*9880d681SAndroid Build Coastguard Worker       NotStrings.push_back(&Pat);
1217*9880d681SAndroid Build Coastguard Worker       continue;
1218*9880d681SAndroid Build Coastguard Worker     }
1219*9880d681SAndroid Build Coastguard Worker 
1220*9880d681SAndroid Build Coastguard Worker     assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
1221*9880d681SAndroid Build Coastguard Worker 
1222*9880d681SAndroid Build Coastguard Worker     size_t MatchLen = 0, MatchPos;
1223*9880d681SAndroid Build Coastguard Worker 
1224*9880d681SAndroid Build Coastguard Worker     // CHECK-DAG always matches from the start.
1225*9880d681SAndroid Build Coastguard Worker     StringRef MatchBuffer = Buffer.substr(StartPos);
1226*9880d681SAndroid Build Coastguard Worker     MatchPos = Pat.Match(MatchBuffer, MatchLen, VariableTable);
1227*9880d681SAndroid Build Coastguard Worker     // With a group of CHECK-DAGs, a single mismatching means the match on
1228*9880d681SAndroid Build Coastguard Worker     // that group of CHECK-DAGs fails immediately.
1229*9880d681SAndroid Build Coastguard Worker     if (MatchPos == StringRef::npos) {
1230*9880d681SAndroid Build Coastguard Worker       PrintCheckFailed(SM, Pat.getLoc(), Pat, MatchBuffer, VariableTable);
1231*9880d681SAndroid Build Coastguard Worker       return StringRef::npos;
1232*9880d681SAndroid Build Coastguard Worker     }
1233*9880d681SAndroid Build Coastguard Worker     // Re-calc it as the offset relative to the start of the original string.
1234*9880d681SAndroid Build Coastguard Worker     MatchPos += StartPos;
1235*9880d681SAndroid Build Coastguard Worker 
1236*9880d681SAndroid Build Coastguard Worker     if (!NotStrings.empty()) {
1237*9880d681SAndroid Build Coastguard Worker       if (MatchPos < LastPos) {
1238*9880d681SAndroid Build Coastguard Worker         // Reordered?
1239*9880d681SAndroid Build Coastguard Worker         SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + MatchPos),
1240*9880d681SAndroid Build Coastguard Worker                         SourceMgr::DK_Error,
1241*9880d681SAndroid Build Coastguard Worker                         Prefix + "-DAG: found a match of CHECK-DAG"
1242*9880d681SAndroid Build Coastguard Worker                         " reordering across a CHECK-NOT");
1243*9880d681SAndroid Build Coastguard Worker         SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + LastPos),
1244*9880d681SAndroid Build Coastguard Worker                         SourceMgr::DK_Note,
1245*9880d681SAndroid Build Coastguard Worker                         Prefix + "-DAG: the farthest match of CHECK-DAG"
1246*9880d681SAndroid Build Coastguard Worker                         " is found here");
1247*9880d681SAndroid Build Coastguard Worker         SM.PrintMessage(NotStrings[0]->getLoc(), SourceMgr::DK_Note,
1248*9880d681SAndroid Build Coastguard Worker                         Prefix + "-NOT: the crossed pattern specified"
1249*9880d681SAndroid Build Coastguard Worker                         " here");
1250*9880d681SAndroid Build Coastguard Worker         SM.PrintMessage(Pat.getLoc(), SourceMgr::DK_Note,
1251*9880d681SAndroid Build Coastguard Worker                         Prefix + "-DAG: the reordered pattern specified"
1252*9880d681SAndroid Build Coastguard Worker                         " here");
1253*9880d681SAndroid Build Coastguard Worker         return StringRef::npos;
1254*9880d681SAndroid Build Coastguard Worker       }
1255*9880d681SAndroid Build Coastguard Worker       // All subsequent CHECK-DAGs should be matched from the farthest
1256*9880d681SAndroid Build Coastguard Worker       // position of all precedent CHECK-DAGs (including this one.)
1257*9880d681SAndroid Build Coastguard Worker       StartPos = LastPos;
1258*9880d681SAndroid Build Coastguard Worker       // If there's CHECK-NOTs between two CHECK-DAGs or from CHECK to
1259*9880d681SAndroid Build Coastguard Worker       // CHECK-DAG, verify that there's no 'not' strings occurred in that
1260*9880d681SAndroid Build Coastguard Worker       // region.
1261*9880d681SAndroid Build Coastguard Worker       StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
1262*9880d681SAndroid Build Coastguard Worker       if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable))
1263*9880d681SAndroid Build Coastguard Worker         return StringRef::npos;
1264*9880d681SAndroid Build Coastguard Worker       // Clear "not strings".
1265*9880d681SAndroid Build Coastguard Worker       NotStrings.clear();
1266*9880d681SAndroid Build Coastguard Worker     }
1267*9880d681SAndroid Build Coastguard Worker 
1268*9880d681SAndroid Build Coastguard Worker     // Update the last position with CHECK-DAG matches.
1269*9880d681SAndroid Build Coastguard Worker     LastPos = std::max(MatchPos + MatchLen, LastPos);
1270*9880d681SAndroid Build Coastguard Worker   }
1271*9880d681SAndroid Build Coastguard Worker 
1272*9880d681SAndroid Build Coastguard Worker   return LastPos;
1273*9880d681SAndroid Build Coastguard Worker }
1274*9880d681SAndroid Build Coastguard Worker 
1275*9880d681SAndroid Build Coastguard Worker // A check prefix must contain only alphanumeric, hyphens and underscores.
ValidateCheckPrefix(StringRef CheckPrefix)1276*9880d681SAndroid Build Coastguard Worker static bool ValidateCheckPrefix(StringRef CheckPrefix) {
1277*9880d681SAndroid Build Coastguard Worker   Regex Validator("^[a-zA-Z0-9_-]*$");
1278*9880d681SAndroid Build Coastguard Worker   return Validator.match(CheckPrefix);
1279*9880d681SAndroid Build Coastguard Worker }
1280*9880d681SAndroid Build Coastguard Worker 
ValidateCheckPrefixes()1281*9880d681SAndroid Build Coastguard Worker static bool ValidateCheckPrefixes() {
1282*9880d681SAndroid Build Coastguard Worker   StringSet<> PrefixSet;
1283*9880d681SAndroid Build Coastguard Worker 
1284*9880d681SAndroid Build Coastguard Worker   for (StringRef Prefix : CheckPrefixes) {
1285*9880d681SAndroid Build Coastguard Worker     // Reject empty prefixes.
1286*9880d681SAndroid Build Coastguard Worker     if (Prefix == "")
1287*9880d681SAndroid Build Coastguard Worker       return false;
1288*9880d681SAndroid Build Coastguard Worker 
1289*9880d681SAndroid Build Coastguard Worker     if (!PrefixSet.insert(Prefix).second)
1290*9880d681SAndroid Build Coastguard Worker       return false;
1291*9880d681SAndroid Build Coastguard Worker 
1292*9880d681SAndroid Build Coastguard Worker     if (!ValidateCheckPrefix(Prefix))
1293*9880d681SAndroid Build Coastguard Worker       return false;
1294*9880d681SAndroid Build Coastguard Worker   }
1295*9880d681SAndroid Build Coastguard Worker 
1296*9880d681SAndroid Build Coastguard Worker   return true;
1297*9880d681SAndroid Build Coastguard Worker }
1298*9880d681SAndroid Build Coastguard Worker 
1299*9880d681SAndroid Build Coastguard Worker // I don't think there's a way to specify an initial value for cl::list,
1300*9880d681SAndroid Build Coastguard Worker // so if nothing was specified, add the default
AddCheckPrefixIfNeeded()1301*9880d681SAndroid Build Coastguard Worker static void AddCheckPrefixIfNeeded() {
1302*9880d681SAndroid Build Coastguard Worker   if (CheckPrefixes.empty())
1303*9880d681SAndroid Build Coastguard Worker     CheckPrefixes.push_back("CHECK");
1304*9880d681SAndroid Build Coastguard Worker }
1305*9880d681SAndroid Build Coastguard Worker 
DumpCommandLine(int argc,char ** argv)1306*9880d681SAndroid Build Coastguard Worker static void DumpCommandLine(int argc, char **argv) {
1307*9880d681SAndroid Build Coastguard Worker   errs() << "FileCheck command line: ";
1308*9880d681SAndroid Build Coastguard Worker   for (int I = 0; I < argc; I++)
1309*9880d681SAndroid Build Coastguard Worker     errs() << " " << argv[I];
1310*9880d681SAndroid Build Coastguard Worker   errs() << "\n";
1311*9880d681SAndroid Build Coastguard Worker }
1312*9880d681SAndroid Build Coastguard Worker 
main(int argc,char ** argv)1313*9880d681SAndroid Build Coastguard Worker int main(int argc, char **argv) {
1314*9880d681SAndroid Build Coastguard Worker   sys::PrintStackTraceOnErrorSignal(argv[0]);
1315*9880d681SAndroid Build Coastguard Worker   PrettyStackTraceProgram X(argc, argv);
1316*9880d681SAndroid Build Coastguard Worker   cl::ParseCommandLineOptions(argc, argv);
1317*9880d681SAndroid Build Coastguard Worker 
1318*9880d681SAndroid Build Coastguard Worker   if (!ValidateCheckPrefixes()) {
1319*9880d681SAndroid Build Coastguard Worker     errs() << "Supplied check-prefix is invalid! Prefixes must be unique and "
1320*9880d681SAndroid Build Coastguard Worker               "start with a letter and contain only alphanumeric characters, "
1321*9880d681SAndroid Build Coastguard Worker               "hyphens and underscores\n";
1322*9880d681SAndroid Build Coastguard Worker     return 2;
1323*9880d681SAndroid Build Coastguard Worker   }
1324*9880d681SAndroid Build Coastguard Worker 
1325*9880d681SAndroid Build Coastguard Worker   AddCheckPrefixIfNeeded();
1326*9880d681SAndroid Build Coastguard Worker 
1327*9880d681SAndroid Build Coastguard Worker   SourceMgr SM;
1328*9880d681SAndroid Build Coastguard Worker 
1329*9880d681SAndroid Build Coastguard Worker   // Read the expected strings from the check file.
1330*9880d681SAndroid Build Coastguard Worker   std::vector<CheckString> CheckStrings;
1331*9880d681SAndroid Build Coastguard Worker   if (ReadCheckFile(SM, CheckStrings))
1332*9880d681SAndroid Build Coastguard Worker     return 2;
1333*9880d681SAndroid Build Coastguard Worker 
1334*9880d681SAndroid Build Coastguard Worker   // Open the file to check and add it to SourceMgr.
1335*9880d681SAndroid Build Coastguard Worker   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
1336*9880d681SAndroid Build Coastguard Worker       MemoryBuffer::getFileOrSTDIN(InputFilename);
1337*9880d681SAndroid Build Coastguard Worker   if (std::error_code EC = FileOrErr.getError()) {
1338*9880d681SAndroid Build Coastguard Worker     errs() << "Could not open input file '" << InputFilename
1339*9880d681SAndroid Build Coastguard Worker            << "': " << EC.message() << '\n';
1340*9880d681SAndroid Build Coastguard Worker     return 2;
1341*9880d681SAndroid Build Coastguard Worker   }
1342*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<MemoryBuffer> &File = FileOrErr.get();
1343*9880d681SAndroid Build Coastguard Worker 
1344*9880d681SAndroid Build Coastguard Worker   if (File->getBufferSize() == 0 && !AllowEmptyInput) {
1345*9880d681SAndroid Build Coastguard Worker     errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
1346*9880d681SAndroid Build Coastguard Worker     DumpCommandLine(argc, argv);
1347*9880d681SAndroid Build Coastguard Worker     return 2;
1348*9880d681SAndroid Build Coastguard Worker   }
1349*9880d681SAndroid Build Coastguard Worker 
1350*9880d681SAndroid Build Coastguard Worker   // Remove duplicate spaces in the input file if requested.
1351*9880d681SAndroid Build Coastguard Worker   // Remove DOS style line endings.
1352*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<MemoryBuffer> F =
1353*9880d681SAndroid Build Coastguard Worker       CanonicalizeInputFile(std::move(File), NoCanonicalizeWhiteSpace);
1354*9880d681SAndroid Build Coastguard Worker 
1355*9880d681SAndroid Build Coastguard Worker   // Check that we have all of the expected strings, in order, in the input
1356*9880d681SAndroid Build Coastguard Worker   // file.
1357*9880d681SAndroid Build Coastguard Worker   StringRef Buffer = F->getBuffer();
1358*9880d681SAndroid Build Coastguard Worker 
1359*9880d681SAndroid Build Coastguard Worker   SM.AddNewSourceBuffer(std::move(F), SMLoc());
1360*9880d681SAndroid Build Coastguard Worker 
1361*9880d681SAndroid Build Coastguard Worker   /// VariableTable - This holds all the current filecheck variables.
1362*9880d681SAndroid Build Coastguard Worker   StringMap<StringRef> VariableTable;
1363*9880d681SAndroid Build Coastguard Worker 
1364*9880d681SAndroid Build Coastguard Worker   bool hasError = false;
1365*9880d681SAndroid Build Coastguard Worker 
1366*9880d681SAndroid Build Coastguard Worker   unsigned i = 0, j = 0, e = CheckStrings.size();
1367*9880d681SAndroid Build Coastguard Worker 
1368*9880d681SAndroid Build Coastguard Worker   while (true) {
1369*9880d681SAndroid Build Coastguard Worker     StringRef CheckRegion;
1370*9880d681SAndroid Build Coastguard Worker     if (j == e) {
1371*9880d681SAndroid Build Coastguard Worker       CheckRegion = Buffer;
1372*9880d681SAndroid Build Coastguard Worker     } else {
1373*9880d681SAndroid Build Coastguard Worker       const CheckString &CheckLabelStr = CheckStrings[j];
1374*9880d681SAndroid Build Coastguard Worker       if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
1375*9880d681SAndroid Build Coastguard Worker         ++j;
1376*9880d681SAndroid Build Coastguard Worker         continue;
1377*9880d681SAndroid Build Coastguard Worker       }
1378*9880d681SAndroid Build Coastguard Worker 
1379*9880d681SAndroid Build Coastguard Worker       // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
1380*9880d681SAndroid Build Coastguard Worker       size_t MatchLabelLen = 0;
1381*9880d681SAndroid Build Coastguard Worker       size_t MatchLabelPos = CheckLabelStr.Check(SM, Buffer, true,
1382*9880d681SAndroid Build Coastguard Worker                                                  MatchLabelLen, VariableTable);
1383*9880d681SAndroid Build Coastguard Worker       if (MatchLabelPos == StringRef::npos) {
1384*9880d681SAndroid Build Coastguard Worker         hasError = true;
1385*9880d681SAndroid Build Coastguard Worker         break;
1386*9880d681SAndroid Build Coastguard Worker       }
1387*9880d681SAndroid Build Coastguard Worker 
1388*9880d681SAndroid Build Coastguard Worker       CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
1389*9880d681SAndroid Build Coastguard Worker       Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
1390*9880d681SAndroid Build Coastguard Worker       ++j;
1391*9880d681SAndroid Build Coastguard Worker     }
1392*9880d681SAndroid Build Coastguard Worker 
1393*9880d681SAndroid Build Coastguard Worker     for ( ; i != j; ++i) {
1394*9880d681SAndroid Build Coastguard Worker       const CheckString &CheckStr = CheckStrings[i];
1395*9880d681SAndroid Build Coastguard Worker 
1396*9880d681SAndroid Build Coastguard Worker       // Check each string within the scanned region, including a second check
1397*9880d681SAndroid Build Coastguard Worker       // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
1398*9880d681SAndroid Build Coastguard Worker       size_t MatchLen = 0;
1399*9880d681SAndroid Build Coastguard Worker       size_t MatchPos = CheckStr.Check(SM, CheckRegion, false, MatchLen,
1400*9880d681SAndroid Build Coastguard Worker                                        VariableTable);
1401*9880d681SAndroid Build Coastguard Worker 
1402*9880d681SAndroid Build Coastguard Worker       if (MatchPos == StringRef::npos) {
1403*9880d681SAndroid Build Coastguard Worker         hasError = true;
1404*9880d681SAndroid Build Coastguard Worker         i = j;
1405*9880d681SAndroid Build Coastguard Worker         break;
1406*9880d681SAndroid Build Coastguard Worker       }
1407*9880d681SAndroid Build Coastguard Worker 
1408*9880d681SAndroid Build Coastguard Worker       CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
1409*9880d681SAndroid Build Coastguard Worker     }
1410*9880d681SAndroid Build Coastguard Worker 
1411*9880d681SAndroid Build Coastguard Worker     if (j == e)
1412*9880d681SAndroid Build Coastguard Worker       break;
1413*9880d681SAndroid Build Coastguard Worker   }
1414*9880d681SAndroid Build Coastguard Worker 
1415*9880d681SAndroid Build Coastguard Worker   return hasError ? 1 : 0;
1416*9880d681SAndroid Build Coastguard Worker }
1417