FileUtilities.cpp revision 599ddf9a7693a315a03fc77cf612eec53dee5f67
1//===- Support/FileUtilities.cpp - File System Utilities ------------------===// 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// This file implements a family of utility functions which are useful for doing 11// various things with files. 12// 13//===----------------------------------------------------------------------===// 14 15#include "llvm/Support/FileUtilities.h" 16#include "llvm/System/Path.h" 17#include "llvm/System/MappedFile.h" 18#include "llvm/ADT/StringExtras.h" 19#include <cstring> 20#include <cctype> 21using namespace llvm; 22 23static bool isSignedChar(char C) { 24 if (C == '+' || C == '-') 25 return true; 26 else 27 return false; 28} 29 30static bool isExpoentChar(char C) { 31 switch (C) { 32 case 'D': // Strange exponential notation. 33 case 'd': // Strange exponential notation. 34 case 'e': 35 case 'E': return true; 36 default: return false; 37 } 38} 39 40static bool isNumberChar(char C) { 41 switch (C) { 42 case '0': case '1': case '2': case '3': case '4': 43 case '5': case '6': case '7': case '8': case '9': 44 case '.': return true; 45 default: return isSignedChar(C) || isExpoentChar(C); 46 } 47} 48 49static char *BackupNumber(char *Pos, char *FirstChar) { 50 // If we didn't stop in the middle of a number, don't backup. 51 if (!isNumberChar(*Pos)) return Pos; 52 53 // Otherwise, return to the start of the number. 54 while (Pos > FirstChar && isNumberChar(Pos[-1])) { 55 --Pos; 56 if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExpoentChar(Pos[-1])) 57 break; 58 } 59 return Pos; 60} 61 62/// CompareNumbers - compare two numbers, returning true if they are different. 63static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End, 64 double AbsTolerance, double RelTolerance, 65 std::string *ErrorMsg) { 66 char *F1NumEnd, *F2NumEnd; 67 double V1 = 0.0, V2 = 0.0; 68 69 // If one of the positions is at a space and the other isn't, chomp up 'til 70 // the end of the space. 71 while (isspace(*F1P) && F1P != F1End) 72 ++F1P; 73 while (isspace(*F2P) && F2P != F2End) 74 ++F2P; 75 76 // If we stop on numbers, compare their difference. Note that some ugliness 77 // is built into this to permit support for numbers that use "D" or "d" as 78 // their exponential marker, e.g. "1.234D45". This occurs in 200.sixtrack in 79 // spec2k. 80 if (isNumberChar(*F1P) && isNumberChar(*F2P)) { 81 bool isDNotation; 82 do { 83 isDNotation = false; 84 V1 = strtod(F1P, &F1NumEnd); 85 V2 = strtod(F2P, &F2NumEnd); 86 87 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') { 88 *F1NumEnd = 'e'; // Strange exponential notation! 89 isDNotation = true; 90 } 91 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') { 92 *F2NumEnd = 'e'; // Strange exponential notation! 93 isDNotation = true; 94 } 95 } while (isDNotation); 96 } else { 97 // Otherwise, the diff failed. 98 F1NumEnd = F1P; 99 F2NumEnd = F2P; 100 } 101 102 if (F1NumEnd == F1P || F2NumEnd == F2P) { 103 if (ErrorMsg) { 104 *ErrorMsg = "FP Comparison failed, not a numeric difference between '"; 105 *ErrorMsg += F1P[0]; 106 *ErrorMsg += "' and '"; 107 *ErrorMsg += F2P[0]; 108 *ErrorMsg += "'"; 109 } 110 return true; 111 } 112 113 // Check to see if these are inside the absolute tolerance 114 if (AbsTolerance < std::abs(V1-V2)) { 115 // Nope, check the relative tolerance... 116 double Diff; 117 if (V2) 118 Diff = std::abs(V1/V2 - 1.0); 119 else if (V1) 120 Diff = std::abs(V2/V1 - 1.0); 121 else 122 Diff = 0; // Both zero. 123 if (Diff > RelTolerance) { 124 if (ErrorMsg) { 125 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) + "\n"; 126 *ErrorMsg += "abs. diff = " + ftostr(std::abs(V1-V2)) + 127 " rel.diff = " + ftostr(Diff) + "\n"; 128 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) + 129 "/" + ftostr(AbsTolerance); 130 } 131 return true; 132 } 133 } 134 135 // Otherwise, advance our read pointers to the end of the numbers. 136 F1P = F1NumEnd; F2P = F2NumEnd; 137 return false; 138} 139 140// PadFileIfNeeded - If the files are not identical, we will have to be doing 141// numeric comparisons in here. There are bad cases involved where we (i.e., 142// strtod) might run off the beginning or end of the file if it starts or ends 143// with a number. Because of this, if needed, we pad the file so that it starts 144// and ends with a null character. 145static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) { 146 if (FileStart-FileEnd < 2 || 147 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) { 148 unsigned FileLen = FileEnd-FileStart; 149 char *NewFile = new char[FileLen+2]; 150 NewFile[0] = 0; // Add null padding 151 NewFile[FileLen+1] = 0; // Add null padding 152 memcpy(NewFile+1, FileStart, FileLen); 153 FP = NewFile+(FP-FileStart)+1; 154 FileStart = NewFile+1; 155 FileEnd = FileStart+FileLen; 156 } 157} 158 159/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the 160/// files match, 1 if they are different, and 2 if there is a file error. This 161/// function differs from DiffFiles in that you can specify an absolete and 162/// relative FP error that is allowed to exist. If you specify a string to fill 163/// in for the error option, it will set the string to an error message if an 164/// error occurs, allowing the caller to distinguish between a failed diff and a 165/// file system error. 166/// 167int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA, 168 const sys::PathWithStatus &FileB, 169 double AbsTol, double RelTol, 170 std::string *Error) { 171 const sys::FileStatus *FileAStat = FileA.getFileStatus(false, Error); 172 if (!FileAStat) 173 return 2; 174 const sys::FileStatus *FileBStat = FileB.getFileStatus(false, Error); 175 if (!FileBStat) 176 return 2; 177 178 // Check for zero length files because some systems croak when you try to 179 // mmap an empty file. 180 size_t A_size = FileAStat->getSize(); 181 size_t B_size = FileBStat->getSize(); 182 183 // If they are both zero sized then they're the same 184 if (A_size == 0 && B_size == 0) 185 return 0; 186 187 // If only one of them is zero sized then they can't be the same 188 if ((A_size == 0 || B_size == 0)) { 189 if (Error) 190 *Error = "Files differ: one is zero-sized, the other isn't"; 191 return 1; 192 } 193 194 // Now its safe to mmap the files into memory becasue both files 195 // have a non-zero size. 196 sys::MappedFile F1; 197 if (F1.open(FileA, sys::MappedFile::READ_ACCESS, Error)) 198 return 2; 199 sys::MappedFile F2; 200 if (F2.open(FileB, sys::MappedFile::READ_ACCESS, Error)) 201 return 2; 202 if (!F1.map(Error)) 203 return 2; 204 if (!F2.map(Error)) 205 return 2; 206 207 // Okay, now that we opened the files, scan them for the first difference. 208 char *File1Start = F1.charBase(); 209 char *File2Start = F2.charBase(); 210 char *File1End = File1Start+A_size; 211 char *File2End = File2Start+B_size; 212 char *F1P = File1Start; 213 char *F2P = File2Start; 214 215 if (A_size == B_size) { 216 // Are the buffers identical? 217 if (std::memcmp(File1Start, File2Start, A_size) == 0) 218 return 0; 219 220 if (AbsTol == 0 && RelTol == 0) { 221 if (Error) 222 *Error = "Files differ without tolerance allowance"; 223 return 1; // Files different! 224 } 225 } 226 227 char *OrigFile1Start = File1Start; 228 char *OrigFile2Start = File2Start; 229 230 // If the files need padding, do so now. 231 PadFileIfNeeded(File1Start, File1End, F1P); 232 PadFileIfNeeded(File2Start, File2End, F2P); 233 234 bool CompareFailed = false; 235 while (1) { 236 // Scan for the end of file or next difference. 237 while (F1P < File1End && F2P < File2End && *F1P == *F2P) 238 ++F1P, ++F2P; 239 240 if (F1P >= File1End || F2P >= File2End) break; 241 242 // Okay, we must have found a difference. Backup to the start of the 243 // current number each stream is at so that we can compare from the 244 // beginning. 245 F1P = BackupNumber(F1P, File1Start); 246 F2P = BackupNumber(F2P, File2Start); 247 248 // Now that we are at the start of the numbers, compare them, exiting if 249 // they don't match. 250 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) { 251 CompareFailed = true; 252 break; 253 } 254 } 255 256 // Okay, we reached the end of file. If both files are at the end, we 257 // succeeded. 258 bool F1AtEnd = F1P >= File1End; 259 bool F2AtEnd = F2P >= File2End; 260 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) { 261 // Else, we might have run off the end due to a number: backup and retry. 262 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P; 263 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P; 264 F1P = BackupNumber(F1P, File1Start); 265 F2P = BackupNumber(F2P, File2Start); 266 267 // Now that we are at the start of the numbers, compare them, exiting if 268 // they don't match. 269 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) 270 CompareFailed = true; 271 272 // If we found the end, we succeeded. 273 if (F1P < File1End || F2P < File2End) 274 CompareFailed = true; 275 } 276 277 if (OrigFile1Start != File1Start) 278 delete[] (File1Start-1); // Back up past null byte 279 if (OrigFile2Start != File2Start) 280 delete[] (File2Start-1); // Back up past null byte 281 return CompareFailed; 282} 283