summaryrefslogtreecommitdiff
path: root/utils/FileCheck
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-07-11 18:58:15 +0000
committerChris Lattner <sabre@nondot.org>2009-07-11 18:58:15 +0000
commit88a7e9ee8dd2f1d2f40cdb9c378f050e45161783 (patch)
tree9aa960b357777ca73eb67bbd3e6eefba5d53d47c /utils/FileCheck
parent44b3e8d79674a0cd5326e60dbe0c7e0d13b8f8f5 (diff)
downloadllvm-88a7e9ee8dd2f1d2f40cdb9c378f050e45161783.tar.gz
llvm-88a7e9ee8dd2f1d2f40cdb9c378f050e45161783.tar.bz2
llvm-88a7e9ee8dd2f1d2f40cdb9c378f050e45161783.tar.xz
make filecheck default to canonicalizing horizontal whitespace
away. This way you can write a space and it matches arbitrary spaces and tabs. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75370 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'utils/FileCheck')
-rw-r--r--utils/FileCheck/FileCheck.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/utils/FileCheck/FileCheck.cpp b/utils/FileCheck/FileCheck.cpp
index da14780acf..8c46f4b4ed 100644
--- a/utils/FileCheck/FileCheck.cpp
+++ b/utils/FileCheck/FileCheck.cpp
@@ -35,6 +35,10 @@ static cl::opt<std::string>
CheckPrefix("check-prefix", cl::init("CHECK"),
cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
+static cl::opt<bool>
+NoCanonicalizeWhiteSpace("strict-whitespace",
+ cl::desc("Do not treat all horizontal whitespace as equivalent"));
+
/// FindStringInBuffer - This is basically just a strstr wrapper that differs in
/// two ways: first it handles 'nul' characters in memory buffers, second, it
@@ -123,6 +127,59 @@ static bool ReadCheckFile(SourceMgr &SM,
return false;
}
+// CanonicalizeCheckStrings - Replace all sequences of horizontal whitespace in
+// the check strings with a single space.
+static void CanonicalizeCheckStrings(std::vector<std::pair<std::string, SMLoc> >
+ &CheckStrings) {
+ for (unsigned i = 0, e = CheckStrings.size(); i != e; ++i) {
+ std::string &Str = CheckStrings[i].first;
+
+ for (unsigned C = 0; C != Str.size(); ++C) {
+ // If C is not a horizontal whitespace, skip it.
+ if (Str[C] != ' ' && Str[C] != '\t')
+ continue;
+
+ // Replace the character with space, then remove any other space
+ // characters after it.
+ Str[C] = ' ';
+
+ while (C+1 != Str.size() &&
+ (Str[C+1] == ' ' || Str[C+1] == '\t'))
+ Str.erase(Str.begin()+C+1);
+ }
+ }
+}
+
+/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
+/// memory buffer, free it, and return a new one.
+static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
+ std::vector<char> NewFile;
+ NewFile.reserve(MB->getBufferSize());
+
+ for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
+ Ptr != End; ++Ptr) {
+ // If C is not a horizontal whitespace, skip it.
+ if (*Ptr != ' ' && *Ptr != '\t') {
+ NewFile.push_back(*Ptr);
+ continue;
+ }
+
+ // Otherwise, add one space and advance over neighboring space.
+ NewFile.push_back(' ');
+ while (Ptr+1 != End &&
+ (Ptr[1] == ' ' || Ptr[1] == '\t'))
+ ++Ptr;
+ }
+
+ // Free the old buffer and return a new one.
+ MemoryBuffer *MB2 =
+ MemoryBuffer::getMemBufferCopy(&NewFile[0], &NewFile[0]+NewFile.size(),
+ MB->getBufferIdentifier());
+
+ delete MB;
+ return MB2;
+}
+
int main(int argc, char **argv) {
sys::PrintStackTraceOnErrorSignal();
@@ -136,6 +193,10 @@ int main(int argc, char **argv) {
if (ReadCheckFile(SM, CheckStrings))
return 2;
+ // Remove duplicate spaces in the check strings if requested.
+ if (!NoCanonicalizeWhiteSpace)
+ CanonicalizeCheckStrings(CheckStrings);
+
// Open the file to check and add it to SourceMgr.
std::string ErrorStr;
MemoryBuffer *F =
@@ -145,6 +206,11 @@ int main(int argc, char **argv) {
<< ErrorStr << '\n';
return true;
}
+
+ // Remove duplicate spaces in the input file if requested.
+ if (!NoCanonicalizeWhiteSpace)
+ F = CanonicalizeInputFile(F);
+
SM.AddNewSourceBuffer(F, SMLoc());
// Check that we have all of the expected strings, in order, in the input