Shrink Thumb2 movcc instructions.
[llvm/avr.git] / utils / FileCheck / FileCheck.cpp
blob45ce610469629a3c3a35bb0091bf6bcc0b787e9b
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.
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"
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);
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;
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;
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";
124 return true;
127 return false;
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);
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();
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;
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;
174 // Free the old buffer and return a new one.
175 MemoryBuffer *MB2 =
176 MemoryBuffer::getMemBufferCopy(NewFile.data(),
177 NewFile.data() + NewFile.size(),
178 MB->getBufferIdentifier());
180 delete MB;
181 return MB2;
185 int main(int argc, char **argv) {
186 sys::PrintStackTraceOnErrorSignal();
187 PrettyStackTraceProgram X(argc, argv);
188 cl::ParseCommandLineOptions(argc, argv);
190 SourceMgr SM;
192 // Read the expected strings from the check file.
193 std::vector<std::pair<std::string, SMLoc> > CheckStrings;
194 if (ReadCheckFile(SM, CheckStrings))
195 return 2;
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;
203 MemoryBuffer *F =
204 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
205 if (F == 0) {
206 errs() << "Could not open input file '" << InputFilename << "': "
207 << ErrorStr << '\n';
208 return true;
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
218 // file.
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();
230 continue;
233 // Otherwise, we have an error, emit an error message.
234 SM.PrintMessage(CheckStr.second, "expected string not found in input",
235 "error");
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'))
242 ++Scan;
243 if (*Scan == '\n' || *Scan == '\r')
244 CurPtr = Scan+1;
247 SM.PrintMessage(SMLoc::getFromPointer(CurPtr), "scanning from here",
248 "note");
249 return 1;
252 return 0;