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/ADT/SmallString.h" 17 #include "llvm/Support/ErrorOr.h" 18 #include "llvm/Support/MemoryBuffer.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <cctype> 21 #include <cmath> 22 #include <cstdint> 23 #include <cstdlib> 24 #include <cstring> 25 #include <memory> 26 #include <system_error> 27 28 using namespace llvm; 29 30 static bool isSignedChar(char C) { 31 return (C == '+' || C == '-'); 32 } 33 34 static bool isExponentChar(char C) { 35 switch (C) { 36 case 'D': // Strange exponential notation. 37 case 'd': // Strange exponential notation. 38 case 'e': 39 case 'E': return true; 40 default: return false; 41 } 42 } 43 44 static bool isNumberChar(char C) { 45 switch (C) { 46 case '0': case '1': case '2': case '3': case '4': 47 case '5': case '6': case '7': case '8': case '9': 48 case '.': return true; 49 default: return isSignedChar(C) || isExponentChar(C); 50 } 51 } 52 53 static const char *BackupNumber(const char *Pos, const char *FirstChar) { 54 // If we didn't stop in the middle of a number, don't backup. 55 if (!isNumberChar(*Pos)) return Pos; 56 57 // Otherwise, return to the start of the number. 58 bool HasPeriod = false; 59 while (Pos > FirstChar && isNumberChar(Pos[-1])) { 60 // Backup over at most one period. 61 if (Pos[-1] == '.') { 62 if (HasPeriod) 63 break; 64 HasPeriod = true; 65 } 66 67 --Pos; 68 if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1])) 69 break; 70 } 71 return Pos; 72 } 73 74 /// EndOfNumber - Return the first character that is not part of the specified 75 /// number. This assumes that the buffer is null terminated, so it won't fall 76 /// off the end. 77 static const char *EndOfNumber(const char *Pos) { 78 while (isNumberChar(*Pos)) 79 ++Pos; 80 return Pos; 81 } 82 83 /// CompareNumbers - compare two numbers, returning true if they are different. 84 static bool CompareNumbers(const char *&F1P, const char *&F2P, 85 const char *F1End, const char *F2End, 86 double AbsTolerance, double RelTolerance, 87 std::string *ErrorMsg) { 88 const char *F1NumEnd, *F2NumEnd; 89 double V1 = 0.0, V2 = 0.0; 90 91 // If one of the positions is at a space and the other isn't, chomp up 'til 92 // the end of the space. 93 while (isspace(static_cast<unsigned char>(*F1P)) && F1P != F1End) 94 ++F1P; 95 while (isspace(static_cast<unsigned char>(*F2P)) && F2P != F2End) 96 ++F2P; 97 98 // If we stop on numbers, compare their difference. 99 if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) { 100 // The diff failed. 101 F1NumEnd = F1P; 102 F2NumEnd = F2P; 103 } else { 104 // Note that some ugliness is built into this to permit support for numbers 105 // that use "D" or "d" as their exponential marker, e.g. "1.234D45". This 106 // occurs in 200.sixtrack in spec2k. 107 V1 = strtod(F1P, const_cast<char**>(&F1NumEnd)); 108 V2 = strtod(F2P, const_cast<char**>(&F2NumEnd)); 109 110 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') { 111 // Copy string into tmp buffer to replace the 'D' with an 'e'. 112 SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1); 113 // Strange exponential notation! 114 StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e'; 115 116 V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd)); 117 F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]); 118 } 119 120 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') { 121 // Copy string into tmp buffer to replace the 'D' with an 'e'. 122 SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1); 123 // Strange exponential notation! 124 StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e'; 125 126 V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd)); 127 F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]); 128 } 129 } 130 131 if (F1NumEnd == F1P || F2NumEnd == F2P) { 132 if (ErrorMsg) { 133 *ErrorMsg = "FP Comparison failed, not a numeric difference between '"; 134 *ErrorMsg += F1P[0]; 135 *ErrorMsg += "' and '"; 136 *ErrorMsg += F2P[0]; 137 *ErrorMsg += "'"; 138 } 139 return true; 140 } 141 142 // Check to see if these are inside the absolute tolerance 143 if (AbsTolerance < std::abs(V1-V2)) { 144 // Nope, check the relative tolerance... 145 double Diff; 146 if (V2) 147 Diff = std::abs(V1/V2 - 1.0); 148 else if (V1) 149 Diff = std::abs(V2/V1 - 1.0); 150 else 151 Diff = 0; // Both zero. 152 if (Diff > RelTolerance) { 153 if (ErrorMsg) { 154 raw_string_ostream(*ErrorMsg) 155 << "Compared: " << V1 << " and " << V2 << '\n' 156 << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n' 157 << "Out of tolerance: rel/abs: " << RelTolerance << '/' 158 << AbsTolerance; 159 } 160 return true; 161 } 162 } 163 164 // Otherwise, advance our read pointers to the end of the numbers. 165 F1P = F1NumEnd; F2P = F2NumEnd; 166 return false; 167 } 168 169 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the 170 /// files match, 1 if they are different, and 2 if there is a file error. This 171 /// function differs from DiffFiles in that you can specify an absolete and 172 /// relative FP error that is allowed to exist. If you specify a string to fill 173 /// in for the error option, it will set the string to an error message if an 174 /// error occurs, allowing the caller to distinguish between a failed diff and a 175 /// file system error. 176 /// 177 int llvm::DiffFilesWithTolerance(StringRef NameA, 178 StringRef NameB, 179 double AbsTol, double RelTol, 180 std::string *Error) { 181 // Now its safe to mmap the files into memory because both files 182 // have a non-zero size. 183 ErrorOr<std::unique_ptr<MemoryBuffer>> F1OrErr = MemoryBuffer::getFile(NameA); 184 if (std::error_code EC = F1OrErr.getError()) { 185 if (Error) 186 *Error = EC.message(); 187 return 2; 188 } 189 MemoryBuffer &F1 = *F1OrErr.get(); 190 191 ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB); 192 if (std::error_code EC = F2OrErr.getError()) { 193 if (Error) 194 *Error = EC.message(); 195 return 2; 196 } 197 MemoryBuffer &F2 = *F2OrErr.get(); 198 199 // Okay, now that we opened the files, scan them for the first difference. 200 const char *File1Start = F1.getBufferStart(); 201 const char *File2Start = F2.getBufferStart(); 202 const char *File1End = F1.getBufferEnd(); 203 const char *File2End = F2.getBufferEnd(); 204 const char *F1P = File1Start; 205 const char *F2P = File2Start; 206 uint64_t A_size = F1.getBufferSize(); 207 uint64_t B_size = F2.getBufferSize(); 208 209 // Are the buffers identical? Common case: Handle this efficiently. 210 if (A_size == B_size && 211 std::memcmp(File1Start, File2Start, A_size) == 0) 212 return 0; 213 214 // Otherwise, we are done a tolerances are set. 215 if (AbsTol == 0 && RelTol == 0) { 216 if (Error) 217 *Error = "Files differ without tolerance allowance"; 218 return 1; // Files different! 219 } 220 221 bool CompareFailed = false; 222 while (true) { 223 // Scan for the end of file or next difference. 224 while (F1P < File1End && F2P < File2End && *F1P == *F2P) { 225 ++F1P; 226 ++F2P; 227 } 228 229 if (F1P >= File1End || F2P >= File2End) break; 230 231 // Okay, we must have found a difference. Backup to the start of the 232 // current number each stream is at so that we can compare from the 233 // beginning. 234 F1P = BackupNumber(F1P, File1Start); 235 F2P = BackupNumber(F2P, File2Start); 236 237 // Now that we are at the start of the numbers, compare them, exiting if 238 // they don't match. 239 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) { 240 CompareFailed = true; 241 break; 242 } 243 } 244 245 // Okay, we reached the end of file. If both files are at the end, we 246 // succeeded. 247 bool F1AtEnd = F1P >= File1End; 248 bool F2AtEnd = F2P >= File2End; 249 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) { 250 // Else, we might have run off the end due to a number: backup and retry. 251 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P; 252 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P; 253 F1P = BackupNumber(F1P, File1Start); 254 F2P = BackupNumber(F2P, File2Start); 255 256 // Now that we are at the start of the numbers, compare them, exiting if 257 // they don't match. 258 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) 259 CompareFailed = true; 260 261 // If we found the end, we succeeded. 262 if (F1P < File1End || F2P < File2End) 263 CompareFailed = true; 264 } 265 266 return CompareFailed; 267 } 268