move some 32-bit instrs to x86instrinfo.td
[oota-llvm.git] / utils / FileCheck / FileCheck.cpp
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 static cl::opt<bool>
39 NoCanonicalizeWhiteSpace("strict-whitespace",
40               cl::desc("Do not treat all horizontal whitespace as equivalent"));
41
42
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;
51   
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);
55   
56   // If we really reached the end of the file, return it.
57   if (Ptr == MB.getBufferEnd())
58     return Ptr;
59     
60   // Otherwise, just skip this section of the file, including the nul.
61   return FindStringInBuffer(Str, Ptr+1, MB);
62 }
63
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());
79
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();
85
86   while (1) {
87     // See if Prefix occurs in the memory buffer.
88     const char *Ptr = FindStringInBuffer(Prefix.c_str(), CurPtr, *F);
89     
90     // If we didn't find a match, we're done.
91     if (Ptr == BufferEnd)
92       break;
93     
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;
99     
100     // Scan ahead to the end of line.
101     CurPtr = Ptr;
102     while (CurPtr != BufferEnd && *CurPtr != '\n' && *CurPtr != '\r')
103       ++CurPtr;
104     
105     // Ignore trailing whitespace.
106     while (CurPtr[-1] == ' ' || CurPtr[-1] == '\t')
107       --CurPtr;
108     
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     }
116     
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   }
121   
122   if (CheckStrings.empty()) {
123     errs() << "error: no check strings found with prefix '" << Prefix << "'\n";
124     return true;
125   }
126   
127   return false;
128 }
129
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;
136     
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;
141       
142       // Replace the character with space, then remove any other space
143       // characters after it.
144       Str[C] = ' ';
145       
146       while (C+1 != Str.size() &&
147              (Str[C+1] == ' ' || Str[C+1] == '\t'))
148         Str.erase(Str.begin()+C+1);
149     }
150   }
151 }
152
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());
158   
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     }
166     
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   }
173   
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());
179
180   delete MB;
181   return MB2;
182 }
183
184
185 int main(int argc, char **argv) {
186   sys::PrintStackTraceOnErrorSignal();
187   PrettyStackTraceProgram X(argc, argv);
188   cl::ParseCommandLineOptions(argc, argv);
189
190   SourceMgr SM;
191   
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;
196
197   // Remove duplicate spaces in the check strings if requested.
198   if (!NoCanonicalizeWhiteSpace)
199     CanonicalizeCheckStrings(CheckStrings);
200
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;
209   }
210   
211   // Remove duplicate spaces in the input file if requested.
212   if (!NoCanonicalizeWhiteSpace)
213     F = CanonicalizeInputFile(F);
214   
215   SM.AddNewSourceBuffer(F, SMLoc());
216   
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();
220   
221   for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
222     const std::pair<std::string, SMLoc> &CheckStr = CheckStrings[StrNo];
223     
224     // Find StrNo in the file.
225     const char *Ptr = FindStringInBuffer(CheckStr.first.c_str(), CurPtr, *F);
226     
227     // If we found a match, we're done, move on.
228     if (Ptr != BufferEnd) {
229       CurPtr = Ptr + CheckStr.first.size();
230       continue;
231     }
232     
233     // Otherwise, we have an error, emit an error message.
234     SM.PrintMessage(CheckStr.second, "expected string not found in input",
235                     "error");
236     
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;
245     
246     
247     SM.PrintMessage(SMLoc::getFromPointer(CurPtr), "scanning from here",
248                     "note");
249     return 1;
250   }
251   
252   return 0;
253 }