Chris Lattner | 81cb8ca | 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 | |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Regex.h" |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SourceMgr.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/System/Signals.h" |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringMap.h" |
| 28 | #include <algorithm> |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | static cl::opt<std::string> |
| 32 | CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required); |
| 33 | |
| 34 | static cl::opt<std::string> |
| 35 | InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), |
| 36 | cl::init("-"), cl::value_desc("filename")); |
| 37 | |
| 38 | static cl::opt<std::string> |
| 39 | CheckPrefix("check-prefix", cl::init("CHECK"), |
| 40 | cl::desc("Prefix to use from check file (defaults to 'CHECK')")); |
| 41 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 42 | static cl::opt<bool> |
| 43 | NoCanonicalizeWhiteSpace("strict-whitespace", |
| 44 | cl::desc("Do not treat all horizontal whitespace as equivalent")); |
| 45 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // Pattern Handling Code. |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 50 | class Pattern { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 51 | SMLoc PatternLoc; |
| 52 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 53 | /// FixedStr - If non-empty, this pattern is a fixed string match with the |
| 54 | /// specified fixed string. |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 55 | StringRef FixedStr; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 56 | |
| 57 | /// RegEx - If non-empty, this is a regex pattern. |
| 58 | std::string RegExStr; |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 59 | |
| 60 | /// VariableUses - Entries in this vector map to uses of a variable in the |
| 61 | /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain |
| 62 | /// "foobaz" and we'll get an entry in this vector that tells us to insert the |
| 63 | /// value of bar at offset 3. |
| 64 | std::vector<std::pair<StringRef, unsigned> > VariableUses; |
| 65 | |
| 66 | /// VariableDefs - Entries in this vector map to definitions of a variable in |
| 67 | /// the pattern, e.g. "foo[[bar:.*]]baz". In this case, the RegExStr will |
| 68 | /// contain "foo(.*)baz" and VariableDefs will contain the pair "bar",1. The |
| 69 | /// index indicates what parenthesized value captures the variable value. |
| 70 | std::vector<std::pair<StringRef, unsigned> > VariableDefs; |
| 71 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 72 | public: |
| 73 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 74 | Pattern() { } |
| 75 | |
| 76 | bool ParsePattern(StringRef PatternStr, SourceMgr &SM); |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 77 | |
| 78 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 79 | /// returns the position that is matched or npos if there is no match. If |
| 80 | /// there is a match, the size of the matched string is returned in MatchLen. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 81 | /// |
| 82 | /// The VariableTable StringMap provides the current values of filecheck |
| 83 | /// variables and is updated if this match defines new values. |
| 84 | size_t Match(StringRef Buffer, size_t &MatchLen, |
| 85 | StringMap<StringRef> &VariableTable) const; |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 86 | |
| 87 | /// PrintFailureInfo - Print additional information about a failure to match |
| 88 | /// involving this pattern. |
| 89 | void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer, |
| 90 | const StringMap<StringRef> &VariableTable) const; |
| 91 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 92 | private: |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 93 | static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr); |
| 94 | bool AddRegExToRegEx(StringRef RegExStr, unsigned &CurParen, SourceMgr &SM); |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 95 | |
| 96 | /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of |
| 97 | /// matching this pattern at the start of \arg Buffer; a distance of zero |
| 98 | /// should correspond to a perfect match. |
| 99 | unsigned ComputeMatchDistance(StringRef Buffer, |
| 100 | const StringMap<StringRef> &VariableTable) const; |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 103 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 104 | bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 105 | PatternLoc = SMLoc::getFromPointer(PatternStr.data()); |
| 106 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 107 | // Ignore trailing whitespace. |
| 108 | while (!PatternStr.empty() && |
| 109 | (PatternStr.back() == ' ' || PatternStr.back() == '\t')) |
| 110 | PatternStr = PatternStr.substr(0, PatternStr.size()-1); |
| 111 | |
| 112 | // Check that there is something on the line. |
| 113 | if (PatternStr.empty()) { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 114 | SM.PrintMessage(PatternLoc, "found empty check string with prefix '" + |
| 115 | CheckPrefix+":'", "error"); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 116 | return true; |
| 117 | } |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 119 | // Check to see if this is a fixed string, or if it has regex pieces. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 120 | if (PatternStr.size() < 2 || |
| 121 | (PatternStr.find("{{") == StringRef::npos && |
| 122 | PatternStr.find("[[") == StringRef::npos)) { |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 123 | FixedStr = PatternStr; |
| 124 | return false; |
| 125 | } |
| 126 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 127 | // Paren value #0 is for the fully matched string. Any new parenthesized |
| 128 | // values add from their. |
| 129 | unsigned CurParen = 1; |
| 130 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 131 | // Otherwise, there is at least one regex piece. Build up the regex pattern |
| 132 | // by escaping scary characters in fixed strings, building up one big regex. |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 133 | while (!PatternStr.empty()) { |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 134 | // RegEx matches. |
| 135 | if (PatternStr.size() >= 2 && |
| 136 | PatternStr[0] == '{' && PatternStr[1] == '{') { |
| 137 | |
| 138 | // Otherwise, this is the start of a regex match. Scan for the }}. |
| 139 | size_t End = PatternStr.find("}}"); |
| 140 | if (End == StringRef::npos) { |
| 141 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
| 142 | "found start of regex string with no end '}}'", "error"); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM)) |
| 147 | return true; |
| 148 | PatternStr = PatternStr.substr(End+2); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 149 | continue; |
| 150 | } |
| 151 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 152 | // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .* |
| 153 | // (or some other regex) and assigns it to the FileCheck variable 'foo'. The |
| 154 | // second form is [[foo]] which is a reference to foo. The variable name |
Daniel Dunbar | 964ac01 | 2009-11-22 22:07:50 +0000 | [diff] [blame] | 155 | // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 156 | // it. This is to catch some common errors. |
| 157 | if (PatternStr.size() >= 2 && |
| 158 | PatternStr[0] == '[' && PatternStr[1] == '[') { |
| 159 | // Verify that it is terminated properly. |
| 160 | size_t End = PatternStr.find("]]"); |
| 161 | if (End == StringRef::npos) { |
| 162 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
| 163 | "invalid named regex reference, no ]] found", "error"); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | StringRef MatchStr = PatternStr.substr(2, End-2); |
| 168 | PatternStr = PatternStr.substr(End+2); |
| 169 | |
| 170 | // Get the regex name (e.g. "foo"). |
| 171 | size_t NameEnd = MatchStr.find(':'); |
| 172 | StringRef Name = MatchStr.substr(0, NameEnd); |
| 173 | |
| 174 | if (Name.empty()) { |
| 175 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 176 | "invalid name in named regex: empty name", "error"); |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | // Verify that the name is well formed. |
| 181 | for (unsigned i = 0, e = Name.size(); i != e; ++i) |
Daniel Dunbar | 964ac01 | 2009-11-22 22:07:50 +0000 | [diff] [blame] | 182 | if (Name[i] != '_' && |
| 183 | (Name[i] < 'a' || Name[i] > 'z') && |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 184 | (Name[i] < 'A' || Name[i] > 'Z') && |
| 185 | (Name[i] < '0' || Name[i] > '9')) { |
| 186 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i), |
| 187 | "invalid name in named regex", "error"); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | // Name can't start with a digit. |
| 192 | if (isdigit(Name[0])) { |
| 193 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 194 | "invalid name in named regex", "error"); |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | // Handle [[foo]]. |
| 199 | if (NameEnd == StringRef::npos) { |
| 200 | VariableUses.push_back(std::make_pair(Name, RegExStr.size())); |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | // Handle [[foo:.*]]. |
| 205 | VariableDefs.push_back(std::make_pair(Name, CurParen)); |
| 206 | RegExStr += '('; |
| 207 | ++CurParen; |
| 208 | |
| 209 | if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM)) |
| 210 | return true; |
| 211 | |
| 212 | RegExStr += ')'; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 215 | // Handle fixed string matches. |
| 216 | // Find the end, which is the start of the next regex. |
| 217 | size_t FixedMatchEnd = PatternStr.find("{{"); |
| 218 | FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[[")); |
| 219 | AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr); |
| 220 | PatternStr = PatternStr.substr(FixedMatchEnd); |
| 221 | continue; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 222 | } |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 223 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 227 | void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) { |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 228 | // Add the characters from FixedStr to the regex, escaping as needed. This |
| 229 | // avoids "leaning toothpicks" in common patterns. |
| 230 | for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) { |
| 231 | switch (FixedStr[i]) { |
| 232 | // These are the special characters matched in "p_ere_exp". |
| 233 | case '(': |
| 234 | case ')': |
| 235 | case '^': |
| 236 | case '$': |
| 237 | case '|': |
| 238 | case '*': |
| 239 | case '+': |
| 240 | case '?': |
| 241 | case '.': |
| 242 | case '[': |
| 243 | case '\\': |
| 244 | case '{': |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 245 | TheStr += '\\'; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 246 | // FALL THROUGH. |
| 247 | default: |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 248 | TheStr += FixedStr[i]; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 254 | bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen, |
| 255 | SourceMgr &SM) { |
| 256 | Regex R(RegexStr); |
| 257 | std::string Error; |
| 258 | if (!R.isValid(Error)) { |
| 259 | SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()), |
| 260 | "invalid regex: " + Error, "error"); |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | RegExStr += RegexStr.str(); |
| 265 | CurParen += R.getNumMatches(); |
| 266 | return false; |
| 267 | } |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 269 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 270 | /// returns the position that is matched or npos if there is no match. If |
| 271 | /// there is a match, the size of the matched string is returned in MatchLen. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 272 | size_t Pattern::Match(StringRef Buffer, size_t &MatchLen, |
| 273 | StringMap<StringRef> &VariableTable) const { |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 274 | // If this is a fixed string pattern, just match it now. |
| 275 | if (!FixedStr.empty()) { |
| 276 | MatchLen = FixedStr.size(); |
| 277 | return Buffer.find(FixedStr); |
| 278 | } |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 280 | // Regex match. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 281 | |
| 282 | // If there are variable uses, we need to create a temporary string with the |
| 283 | // actual value. |
| 284 | StringRef RegExToMatch = RegExStr; |
| 285 | std::string TmpStr; |
| 286 | if (!VariableUses.empty()) { |
| 287 | TmpStr = RegExStr; |
| 288 | |
| 289 | unsigned InsertOffset = 0; |
| 290 | for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) { |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 291 | StringMap<StringRef>::iterator it = |
| 292 | VariableTable.find(VariableUses[i].first); |
| 293 | // If the variable is undefined, return an error. |
| 294 | if (it == VariableTable.end()) |
| 295 | return StringRef::npos; |
| 296 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 297 | // Look up the value and escape it so that we can plop it into the regex. |
| 298 | std::string Value; |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 299 | AddFixedStringToRegEx(it->second, Value); |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 300 | |
| 301 | // Plop it into the regex at the adjusted offset. |
| 302 | TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset, |
| 303 | Value.begin(), Value.end()); |
| 304 | InsertOffset += Value.size(); |
| 305 | } |
| 306 | |
| 307 | // Match the newly constructed regex. |
| 308 | RegExToMatch = TmpStr; |
| 309 | } |
| 310 | |
| 311 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 312 | SmallVector<StringRef, 4> MatchInfo; |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 313 | if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo)) |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 314 | return StringRef::npos; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 316 | // Successful regex match. |
| 317 | assert(!MatchInfo.empty() && "Didn't get any match"); |
| 318 | StringRef FullMatch = MatchInfo[0]; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 319 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 320 | // If this defines any variables, remember their values. |
| 321 | for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) { |
| 322 | assert(VariableDefs[i].second < MatchInfo.size() && |
| 323 | "Internal paren error"); |
| 324 | VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second]; |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 325 | } |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 327 | MatchLen = FullMatch.size(); |
| 328 | return FullMatch.data()-Buffer.data(); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 331 | unsigned Pattern::ComputeMatchDistance(StringRef Buffer, |
| 332 | const StringMap<StringRef> &VariableTable) const { |
| 333 | // Just compute the number of matching characters. For regular expressions, we |
| 334 | // just compare against the regex itself and hope for the best. |
| 335 | // |
| 336 | // FIXME: One easy improvement here is have the regex lib generate a single |
| 337 | // example regular expression which matches, and use that as the example |
| 338 | // string. |
| 339 | StringRef ExampleString(FixedStr); |
| 340 | if (ExampleString.empty()) |
| 341 | ExampleString = RegExStr; |
| 342 | |
Daniel Dunbar | 0806f9f | 2010-01-30 00:24:06 +0000 | [diff] [blame] | 343 | // Only compare up to the first line in the buffer, or the string size. |
| 344 | StringRef BufferPrefix = Buffer.substr(0, ExampleString.size()); |
| 345 | BufferPrefix = BufferPrefix.split('\n').first; |
| 346 | return BufferPrefix.edit_distance(ExampleString); |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 349 | void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer, |
| 350 | const StringMap<StringRef> &VariableTable) const{ |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 351 | // If this was a regular expression using variables, print the current |
| 352 | // variable values. |
| 353 | if (!VariableUses.empty()) { |
| 354 | for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) { |
| 355 | StringRef Var = VariableUses[i].first; |
| 356 | StringMap<StringRef>::const_iterator it = VariableTable.find(Var); |
| 357 | SmallString<256> Msg; |
| 358 | raw_svector_ostream OS(Msg); |
| 359 | |
| 360 | // Check for undefined variable references. |
| 361 | if (it == VariableTable.end()) { |
| 362 | OS << "uses undefined variable \""; |
| 363 | OS.write_escaped(Var) << "\"";; |
| 364 | } else { |
| 365 | OS << "with variable \""; |
| 366 | OS.write_escaped(Var) << "\" equal to \""; |
| 367 | OS.write_escaped(it->second) << "\""; |
| 368 | } |
| 369 | |
| 370 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), OS.str(), "note", |
| 371 | /*ShowLine=*/false); |
| 372 | } |
| 373 | } |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 374 | |
| 375 | // Attempt to find the closest/best fuzzy match. Usually an error happens |
| 376 | // because some string in the output didn't exactly match. In these cases, we |
| 377 | // would like to show the user a best guess at what "should have" matched, to |
| 378 | // save them having to actually check the input manually. |
| 379 | size_t NumLinesForward = 0; |
| 380 | size_t Best = StringRef::npos; |
| 381 | double BestQuality = 0; |
| 382 | |
| 383 | // Use an arbitrary 4k limit on how far we will search. |
Dan Gohman | e3a1e50 | 2010-01-29 21:57:46 +0000 | [diff] [blame] | 384 | for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) { |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 385 | if (Buffer[i] == '\n') |
| 386 | ++NumLinesForward; |
| 387 | |
Dan Gohman | d8a5541 | 2010-01-29 21:55:16 +0000 | [diff] [blame] | 388 | // Patterns have leading whitespace stripped, so skip whitespace when |
| 389 | // looking for something which looks like a pattern. |
| 390 | if (Buffer[i] == ' ' || Buffer[i] == '\t') |
| 391 | continue; |
| 392 | |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 393 | // Compute the "quality" of this match as an arbitrary combination of the |
| 394 | // match distance and the number of lines skipped to get to this match. |
| 395 | unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable); |
| 396 | double Quality = Distance + (NumLinesForward / 100.); |
| 397 | |
| 398 | if (Quality < BestQuality || Best == StringRef::npos) { |
| 399 | Best = i; |
| 400 | BestQuality = Quality; |
| 401 | } |
| 402 | } |
| 403 | |
Daniel Dunbar | 7a68e0d | 2010-03-19 18:07:43 +0000 | [diff] [blame] | 404 | // Print the "possible intended match here" line if we found something |
| 405 | // reasonable and not equal to what we showed in the "scanning from here" |
| 406 | // line. |
| 407 | if (Best && Best != StringRef::npos && BestQuality < 50) { |
| 408 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best), |
| 409 | "possible intended match here", "note"); |
Daniel Dunbar | ead2dac | 2009-11-22 22:59:26 +0000 | [diff] [blame] | 410 | |
| 411 | // FIXME: If we wanted to be really friendly we would show why the match |
| 412 | // failed, as it can be hard to spot simple one character differences. |
| 413 | } |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 414 | } |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 415 | |
| 416 | //===----------------------------------------------------------------------===// |
| 417 | // Check Strings. |
| 418 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 419 | |
| 420 | /// CheckString - This is a check that we found in the input file. |
| 421 | struct CheckString { |
| 422 | /// Pat - The pattern to match. |
| 423 | Pattern Pat; |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 424 | |
| 425 | /// Loc - The location in the match file that the check string was specified. |
| 426 | SMLoc Loc; |
| 427 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 428 | /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed |
| 429 | /// to a CHECK: directive. |
| 430 | bool IsCheckNext; |
| 431 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 432 | /// NotStrings - These are all of the strings that are disallowed from |
| 433 | /// occurring between this match string and the previous one (or start of |
| 434 | /// file). |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 435 | std::vector<std::pair<SMLoc, Pattern> > NotStrings; |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 437 | CheckString(const Pattern &P, SMLoc L, bool isCheckNext) |
| 438 | : Pat(P), Loc(L), IsCheckNext(isCheckNext) {} |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 439 | }; |
| 440 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 441 | /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified |
| 442 | /// memory buffer, free it, and return a new one. |
| 443 | static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) { |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 444 | SmallString<128> NewFile; |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 445 | NewFile.reserve(MB->getBufferSize()); |
| 446 | |
| 447 | for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); |
| 448 | Ptr != End; ++Ptr) { |
| 449 | // If C is not a horizontal whitespace, skip it. |
| 450 | if (*Ptr != ' ' && *Ptr != '\t') { |
| 451 | NewFile.push_back(*Ptr); |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | // Otherwise, add one space and advance over neighboring space. |
| 456 | NewFile.push_back(' '); |
| 457 | while (Ptr+1 != End && |
| 458 | (Ptr[1] == ' ' || Ptr[1] == '\t')) |
| 459 | ++Ptr; |
| 460 | } |
| 461 | |
| 462 | // Free the old buffer and return a new one. |
| 463 | MemoryBuffer *MB2 = |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 464 | MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier()); |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 465 | |
| 466 | delete MB; |
| 467 | return MB2; |
| 468 | } |
| 469 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 471 | /// ReadCheckFile - Read the check file, which specifies the sequence of |
| 472 | /// expected strings. The strings are added to the CheckStrings vector. |
| 473 | static bool ReadCheckFile(SourceMgr &SM, |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 474 | std::vector<CheckString> &CheckStrings) { |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 475 | // Open the check file, and tell SourceMgr about it. |
| 476 | std::string ErrorStr; |
| 477 | MemoryBuffer *F = |
| 478 | MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr); |
| 479 | if (F == 0) { |
| 480 | errs() << "Could not open check file '" << CheckFilename << "': " |
| 481 | << ErrorStr << '\n'; |
| 482 | return true; |
| 483 | } |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 484 | |
| 485 | // If we want to canonicalize whitespace, strip excess whitespace from the |
| 486 | // buffer containing the CHECK lines. |
| 487 | if (!NoCanonicalizeWhiteSpace) |
| 488 | F = CanonicalizeInputFile(F); |
| 489 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 490 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 491 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 492 | // Find all instances of CheckPrefix followed by : in the file. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 493 | StringRef Buffer = F->getBuffer(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 494 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 495 | std::vector<std::pair<SMLoc, Pattern> > NotMatches; |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 497 | while (1) { |
| 498 | // See if Prefix occurs in the memory buffer. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 499 | Buffer = Buffer.substr(Buffer.find(CheckPrefix)); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 500 | |
| 501 | // If we didn't find a match, we're done. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 502 | if (Buffer.empty()) |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 503 | break; |
| 504 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 505 | const char *CheckPrefixStart = Buffer.data(); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 506 | |
| 507 | // When we find a check prefix, keep track of whether we find CHECK: or |
| 508 | // CHECK-NEXT: |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 509 | bool IsCheckNext = false, IsCheckNot = false; |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 510 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 511 | // Verify that the : is present after the prefix. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 512 | if (Buffer[CheckPrefix.size()] == ':') { |
| 513 | Buffer = Buffer.substr(CheckPrefix.size()+1); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 514 | } else if (Buffer.size() > CheckPrefix.size()+6 && |
| 515 | memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) { |
| 516 | Buffer = Buffer.substr(CheckPrefix.size()+7); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 517 | IsCheckNext = true; |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 518 | } else if (Buffer.size() > CheckPrefix.size()+5 && |
| 519 | memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) { |
| 520 | Buffer = Buffer.substr(CheckPrefix.size()+6); |
| 521 | IsCheckNot = true; |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 522 | } else { |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 523 | Buffer = Buffer.substr(1); |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 524 | continue; |
| 525 | } |
| 526 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 527 | // Okay, we found the prefix, yay. Remember the rest of the line, but |
| 528 | // ignore leading and trailing whitespace. |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 529 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t")); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 530 | |
| 531 | // Scan ahead to the end of line. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 532 | size_t EOL = Buffer.find_first_of("\n\r"); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 533 | |
Dan Gohman | e546343 | 2010-01-29 21:53:18 +0000 | [diff] [blame] | 534 | // Remember the location of the start of the pattern, for diagnostics. |
| 535 | SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data()); |
| 536 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 537 | // Parse the pattern. |
| 538 | Pattern P; |
| 539 | if (P.ParsePattern(Buffer.substr(0, EOL), SM)) |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 540 | return true; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 541 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 542 | Buffer = Buffer.substr(EOL); |
| 543 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 545 | // Verify that CHECK-NEXT lines have at least one CHECK line before them. |
| 546 | if (IsCheckNext && CheckStrings.empty()) { |
| 547 | SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart), |
| 548 | "found '"+CheckPrefix+"-NEXT:' without previous '"+ |
| 549 | CheckPrefix+ ": line", "error"); |
| 550 | return true; |
| 551 | } |
| 552 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 553 | // Handle CHECK-NOT. |
| 554 | if (IsCheckNot) { |
| 555 | NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()), |
| 556 | P)); |
| 557 | continue; |
| 558 | } |
| 559 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 561 | // Okay, add the string we captured to the output vector and move on. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 562 | CheckStrings.push_back(CheckString(P, |
Dan Gohman | e546343 | 2010-01-29 21:53:18 +0000 | [diff] [blame] | 563 | PatternLoc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 564 | IsCheckNext)); |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 565 | std::swap(NotMatches, CheckStrings.back().NotStrings); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | if (CheckStrings.empty()) { |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 569 | errs() << "error: no check strings found with prefix '" << CheckPrefix |
| 570 | << ":'\n"; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 571 | return true; |
| 572 | } |
| 573 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 574 | if (!NotMatches.empty()) { |
| 575 | errs() << "error: '" << CheckPrefix |
| 576 | << "-NOT:' not supported after last check line.\n"; |
| 577 | return true; |
| 578 | } |
| 579 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 580 | return false; |
| 581 | } |
| 582 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 583 | static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr, |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 584 | StringRef Buffer, |
| 585 | StringMap<StringRef> &VariableTable) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 586 | // Otherwise, we have an error, emit an error message. |
| 587 | SM.PrintMessage(CheckStr.Loc, "expected string not found in input", |
| 588 | "error"); |
| 589 | |
| 590 | // Print the "scanning from here" line. If the current position is at the |
| 591 | // end of a line, advance to the start of the next line. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 592 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r")); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 594 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here", |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 595 | "note"); |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 596 | |
| 597 | // Allow the pattern to print additional information if desired. |
| 598 | CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 601 | /// CountNumNewlinesBetween - Count the number of newlines in the specified |
| 602 | /// range. |
| 603 | static unsigned CountNumNewlinesBetween(StringRef Range) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 604 | unsigned NumNewLines = 0; |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 605 | while (1) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 606 | // Scan for newline. |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 607 | Range = Range.substr(Range.find_first_of("\n\r")); |
| 608 | if (Range.empty()) return NumNewLines; |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 609 | |
| 610 | ++NumNewLines; |
| 611 | |
| 612 | // Handle \n\r and \r\n as a single newline. |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 613 | if (Range.size() > 1 && |
| 614 | (Range[1] == '\n' || Range[1] == '\r') && |
| 615 | (Range[0] != Range[1])) |
| 616 | Range = Range.substr(1); |
| 617 | Range = Range.substr(1); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 618 | } |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 621 | int main(int argc, char **argv) { |
| 622 | sys::PrintStackTraceOnErrorSignal(); |
| 623 | PrettyStackTraceProgram X(argc, argv); |
| 624 | cl::ParseCommandLineOptions(argc, argv); |
| 625 | |
| 626 | SourceMgr SM; |
| 627 | |
| 628 | // Read the expected strings from the check file. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 629 | std::vector<CheckString> CheckStrings; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 630 | if (ReadCheckFile(SM, CheckStrings)) |
| 631 | return 2; |
| 632 | |
| 633 | // Open the file to check and add it to SourceMgr. |
| 634 | std::string ErrorStr; |
| 635 | MemoryBuffer *F = |
| 636 | MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr); |
| 637 | if (F == 0) { |
| 638 | errs() << "Could not open input file '" << InputFilename << "': " |
| 639 | << ErrorStr << '\n'; |
| 640 | return true; |
| 641 | } |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 642 | |
| 643 | // Remove duplicate spaces in the input file if requested. |
| 644 | if (!NoCanonicalizeWhiteSpace) |
| 645 | F = CanonicalizeInputFile(F); |
| 646 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 647 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 648 | |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 649 | /// VariableTable - This holds all the current filecheck variables. |
| 650 | StringMap<StringRef> VariableTable; |
| 651 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 652 | // Check that we have all of the expected strings, in order, in the input |
| 653 | // file. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 654 | StringRef Buffer = F->getBuffer(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 655 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 656 | const char *LastMatch = Buffer.data(); |
| 657 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 658 | for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 659 | const CheckString &CheckStr = CheckStrings[StrNo]; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 660 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 661 | StringRef SearchFrom = Buffer; |
| 662 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 663 | // Find StrNo in the file. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 664 | size_t MatchLen = 0; |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 665 | Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen, VariableTable)); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 667 | // If we didn't find a match, reject the input. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 668 | if (Buffer.empty()) { |
Daniel Dunbar | fafe93c | 2009-11-22 22:08:06 +0000 | [diff] [blame] | 669 | PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 670 | return 1; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 672 | |
| 673 | StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch); |
| 674 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 675 | // If this check is a "CHECK-NEXT", verify that the previous match was on |
| 676 | // the previous line (i.e. that there is one newline between them). |
| 677 | if (CheckStr.IsCheckNext) { |
| 678 | // Count the number of newlines between the previous match and this one. |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 679 | assert(LastMatch != F->getBufferStart() && |
| 680 | "CHECK-NEXT can't be the first check in a file"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 682 | unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 683 | if (NumNewLines == 0) { |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 684 | SM.PrintMessage(CheckStr.Loc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 685 | CheckPrefix+"-NEXT: is on the same line as previous match", |
| 686 | "error"); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 687 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 688 | "'next' match was here", "note"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 689 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch), |
| 690 | "previous match was here", "note"); |
| 691 | return 1; |
| 692 | } |
| 693 | |
| 694 | if (NumNewLines != 1) { |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 695 | SM.PrintMessage(CheckStr.Loc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 696 | CheckPrefix+ |
| 697 | "-NEXT: is not on the line after the previous match", |
| 698 | "error"); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 699 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 700 | "'next' match was here", "note"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 701 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch), |
| 702 | "previous match was here", "note"); |
| 703 | return 1; |
| 704 | } |
| 705 | } |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 706 | |
| 707 | // If this match had "not strings", verify that they don't exist in the |
| 708 | // skipped region. |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 709 | for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size(); |
| 710 | ChunkNo != e; ++ChunkNo) { |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 711 | size_t MatchLen = 0; |
Chris Lattner | eec9695 | 2009-09-27 07:56:52 +0000 | [diff] [blame] | 712 | size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion, |
| 713 | MatchLen, |
| 714 | VariableTable); |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 715 | if (Pos == StringRef::npos) continue; |
| 716 | |
| 717 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos), |
| 718 | CheckPrefix+"-NOT: string occurred!", "error"); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 719 | SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first, |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 720 | CheckPrefix+"-NOT: pattern specified here", "note"); |
| 721 | return 1; |
| 722 | } |
| 723 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 8111576 | 2009-09-21 02:30:42 +0000 | [diff] [blame] | 725 | // Otherwise, everything is good. Step over the matched text and remember |
| 726 | // the position after the match as the end of the last match. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 727 | Buffer = Buffer.substr(MatchLen); |
Chris Lattner | 8111576 | 2009-09-21 02:30:42 +0000 | [diff] [blame] | 728 | LastMatch = Buffer.data(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | return 0; |
| 732 | } |