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