blob: 42083e46bd2f74341ba185bbbcdd1cd3ea72ec30 [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"
Chris Lattner2ee51cb2005-02-15 22:12:10 +000019#include <cstring>
Chris Lattnerc0d0e772005-03-17 04:49:04 +000020#include <cctype>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000023static bool isSignedChar(char C) {
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000024 return (C == '+' || C == '-');
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000025}
26
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000027static bool isExponentChar(char C) {
Chris Lattner44542532005-01-23 03:13:43 +000028 switch (C) {
Chris Lattner8dcd5482005-08-02 00:11:53 +000029 case 'D': // Strange exponential notation.
30 case 'd': // Strange exponential notation.
Chris Lattner44542532005-01-23 03:13:43 +000031 case 'e':
32 case 'E': return true;
33 default: return false;
34 }
35}
36
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000037static bool isNumberChar(char C) {
38 switch (C) {
39 case '0': case '1': case '2': case '3': case '4':
40 case '5': case '6': case '7': case '8': case '9':
41 case '.': return true;
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000042 default: return isSignedChar(C) || isExponentChar(C);
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000043 }
44}
45
Chris Lattner44542532005-01-23 03:13:43 +000046static char *BackupNumber(char *Pos, char *FirstChar) {
47 // If we didn't stop in the middle of a number, don't backup.
48 if (!isNumberChar(*Pos)) return Pos;
49
50 // Otherwise, return to the start of the number.
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000051 while (Pos > FirstChar && isNumberChar(Pos[-1])) {
Chris Lattner44542532005-01-23 03:13:43 +000052 --Pos;
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000053 if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000054 break;
55 }
Chris Lattner44542532005-01-23 03:13:43 +000056 return Pos;
57}
58
59/// CompareNumbers - compare two numbers, returning true if they are different.
60static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End,
61 double AbsTolerance, double RelTolerance,
62 std::string *ErrorMsg) {
63 char *F1NumEnd, *F2NumEnd;
Misha Brukmanf976c852005-04-21 22:55:34 +000064 double V1 = 0.0, V2 = 0.0;
Chris Lattnerc0d0e772005-03-17 04:49:04 +000065
66 // If one of the positions is at a space and the other isn't, chomp up 'til
67 // the end of the space.
68 while (isspace(*F1P) && F1P != F1End)
69 ++F1P;
70 while (isspace(*F2P) && F2P != F2End)
71 ++F2P;
72
Chris Lattner8dcd5482005-08-02 00:11:53 +000073 // If we stop on numbers, compare their difference. Note that some ugliness
74 // is built into this to permit support for numbers that use "D" or "d" as
75 // their exponential marker, e.g. "1.234D45". This occurs in 200.sixtrack in
76 // spec2k.
Chris Lattner44542532005-01-23 03:13:43 +000077 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
Chris Lattner8dcd5482005-08-02 00:11:53 +000078 bool isDNotation;
79 do {
80 isDNotation = false;
81 V1 = strtod(F1P, &F1NumEnd);
82 V2 = strtod(F2P, &F2NumEnd);
83
84 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
85 *F1NumEnd = 'e'; // Strange exponential notation!
86 isDNotation = true;
87 }
88 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
89 *F2NumEnd = 'e'; // Strange exponential notation!
90 isDNotation = true;
91 }
92 } while (isDNotation);
Chris Lattner44542532005-01-23 03:13:43 +000093 } else {
94 // Otherwise, the diff failed.
95 F1NumEnd = F1P;
96 F2NumEnd = F2P;
97 }
98
99 if (F1NumEnd == F1P || F2NumEnd == F2P) {
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000100 if (ErrorMsg) {
101 *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
102 *ErrorMsg += F1P[0];
103 *ErrorMsg += "' and '";
104 *ErrorMsg += F2P[0];
105 *ErrorMsg += "'";
106 }
Chris Lattner44542532005-01-23 03:13:43 +0000107 return true;
108 }
109
110 // Check to see if these are inside the absolute tolerance
111 if (AbsTolerance < std::abs(V1-V2)) {
112 // Nope, check the relative tolerance...
113 double Diff;
114 if (V2)
115 Diff = std::abs(V1/V2 - 1.0);
116 else if (V1)
117 Diff = std::abs(V2/V1 - 1.0);
118 else
119 Diff = 0; // Both zero.
120 if (Diff > RelTolerance) {
121 if (ErrorMsg) {
Reid Spencer9df12462006-11-25 08:38:44 +0000122 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) + "\n";
123 *ErrorMsg += "abs. diff = " + ftostr(std::abs(V1-V2)) +
124 " rel.diff = " + ftostr(Diff) + "\n";
Chris Lattner44542532005-01-23 03:13:43 +0000125 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) +
126 "/" + ftostr(AbsTolerance);
127 }
128 return true;
129 }
130 }
131
132 // Otherwise, advance our read pointers to the end of the numbers.
133 F1P = F1NumEnd; F2P = F2NumEnd;
134 return false;
135}
136
137// PadFileIfNeeded - If the files are not identical, we will have to be doing
138// numeric comparisons in here. There are bad cases involved where we (i.e.,
139// strtod) might run off the beginning or end of the file if it starts or ends
140// with a number. Because of this, if needed, we pad the file so that it starts
141// and ends with a null character.
142static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
Chris Lattnerda7e70e2005-01-23 06:02:40 +0000143 if (FileStart-FileEnd < 2 ||
144 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
Chris Lattner44542532005-01-23 03:13:43 +0000145 unsigned FileLen = FileEnd-FileStart;
146 char *NewFile = new char[FileLen+2];
147 NewFile[0] = 0; // Add null padding
148 NewFile[FileLen+1] = 0; // Add null padding
149 memcpy(NewFile+1, FileStart, FileLen);
150 FP = NewFile+(FP-FileStart)+1;
151 FileStart = NewFile+1;
152 FileEnd = FileStart+FileLen;
153 }
154}
155
156/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
157/// files match, 1 if they are different, and 2 if there is a file error. This
158/// function differs from DiffFiles in that you can specify an absolete and
159/// relative FP error that is allowed to exist. If you specify a string to fill
160/// in for the error option, it will set the string to an error message if an
161/// error occurs, allowing the caller to distinguish between a failed diff and a
162/// file system error.
163///
Reid Spencerc74b4612007-04-07 18:53:16 +0000164int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
165 const sys::PathWithStatus &FileB,
Chris Lattner44542532005-01-23 03:13:43 +0000166 double AbsTol, double RelTol,
167 std::string *Error) {
Reid Spencer8475ec02007-03-29 19:05:44 +0000168 const sys::FileStatus *FileAStat = FileA.getFileStatus(false, Error);
169 if (!FileAStat)
Chris Lattner252ad032006-07-28 22:03:44 +0000170 return 2;
Reid Spencer8475ec02007-03-29 19:05:44 +0000171 const sys::FileStatus *FileBStat = FileB.getFileStatus(false, Error);
172 if (!FileBStat)
Reid Spencer86279692006-08-22 16:06:27 +0000173 return 2;
174
Chris Lattner252ad032006-07-28 22:03:44 +0000175 // Check for zero length files because some systems croak when you try to
176 // mmap an empty file.
Reid Spencer8475ec02007-03-29 19:05:44 +0000177 size_t A_size = FileAStat->getSize();
178 size_t B_size = FileBStat->getSize();
Chris Lattner252ad032006-07-28 22:03:44 +0000179
180 // If they are both zero sized then they're the same
181 if (A_size == 0 && B_size == 0)
182 return 0;
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000183
Chris Lattner252ad032006-07-28 22:03:44 +0000184 // If only one of them is zero sized then they can't be the same
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000185 if ((A_size == 0 || B_size == 0)) {
186 if (Error)
187 *Error = "Files differ: one is zero-sized, the other isn't";
Chris Lattner252ad032006-07-28 22:03:44 +0000188 return 1;
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000189 }
Chris Lattner252ad032006-07-28 22:03:44 +0000190
Reid Spencer30493372006-08-23 20:37:59 +0000191 // Now its safe to mmap the files into memory becasue both files
192 // have a non-zero size.
193 sys::MappedFile F1;
194 if (F1.open(FileA, sys::MappedFile::READ_ACCESS, Error))
Chris Lattner44542532005-01-23 03:13:43 +0000195 return 2;
Reid Spencer30493372006-08-23 20:37:59 +0000196 sys::MappedFile F2;
197 if (F2.open(FileB, sys::MappedFile::READ_ACCESS, Error))
Reid Spencerc64f1482006-05-15 22:12:42 +0000198 return 2;
Reid Spencer30493372006-08-23 20:37:59 +0000199 if (!F1.map(Error))
200 return 2;
201 if (!F2.map(Error))
202 return 2;
203
204 // Okay, now that we opened the files, scan them for the first difference.
205 char *File1Start = F1.charBase();
206 char *File2Start = F2.charBase();
207 char *File1End = File1Start+A_size;
208 char *File2End = File2Start+B_size;
209 char *F1P = File1Start;
210 char *F2P = File2Start;
211
212 if (A_size == B_size) {
213 // Are the buffers identical?
214 if (std::memcmp(File1Start, File2Start, A_size) == 0)
215 return 0;
216
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000217 if (AbsTol == 0 && RelTol == 0) {
218 if (Error)
219 *Error = "Files differ without tolerance allowance";
Reid Spencer30493372006-08-23 20:37:59 +0000220 return 1; // Files different!
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000221 }
Chris Lattner44542532005-01-23 03:13:43 +0000222 }
Reid Spencer30493372006-08-23 20:37:59 +0000223
224 char *OrigFile1Start = File1Start;
225 char *OrigFile2Start = File2Start;
226
227 // If the files need padding, do so now.
228 PadFileIfNeeded(File1Start, File1End, F1P);
229 PadFileIfNeeded(File2Start, File2End, F2P);
230
231 bool CompareFailed = false;
232 while (1) {
233 // Scan for the end of file or next difference.
234 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
235 ++F1P, ++F2P;
236
237 if (F1P >= File1End || F2P >= File2End) break;
238
239 // Okay, we must have found a difference. Backup to the start of the
240 // current number each stream is at so that we can compare from the
241 // beginning.
242 F1P = BackupNumber(F1P, File1Start);
243 F2P = BackupNumber(F2P, File2Start);
244
245 // Now that we are at the start of the numbers, compare them, exiting if
246 // they don't match.
247 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
248 CompareFailed = true;
249 break;
250 }
251 }
252
253 // Okay, we reached the end of file. If both files are at the end, we
254 // succeeded.
255 bool F1AtEnd = F1P >= File1End;
256 bool F2AtEnd = F2P >= File2End;
257 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
258 // Else, we might have run off the end due to a number: backup and retry.
259 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
260 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
261 F1P = BackupNumber(F1P, File1Start);
262 F2P = BackupNumber(F2P, File2Start);
263
264 // Now that we are at the start of the numbers, compare them, exiting if
265 // they don't match.
266 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
267 CompareFailed = true;
268
269 // If we found the end, we succeeded.
270 if (F1P < File1End || F2P < File2End)
271 CompareFailed = true;
272 }
273
274 if (OrigFile1Start != File1Start)
275 delete[] (File1Start-1); // Back up past null byte
276 if (OrigFile2Start != File2Start)
277 delete[] (File2Start-1); // Back up past null byte
278 return CompareFailed;
Chris Lattner44542532005-01-23 03:13:43 +0000279}