blob: cde08d188cab7c875232548817fa9490b2bcbd4d [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"
Reid Spencer9d88d1a2004-12-13 17:01:53 +000016#include "llvm/System/Path.h"
Chris Lattner44542532005-01-23 03:13:43 +000017#include "llvm/System/MappedFile.h"
18#include "llvm/ADT/StringExtras.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000019#include <cstdlib>
Chris Lattner2ee51cb2005-02-15 22:12:10 +000020#include <cstring>
Chris Lattnerc0d0e772005-03-17 04:49:04 +000021#include <cctype>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000024static bool isSignedChar(char C) {
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000025 return (C == '+' || C == '-');
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000026}
27
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000028static bool isExponentChar(char C) {
Chris Lattner44542532005-01-23 03:13:43 +000029 switch (C) {
Chris Lattner8dcd5482005-08-02 00:11:53 +000030 case 'D': // Strange exponential notation.
31 case 'd': // Strange exponential notation.
Chris Lattner44542532005-01-23 03:13:43 +000032 case 'e':
33 case 'E': return true;
34 default: return false;
35 }
36}
37
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000038static bool isNumberChar(char C) {
39 switch (C) {
40 case '0': case '1': case '2': case '3': case '4':
41 case '5': case '6': case '7': case '8': case '9':
42 case '.': return true;
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000043 default: return isSignedChar(C) || isExponentChar(C);
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000044 }
45}
46
Chris Lattner44542532005-01-23 03:13:43 +000047static char *BackupNumber(char *Pos, char *FirstChar) {
48 // If we didn't stop in the middle of a number, don't backup.
49 if (!isNumberChar(*Pos)) return Pos;
50
51 // Otherwise, return to the start of the number.
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000052 while (Pos > FirstChar && isNumberChar(Pos[-1])) {
Chris Lattner44542532005-01-23 03:13:43 +000053 --Pos;
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000054 if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000055 break;
56 }
Chris Lattner44542532005-01-23 03:13:43 +000057 return Pos;
58}
59
60/// CompareNumbers - compare two numbers, returning true if they are different.
61static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End,
62 double AbsTolerance, double RelTolerance,
63 std::string *ErrorMsg) {
64 char *F1NumEnd, *F2NumEnd;
Misha Brukmanf976c852005-04-21 22:55:34 +000065 double V1 = 0.0, V2 = 0.0;
Chris Lattnerc0d0e772005-03-17 04:49:04 +000066
67 // If one of the positions is at a space and the other isn't, chomp up 'til
68 // the end of the space.
69 while (isspace(*F1P) && F1P != F1End)
70 ++F1P;
71 while (isspace(*F2P) && F2P != F2End)
72 ++F2P;
73
Chris Lattner8dcd5482005-08-02 00:11:53 +000074 // If we stop on numbers, compare their difference. Note that some ugliness
75 // is built into this to permit support for numbers that use "D" or "d" as
76 // their exponential marker, e.g. "1.234D45". This occurs in 200.sixtrack in
77 // spec2k.
Chris Lattner44542532005-01-23 03:13:43 +000078 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
Chris Lattner8dcd5482005-08-02 00:11:53 +000079 bool isDNotation;
80 do {
81 isDNotation = false;
82 V1 = strtod(F1P, &F1NumEnd);
83 V2 = strtod(F2P, &F2NumEnd);
84
85 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
86 *F1NumEnd = 'e'; // Strange exponential notation!
87 isDNotation = true;
88 }
89 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
90 *F2NumEnd = 'e'; // Strange exponential notation!
91 isDNotation = true;
92 }
93 } while (isDNotation);
Chris Lattner44542532005-01-23 03:13:43 +000094 } else {
95 // Otherwise, the diff failed.
96 F1NumEnd = F1P;
97 F2NumEnd = F2P;
98 }
99
100 if (F1NumEnd == F1P || F2NumEnd == F2P) {
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000101 if (ErrorMsg) {
102 *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
103 *ErrorMsg += F1P[0];
104 *ErrorMsg += "' and '";
105 *ErrorMsg += F2P[0];
106 *ErrorMsg += "'";
107 }
Chris Lattner44542532005-01-23 03:13:43 +0000108 return true;
109 }
110
111 // Check to see if these are inside the absolute tolerance
112 if (AbsTolerance < std::abs(V1-V2)) {
113 // Nope, check the relative tolerance...
114 double Diff;
115 if (V2)
116 Diff = std::abs(V1/V2 - 1.0);
117 else if (V1)
118 Diff = std::abs(V2/V1 - 1.0);
119 else
120 Diff = 0; // Both zero.
121 if (Diff > RelTolerance) {
122 if (ErrorMsg) {
Reid Spencer9df12462006-11-25 08:38:44 +0000123 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) + "\n";
124 *ErrorMsg += "abs. diff = " + ftostr(std::abs(V1-V2)) +
125 " rel.diff = " + ftostr(Diff) + "\n";
Chris Lattner44542532005-01-23 03:13:43 +0000126 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) +
127 "/" + ftostr(AbsTolerance);
128 }
129 return true;
130 }
131 }
132
133 // Otherwise, advance our read pointers to the end of the numbers.
134 F1P = F1NumEnd; F2P = F2NumEnd;
135 return false;
136}
137
138// PadFileIfNeeded - If the files are not identical, we will have to be doing
139// numeric comparisons in here. There are bad cases involved where we (i.e.,
140// strtod) might run off the beginning or end of the file if it starts or ends
141// with a number. Because of this, if needed, we pad the file so that it starts
142// and ends with a null character.
143static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
Chris Lattnerda7e70e2005-01-23 06:02:40 +0000144 if (FileStart-FileEnd < 2 ||
145 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
Chris Lattner44542532005-01-23 03:13:43 +0000146 unsigned FileLen = FileEnd-FileStart;
147 char *NewFile = new char[FileLen+2];
148 NewFile[0] = 0; // Add null padding
149 NewFile[FileLen+1] = 0; // Add null padding
150 memcpy(NewFile+1, FileStart, FileLen);
151 FP = NewFile+(FP-FileStart)+1;
152 FileStart = NewFile+1;
153 FileEnd = FileStart+FileLen;
154 }
155}
156
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 Spencerc74b4612007-04-07 18:53:16 +0000165int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
166 const sys::PathWithStatus &FileB,
Chris Lattner44542532005-01-23 03:13:43 +0000167 double AbsTol, double RelTol,
168 std::string *Error) {
Reid Spencer8475ec02007-03-29 19:05:44 +0000169 const sys::FileStatus *FileAStat = FileA.getFileStatus(false, Error);
170 if (!FileAStat)
Chris Lattner252ad032006-07-28 22:03:44 +0000171 return 2;
Reid Spencer8475ec02007-03-29 19:05:44 +0000172 const sys::FileStatus *FileBStat = FileB.getFileStatus(false, Error);
173 if (!FileBStat)
Reid Spencer86279692006-08-22 16:06:27 +0000174 return 2;
175
Chris Lattner252ad032006-07-28 22:03:44 +0000176 // Check for zero length files because some systems croak when you try to
177 // mmap an empty file.
Reid Spencer8475ec02007-03-29 19:05:44 +0000178 size_t A_size = FileAStat->getSize();
179 size_t B_size = FileBStat->getSize();
Chris Lattner252ad032006-07-28 22:03:44 +0000180
181 // If they are both zero sized then they're the same
182 if (A_size == 0 && B_size == 0)
183 return 0;
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000184
Chris Lattner252ad032006-07-28 22:03:44 +0000185 // If only one of them is zero sized then they can't be the same
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000186 if ((A_size == 0 || B_size == 0)) {
187 if (Error)
188 *Error = "Files differ: one is zero-sized, the other isn't";
Chris Lattner252ad032006-07-28 22:03:44 +0000189 return 1;
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000190 }
Chris Lattner252ad032006-07-28 22:03:44 +0000191
Reid Spencer30493372006-08-23 20:37:59 +0000192 // Now its safe to mmap the files into memory becasue both files
193 // have a non-zero size.
194 sys::MappedFile F1;
195 if (F1.open(FileA, sys::MappedFile::READ_ACCESS, Error))
Chris Lattner44542532005-01-23 03:13:43 +0000196 return 2;
Reid Spencer30493372006-08-23 20:37:59 +0000197 sys::MappedFile F2;
198 if (F2.open(FileB, sys::MappedFile::READ_ACCESS, Error))
Reid Spencerc64f1482006-05-15 22:12:42 +0000199 return 2;
Reid Spencer30493372006-08-23 20:37:59 +0000200 if (!F1.map(Error))
201 return 2;
202 if (!F2.map(Error))
203 return 2;
204
205 // Okay, now that we opened the files, scan them for the first difference.
206 char *File1Start = F1.charBase();
207 char *File2Start = F2.charBase();
208 char *File1End = File1Start+A_size;
209 char *File2End = File2Start+B_size;
210 char *F1P = File1Start;
211 char *F2P = File2Start;
212
213 if (A_size == B_size) {
214 // Are the buffers identical?
215 if (std::memcmp(File1Start, File2Start, A_size) == 0)
216 return 0;
217
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000218 if (AbsTol == 0 && RelTol == 0) {
219 if (Error)
220 *Error = "Files differ without tolerance allowance";
Reid Spencer30493372006-08-23 20:37:59 +0000221 return 1; // Files different!
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000222 }
Chris Lattner44542532005-01-23 03:13:43 +0000223 }
Reid Spencer30493372006-08-23 20:37:59 +0000224
225 char *OrigFile1Start = File1Start;
226 char *OrigFile2Start = File2Start;
227
228 // If the files need padding, do so now.
229 PadFileIfNeeded(File1Start, File1End, F1P);
230 PadFileIfNeeded(File2Start, File2End, F2P);
231
232 bool CompareFailed = false;
233 while (1) {
234 // Scan for the end of file or next difference.
235 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
236 ++F1P, ++F2P;
237
238 if (F1P >= File1End || F2P >= File2End) break;
239
240 // Okay, we must have found a difference. Backup to the start of the
241 // current number each stream is at so that we can compare from the
242 // beginning.
243 F1P = BackupNumber(F1P, File1Start);
244 F2P = BackupNumber(F2P, File2Start);
245
246 // Now that we are at the start of the numbers, compare them, exiting if
247 // they don't match.
248 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
249 CompareFailed = true;
250 break;
251 }
252 }
253
254 // Okay, we reached the end of file. If both files are at the end, we
255 // succeeded.
256 bool F1AtEnd = F1P >= File1End;
257 bool F2AtEnd = F2P >= File2End;
258 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
259 // Else, we might have run off the end due to a number: backup and retry.
260 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
261 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
262 F1P = BackupNumber(F1P, File1Start);
263 F2P = BackupNumber(F2P, File2Start);
264
265 // Now that we are at the start of the numbers, compare them, exiting if
266 // they don't match.
267 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
268 CompareFailed = true;
269
270 // If we found the end, we succeeded.
271 if (F1P < File1End || F2P < File2End)
272 CompareFailed = true;
273 }
274
275 if (OrigFile1Start != File1Start)
276 delete[] (File1Start-1); // Back up past null byte
277 if (OrigFile2Start != File2Start)
278 delete[] (File2Start-1); // Back up past null byte
279 return CompareFailed;
Chris Lattner44542532005-01-23 03:13:43 +0000280}