X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FFileUtilities.cpp;h=6a65ccae3e40e7a3c7caf624b151e7f2f5cff2dc;hb=ef9531efedd2233269f670227fb0e6aae7480d53;hp=99db439d327a2717b4ca0566e26beb86510f0390;hpb=da7e70e058108fb444741a721ccfb38d40154e5b;p=oota-llvm.git diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp index 99db439d327..6a65ccae3e4 100644 --- a/lib/Support/FileUtilities.cpp +++ b/lib/Support/FileUtilities.cpp @@ -17,6 +17,8 @@ #include "llvm/System/MappedFile.h" #include "llvm/ADT/StringExtras.h" #include +#include +#include using namespace llvm; static bool isNumberChar(char C) { @@ -46,6 +48,14 @@ static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End, std::string *ErrorMsg) { char *F1NumEnd, *F2NumEnd; double V1 = 0.0, V2 = 0.0; + + // If one of the positions is at a space and the other isn't, chomp up 'til + // the end of the space. + while (isspace(*F1P) && F1P != F1End) + ++F1P; + while (isspace(*F2P) && F2P != F2End) + ++F2P; + // If we stop on numbers, compare their difference. if (isNumberChar(*F1P) && isNumberChar(*F2P)) { V1 = strtod(F1P, &F1NumEnd); @@ -119,7 +129,20 @@ int llvm::DiffFilesWithTolerance(const sys::Path &FileA, double AbsTol, double RelTol, std::string *Error) { try { - // Map in the files into memory. + // Check for zero length files because some systems croak when you try to + // mmap an empty file. + size_t A_size = FileA.getSize(); + size_t B_size = FileB.getSize(); + + // If they are both zero sized then they're the same + if (A_size == 0 && B_size == 0) + return 0; + // If only one of them is zero sized then they can't be the same + if ((A_size == 0 || B_size == 0)) + return 1; + + // Now its safe to mmap the files into memory becasue both files + // have a non-zero size. sys::MappedFile F1(FileA); sys::MappedFile F2(FileB); F1.map(); @@ -128,20 +151,19 @@ int llvm::DiffFilesWithTolerance(const sys::Path &FileA, // Okay, now that we opened the files, scan them for the first difference. char *File1Start = F1.charBase(); char *File2Start = F2.charBase(); - char *File1End = File1Start+F1.size(); - char *File2End = File2Start+F2.size(); + char *File1End = File1Start+A_size; + char *File2End = File2Start+B_size; char *F1P = File1Start; char *F2P = File2Start; - // Scan for the end of file or first difference. - while (F1P < File1End && F2P < File2End && *F1P == *F2P) - ++F1P, ++F2P; + if (A_size == B_size) { + // Are the buffers identical? + if (std::memcmp(File1Start, File2Start, A_size) == 0) + return 0; - // Common case: identifical files. - if (F1P == File1End && F2P == File2End) return 0; - - if (AbsTol == 0 && RelTol == 0) - return 1; // Files different! + if (AbsTol == 0 && RelTol == 0) + return 1; // Files different! + } char *OrigFile1Start = File1Start; char *OrigFile2Start = File2Start;