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