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" |
| 22 | #include "llvm/Support/SourceMgr.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/System/Signals.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | static cl::opt<std::string> |
| 28 | CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required); |
| 29 | |
| 30 | static cl::opt<std::string> |
| 31 | InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), |
| 32 | cl::init("-"), cl::value_desc("filename")); |
| 33 | |
| 34 | static cl::opt<std::string> |
| 35 | CheckPrefix("check-prefix", cl::init("CHECK"), |
| 36 | cl::desc("Prefix to use from check file (defaults to 'CHECK')")); |
| 37 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 38 | static cl::opt<bool> |
| 39 | NoCanonicalizeWhiteSpace("strict-whitespace", |
| 40 | cl::desc("Do not treat all horizontal whitespace as equivalent")); |
| 41 | |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 42 | /// CheckString - This is a check that we found in the input file. |
| 43 | struct CheckString { |
| 44 | /// Str - The string to match. |
| 45 | std::string Str; |
| 46 | |
| 47 | /// Loc - The location in the match file that the check string was specified. |
| 48 | SMLoc Loc; |
| 49 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 50 | /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed |
| 51 | /// to a CHECK: directive. |
| 52 | bool IsCheckNext; |
| 53 | |
| 54 | CheckString(const std::string &S, SMLoc L, bool isCheckNext) |
| 55 | : Str(S), Loc(L), IsCheckNext(isCheckNext) {} |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 7bee327 | 2009-08-15 17:53:12 +0000 | [diff] [blame] | 59 | /// FindFixedStringInBuffer - This works like strstr, except for two things: |
| 60 | /// 1) it handles 'nul' characters in memory buffers. 2) it returns the end of |
| 61 | /// the memory buffer on match failure instead of null. |
| 62 | static const char *FindFixedStringInBuffer(StringRef Str, const char *CurPtr, |
| 63 | const MemoryBuffer &MB) { |
| 64 | assert(!Str.empty() && "Can't find an empty string"); |
| 65 | const char *BufEnd = MB.getBufferEnd(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 7bee327 | 2009-08-15 17:53:12 +0000 | [diff] [blame] | 67 | while (1) { |
| 68 | // Scan for the first character in the match string. |
| 69 | CurPtr = (char*)memchr(CurPtr, Str[0], BufEnd-CurPtr); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 7bee327 | 2009-08-15 17:53:12 +0000 | [diff] [blame] | 71 | // If we didn't find the first character of the string, then we failed to |
| 72 | // match. |
| 73 | if (CurPtr == 0) return BufEnd; |
| 74 | |
| 75 | // If the match string is one character, then we win. |
| 76 | if (Str.size() == 1) return CurPtr; |
| 77 | |
| 78 | // Otherwise, verify that the rest of the string matches. |
| 79 | if (Str.size() <= unsigned(BufEnd-CurPtr) && |
| 80 | memcmp(CurPtr+1, Str.data()+1, Str.size()-1) == 0) |
| 81 | return CurPtr; |
| 82 | |
| 83 | // If not, advance past this character and try again. |
| 84 | ++CurPtr; |
| 85 | } |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /// ReadCheckFile - Read the check file, which specifies the sequence of |
| 89 | /// expected strings. The strings are added to the CheckStrings vector. |
| 90 | static bool ReadCheckFile(SourceMgr &SM, |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 91 | std::vector<CheckString> &CheckStrings) { |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 92 | // Open the check file, and tell SourceMgr about it. |
| 93 | std::string ErrorStr; |
| 94 | MemoryBuffer *F = |
| 95 | MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr); |
| 96 | if (F == 0) { |
| 97 | errs() << "Could not open check file '" << CheckFilename << "': " |
| 98 | << ErrorStr << '\n'; |
| 99 | return true; |
| 100 | } |
| 101 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 102 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 103 | // Find all instances of CheckPrefix followed by : in the file. |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 104 | const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd(); |
| 105 | |
| 106 | while (1) { |
| 107 | // See if Prefix occurs in the memory buffer. |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 108 | const char *Ptr = FindFixedStringInBuffer(CheckPrefix, CurPtr, *F); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 109 | |
| 110 | // If we didn't find a match, we're done. |
| 111 | if (Ptr == BufferEnd) |
| 112 | break; |
| 113 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 114 | const char *CheckPrefixStart = Ptr; |
| 115 | |
| 116 | // When we find a check prefix, keep track of whether we find CHECK: or |
| 117 | // CHECK-NEXT: |
| 118 | bool IsCheckNext; |
| 119 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 120 | // Verify that the : is present after the prefix. |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 121 | if (Ptr[CheckPrefix.size()] == ':') { |
| 122 | Ptr += CheckPrefix.size()+1; |
| 123 | IsCheckNext = false; |
| 124 | } else if (BufferEnd-Ptr > 6 && |
| 125 | memcmp(Ptr+CheckPrefix.size(), "-NEXT:", 6) == 0) { |
| 126 | Ptr += CheckPrefix.size()+7; |
| 127 | IsCheckNext = true; |
| 128 | } else { |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 129 | CurPtr = Ptr+1; |
| 130 | continue; |
| 131 | } |
| 132 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 133 | // Okay, we found the prefix, yay. Remember the rest of the line, but |
| 134 | // ignore leading and trailing whitespace. |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 135 | while (*Ptr == ' ' || *Ptr == '\t') |
| 136 | ++Ptr; |
| 137 | |
| 138 | // Scan ahead to the end of line. |
| 139 | CurPtr = Ptr; |
| 140 | while (CurPtr != BufferEnd && *CurPtr != '\n' && *CurPtr != '\r') |
| 141 | ++CurPtr; |
| 142 | |
| 143 | // Ignore trailing whitespace. |
| 144 | while (CurPtr[-1] == ' ' || CurPtr[-1] == '\t') |
| 145 | --CurPtr; |
| 146 | |
| 147 | // Check that there is something on the line. |
| 148 | if (Ptr >= CurPtr) { |
| 149 | SM.PrintMessage(SMLoc::getFromPointer(CurPtr), |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 150 | "found empty check string with prefix '"+CheckPrefix+":'", |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 151 | "error"); |
| 152 | return true; |
| 153 | } |
| 154 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 155 | // Verify that CHECK-NEXT lines have at least one CHECK line before them. |
| 156 | if (IsCheckNext && CheckStrings.empty()) { |
| 157 | SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart), |
| 158 | "found '"+CheckPrefix+"-NEXT:' without previous '"+ |
| 159 | CheckPrefix+ ": line", "error"); |
| 160 | return true; |
| 161 | } |
| 162 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 163 | // Okay, add the string we captured to the output vector and move on. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 164 | CheckStrings.push_back(CheckString(std::string(Ptr, CurPtr), |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 165 | SMLoc::getFromPointer(Ptr), |
| 166 | IsCheckNext)); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (CheckStrings.empty()) { |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 170 | errs() << "error: no check strings found with prefix '" << CheckPrefix |
| 171 | << ":'\n"; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 178 | // CanonicalizeCheckStrings - Replace all sequences of horizontal whitespace in |
| 179 | // the check strings with a single space. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 180 | static void CanonicalizeCheckStrings(std::vector<CheckString> &CheckStrings) { |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 181 | for (unsigned i = 0, e = CheckStrings.size(); i != e; ++i) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 182 | std::string &Str = CheckStrings[i].Str; |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 183 | |
| 184 | for (unsigned C = 0; C != Str.size(); ++C) { |
| 185 | // If C is not a horizontal whitespace, skip it. |
| 186 | if (Str[C] != ' ' && Str[C] != '\t') |
| 187 | continue; |
| 188 | |
| 189 | // Replace the character with space, then remove any other space |
| 190 | // characters after it. |
| 191 | Str[C] = ' '; |
| 192 | |
| 193 | while (C+1 != Str.size() && |
| 194 | (Str[C+1] == ' ' || Str[C+1] == '\t')) |
| 195 | Str.erase(Str.begin()+C+1); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified |
| 201 | /// memory buffer, free it, and return a new one. |
| 202 | static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) { |
Daniel Dunbar | 6f69aa3 | 2009-08-02 01:21:22 +0000 | [diff] [blame] | 203 | SmallVector<char, 16> NewFile; |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 204 | NewFile.reserve(MB->getBufferSize()); |
| 205 | |
| 206 | for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); |
| 207 | Ptr != End; ++Ptr) { |
| 208 | // If C is not a horizontal whitespace, skip it. |
| 209 | if (*Ptr != ' ' && *Ptr != '\t') { |
| 210 | NewFile.push_back(*Ptr); |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | // Otherwise, add one space and advance over neighboring space. |
| 215 | NewFile.push_back(' '); |
| 216 | while (Ptr+1 != End && |
| 217 | (Ptr[1] == ' ' || Ptr[1] == '\t')) |
| 218 | ++Ptr; |
| 219 | } |
| 220 | |
| 221 | // Free the old buffer and return a new one. |
| 222 | MemoryBuffer *MB2 = |
Daniel Dunbar | 6f69aa3 | 2009-08-02 01:21:22 +0000 | [diff] [blame] | 223 | MemoryBuffer::getMemBufferCopy(NewFile.data(), |
| 224 | NewFile.data() + NewFile.size(), |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 225 | MB->getBufferIdentifier()); |
| 226 | |
| 227 | delete MB; |
| 228 | return MB2; |
| 229 | } |
| 230 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 232 | static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr, |
| 233 | const char *CurPtr, const char *BufferEnd) { |
| 234 | // Otherwise, we have an error, emit an error message. |
| 235 | SM.PrintMessage(CheckStr.Loc, "expected string not found in input", |
| 236 | "error"); |
| 237 | |
| 238 | // Print the "scanning from here" line. If the current position is at the |
| 239 | // end of a line, advance to the start of the next line. |
| 240 | const char *Scan = CurPtr; |
| 241 | while (Scan != BufferEnd && |
| 242 | (*Scan == ' ' || *Scan == '\t')) |
| 243 | ++Scan; |
| 244 | if (*Scan == '\n' || *Scan == '\r') |
| 245 | CurPtr = Scan+1; |
| 246 | |
| 247 | |
| 248 | SM.PrintMessage(SMLoc::getFromPointer(CurPtr), "scanning from here", |
| 249 | "note"); |
| 250 | } |
| 251 | |
| 252 | static unsigned CountNumNewlinesBetween(const char *Start, const char *End) { |
| 253 | unsigned NumNewLines = 0; |
| 254 | for (; Start != End; ++Start) { |
| 255 | // Scan for newline. |
| 256 | if (Start[0] != '\n' && Start[0] != '\r') |
| 257 | continue; |
| 258 | |
| 259 | ++NumNewLines; |
| 260 | |
| 261 | // Handle \n\r and \r\n as a single newline. |
| 262 | if (Start+1 != End && |
| 263 | (Start[0] == '\n' || Start[0] == '\r') && |
| 264 | (Start[0] != Start[1])) |
| 265 | ++Start; |
| 266 | } |
| 267 | |
| 268 | return NumNewLines; |
| 269 | } |
| 270 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 271 | int main(int argc, char **argv) { |
| 272 | sys::PrintStackTraceOnErrorSignal(); |
| 273 | PrettyStackTraceProgram X(argc, argv); |
| 274 | cl::ParseCommandLineOptions(argc, argv); |
| 275 | |
| 276 | SourceMgr SM; |
| 277 | |
| 278 | // Read the expected strings from the check file. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 279 | std::vector<CheckString> CheckStrings; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 280 | if (ReadCheckFile(SM, CheckStrings)) |
| 281 | return 2; |
| 282 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 283 | // Remove duplicate spaces in the check strings if requested. |
| 284 | if (!NoCanonicalizeWhiteSpace) |
| 285 | CanonicalizeCheckStrings(CheckStrings); |
| 286 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 287 | // Open the file to check and add it to SourceMgr. |
| 288 | std::string ErrorStr; |
| 289 | MemoryBuffer *F = |
| 290 | MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr); |
| 291 | if (F == 0) { |
| 292 | errs() << "Could not open input file '" << InputFilename << "': " |
| 293 | << ErrorStr << '\n'; |
| 294 | return true; |
| 295 | } |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 296 | |
| 297 | // Remove duplicate spaces in the input file if requested. |
| 298 | if (!NoCanonicalizeWhiteSpace) |
| 299 | F = CanonicalizeInputFile(F); |
| 300 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 301 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 302 | |
| 303 | // Check that we have all of the expected strings, in order, in the input |
| 304 | // file. |
| 305 | const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd(); |
| 306 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 307 | const char *LastMatch = 0; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 308 | for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 309 | const CheckString &CheckStr = CheckStrings[StrNo]; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 310 | |
| 311 | // Find StrNo in the file. |
Chris Lattner | 7bee327 | 2009-08-15 17:53:12 +0000 | [diff] [blame] | 312 | const char *Ptr = FindFixedStringInBuffer(CheckStr.Str, CurPtr, *F); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 314 | // If we didn't find a match, reject the input. |
| 315 | if (Ptr == BufferEnd) { |
| 316 | PrintCheckFailed(SM, CheckStr, CurPtr, BufferEnd); |
| 317 | return 1; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 320 | // If this check is a "CHECK-NEXT", verify that the previous match was on |
| 321 | // the previous line (i.e. that there is one newline between them). |
| 322 | if (CheckStr.IsCheckNext) { |
| 323 | // Count the number of newlines between the previous match and this one. |
| 324 | assert(LastMatch && "CHECK-NEXT can't be the first check in a file"); |
| 325 | |
| 326 | unsigned NumNewLines = CountNumNewlinesBetween(LastMatch, Ptr); |
| 327 | if (NumNewLines == 0) { |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame^] | 328 | SM.PrintMessage(CheckStr.Loc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 329 | CheckPrefix+"-NEXT: is on the same line as previous match", |
| 330 | "error"); |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame^] | 331 | SM.PrintMessage(SMLoc::getFromPointer(Ptr), |
| 332 | "'next' match was here", "note"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 333 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch), |
| 334 | "previous match was here", "note"); |
| 335 | return 1; |
| 336 | } |
| 337 | |
| 338 | if (NumNewLines != 1) { |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame^] | 339 | SM.PrintMessage(CheckStr.Loc, |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 340 | CheckPrefix+ |
| 341 | "-NEXT: is not on the line after the previous match", |
| 342 | "error"); |
Chris Lattner | 0b2353f | 2009-08-16 02:22:31 +0000 | [diff] [blame^] | 343 | SM.PrintMessage(SMLoc::getFromPointer(Ptr), |
| 344 | "'next' match was here", "note"); |
Chris Lattner | 5dafafd | 2009-08-15 18:32:21 +0000 | [diff] [blame] | 345 | SM.PrintMessage(SMLoc::getFromPointer(LastMatch), |
| 346 | "previous match was here", "note"); |
| 347 | return 1; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Otherwise, everything is good. Remember this as the last match and move |
| 352 | // on to the next one. |
| 353 | LastMatch = Ptr; |
| 354 | CurPtr = Ptr + CheckStr.Str.size(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |