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 38 39 /// FindStringInBuffer - This is basically just a strstr wrapper that differs in 40 /// two ways: first it handles 'nul' characters in memory buffers, second, it 41 /// returns the end of the memory buffer on match failure. 42 static const char *FindStringInBuffer(const char *Str, const char *CurPtr, 43 const MemoryBuffer &MB) { 44 // Check to see if we have a match. If so, just return it. 45 if (const char *Res = strstr(CurPtr, Str)) 46 return Res; 47 48 // If not, check to make sure we didn't just find an embedded nul in the 49 // memory buffer. 50 const char *Ptr = CurPtr + strlen(CurPtr); 51 52 // If we really reached the end of the file, return it. 53 if (Ptr == MB.getBufferEnd()) 54 return Ptr; 55 56 // Otherwise, just skip this section of the file, including the nul. 57 return FindStringInBuffer(Str, Ptr+1, MB); 58 } 59 60 /// ReadCheckFile - Read the check file, which specifies the sequence of 61 /// expected strings. The strings are added to the CheckStrings vector. 62 static bool ReadCheckFile(SourceMgr &SM, 63 std::vector<std::pair<std::string, SMLoc> > 64 &CheckStrings) { 65 // Open the check file, and tell SourceMgr about it. 66 std::string ErrorStr; 67 MemoryBuffer *F = 68 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr); 69 if (F == 0) { 70 errs() << "Could not open check file '" << CheckFilename << "': " 71 << ErrorStr << '\n'; 72 return true; 73 } 74 SM.AddNewSourceBuffer(F, SMLoc()); 75 76 // Find all instances of CheckPrefix followed by : in the file. The 77 // MemoryBuffer is guaranteed to be nul terminated, but may have nul's 78 // embedded into it. We don't support check strings with embedded nuls. 79 std::string Prefix = CheckPrefix + ":"; 80 const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd(); 81 82 while (1) { 83 // See if Prefix occurs in the memory buffer. 84 const char *Ptr = FindStringInBuffer(Prefix.c_str(), CurPtr, *F); 85 86 // If we didn't find a match, we're done. 87 if (Ptr == BufferEnd) 88 break; 89 90 // Okay, we found the prefix, yay. Remember the rest of the line, but 91 // ignore leading and trailing whitespace. 92 Ptr += Prefix.size(); 93 while (*Ptr == ' ' || *Ptr == '\t') 94 ++Ptr; 95 96 // Scan ahead to the end of line. 97 CurPtr = Ptr; 98 while (CurPtr != BufferEnd && *CurPtr != '\n' && *CurPtr != '\r') 99 ++CurPtr; 100 101 // Ignore trailing whitespace. 102 while (CurPtr[-1] == ' ' || CurPtr[-1] == '\t') 103 --CurPtr; 104 105 // Check that there is something on the line. 106 if (Ptr >= CurPtr) { 107 SM.PrintMessage(SMLoc::getFromPointer(CurPtr), 108 "found empty check string with prefix '"+Prefix+"'", 109 "error"); 110 return true; 111 } 112 113 // Okay, add the string we captured to the output vector and move on. 114 CheckStrings.push_back(std::make_pair(std::string(Ptr, CurPtr), 115 SMLoc::getFromPointer(Ptr))); 116 } 117 118 if (CheckStrings.empty()) { 119 errs() << "error: no check strings found with prefix '" << Prefix << "'\n"; 120 return true; 121 } 122 123 return false; 124 } 125 126 127 int main(int argc, char **argv) { 128 sys::PrintStackTraceOnErrorSignal(); 129 PrettyStackTraceProgram X(argc, argv); 130 cl::ParseCommandLineOptions(argc, argv); 131 132 SourceMgr SM; 133 134 // Read the expected strings from the check file. 135 std::vector<std::pair<std::string, SMLoc> > CheckStrings; 136 if (ReadCheckFile(SM, CheckStrings)) 137 return 2; 138 139 // Open the file to check and add it to SourceMgr. 140 std::string ErrorStr; 141 MemoryBuffer *F = 142 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr); 143 if (F == 0) { 144 errs() << "Could not open input file '" << InputFilename << "': " 145 << ErrorStr << '\n'; 146 return true; 147 } 148 SM.AddNewSourceBuffer(F, SMLoc()); 149 150 // Check that we have all of the expected strings, in order, in the input 151 // file. 152 const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd(); 153 154 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) { 155 const std::pair<std::string, SMLoc> &CheckStr = CheckStrings[StrNo]; 156 157 // Find StrNo in the file. 158 const char *Ptr = FindStringInBuffer(CheckStr.first.c_str(), CurPtr, *F); 159 160 // If we found a match, we're done, move on. 161 if (Ptr != BufferEnd) { 162 CurPtr = Ptr + CheckStr.first.size(); 163 continue; 164 } 165 166 // Otherwise, we have an error, emit an error message. 167 SM.PrintMessage(CheckStr.second, "expected string not found in input", 168 "error"); 169 SM.PrintMessage(SMLoc::getFromPointer(CurPtr), "scanning from here", 170 "note"); 171 } 172 173 return 0; 174 } 175