1 //===- FileCheck.cpp - Check that File's Contents match what is expected --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
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"
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')"));
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))
48 // If not, check to make sure we didn't just find an embedded nul in the
50 const char *Ptr = CurPtr + strlen(CurPtr);
52 // If we really reached the end of the file, return it.
53 if (Ptr == MB.getBufferEnd())
56 // Otherwise, just skip this section of the file, including the nul.
57 return FindStringInBuffer(Str, Ptr+1, MB);
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> >
65 // Open the check file, and tell SourceMgr about it.
68 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
70 errs() << "Could not open check file '" << CheckFilename << "': "
74 SM.AddNewSourceBuffer(F, SMLoc());
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();
83 // See if Prefix occurs in the memory buffer.
84 const char *Ptr = FindStringInBuffer(Prefix.c_str(), CurPtr, *F);
86 // If we didn't find a match, we're done.
90 // Okay, we found the prefix, yay. Remember the rest of the line, but
91 // ignore leading and trailing whitespace.
93 while (*Ptr == ' ' || *Ptr == '\t')
96 // Scan ahead to the end of line.
98 while (CurPtr != BufferEnd && *CurPtr != '\n' && *CurPtr != '\r')
101 // Ignore trailing whitespace.
102 while (CurPtr[-1] == ' ' || CurPtr[-1] == '\t')
105 // Check that there is something on the line.
107 SM.PrintMessage(SMLoc::getFromPointer(CurPtr),
108 "found empty check string with prefix '"+Prefix+"'",
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)));
118 if (CheckStrings.empty()) {
119 errs() << "error: no check strings found with prefix '" << Prefix << "'\n";
127 int main(int argc, char **argv) {
128 sys::PrintStackTraceOnErrorSignal();
129 PrettyStackTraceProgram X(argc, argv);
130 cl::ParseCommandLineOptions(argc, argv);
134 // Read the expected strings from the check file.
135 std::vector<std::pair<std::string, SMLoc> > CheckStrings;
136 if (ReadCheckFile(SM, CheckStrings))
139 // Open the file to check and add it to SourceMgr.
140 std::string ErrorStr;
142 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
144 errs() << "Could not open input file '" << InputFilename << "': "
148 SM.AddNewSourceBuffer(F, SMLoc());
150 // Check that we have all of the expected strings, in order, in the input
152 const char *CurPtr = F->getBufferStart(), *BufferEnd = F->getBufferEnd();
154 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
155 const std::pair<std::string, SMLoc> &CheckStr = CheckStrings[StrNo];
157 // Find StrNo in the file.
158 const char *Ptr = FindStringInBuffer(CheckStr.first.c_str(), CurPtr, *F);
160 // If we found a match, we're done, move on.
161 if (Ptr != BufferEnd) {
162 CurPtr = Ptr + CheckStr.first.size();
166 // Otherwise, we have an error, emit an error message.
167 SM.PrintMessage(CheckStr.second, "expected string not found in input",
169 SM.PrintMessage(SMLoc::getFromPointer(CurPtr), "scanning from here",