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 | |
| 50 | CheckString(const std::string &S, SMLoc L) : Str(S), Loc(L) {} |
| 51 | }; |
| 52 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 7bee327 | 2009-08-15 17:53:12 +0000 | [diff] [blame] | 54 | /// FindFixedStringInBuffer - This works like strstr, except for two things: |
| 55 | /// 1) it handles 'nul' characters in memory buffers. 2) it returns the end of |
| 56 | /// the memory buffer on match failure instead of null. |
| 57 | static const char *FindFixedStringInBuffer(StringRef Str, const char *CurPtr, |
| 58 | const MemoryBuffer &MB) { |
| 59 | assert(!Str.empty() && "Can't find an empty string"); |
| 60 | const char *BufEnd = MB.getBufferEnd(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 7bee327 | 2009-08-15 17:53:12 +0000 | [diff] [blame] | 62 | while (1) { |
| 63 | // Scan for the first character in the match string. |
| 64 | CurPtr = (char*)memchr(CurPtr, Str[0], BufEnd-CurPtr); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 7bee327 | 2009-08-15 17:53:12 +0000 | [diff] [blame] | 66 | // If we didn't find the first character of the string, then we failed to |
| 67 | // match. |
| 68 | if (CurPtr == 0) return BufEnd; |
| 69 | |
| 70 | // If the match string is one character, then we win. |
| 71 | if (Str.size() == 1) return CurPtr; |
| 72 | |
| 73 | // Otherwise, verify that the rest of the string matches. |
| 74 | if (Str.size() <= unsigned(BufEnd-CurPtr) && |
| 75 | memcmp(CurPtr+1, Str.data()+1, Str.size()-1) == 0) |
| 76 | return CurPtr; |
| 77 | |
| 78 | // If not, advance past this character and try again. |
| 79 | ++CurPtr; |
| 80 | } |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /// ReadCheckFile - Read the check file, which specifies the sequence of |
| 84 | /// expected strings. The strings are added to the CheckStrings vector. |
| 85 | static bool ReadCheckFile(SourceMgr &SM, |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 86 | std::vector<CheckString> &CheckStrings) { |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 87 | // Open the check file, and tell SourceMgr about it. |
| 88 | std::string ErrorStr; |
| 89 | MemoryBuffer *F = |
| 90 | MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr); |
| 91 | if (F == 0) { |
| 92 | errs() << "Could not open check file '" << CheckFilename << "': " |
| 93 | << ErrorStr << '\n'; |
| 94 | return true; |
| 95 | } |
| 96 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 97 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 98 | // Find all instances of CheckPrefix followed by : in the file. |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 99 | const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd(); |
| 100 | |
| 101 | while (1) { |
| 102 | // See if Prefix occurs in the memory buffer. |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 103 | const char *Ptr = FindFixedStringInBuffer(CheckPrefix, CurPtr, *F); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 104 | |
| 105 | // If we didn't find a match, we're done. |
| 106 | if (Ptr == BufferEnd) |
| 107 | break; |
| 108 | |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 109 | // Verify that the : is present after the prefix. |
| 110 | if (Ptr[CheckPrefix.size()] != ':') { |
| 111 | CurPtr = Ptr+1; |
| 112 | continue; |
| 113 | } |
| 114 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 115 | // Okay, we found the prefix, yay. Remember the rest of the line, but |
| 116 | // ignore leading and trailing whitespace. |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 117 | Ptr += CheckPrefix.size()+1; |
| 118 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 119 | while (*Ptr == ' ' || *Ptr == '\t') |
| 120 | ++Ptr; |
| 121 | |
| 122 | // Scan ahead to the end of line. |
| 123 | CurPtr = Ptr; |
| 124 | while (CurPtr != BufferEnd && *CurPtr != '\n' && *CurPtr != '\r') |
| 125 | ++CurPtr; |
| 126 | |
| 127 | // Ignore trailing whitespace. |
| 128 | while (CurPtr[-1] == ' ' || CurPtr[-1] == '\t') |
| 129 | --CurPtr; |
| 130 | |
| 131 | // Check that there is something on the line. |
| 132 | if (Ptr >= CurPtr) { |
| 133 | SM.PrintMessage(SMLoc::getFromPointer(CurPtr), |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 134 | "found empty check string with prefix '"+CheckPrefix+":'", |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 135 | "error"); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | // 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] | 140 | CheckStrings.push_back(CheckString(std::string(Ptr, CurPtr), |
| 141 | SMLoc::getFromPointer(Ptr))); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (CheckStrings.empty()) { |
Chris Lattner | d7e2505 | 2009-08-15 18:00:42 +0000 | [diff] [blame] | 145 | errs() << "error: no check strings found with prefix '" << CheckPrefix |
| 146 | << ":'\n"; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 153 | // CanonicalizeCheckStrings - Replace all sequences of horizontal whitespace in |
| 154 | // the check strings with a single space. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 155 | static void CanonicalizeCheckStrings(std::vector<CheckString> &CheckStrings) { |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 156 | for (unsigned i = 0, e = CheckStrings.size(); i != e; ++i) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 157 | std::string &Str = CheckStrings[i].Str; |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 158 | |
| 159 | for (unsigned C = 0; C != Str.size(); ++C) { |
| 160 | // If C is not a horizontal whitespace, skip it. |
| 161 | if (Str[C] != ' ' && Str[C] != '\t') |
| 162 | continue; |
| 163 | |
| 164 | // Replace the character with space, then remove any other space |
| 165 | // characters after it. |
| 166 | Str[C] = ' '; |
| 167 | |
| 168 | while (C+1 != Str.size() && |
| 169 | (Str[C+1] == ' ' || Str[C+1] == '\t')) |
| 170 | Str.erase(Str.begin()+C+1); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified |
| 176 | /// memory buffer, free it, and return a new one. |
| 177 | static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) { |
Daniel Dunbar | 6f69aa3 | 2009-08-02 01:21:22 +0000 | [diff] [blame] | 178 | SmallVector<char, 16> NewFile; |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 179 | NewFile.reserve(MB->getBufferSize()); |
| 180 | |
| 181 | for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd(); |
| 182 | Ptr != End; ++Ptr) { |
| 183 | // If C is not a horizontal whitespace, skip it. |
| 184 | if (*Ptr != ' ' && *Ptr != '\t') { |
| 185 | NewFile.push_back(*Ptr); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | // Otherwise, add one space and advance over neighboring space. |
| 190 | NewFile.push_back(' '); |
| 191 | while (Ptr+1 != End && |
| 192 | (Ptr[1] == ' ' || Ptr[1] == '\t')) |
| 193 | ++Ptr; |
| 194 | } |
| 195 | |
| 196 | // Free the old buffer and return a new one. |
| 197 | MemoryBuffer *MB2 = |
Daniel Dunbar | 6f69aa3 | 2009-08-02 01:21:22 +0000 | [diff] [blame] | 198 | MemoryBuffer::getMemBufferCopy(NewFile.data(), |
| 199 | NewFile.data() + NewFile.size(), |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 200 | MB->getBufferIdentifier()); |
| 201 | |
| 202 | delete MB; |
| 203 | return MB2; |
| 204 | } |
| 205 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 206 | |
| 207 | int main(int argc, char **argv) { |
| 208 | sys::PrintStackTraceOnErrorSignal(); |
| 209 | PrettyStackTraceProgram X(argc, argv); |
| 210 | cl::ParseCommandLineOptions(argc, argv); |
| 211 | |
| 212 | SourceMgr SM; |
| 213 | |
| 214 | // Read the expected strings from the check file. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 215 | std::vector<CheckString> CheckStrings; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 216 | if (ReadCheckFile(SM, CheckStrings)) |
| 217 | return 2; |
| 218 | |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 219 | // Remove duplicate spaces in the check strings if requested. |
| 220 | if (!NoCanonicalizeWhiteSpace) |
| 221 | CanonicalizeCheckStrings(CheckStrings); |
| 222 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 223 | // Open the file to check and add it to SourceMgr. |
| 224 | std::string ErrorStr; |
| 225 | MemoryBuffer *F = |
| 226 | MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr); |
| 227 | if (F == 0) { |
| 228 | errs() << "Could not open input file '" << InputFilename << "': " |
| 229 | << ErrorStr << '\n'; |
| 230 | return true; |
| 231 | } |
Chris Lattner | 88a7e9e | 2009-07-11 18:58:15 +0000 | [diff] [blame] | 232 | |
| 233 | // Remove duplicate spaces in the input file if requested. |
| 234 | if (!NoCanonicalizeWhiteSpace) |
| 235 | F = CanonicalizeInputFile(F); |
| 236 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 237 | SM.AddNewSourceBuffer(F, SMLoc()); |
| 238 | |
| 239 | // Check that we have all of the expected strings, in order, in the input |
| 240 | // file. |
| 241 | const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd(); |
| 242 | |
| 243 | for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 244 | const CheckString &CheckStr = CheckStrings[StrNo]; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 245 | |
| 246 | // Find StrNo in the file. |
Chris Lattner | 7bee327 | 2009-08-15 17:53:12 +0000 | [diff] [blame] | 247 | const char *Ptr = FindFixedStringInBuffer(CheckStr.Str, CurPtr, *F); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 248 | |
| 249 | // If we found a match, we're done, move on. |
| 250 | if (Ptr != BufferEnd) { |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 251 | CurPtr = Ptr + CheckStr.Str.size(); |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | |
| 255 | // Otherwise, we have an error, emit an error message. |
Chris Lattner | 207e1bc | 2009-08-15 17:41:04 +0000 | [diff] [blame] | 256 | SM.PrintMessage(CheckStr.Loc, "expected string not found in input", |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 257 | "error"); |
Chris Lattner | d7073db | 2009-07-11 19:21:09 +0000 | [diff] [blame] | 258 | |
Daniel Dunbar | e9e2733 | 2009-07-11 22:06:10 +0000 | [diff] [blame] | 259 | // Print the "scanning from here" line. If the current position is at the |
| 260 | // end of a line, advance to the start of the next line. |
Chris Lattner | d7073db | 2009-07-11 19:21:09 +0000 | [diff] [blame] | 261 | const char *Scan = CurPtr; |
| 262 | while (Scan != BufferEnd && |
| 263 | (*Scan == ' ' || *Scan == '\t')) |
| 264 | ++Scan; |
| 265 | if (*Scan == '\n' || *Scan == '\r') |
| 266 | CurPtr = Scan+1; |
| 267 | |
| 268 | |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 269 | SM.PrintMessage(SMLoc::getFromPointer(CurPtr), "scanning from here", |
| 270 | "note"); |
Chris Lattner | af9005d | 2009-07-09 00:19:21 +0000 | [diff] [blame] | 271 | return 1; |
Chris Lattner | 81cb8ca | 2009-07-08 18:44:05 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |