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