blob: 0a55bc831247f4c37207f87dfa2ed4558095561f [file] [log] [blame]
Chris Lattner7a6ff2b2003-08-01 21:16:14 +00001//===- Support/FileUtilities.cpp - File System Utilities ------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattner2cdd21c2003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner44542532005-01-23 03:13:43 +000022static bool isNumberChar(char C) {
23 switch (C) {
24 case '0': case '1': case '2': case '3': case '4':
25 case '5': case '6': case '7': case '8': case '9':
26 case '.': case '+': case '-':
27 case 'e':
28 case 'E': return true;
29 default: return false;
30 }
31}
32
33static char *BackupNumber(char *Pos, char *FirstChar) {
34 // If we didn't stop in the middle of a number, don't backup.
35 if (!isNumberChar(*Pos)) return Pos;
36
37 // Otherwise, return to the start of the number.
38 while (Pos > FirstChar && isNumberChar(Pos[-1]))
39 --Pos;
40 return Pos;
41}
42
43/// CompareNumbers - compare two numbers, returning true if they are different.
44static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End,
45 double AbsTolerance, double RelTolerance,
46 std::string *ErrorMsg) {
47 char *F1NumEnd, *F2NumEnd;
48 double V1 = 0.0, V2 = 0.0;
49 // If we stop on numbers, compare their difference.
50 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
51 V1 = strtod(F1P, &F1NumEnd);
52 V2 = strtod(F2P, &F2NumEnd);
53 } else {
54 // Otherwise, the diff failed.
55 F1NumEnd = F1P;
56 F2NumEnd = F2P;
57 }
58
59 if (F1NumEnd == F1P || F2NumEnd == F2P) {
60 if (ErrorMsg) *ErrorMsg = "Comparison failed, not a numeric difference.";
61 return true;
62 }
63
64 // Check to see if these are inside the absolute tolerance
65 if (AbsTolerance < std::abs(V1-V2)) {
66 // Nope, check the relative tolerance...
67 double Diff;
68 if (V2)
69 Diff = std::abs(V1/V2 - 1.0);
70 else if (V1)
71 Diff = std::abs(V2/V1 - 1.0);
72 else
73 Diff = 0; // Both zero.
74 if (Diff > RelTolerance) {
75 if (ErrorMsg) {
76 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) +
77 ": diff = " + ftostr(Diff) + "\n";
78 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) +
79 "/" + ftostr(AbsTolerance);
80 }
81 return true;
82 }
83 }
84
85 // Otherwise, advance our read pointers to the end of the numbers.
86 F1P = F1NumEnd; F2P = F2NumEnd;
87 return false;
88}
89
90// PadFileIfNeeded - If the files are not identical, we will have to be doing
91// numeric comparisons in here. There are bad cases involved where we (i.e.,
92// strtod) might run off the beginning or end of the file if it starts or ends
93// with a number. Because of this, if needed, we pad the file so that it starts
94// and ends with a null character.
95static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
Chris Lattnerda7e70e2005-01-23 06:02:40 +000096 if (FileStart-FileEnd < 2 ||
97 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
Chris Lattner44542532005-01-23 03:13:43 +000098 unsigned FileLen = FileEnd-FileStart;
99 char *NewFile = new char[FileLen+2];
100 NewFile[0] = 0; // Add null padding
101 NewFile[FileLen+1] = 0; // Add null padding
102 memcpy(NewFile+1, FileStart, FileLen);
103 FP = NewFile+(FP-FileStart)+1;
104 FileStart = NewFile+1;
105 FileEnd = FileStart+FileLen;
106 }
107}
108
109/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
110/// files match, 1 if they are different, and 2 if there is a file error. This
111/// function differs from DiffFiles in that you can specify an absolete and
112/// relative FP error that is allowed to exist. If you specify a string to fill
113/// in for the error option, it will set the string to an error message if an
114/// error occurs, allowing the caller to distinguish between a failed diff and a
115/// file system error.
116///
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000117int llvm::DiffFilesWithTolerance(const sys::Path &FileA,
118 const sys::Path &FileB,
Chris Lattner44542532005-01-23 03:13:43 +0000119 double AbsTol, double RelTol,
120 std::string *Error) {
121 try {
Misha Brukman1e1a99f2005-02-15 21:59:53 +0000122 // Check for zero length files because some systems croak when you try to
Reid Spencer45d55642005-02-15 21:47:02 +0000123 // mmap an empty file.
124 size_t A_size = FileA.getSize();
125 size_t B_size = FileB.getSize();
126
127 // If they are both zero sized then they're the same
128 if (A_size == 0 && B_size == 0)
129 return 0;
130 // If only one of them is zero sized then they can't be the same
131 if ((A_size == 0 || B_size == 0))
132 return 1;
133
134 // Now its safe to mmap the files into memory becasue both files
135 // have a non-zero size.
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000136 sys::MappedFile F1(FileA);
137 sys::MappedFile F2(FileB);
Chris Lattner44542532005-01-23 03:13:43 +0000138 F1.map();
139 F2.map();
140
141 // Okay, now that we opened the files, scan them for the first difference.
142 char *File1Start = F1.charBase();
143 char *File2Start = F2.charBase();
144 char *File1End = File1Start+F1.size();
145 char *File2End = File2Start+F2.size();
146 char *F1P = File1Start;
147 char *F2P = File2Start;
148
Reid Spencer45d55642005-02-15 21:47:02 +0000149 if (A_size == B_size) {
150 // Scan for the end of file or first difference.
151 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
152 ++F1P, ++F2P;
Chris Lattner44542532005-01-23 03:13:43 +0000153
Reid Spencer45d55642005-02-15 21:47:02 +0000154 // Common case: identifical files.
155 if (F1P == File1End && F2P == File2End)
156 return 0; // Scanned to end, files same
Chris Lattner44542532005-01-23 03:13:43 +0000157
Reid Spencer45d55642005-02-15 21:47:02 +0000158 if (AbsTol == 0 && RelTol == 0)
159 return 1; // Files different!
160 }
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000161
Chris Lattner44542532005-01-23 03:13:43 +0000162 char *OrigFile1Start = File1Start;
163 char *OrigFile2Start = File2Start;
164
165 // If the files need padding, do so now.
166 PadFileIfNeeded(File1Start, File1End, F1P);
167 PadFileIfNeeded(File2Start, File2End, F2P);
168
169 bool CompareFailed = false;
170 while (1) {
171 // Scan for the end of file or next difference.
172 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
173 ++F1P, ++F2P;
174
175 if (F1P >= File1End || F2P >= File2End) break;
176
177 // Okay, we must have found a difference. Backup to the start of the
178 // current number each stream is at so that we can compare from the
179 // beginning.
180 F1P = BackupNumber(F1P, File1Start);
181 F2P = BackupNumber(F2P, File2Start);
182
183 // Now that we are at the start of the numbers, compare them, exiting if
184 // they don't match.
185 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
186 CompareFailed = true;
187 break;
188 }
189 }
190
191 // Okay, we reached the end of file. If both files are at the end, we
192 // succeeded.
193 bool F1AtEnd = F1P >= File1End;
194 bool F2AtEnd = F2P >= File2End;
195 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
196 // Else, we might have run off the end due to a number: backup and retry.
197 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
198 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
199 F1P = BackupNumber(F1P, File1Start);
200 F2P = BackupNumber(F2P, File2Start);
201
202 // Now that we are at the start of the numbers, compare them, exiting if
203 // they don't match.
204 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
205 CompareFailed = true;
206
207 // If we found the end, we succeeded.
208 if (F1P < File1End || F2P < File2End)
209 CompareFailed = true;
210 }
211
212 if (OrigFile1Start != File1Start)
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000213 delete[] (File1Start-1); // Back up past null byte
Chris Lattner44542532005-01-23 03:13:43 +0000214 if (OrigFile2Start != File2Start)
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000215 delete[] (File2Start-1); // Back up past null byte
Chris Lattner44542532005-01-23 03:13:43 +0000216 return CompareFailed;
217 } catch (const std::string &Msg) {
218 if (Error) *Error = Msg;
219 return 2;
220 }
221}