blob: f13c04b961e3e8d14781a29207bf99b0493a1129 [file] [log] [blame]
Chris Lattner7a6ff2b2003-08-01 21:16:14 +00001//===- Support/FileUtilities.cpp - File System Utilities ------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner7a6ff2b2003-08-01 21:16:14 +00009//
10// This file implements a family of utility functions which are useful for doing
11// various things with files.
12//
13//===----------------------------------------------------------------------===//
14
Reid Spencer551ccae2004-09-01 22:55:40 +000015#include "llvm/Support/FileUtilities.h"
Chris Lattnera7750c12008-04-01 03:39:49 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/SmallString.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/Path.h"
Rafael Espindolabbf97ea2013-06-13 20:41:00 +000020#include "llvm/Support/PathV1.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/Support/raw_ostream.h"
22#include "llvm/Support/system_error.h"
23#include <cctype>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000024#include <cstdlib>
Chris Lattner2ee51cb2005-02-15 22:12:10 +000025#include <cstring>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000028static bool isSignedChar(char C) {
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000029 return (C == '+' || C == '-');
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000030}
31
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000032static bool isExponentChar(char C) {
Chris Lattner44542532005-01-23 03:13:43 +000033 switch (C) {
Chris Lattner8dcd5482005-08-02 00:11:53 +000034 case 'D': // Strange exponential notation.
35 case 'd': // Strange exponential notation.
Chris Lattner44542532005-01-23 03:13:43 +000036 case 'e':
37 case 'E': return true;
38 default: return false;
39 }
40}
41
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000042static bool isNumberChar(char C) {
43 switch (C) {
44 case '0': case '1': case '2': case '3': case '4':
45 case '5': case '6': case '7': case '8': case '9':
46 case '.': return true;
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000047 default: return isSignedChar(C) || isExponentChar(C);
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000048 }
49}
50
Chris Lattnera7750c12008-04-01 03:39:49 +000051static const char *BackupNumber(const char *Pos, const char *FirstChar) {
Chris Lattner44542532005-01-23 03:13:43 +000052 // If we didn't stop in the middle of a number, don't backup.
53 if (!isNumberChar(*Pos)) return Pos;
54
55 // Otherwise, return to the start of the number.
Daniel Dunbard1c82fe2010-06-15 19:20:28 +000056 bool HasPeriod = false;
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000057 while (Pos > FirstChar && isNumberChar(Pos[-1])) {
Daniel Dunbard1c82fe2010-06-15 19:20:28 +000058 // Backup over at most one period.
59 if (Pos[-1] == '.') {
60 if (HasPeriod)
61 break;
62 HasPeriod = true;
63 }
64
Chris Lattner44542532005-01-23 03:13:43 +000065 --Pos;
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000066 if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000067 break;
68 }
Chris Lattner44542532005-01-23 03:13:43 +000069 return Pos;
70}
71
Chris Lattnera7750c12008-04-01 03:39:49 +000072/// EndOfNumber - Return the first character that is not part of the specified
73/// number. This assumes that the buffer is null terminated, so it won't fall
74/// off the end.
75static const char *EndOfNumber(const char *Pos) {
76 while (isNumberChar(*Pos))
77 ++Pos;
78 return Pos;
79}
80
Chris Lattner44542532005-01-23 03:13:43 +000081/// CompareNumbers - compare two numbers, returning true if they are different.
Chris Lattnera7750c12008-04-01 03:39:49 +000082static bool CompareNumbers(const char *&F1P, const char *&F2P,
83 const char *F1End, const char *F2End,
Chris Lattner44542532005-01-23 03:13:43 +000084 double AbsTolerance, double RelTolerance,
85 std::string *ErrorMsg) {
Chris Lattnera7750c12008-04-01 03:39:49 +000086 const char *F1NumEnd, *F2NumEnd;
Misha Brukmanf976c852005-04-21 22:55:34 +000087 double V1 = 0.0, V2 = 0.0;
Chris Lattnerc0d0e772005-03-17 04:49:04 +000088
89 // If one of the positions is at a space and the other isn't, chomp up 'til
90 // the end of the space.
Guy Benyei87d0b9e2013-02-12 21:21:59 +000091 while (isspace(static_cast<unsigned char>(*F1P)) && F1P != F1End)
Chris Lattnerc0d0e772005-03-17 04:49:04 +000092 ++F1P;
Guy Benyei87d0b9e2013-02-12 21:21:59 +000093 while (isspace(static_cast<unsigned char>(*F2P)) && F2P != F2End)
Chris Lattnerc0d0e772005-03-17 04:49:04 +000094 ++F2P;
95
Chris Lattnera7750c12008-04-01 03:39:49 +000096 // If we stop on numbers, compare their difference.
97 if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) {
98 // The diff failed.
Chris Lattner44542532005-01-23 03:13:43 +000099 F1NumEnd = F1P;
100 F2NumEnd = F2P;
Chris Lattnera7750c12008-04-01 03:39:49 +0000101 } else {
102 // Note that some ugliness is built into this to permit support for numbers
103 // that use "D" or "d" as their exponential marker, e.g. "1.234D45". This
104 // occurs in 200.sixtrack in spec2k.
105 V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
106 V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
107
108 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
109 // Copy string into tmp buffer to replace the 'D' with an 'e'.
110 SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000111 // Strange exponential notation!
112 StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';
Michael J. Spencer333ad3f2010-12-09 17:37:32 +0000113
Chris Lattnera7750c12008-04-01 03:39:49 +0000114 V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
115 F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
116 }
Michael J. Spencer333ad3f2010-12-09 17:37:32 +0000117
Chris Lattnera7750c12008-04-01 03:39:49 +0000118 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
119 // Copy string into tmp buffer to replace the 'D' with an 'e'.
120 SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000121 // Strange exponential notation!
122 StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';
Michael J. Spencer333ad3f2010-12-09 17:37:32 +0000123
Chris Lattnera7750c12008-04-01 03:39:49 +0000124 V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
125 F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
126 }
Chris Lattner44542532005-01-23 03:13:43 +0000127 }
128
129 if (F1NumEnd == F1P || F2NumEnd == F2P) {
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000130 if (ErrorMsg) {
131 *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
132 *ErrorMsg += F1P[0];
133 *ErrorMsg += "' and '";
134 *ErrorMsg += F2P[0];
135 *ErrorMsg += "'";
136 }
Chris Lattner44542532005-01-23 03:13:43 +0000137 return true;
138 }
139
140 // Check to see if these are inside the absolute tolerance
141 if (AbsTolerance < std::abs(V1-V2)) {
142 // Nope, check the relative tolerance...
143 double Diff;
144 if (V2)
145 Diff = std::abs(V1/V2 - 1.0);
146 else if (V1)
147 Diff = std::abs(V2/V1 - 1.0);
148 else
149 Diff = 0; // Both zero.
150 if (Diff > RelTolerance) {
151 if (ErrorMsg) {
Benjamin Kramer59088392010-01-29 14:42:22 +0000152 raw_string_ostream(*ErrorMsg)
153 << "Compared: " << V1 << " and " << V2 << '\n'
154 << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'
155 << "Out of tolerance: rel/abs: " << RelTolerance << '/'
156 << AbsTolerance;
Chris Lattner44542532005-01-23 03:13:43 +0000157 }
158 return true;
159 }
160 }
161
162 // Otherwise, advance our read pointers to the end of the numbers.
163 F1P = F1NumEnd; F2P = F2NumEnd;
164 return false;
165}
166
Chris Lattner44542532005-01-23 03:13:43 +0000167/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
168/// files match, 1 if they are different, and 2 if there is a file error. This
169/// function differs from DiffFiles in that you can specify an absolete and
170/// relative FP error that is allowed to exist. If you specify a string to fill
171/// in for the error option, it will set the string to an error message if an
172/// error occurs, allowing the caller to distinguish between a failed diff and a
173/// file system error.
174///
Rafael Espindolabbf97ea2013-06-13 20:41:00 +0000175int llvm::DiffFilesWithTolerance(StringRef NameA,
176 StringRef NameB,
Chris Lattner44542532005-01-23 03:13:43 +0000177 double AbsTol, double RelTol,
178 std::string *Error) {
Rafael Espindolabbf97ea2013-06-13 20:41:00 +0000179 sys::PathWithStatus FileA(NameA);
180 sys::PathWithStatus FileB(NameB);
181
Reid Spencer8475ec02007-03-29 19:05:44 +0000182 const sys::FileStatus *FileAStat = FileA.getFileStatus(false, Error);
183 if (!FileAStat)
Chris Lattner252ad032006-07-28 22:03:44 +0000184 return 2;
Reid Spencer8475ec02007-03-29 19:05:44 +0000185 const sys::FileStatus *FileBStat = FileB.getFileStatus(false, Error);
186 if (!FileBStat)
Reid Spencer86279692006-08-22 16:06:27 +0000187 return 2;
188
Chris Lattner252ad032006-07-28 22:03:44 +0000189 // Check for zero length files because some systems croak when you try to
190 // mmap an empty file.
Reid Spencer8475ec02007-03-29 19:05:44 +0000191 size_t A_size = FileAStat->getSize();
192 size_t B_size = FileBStat->getSize();
Chris Lattner252ad032006-07-28 22:03:44 +0000193
194 // If they are both zero sized then they're the same
195 if (A_size == 0 && B_size == 0)
196 return 0;
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000197
Chris Lattner252ad032006-07-28 22:03:44 +0000198 // If only one of them is zero sized then they can't be the same
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000199 if ((A_size == 0 || B_size == 0)) {
200 if (Error)
201 *Error = "Files differ: one is zero-sized, the other isn't";
Chris Lattner252ad032006-07-28 22:03:44 +0000202 return 1;
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000203 }
Chris Lattner252ad032006-07-28 22:03:44 +0000204
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000205 // Now its safe to mmap the files into memory because both files
Reid Spencer30493372006-08-23 20:37:59 +0000206 // have a non-zero size.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000207 OwningPtr<MemoryBuffer> F1;
208 if (error_code ec = MemoryBuffer::getFile(FileA.c_str(), F1)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000209 if (Error)
210 *Error = ec.message();
Chris Lattner44542532005-01-23 03:13:43 +0000211 return 2;
Michael J. Spencer333fb042010-12-09 17:36:48 +0000212 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000213 OwningPtr<MemoryBuffer> F2;
214 if (error_code ec = MemoryBuffer::getFile(FileB.c_str(), F2)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000215 if (Error)
216 *Error = ec.message();
217 return 2;
218 }
219
Reid Spencer30493372006-08-23 20:37:59 +0000220 // Okay, now that we opened the files, scan them for the first difference.
Chris Lattnera7750c12008-04-01 03:39:49 +0000221 const char *File1Start = F1->getBufferStart();
222 const char *File2Start = F2->getBufferStart();
223 const char *File1End = F1->getBufferEnd();
224 const char *File2End = F2->getBufferEnd();
225 const char *F1P = File1Start;
226 const char *F2P = File2Start;
Reid Spencer30493372006-08-23 20:37:59 +0000227
Daniel Dunbar71907fb2010-06-15 19:20:30 +0000228 // Are the buffers identical? Common case: Handle this efficiently.
229 if (A_size == B_size &&
230 std::memcmp(File1Start, File2Start, A_size) == 0)
231 return 0;
Reid Spencer30493372006-08-23 20:37:59 +0000232
Daniel Dunbar71907fb2010-06-15 19:20:30 +0000233 // Otherwise, we are done a tolerances are set.
234 if (AbsTol == 0 && RelTol == 0) {
235 if (Error)
236 *Error = "Files differ without tolerance allowance";
237 return 1; // Files different!
Chris Lattner44542532005-01-23 03:13:43 +0000238 }
Reid Spencer30493372006-08-23 20:37:59 +0000239
Reid Spencer30493372006-08-23 20:37:59 +0000240 bool CompareFailed = false;
241 while (1) {
242 // Scan for the end of file or next difference.
243 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
244 ++F1P, ++F2P;
245
246 if (F1P >= File1End || F2P >= File2End) break;
247
248 // Okay, we must have found a difference. Backup to the start of the
249 // current number each stream is at so that we can compare from the
250 // beginning.
251 F1P = BackupNumber(F1P, File1Start);
252 F2P = BackupNumber(F2P, File2Start);
253
254 // Now that we are at the start of the numbers, compare them, exiting if
255 // they don't match.
256 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
257 CompareFailed = true;
258 break;
259 }
260 }
261
262 // Okay, we reached the end of file. If both files are at the end, we
263 // succeeded.
264 bool F1AtEnd = F1P >= File1End;
265 bool F2AtEnd = F2P >= File2End;
266 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
267 // Else, we might have run off the end due to a number: backup and retry.
268 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
269 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
270 F1P = BackupNumber(F1P, File1Start);
271 F2P = BackupNumber(F2P, File2Start);
272
273 // Now that we are at the start of the numbers, compare them, exiting if
274 // they don't match.
275 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
276 CompareFailed = true;
277
278 // If we found the end, we succeeded.
279 if (F1P < File1End || F2P < File2End)
280 CompareFailed = true;
281 }
282
Reid Spencer30493372006-08-23 20:37:59 +0000283 return CompareFailed;
Chris Lattner44542532005-01-23 03:13:43 +0000284}