blob: dd296f30a2279b451d29a18f5d3de0c8bbb1b16f [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 '-':
29 case 'e':
30 case 'E': return true;
31 default: return false;
32 }
33}
34
35static char *BackupNumber(char *Pos, char *FirstChar) {
36 // If we didn't stop in the middle of a number, don't backup.
37 if (!isNumberChar(*Pos)) return Pos;
38
39 // Otherwise, return to the start of the number.
40 while (Pos > FirstChar && isNumberChar(Pos[-1]))
41 --Pos;
42 return Pos;
43}
44
45/// CompareNumbers - compare two numbers, returning true if they are different.
46static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End,
47 double AbsTolerance, double RelTolerance,
48 std::string *ErrorMsg) {
49 char *F1NumEnd, *F2NumEnd;
Misha Brukmanf976c852005-04-21 22:55:34 +000050 double V1 = 0.0, V2 = 0.0;
Chris Lattnerc0d0e772005-03-17 04:49:04 +000051
52 // If one of the positions is at a space and the other isn't, chomp up 'til
53 // the end of the space.
54 while (isspace(*F1P) && F1P != F1End)
55 ++F1P;
56 while (isspace(*F2P) && F2P != F2End)
57 ++F2P;
58
Chris Lattner44542532005-01-23 03:13:43 +000059 // If we stop on numbers, compare their difference.
60 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
61 V1 = strtod(F1P, &F1NumEnd);
62 V2 = strtod(F2P, &F2NumEnd);
63 } else {
64 // Otherwise, the diff failed.
65 F1NumEnd = F1P;
66 F2NumEnd = F2P;
67 }
68
69 if (F1NumEnd == F1P || F2NumEnd == F2P) {
70 if (ErrorMsg) *ErrorMsg = "Comparison failed, not a numeric difference.";
71 return true;
72 }
73
74 // Check to see if these are inside the absolute tolerance
75 if (AbsTolerance < std::abs(V1-V2)) {
76 // Nope, check the relative tolerance...
77 double Diff;
78 if (V2)
79 Diff = std::abs(V1/V2 - 1.0);
80 else if (V1)
81 Diff = std::abs(V2/V1 - 1.0);
82 else
83 Diff = 0; // Both zero.
84 if (Diff > RelTolerance) {
85 if (ErrorMsg) {
86 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) +
87 ": diff = " + ftostr(Diff) + "\n";
88 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) +
89 "/" + ftostr(AbsTolerance);
90 }
91 return true;
92 }
93 }
94
95 // Otherwise, advance our read pointers to the end of the numbers.
96 F1P = F1NumEnd; F2P = F2NumEnd;
97 return false;
98}
99
100// PadFileIfNeeded - If the files are not identical, we will have to be doing
101// numeric comparisons in here. There are bad cases involved where we (i.e.,
102// strtod) might run off the beginning or end of the file if it starts or ends
103// with a number. Because of this, if needed, we pad the file so that it starts
104// and ends with a null character.
105static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
Chris Lattnerda7e70e2005-01-23 06:02:40 +0000106 if (FileStart-FileEnd < 2 ||
107 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
Chris Lattner44542532005-01-23 03:13:43 +0000108 unsigned FileLen = FileEnd-FileStart;
109 char *NewFile = new char[FileLen+2];
110 NewFile[0] = 0; // Add null padding
111 NewFile[FileLen+1] = 0; // Add null padding
112 memcpy(NewFile+1, FileStart, FileLen);
113 FP = NewFile+(FP-FileStart)+1;
114 FileStart = NewFile+1;
115 FileEnd = FileStart+FileLen;
116 }
117}
118
119/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
120/// files match, 1 if they are different, and 2 if there is a file error. This
121/// function differs from DiffFiles in that you can specify an absolete and
122/// relative FP error that is allowed to exist. If you specify a string to fill
123/// in for the error option, it will set the string to an error message if an
124/// error occurs, allowing the caller to distinguish between a failed diff and a
125/// file system error.
126///
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000127int llvm::DiffFilesWithTolerance(const sys::Path &FileA,
128 const sys::Path &FileB,
Chris Lattner44542532005-01-23 03:13:43 +0000129 double AbsTol, double RelTol,
130 std::string *Error) {
131 try {
Misha Brukman1e1a99f2005-02-15 21:59:53 +0000132 // Check for zero length files because some systems croak when you try to
Reid Spencer45d55642005-02-15 21:47:02 +0000133 // mmap an empty file.
134 size_t A_size = FileA.getSize();
135 size_t B_size = FileB.getSize();
136
137 // If they are both zero sized then they're the same
138 if (A_size == 0 && B_size == 0)
139 return 0;
140 // If only one of them is zero sized then they can't be the same
141 if ((A_size == 0 || B_size == 0))
142 return 1;
143
144 // Now its safe to mmap the files into memory becasue both files
Misha Brukmanf976c852005-04-21 22:55:34 +0000145 // have a non-zero size.
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000146 sys::MappedFile F1(FileA);
147 sys::MappedFile F2(FileB);
Chris Lattner44542532005-01-23 03:13:43 +0000148 F1.map();
149 F2.map();
150
151 // Okay, now that we opened the files, scan them for the first difference.
152 char *File1Start = F1.charBase();
153 char *File2Start = F2.charBase();
Chris Lattner2ee51cb2005-02-15 22:12:10 +0000154 char *File1End = File1Start+A_size;
155 char *File2End = File2Start+B_size;
Chris Lattner44542532005-01-23 03:13:43 +0000156 char *F1P = File1Start;
157 char *F2P = File2Start;
158
Reid Spencer45d55642005-02-15 21:47:02 +0000159 if (A_size == B_size) {
Chris Lattner2ee51cb2005-02-15 22:12:10 +0000160 // Are the buffers identical?
161 if (std::memcmp(File1Start, File2Start, A_size) == 0)
162 return 0;
Chris Lattner44542532005-01-23 03:13:43 +0000163
Reid Spencer45d55642005-02-15 21:47:02 +0000164 if (AbsTol == 0 && RelTol == 0)
165 return 1; // Files different!
166 }
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000167
Chris Lattner44542532005-01-23 03:13:43 +0000168 char *OrigFile1Start = File1Start;
169 char *OrigFile2Start = File2Start;
170
171 // If the files need padding, do so now.
172 PadFileIfNeeded(File1Start, File1End, F1P);
173 PadFileIfNeeded(File2Start, File2End, F2P);
Misha Brukmanf976c852005-04-21 22:55:34 +0000174
Chris Lattner44542532005-01-23 03:13:43 +0000175 bool CompareFailed = false;
176 while (1) {
177 // Scan for the end of file or next difference.
178 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
179 ++F1P, ++F2P;
180
181 if (F1P >= File1End || F2P >= File2End) break;
182
183 // Okay, we must have found a difference. Backup to the start of the
184 // current number each stream is at so that we can compare from the
185 // beginning.
186 F1P = BackupNumber(F1P, File1Start);
187 F2P = BackupNumber(F2P, File2Start);
188
189 // Now that we are at the start of the numbers, compare them, exiting if
190 // they don't match.
191 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
192 CompareFailed = true;
193 break;
194 }
195 }
196
197 // Okay, we reached the end of file. If both files are at the end, we
198 // succeeded.
199 bool F1AtEnd = F1P >= File1End;
200 bool F2AtEnd = F2P >= File2End;
201 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
202 // Else, we might have run off the end due to a number: backup and retry.
203 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
204 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
205 F1P = BackupNumber(F1P, File1Start);
206 F2P = BackupNumber(F2P, File2Start);
207
208 // Now that we are at the start of the numbers, compare them, exiting if
209 // they don't match.
210 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
211 CompareFailed = true;
212
213 // If we found the end, we succeeded.
214 if (F1P < File1End || F2P < File2End)
215 CompareFailed = true;
216 }
217
218 if (OrigFile1Start != File1Start)
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000219 delete[] (File1Start-1); // Back up past null byte
Chris Lattner44542532005-01-23 03:13:43 +0000220 if (OrigFile2Start != File2Start)
Chris Lattnercc1b90b2005-01-23 03:31:02 +0000221 delete[] (File2Start-1); // Back up past null byte
Chris Lattner44542532005-01-23 03:13:43 +0000222 return CompareFailed;
223 } catch (const std::string &Msg) {
224 if (Error) *Error = Msg;
225 return 2;
226 }
227}