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