blob: a7f42ddebf8c165dec4232b61e38874ef623cbbc [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
19#include <cmath>
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
Chris Lattner44542532005-01-23 03:13:43 +000024static bool isNumberChar(char C) {
25 switch (C) {
26 case '0': case '1': case '2': case '3': case '4':
Misha Brukmanf976c852005-04-21 22:55:34 +000027 case '5': case '6': case '7': case '8': case '9':
Chris Lattner44542532005-01-23 03:13:43 +000028 case '.': case '+': case '-':
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
37static char *BackupNumber(char *Pos, char *FirstChar) {
38 // If we didn't stop in the middle of a number, don't backup.
39 if (!isNumberChar(*Pos)) return Pos;
40
41 // Otherwise, return to the start of the number.
42 while (Pos > FirstChar && isNumberChar(Pos[-1]))
43 --Pos;
44 return Pos;
45}
46
47/// CompareNumbers - compare two numbers, returning true if they are different.
48static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End,
49 double AbsTolerance, double RelTolerance,
50 std::string *ErrorMsg) {
51 char *F1NumEnd, *F2NumEnd;
Misha Brukmanf976c852005-04-21 22:55:34 +000052 double V1 = 0.0, V2 = 0.0;
Chris Lattnerc0d0e772005-03-17 04:49:04 +000053
54 // If one of the positions is at a space and the other isn't, chomp up 'til
55 // the end of the space.
56 while (isspace(*F1P) && F1P != F1End)
57 ++F1P;
58 while (isspace(*F2P) && F2P != F2End)
59 ++F2P;
60
Chris Lattner8dcd5482005-08-02 00:11:53 +000061 // If we stop on numbers, compare their difference. Note that some ugliness
62 // is built into this to permit support for numbers that use "D" or "d" as
63 // their exponential marker, e.g. "1.234D45". This occurs in 200.sixtrack in
64 // spec2k.
Chris Lattner44542532005-01-23 03:13:43 +000065 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
Chris Lattner8dcd5482005-08-02 00:11:53 +000066 bool isDNotation;
67 do {
68 isDNotation = false;
69 V1 = strtod(F1P, &F1NumEnd);
70 V2 = strtod(F2P, &F2NumEnd);
71
72 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
73 *F1NumEnd = 'e'; // Strange exponential notation!
74 isDNotation = true;
75 }
76 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
77 *F2NumEnd = 'e'; // Strange exponential notation!
78 isDNotation = true;
79 }
80 } while (isDNotation);
Chris Lattner44542532005-01-23 03:13:43 +000081 } else {
82 // Otherwise, the diff failed.
83 F1NumEnd = F1P;
84 F2NumEnd = F2P;
85 }
86
87 if (F1NumEnd == F1P || F2NumEnd == F2P) {
88 if (ErrorMsg) *ErrorMsg = "Comparison failed, not a numeric difference.";
89 return true;
90 }
91
92 // Check to see if these are inside the absolute tolerance
93 if (AbsTolerance < std::abs(V1-V2)) {
94 // Nope, check the relative tolerance...
95 double Diff;
96 if (V2)
97 Diff = std::abs(V1/V2 - 1.0);
98 else if (V1)
99 Diff = std::abs(V2/V1 - 1.0);
100 else
101 Diff = 0; // Both zero.
102 if (Diff > RelTolerance) {
103 if (ErrorMsg) {
104 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) +
105 ": diff = " + ftostr(Diff) + "\n";
106 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) +
107 "/" + ftostr(AbsTolerance);
108 }
109 return true;
110 }
111 }
112
113 // Otherwise, advance our read pointers to the end of the numbers.
114 F1P = F1NumEnd; F2P = F2NumEnd;
115 return false;
116}
117
118// PadFileIfNeeded - If the files are not identical, we will have to be doing
119// numeric comparisons in here. There are bad cases involved where we (i.e.,
120// strtod) might run off the beginning or end of the file if it starts or ends
121// with a number. Because of this, if needed, we pad the file so that it starts
122// and ends with a null character.
123static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
Chris Lattnerda7e70e2005-01-23 06:02:40 +0000124 if (FileStart-FileEnd < 2 ||
125 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
Chris Lattner44542532005-01-23 03:13:43 +0000126 unsigned FileLen = FileEnd-FileStart;
127 char *NewFile = new char[FileLen+2];
128 NewFile[0] = 0; // Add null padding
129 NewFile[FileLen+1] = 0; // Add null padding
130 memcpy(NewFile+1, FileStart, FileLen);
131 FP = NewFile+(FP-FileStart)+1;
132 FileStart = NewFile+1;
133 FileEnd = FileStart+FileLen;
134 }
135}
136
137/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
138/// files match, 1 if they are different, and 2 if there is a file error. This
139/// function differs from DiffFiles in that you can specify an absolete and
140/// relative FP error that is allowed to exist. If you specify a string to fill
141/// in for the error option, it will set the string to an error message if an
142/// error occurs, allowing the caller to distinguish between a failed diff and a
143/// file system error.
144///
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000145int llvm::DiffFilesWithTolerance(const sys::Path &FileA,
146 const sys::Path &FileB,
Chris Lattner44542532005-01-23 03:13:43 +0000147 double AbsTol, double RelTol,
148 std::string *Error) {
149 try {
Misha Brukman1e1a99f2005-02-15 21:59:53 +0000150 // Check for zero length files because some systems croak when you try to
Reid Spencer45d55642005-02-15 21:47:02 +0000151 // mmap an empty file.
152 size_t A_size = FileA.getSize();
153 size_t B_size = FileB.getSize();
154
155 // If they are both zero sized then they're the same
156 if (A_size == 0 && B_size == 0)
157 return 0;
158 // If only one of them is zero sized then they can't be the same
159 if ((A_size == 0 || B_size == 0))
160 return 1;
161
162 // Now its safe to mmap the files into memory becasue both files
Misha Brukmanf976c852005-04-21 22:55:34 +0000163 // have a non-zero size.
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000164 sys::MappedFile F1(FileA);
165 sys::MappedFile F2(FileB);
Chris Lattner44542532005-01-23 03:13:43 +0000166 F1.map();
167 F2.map();
168
169 // Okay, now that we opened the files, scan them for the first difference.
170 char *File1Start = F1.charBase();
171 char *File2Start = F2.charBase();
Chris Lattner2ee51cb2005-02-15 22:12:10 +0000172 char *File1End = File1Start+A_size;
173 char *File2End = File2Start+B_size;
Chris Lattner44542532005-01-23 03:13:43 +0000174 char *F1P = File1Start;
175 char *F2P = File2Start;
176
Reid Spencer45d55642005-02-15 21:47:02 +0000177 if (A_size == B_size) {
Chris Lattner2ee51cb2005-02-15 22:12:10 +0000178 // Are the buffers identical?
179 if (std::memcmp(File1Start, File2Start, A_size) == 0)
180 return 0;
Chris Lattner44542532005-01-23 03:13:43 +0000181
Reid Spencer45d55642005-02-15 21:47:02 +0000182 if (AbsTol == 0 && RelTol == 0)
183 return 1; // Files different!
184 }
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000185
Chris Lattner44542532005-01-23 03:13:43 +0000186 char *OrigFile1Start = File1Start;
187 char *OrigFile2Start = File2Start;
188
189 // If the files need padding, do so now.
190 PadFileIfNeeded(File1Start, File1End, F1P);
191 PadFileIfNeeded(File2Start, File2End, F2P);
Misha Brukmanf976c852005-04-21 22:55:34 +0000192
Chris Lattner44542532005-01-23 03:13:43 +0000193 bool CompareFailed = false;
194 while (1) {
195 // Scan for the end of file or next difference.
196 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
197 ++F1P, ++F2P;
198
199 if (F1P >= File1End || F2P >= File2End) break;
200
201 // Okay, we must have found a difference. Backup to the start of the
202 // current number each stream is at so that we can compare from the
203 // beginning.
204 F1P = BackupNumber(F1P, File1Start);
205 F2P = BackupNumber(F2P, File2Start);
206
207 // Now that we are at the start of the numbers, compare them, exiting if
208 // they don't match.
209 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
210 CompareFailed = true;
211 break;
212 }
213 }
214
215 // Okay, we reached the end of file. If both files are at the end, we
216 // succeeded.
217 bool F1AtEnd = F1P >= File1End;
218 bool F2AtEnd = F2P >= File2End;
219 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
220 // Else, we might have run off the end due to a number: backup and retry.
221 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
222 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
223 F1P = BackupNumber(F1P, File1Start);
224 F2P = BackupNumber(F2P, File2Start);
225
226 // Now that we are at the start of the numbers, compare them, exiting if
227 // they don't match.
228 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
229 CompareFailed = true;
230
231 // If we found the end, we succeeded.
232 if (F1P < File1End || F2P < File2End)
233 CompareFailed = true;
234 }
235
236 if (OrigFile1Start != File1Start)
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000237 delete[] (File1Start-1); // Back up past null byte
Chris Lattner44542532005-01-23 03:13:43 +0000238 if (OrigFile2Start != File2Start)
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000239 delete[] (File2Start-1); // Back up past null byte
Chris Lattner44542532005-01-23 03:13:43 +0000240 return CompareFailed;
241 } catch (const std::string &Msg) {
242 if (Error) *Error = Msg;
243 return 2;
Reid Spencerc64f1482006-05-15 22:12:42 +0000244 } catch (...) {
245 *Error = "Unknown Exception Occurred";
246 return 2;
Chris Lattner44542532005-01-23 03:13:43 +0000247 }
248}