From adea46ed617982ea07fc3266d52717496c0076ce Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 24 Sep 2009 20:45:07 +0000 Subject: Use CanonicalizeInputFile to canonicalize the entire buffer containing the CHECK strings, instead of canonicalizing the patterns directly. This allows Pattern to just contain a StringRef instead of std::string. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82713 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/FileCheck/FileCheck.cpp | 97 ++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 57 deletions(-) (limited to 'utils/FileCheck') diff --git a/utils/FileCheck/FileCheck.cpp b/utils/FileCheck/FileCheck.cpp index cab37fb..cd62870 100644 --- a/utils/FileCheck/FileCheck.cpp +++ b/utils/FileCheck/FileCheck.cpp @@ -45,7 +45,7 @@ NoCanonicalizeWhiteSpace("strict-whitespace", class Pattern { /// Str - The string to match. - std::string Str; + StringRef Str; public: Pattern() { } @@ -59,25 +59,6 @@ public: MatchLen = Str.size(); return Buffer.find(Str); } - -private: - /// CanonicalizeCheckString - Replace all sequences of horizontal whitespace - /// in the check strings with a single space. - void CanonicalizeCheckString() { - 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); - } - } }; bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) { @@ -93,13 +74,10 @@ bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) { "error"); return true; } + - Str = PatternStr.str(); - - // Remove duplicate spaces in the check strings if requested. - if (!NoCanonicalizeWhiteSpace) - CanonicalizeCheckString(); + Str = PatternStr; return false; } @@ -129,6 +107,37 @@ struct CheckString { : Pat(P), Loc(L), IsCheckNext(isCheckNext) {} }; +/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified +/// memory buffer, free it, and return a new one. +static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) { + SmallVector 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.data(), + NewFile.data() + NewFile.size(), + MB->getBufferIdentifier()); + + delete MB; + return MB2; +} + /// ReadCheckFile - Read the check file, which specifies the sequence of /// expected strings. The strings are added to the CheckStrings vector. @@ -143,6 +152,12 @@ static bool ReadCheckFile(SourceMgr &SM, << ErrorStr << '\n'; return true; } + + // If we want to canonicalize whitespace, strip excess whitespace from the + // buffer containing the CHECK lines. + if (!NoCanonicalizeWhiteSpace) + F = CanonicalizeInputFile(F); + SM.AddNewSourceBuffer(F, SMLoc()); // Find all instances of CheckPrefix followed by : in the file. @@ -233,38 +248,6 @@ static bool ReadCheckFile(SourceMgr &SM, return false; } -/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified -/// memory buffer, free it, and return a new one. -static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) { - SmallVector 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.data(), - NewFile.data() + NewFile.size(), - MB->getBufferIdentifier()); - - delete MB; - return MB2; -} - - static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr, StringRef Buffer) { // Otherwise, we have an error, emit an error message. -- cgit v1.1