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 //===----------------------------------------------------------------------===//
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;
27 static cl::opt<std::string>
28 CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
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"));
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')"));
38 static cl::opt<bool>
39 NoCanonicalizeWhiteSpace("strict-whitespace",
40 cl::desc("Do not treat all horizontal whitespace as equivalent"));
43 /// FindStringInBuffer - This is basically just a strstr wrapper that differs in
44 /// two ways: first it handles 'nul' characters in memory buffers, second, it
45 /// returns the end of the memory buffer on match failure.
46 static const char *FindStringInBuffer(const char *Str, const char *CurPtr,
47 const MemoryBuffer &MB) {
48 // Check to see if we have a match. If so, just return it.
49 if (const char *Res = strstr(CurPtr, Str))
50 return Res;
52 // If not, check to make sure we didn't just find an embedded nul in the
53 // memory buffer.
54 const char *Ptr = CurPtr + strlen(CurPtr);
56 // If we really reached the end of the file, return it.
57 if (Ptr == MB.getBufferEnd())
58 return Ptr;
60 // Otherwise, just skip this section of the file, including the nul.
61 return FindStringInBuffer(Str, Ptr+1, MB);
62 }
64 /// ReadCheckFile - Read the check file, which specifies the sequence of
65 /// expected strings. The strings are added to the CheckStrings vector.
66 static bool ReadCheckFile(SourceMgr &SM,
67 std::vector<std::pair<std::string, SMLoc> >
68 &CheckStrings) {
69 // Open the check file, and tell SourceMgr about it.
70 std::string ErrorStr;
71 MemoryBuffer *F =
72 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
73 if (F == 0) {
74 errs() << "Could not open check file '" << CheckFilename << "': "
75 << ErrorStr << '\n';
76 return true;
77 }
78 SM.AddNewSourceBuffer(F, SMLoc());
80 // Find all instances of CheckPrefix followed by : in the file. The
81 // MemoryBuffer is guaranteed to be nul terminated, but may have nul's
82 // embedded into it. We don't support check strings with embedded nuls.
83 std::string Prefix = CheckPrefix + ":";
84 const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd();
86 while (1) {
87 // See if Prefix occurs in the memory buffer.
88 const char *Ptr = FindStringInBuffer(Prefix.c_str(), CurPtr, *F);
90 // If we didn't find a match, we're done.
91 if (Ptr == BufferEnd)
92 break;
94 // Okay, we found the prefix, yay. Remember the rest of the line, but
95 // ignore leading and trailing whitespace.
96 Ptr += Prefix.size();
97 while (*Ptr == ' ' || *Ptr == '\t')
98 ++Ptr;
100 // Scan ahead to the end of line.
101 CurPtr = Ptr;
102 while (CurPtr != BufferEnd && *CurPtr != '\n' && *CurPtr != '\r')
103 ++CurPtr;
105 // Ignore trailing whitespace.
106 while (CurPtr[-1] == ' ' || CurPtr[-1] == '\t')
107 --CurPtr;
109 // Check that there is something on the line.
110 if (Ptr >= CurPtr) {
111 SM.PrintMessage(SMLoc::getFromPointer(CurPtr),
112 "found empty check string with prefix '"+Prefix+"'",
113 "error");
114 return true;
115 }
117 // Okay, add the string we captured to the output vector and move on.
118 CheckStrings.push_back(std::make_pair(std::string(Ptr, CurPtr),
119 SMLoc::getFromPointer(Ptr)));
120 }
122 if (CheckStrings.empty()) {
123 errs() << "error: no check strings found with prefix '" << Prefix << "'\n";
124 return true;
125 }
127 return false;
128 }
130 // CanonicalizeCheckStrings - Replace all sequences of horizontal whitespace in
131 // the check strings with a single space.
132 static void CanonicalizeCheckStrings(std::vector<std::pair<std::string, SMLoc> >
133 &CheckStrings) {
134 for (unsigned i = 0, e = CheckStrings.size(); i != e; ++i) {
135 std::string &Str = CheckStrings[i].first;
137 for (unsigned C = 0; C != Str.size(); ++C) {
138 // If C is not a horizontal whitespace, skip it.
139 if (Str[C] != ' ' && Str[C] != '\t')
140 continue;
142 // Replace the character with space, then remove any other space
143 // characters after it.
144 Str[C] = ' ';
146 while (C+1 != Str.size() &&
147 (Str[C+1] == ' ' || Str[C+1] == '\t'))
148 Str.erase(Str.begin()+C+1);
149 }
150 }
151 }
153 /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
154 /// memory buffer, free it, and return a new one.
155 static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
156 std::vector<char> NewFile;
157 NewFile.reserve(MB->getBufferSize());
159 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
160 Ptr != End; ++Ptr) {
161 // If C is not a horizontal whitespace, skip it.
162 if (*Ptr != ' ' && *Ptr != '\t') {
163 NewFile.push_back(*Ptr);
164 continue;
165 }
167 // Otherwise, add one space and advance over neighboring space.
168 NewFile.push_back(' ');
169 while (Ptr+1 != End &&
170 (Ptr[1] == ' ' || Ptr[1] == '\t'))
171 ++Ptr;
172 }
174 // Free the old buffer and return a new one.
175 MemoryBuffer *MB2 =
176 MemoryBuffer::getMemBufferCopy(&NewFile[0], &NewFile[0]+NewFile.size(),
177 MB->getBufferIdentifier());
179 delete MB;
180 return MB2;
181 }
184 int main(int argc, char **argv) {
185 sys::PrintStackTraceOnErrorSignal();
186 PrettyStackTraceProgram X(argc, argv);
187 cl::ParseCommandLineOptions(argc, argv);
189 SourceMgr SM;
191 // Read the expected strings from the check file.
192 std::vector<std::pair<std::string, SMLoc> > CheckStrings;
193 if (ReadCheckFile(SM, CheckStrings))
194 return 2;
196 // Remove duplicate spaces in the check strings if requested.
197 if (!NoCanonicalizeWhiteSpace)
198 CanonicalizeCheckStrings(CheckStrings);
200 // Open the file to check and add it to SourceMgr.
201 std::string ErrorStr;
202 MemoryBuffer *F =
203 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
204 if (F == 0) {
205 errs() << "Could not open input file '" << InputFilename << "': "
206 << ErrorStr << '\n';
207 return true;
208 }
210 // Remove duplicate spaces in the input file if requested.
211 if (!NoCanonicalizeWhiteSpace)
212 F = CanonicalizeInputFile(F);
214 SM.AddNewSourceBuffer(F, SMLoc());
216 // Check that we have all of the expected strings, in order, in the input
217 // file.
218 const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd();
220 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
221 const std::pair<std::string, SMLoc> &CheckStr = CheckStrings[StrNo];
223 // Find StrNo in the file.
224 const char *Ptr = FindStringInBuffer(CheckStr.first.c_str(), CurPtr, *F);
226 // If we found a match, we're done, move on.
227 if (Ptr != BufferEnd) {
228 CurPtr = Ptr + CheckStr.first.size();
229 continue;
230 }
232 // Otherwise, we have an error, emit an error message.
233 SM.PrintMessage(CheckStr.second, "expected string not found in input",
234 "error");
236 // Print the scanning from here line. If the current position is at the end
237 // of a line, advance to the start of the next line.
238 const char *Scan = CurPtr;
239 while (Scan != BufferEnd &&
240 (*Scan == ' ' || *Scan == '\t'))
241 ++Scan;
242 if (*Scan == '\n' || *Scan == '\r')
243 CurPtr = Scan+1;
246 SM.PrintMessage(SMLoc::getFromPointer(CurPtr), "scanning from here",
247 "note");
248 return 1;
249 }
251 return 0;
252 }