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" |
| 26 | using namespace llvm; |
| 27 | |
| 28 | static cl::opt<std::string> |
| 29 | CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required); |
| 30 | |
| 31 | static cl::opt<std::string> |
| 32 | InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), |
| 33 | cl::init("-"), cl::value_desc("filename")); |
| 34 | |
| 35 | static cl::opt<std::string> |
| 36 | CheckPrefix("check-prefix", cl::init("CHECK"), |
| 37 | cl::desc("Prefix to use from check file (defaults to 'CHECK')")); |
| 38 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 39 | static cl::opt<bool> |
| 40 | NoCanonicalizeWhiteSpace("strict-whitespace", |
| 41 | cl::desc("Do not treat all horizontal whitespace as equivalent")); |
| 42 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | // Pattern Handling Code. |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 47 | class Pattern { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame^] | 48 | SourceMgr *SM; |
| 49 | SMLoc PatternLoc; |
| 50 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 51 | /// FixedStr - If non-empty, this pattern is a fixed string match with the |
| 52 | /// specified fixed string. |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 53 | StringRef FixedStr; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 54 | |
| 55 | /// RegEx - If non-empty, this is a regex pattern. |
| 56 | std::string RegExStr; |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 57 | public: |
| 58 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 59 | Pattern() { } |
| 60 | |
| 61 | bool ParsePattern(StringRef PatternStr, SourceMgr &SM); |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 62 | |
| 63 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 64 | /// returns the position that is matched or npos if there is no match. If |
| 65 | /// there is a match, the size of the matched string is returned in MatchLen. |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 66 | size_t Match(StringRef Buffer, size_t &MatchLen) const; |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | void AddFixedStringToRegEx(StringRef FixedStr); |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 72 | bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame^] | 73 | this->SM = &SM; |
| 74 | PatternLoc = SMLoc::getFromPointer(PatternStr.data()); |
| 75 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 76 | // Ignore trailing whitespace. |
| 77 | while (!PatternStr.empty() && |
| 78 | (PatternStr.back() == ' ' || PatternStr.back() == '\t')) |
| 79 | PatternStr = PatternStr.substr(0, PatternStr.size()-1); |
| 80 | |
| 81 | // Check that there is something on the line. |
| 82 | if (PatternStr.empty()) { |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame^] | 83 | SM.PrintMessage(PatternLoc, "found empty check string with prefix '" + |
| 84 | CheckPrefix+":'", "error"); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 85 | return true; |
| 86 | } |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 88 | // Check to see if this is a fixed string, or if it has regex pieces. |
| 89 | if (PatternStr.size() < 2 || PatternStr.find("{{") == StringRef::npos) { |
| 90 | FixedStr = PatternStr; |
| 91 | return false; |
| 92 | } |
| 93 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 94 | // Otherwise, there is at least one regex piece. Build up the regex pattern |
| 95 | // by escaping scary characters in fixed strings, building up one big regex. |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 96 | while (!PatternStr.empty()) { |
| 97 | // Handle fixed string matches. |
| 98 | if (PatternStr.size() < 2 || |
| 99 | PatternStr[0] != '{' || PatternStr[1] != '{') { |
| 100 | // Find the end, which is the start of the next regex. |
| 101 | size_t FixedMatchEnd = PatternStr.find("{{"); |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 102 | AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd)); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 103 | PatternStr = PatternStr.substr(FixedMatchEnd); |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | // Otherwise, this is the start of a regex match. Scan for the }}. |
| 108 | size_t End = PatternStr.find("}}"); |
| 109 | if (End == StringRef::npos) { |
| 110 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
| 111 | "found start of regex string with no end '}}'", "error"); |
| 112 | return true; |
| 113 | } |
| 114 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 115 | StringRef RegexStr = PatternStr.substr(2, End-2); |
| 116 | Regex R(RegexStr); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 117 | std::string Error; |
| 118 | if (!R.isValid(Error)) { |
| 119 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()+2), |
| 120 | "invalid regex: " + Error, "error"); |
| 121 | return true; |
| 122 | } |
| 123 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 124 | RegExStr += RegexStr.str(); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 125 | PatternStr = PatternStr.substr(End+2); |
| 126 | } |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 127 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 131 | void Pattern::AddFixedStringToRegEx(StringRef FixedStr) { |
| 132 | // Add the characters from FixedStr to the regex, escaping as needed. This |
| 133 | // avoids "leaning toothpicks" in common patterns. |
| 134 | for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) { |
| 135 | switch (FixedStr[i]) { |
| 136 | // These are the special characters matched in "p_ere_exp". |
| 137 | case '(': |
| 138 | case ')': |
| 139 | case '^': |
| 140 | case '$': |
| 141 | case '|': |
| 142 | case '*': |
| 143 | case '+': |
| 144 | case '?': |
| 145 | case '.': |
| 146 | case '[': |
| 147 | case '\\': |
| 148 | case '{': |
| 149 | RegExStr += '\\'; |
| 150 | // FALL THROUGH. |
| 151 | default: |
| 152 | RegExStr += FixedStr[i]; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 159 | /// Match - Match the pattern string against the input buffer Buffer. This |
| 160 | /// returns the position that is matched or npos if there is no match. If |
| 161 | /// there is a match, the size of the matched string is returned in MatchLen. |
| 162 | size_t Pattern::Match(StringRef Buffer, size_t &MatchLen) const { |
Chris Lattner | 2702e6a | 2009-09-25 17:09:12 +0000 | [diff] [blame] | 163 | // If this is a fixed string pattern, just match it now. |
| 164 | if (!FixedStr.empty()) { |
| 165 | MatchLen = FixedStr.size(); |
| 166 | return Buffer.find(FixedStr); |
| 167 | } |
| 168 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 169 | // Regex match. |
| 170 | SmallVector<StringRef, 4> MatchInfo; |
| 171 | if (!Regex(RegExStr, Regex::Sub|Regex::Newline).match(Buffer, &MatchInfo)) |
| 172 | return StringRef::npos; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 174 | // Successful regex match. |
| 175 | assert(!MatchInfo.empty() && "Didn't get any match"); |
| 176 | StringRef FullMatch = MatchInfo[0]; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 94638f0 | 2009-09-25 17:29:36 +0000 | [diff] [blame^] | 178 | |
| 179 | if (MatchInfo.size() != 1) { |
| 180 | SM->PrintMessage(PatternLoc, "regex cannot use grouping parens", "error"); |
| 181 | exit(1); |
| 182 | } |
| 183 | |
| 184 | |
Chris Lattner | 5d6a05f | 2009-09-25 17:23:43 +0000 | [diff] [blame] | 185 | MatchLen = FullMatch.size(); |
| 186 | return FullMatch.data()-Buffer.data(); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 189 | |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | // Check Strings. |
| 192 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 193 | |
| 194 | /// CheckString - This is a check that we found in the input file. |
| 195 | struct CheckString { |
| 196 | /// Pat - The pattern to match. |
| 197 | Pattern Pat; |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 198 | |
| 199 | /// Loc - The location in the match file that the check string was specified. |
| 200 | SMLoc Loc; |
| 201 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 202 | /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed |
| 203 | /// to a CHECK: directive. |
| 204 | bool IsCheckNext; |
| 205 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 206 | /// NotStrings - These are all of the strings that are disallowed from |
| 207 | /// occurring between this match string and the previous one (or start of |
| 208 | /// file). |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 209 | std::vector<std::pair<SMLoc, Pattern> > NotStrings; |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 211 | CheckString(const Pattern &P, SMLoc L, bool isCheckNext) |
| 212 | : Pat(P), Loc(L), IsCheckNext(isCheckNext) {} |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 215 | /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified |
| 216 | /// memory buffer, free it, and return a new one. |
| 217 | static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) { |
| 218 | SmallVector<char, 16> NewFile; |
| 219 | NewFile.reserve(MB->getBufferSize()); |
| 220 | |
| 221 | for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); |
| 222 | Ptr != End; ++Ptr) { |
| 223 | // If C is not a horizontal whitespace, skip it. |
| 224 | if (*Ptr != ' ' && *Ptr != '\t') { |
| 225 | NewFile.push_back(*Ptr); |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | // Otherwise, add one space and advance over neighboring space. |
| 230 | NewFile.push_back(' '); |
| 231 | while (Ptr+1 != End && |
| 232 | (Ptr[1] == ' ' || Ptr[1] == '\t')) |
| 233 | ++Ptr; |
| 234 | } |
| 235 | |
| 236 | // Free the old buffer and return a new one. |
| 237 | MemoryBuffer *MB2 = |
| 238 | MemoryBuffer::getMemBufferCopy(NewFile.data(), |
| 239 | NewFile.data() + NewFile.size(), |
| 240 | MB->getBufferIdentifier()); |
| 241 | |
| 242 | delete MB; |
| 243 | return MB2; |
| 244 | } |
| 245 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 247 | /// ReadCheckFile - Read the check file, which specifies the sequence of |
| 248 | /// expected strings. The strings are added to the CheckStrings vector. |
| 249 | static bool ReadCheckFile(SourceMgr &SM, |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 250 | std::vector<CheckString> &CheckStrings) { |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 251 | // Open the check file, and tell SourceMgr about it. |
| 252 | std::string ErrorStr; |
| 253 | MemoryBuffer *F = |
| 254 | MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr); |
| 255 | if (F == 0) { |
| 256 | errs() << "Could not open check file '" << CheckFilename << "': " |
| 257 | << ErrorStr << '\n'; |
| 258 | return true; |
| 259 | } |
Chris Lattner | adea46e | 2009-09-24 20:45:07 +0000 | [diff] [blame] | 260 | |
| 261 | // If we want to canonicalize whitespace, strip excess whitespace from the |
| 262 | // buffer containing the CHECK lines. |
| 263 | if (!NoCanonicalizeWhiteSpace) |
| 264 | F = CanonicalizeInputFile(F); |
| 265 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 266 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 267 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 268 | // Find all instances of CheckPrefix followed by : in the file. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 269 | StringRef Buffer = F->getBuffer(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 270 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 271 | std::vector<std::pair<SMLoc, Pattern> > NotMatches; |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 273 | while (1) { |
| 274 | // See if Prefix occurs in the memory buffer. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 275 | Buffer = Buffer.substr(Buffer.find(CheckPrefix)); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 276 | |
| 277 | // If we didn't find a match, we're done. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 278 | if (Buffer.empty()) |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 279 | break; |
| 280 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 281 | const char *CheckPrefixStart = Buffer.data(); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 282 | |
| 283 | // When we find a check prefix, keep track of whether we find CHECK: or |
| 284 | // CHECK-NEXT: |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 285 | bool IsCheckNext = false, IsCheckNot = false; |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 286 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 287 | // Verify that the : is present after the prefix. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 288 | if (Buffer[CheckPrefix.size()] == ':') { |
| 289 | Buffer = Buffer.substr(CheckPrefix.size()+1); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 290 | } else if (Buffer.size() > CheckPrefix.size()+6 && |
| 291 | memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) { |
| 292 | Buffer = Buffer.substr(CheckPrefix.size()+7); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 293 | IsCheckNext = true; |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 294 | } else if (Buffer.size() > CheckPrefix.size()+5 && |
| 295 | memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) { |
| 296 | Buffer = Buffer.substr(CheckPrefix.size()+6); |
| 297 | IsCheckNot = true; |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 298 | } else { |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 299 | Buffer = Buffer.substr(1); |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 300 | continue; |
| 301 | } |
| 302 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 303 | // Okay, we found the prefix, yay. Remember the rest of the line, but |
| 304 | // ignore leading and trailing whitespace. |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 305 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t")); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 306 | |
| 307 | // Scan ahead to the end of line. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 308 | size_t EOL = Buffer.find_first_of("\n\r"); |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 309 | |
| 310 | // Parse the pattern. |
| 311 | Pattern P; |
| 312 | if (P.ParsePattern(Buffer.substr(0, EOL), SM)) |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 313 | return true; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 314 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 315 | Buffer = Buffer.substr(EOL); |
| 316 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 318 | // Verify that CHECK-NEXT lines have at least one CHECK line before them. |
| 319 | if (IsCheckNext && CheckStrings.empty()) { |
| 320 | SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart), |
| 321 | "found '"+CheckPrefix+"-NEXT:' without previous '"+ |
| 322 | CheckPrefix+ ": line", "error"); |
| 323 | return true; |
| 324 | } |
| 325 | |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 326 | // Handle CHECK-NOT. |
| 327 | if (IsCheckNot) { |
| 328 | NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()), |
| 329 | P)); |
| 330 | continue; |
| 331 | } |
| 332 | |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 334 | // 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] | 335 | CheckStrings.push_back(CheckString(P, |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 336 | SMLoc::getFromPointer(Buffer.data()), |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 337 | IsCheckNext)); |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 338 | std::swap(NotMatches, CheckStrings.back().NotStrings); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | if (CheckStrings.empty()) { |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 342 | errs() << "error: no check strings found with prefix '" << CheckPrefix |
| 343 | << ":'\n"; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 347 | if (!NotMatches.empty()) { |
| 348 | errs() << "error: '" << CheckPrefix |
| 349 | << "-NOT:' not supported after last check line.\n"; |
| 350 | return true; |
| 351 | } |
| 352 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 356 | static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr, |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 357 | StringRef Buffer) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 358 | // Otherwise, we have an error, emit an error message. |
| 359 | SM.PrintMessage(CheckStr.Loc, "expected string not found in input", |
| 360 | "error"); |
| 361 | |
| 362 | // Print the "scanning from here" line. If the current position is at the |
| 363 | // end of a line, advance to the start of the next line. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 364 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r")); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 366 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here", |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 367 | "note"); |
| 368 | } |
| 369 | |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 370 | /// CountNumNewlinesBetween - Count the number of newlines in the specified |
| 371 | /// range. |
| 372 | static unsigned CountNumNewlinesBetween(StringRef Range) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 373 | unsigned NumNewLines = 0; |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 374 | while (1) { |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 375 | // Scan for newline. |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 376 | Range = Range.substr(Range.find_first_of("\n\r")); |
| 377 | if (Range.empty()) return NumNewLines; |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 378 | |
| 379 | ++NumNewLines; |
| 380 | |
| 381 | // Handle \n\r and \r\n as a single newline. |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 382 | if (Range.size() > 1 && |
| 383 | (Range[1] == '\n' || Range[1] == '\r') && |
| 384 | (Range[0] != Range[1])) |
| 385 | Range = Range.substr(1); |
| 386 | Range = Range.substr(1); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 387 | } |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 390 | int main(int argc, char **argv) { |
| 391 | sys::PrintStackTraceOnErrorSignal(); |
| 392 | PrettyStackTraceProgram X(argc, argv); |
| 393 | cl::ParseCommandLineOptions(argc, argv); |
| 394 | |
| 395 | SourceMgr SM; |
| 396 | |
| 397 | // Read the expected strings from the check file. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 398 | std::vector<CheckString> CheckStrings; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 399 | if (ReadCheckFile(SM, CheckStrings)) |
| 400 | return 2; |
| 401 | |
| 402 | // Open the file to check and add it to SourceMgr. |
| 403 | std::string ErrorStr; |
| 404 | MemoryBuffer *F = |
| 405 | MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr); |
| 406 | if (F == 0) { |
| 407 | errs() << "Could not open input file '" << InputFilename << "': " |
| 408 | << ErrorStr << '\n'; |
| 409 | return true; |
| 410 | } |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 411 | |
| 412 | // Remove duplicate spaces in the input file if requested. |
| 413 | if (!NoCanonicalizeWhiteSpace) |
| 414 | F = CanonicalizeInputFile(F); |
| 415 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 416 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 417 | |
| 418 | // Check that we have all of the expected strings, in order, in the input |
| 419 | // file. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 420 | StringRef Buffer = F->getBuffer(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 421 | |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 422 | const char *LastMatch = Buffer.data(); |
| 423 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 424 | for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 425 | const CheckString &CheckStr = CheckStrings[StrNo]; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 427 | StringRef SearchFrom = Buffer; |
| 428 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 429 | // Find StrNo in the file. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 430 | size_t MatchLen = 0; |
| 431 | Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen)); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 433 | // If we didn't find a match, reject the input. |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 434 | if (Buffer.empty()) { |
| 435 | PrintCheckFailed(SM, CheckStr, SearchFrom); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 436 | return 1; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 437 | } |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 438 | |
| 439 | StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch); |
| 440 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 441 | // If this check is a "CHECK-NEXT", verify that the previous match was on |
| 442 | // the previous line (i.e. that there is one newline between them). |
| 443 | if (CheckStr.IsCheckNext) { |
| 444 | // Count the number of newlines between the previous match and this one. |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 445 | assert(LastMatch != F->getBufferStart() && |
| 446 | "CHECK-NEXT can't be the first check in a file"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 3711b7a | 2009-09-20 22:42:44 +0000 | [diff] [blame] | 448 | unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 449 | if (NumNewLines == 0) { |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 450 | SM.PrintMessage(CheckStr.Loc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 451 | CheckPrefix+"-NEXT: is on the same line as previous match", |
| 452 | "error"); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 453 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 454 | "'next' match was here", "note"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 455 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch), |
| 456 | "previous match was here", "note"); |
| 457 | return 1; |
| 458 | } |
| 459 | |
| 460 | if (NumNewLines != 1) { |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 461 | SM.PrintMessage(CheckStr.Loc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 462 | CheckPrefix+ |
| 463 | "-NEXT: is not on the line after the previous match", |
| 464 | "error"); |
Chris Lattner | 9607703 | 2009-09-20 22:11:44 +0000 | [diff] [blame] | 465 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame] | 466 | "'next' match was here", "note"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 467 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch), |
| 468 | "previous match was here", "note"); |
| 469 | return 1; |
| 470 | } |
| 471 | } |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 472 | |
| 473 | // If this match had "not strings", verify that they don't exist in the |
| 474 | // skipped region. |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 475 | for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size(); ChunkNo != e; ++ChunkNo) { |
Chris Lattner | a29703e | 2009-09-24 20:39:13 +0000 | [diff] [blame] | 476 | size_t MatchLen = 0; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 477 | size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion, MatchLen); |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 478 | if (Pos == StringRef::npos) continue; |
| 479 | |
| 480 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos), |
| 481 | CheckPrefix+"-NOT: string occurred!", "error"); |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 482 | SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first, |
Chris Lattner | f15380b | 2009-09-20 22:35:26 +0000 | [diff] [blame] | 483 | CheckPrefix+"-NOT: pattern specified here", "note"); |
| 484 | return 1; |
| 485 | } |
| 486 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 8111576 | 2009-09-21 02:30:42 +0000 | [diff] [blame] | 488 | // Otherwise, everything is good. Step over the matched text and remember |
| 489 | // the position after the match as the end of the last match. |
Chris Lattner | 9fc6678 | 2009-09-24 20:25:55 +0000 | [diff] [blame] | 490 | Buffer = Buffer.substr(MatchLen); |
Chris Lattner | 8111576 | 2009-09-21 02:30:42 +0000 | [diff] [blame] | 491 | LastMatch = Buffer.data(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | } |