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 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
))
52 // If not, check to make sure we didn't just find an embedded nul in the
54 const char *Ptr
= CurPtr
+ strlen(CurPtr
);
56 // If we really reached the end of the file, return it.
57 if (Ptr
== MB
.getBufferEnd())
60 // Otherwise, just skip this section of the file, including the nul.
61 return FindStringInBuffer(Str
, Ptr
+1, MB
);
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
> >
69 // Open the check file, and tell SourceMgr about it.
72 MemoryBuffer::getFileOrSTDIN(CheckFilename
.c_str(), &ErrorStr
);
74 errs() << "Could not open check file '" << CheckFilename
<< "': "
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();
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.
94 // Okay, we found the prefix, yay. Remember the rest of the line, but
95 // ignore leading and trailing whitespace.
97 while (*Ptr
== ' ' || *Ptr
== '\t')
100 // Scan ahead to the end of line.
102 while (CurPtr
!= BufferEnd
&& *CurPtr
!= '\n' && *CurPtr
!= '\r')
105 // Ignore trailing whitespace.
106 while (CurPtr
[-1] == ' ' || CurPtr
[-1] == '\t')
109 // Check that there is something on the line.
111 SM
.PrintMessage(SMLoc::getFromPointer(CurPtr
),
112 "found empty check string with prefix '"+Prefix
+"'",
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
)));
122 if (CheckStrings
.empty()) {
123 errs() << "error: no check strings found with prefix '" << Prefix
<< "'\n";
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
> >
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')
142 // Replace the character with space, then remove any other space
143 // characters after it.
146 while (C
+1 != Str
.size() &&
147 (Str
[C
+1] == ' ' || Str
[C
+1] == '\t'))
148 Str
.erase(Str
.begin()+C
+1);
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 SmallVector
<char, 16> NewFile
;
157 NewFile
.reserve(MB
->getBufferSize());
159 for (const char *Ptr
= MB
->getBufferStart(), *End
= MB
->getBufferEnd();
161 // If C is not a horizontal whitespace, skip it.
162 if (*Ptr
!= ' ' && *Ptr
!= '\t') {
163 NewFile
.push_back(*Ptr
);
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'))
174 // Free the old buffer and return a new one.
176 MemoryBuffer::getMemBufferCopy(NewFile
.data(),
177 NewFile
.data() + NewFile
.size(),
178 MB
->getBufferIdentifier());
185 int main(int argc
, char **argv
) {
186 sys::PrintStackTraceOnErrorSignal();
187 PrettyStackTraceProgram
X(argc
, argv
);
188 cl::ParseCommandLineOptions(argc
, argv
);
192 // Read the expected strings from the check file.
193 std::vector
<std::pair
<std::string
, SMLoc
> > CheckStrings
;
194 if (ReadCheckFile(SM
, CheckStrings
))
197 // Remove duplicate spaces in the check strings if requested.
198 if (!NoCanonicalizeWhiteSpace
)
199 CanonicalizeCheckStrings(CheckStrings
);
201 // Open the file to check and add it to SourceMgr.
202 std::string ErrorStr
;
204 MemoryBuffer::getFileOrSTDIN(InputFilename
.c_str(), &ErrorStr
);
206 errs() << "Could not open input file '" << InputFilename
<< "': "
211 // Remove duplicate spaces in the input file if requested.
212 if (!NoCanonicalizeWhiteSpace
)
213 F
= CanonicalizeInputFile(F
);
215 SM
.AddNewSourceBuffer(F
, SMLoc());
217 // Check that we have all of the expected strings, in order, in the input
219 const char *CurPtr
= F
->getBufferStart(), *BufferEnd
= F
->getBufferEnd();
221 for (unsigned StrNo
= 0, e
= CheckStrings
.size(); StrNo
!= e
; ++StrNo
) {
222 const std::pair
<std::string
, SMLoc
> &CheckStr
= CheckStrings
[StrNo
];
224 // Find StrNo in the file.
225 const char *Ptr
= FindStringInBuffer(CheckStr
.first
.c_str(), CurPtr
, *F
);
227 // If we found a match, we're done, move on.
228 if (Ptr
!= BufferEnd
) {
229 CurPtr
= Ptr
+ CheckStr
.first
.size();
233 // Otherwise, we have an error, emit an error message.
234 SM
.PrintMessage(CheckStr
.second
, "expected string not found in input",
237 // Print the "scanning from here" line. If the current position is at the
238 // end of a line, advance to the start of the next line.
239 const char *Scan
= CurPtr
;
240 while (Scan
!= BufferEnd
&&
241 (*Scan
== ' ' || *Scan
== '\t'))
243 if (*Scan
== '\n' || *Scan
== '\r')
247 SM
.PrintMessage(SMLoc::getFromPointer(CurPtr
), "scanning from here",