blob: 72200850c572c0e4ca07d7835ea0ba6df326dc54 [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"
Chris Lattnera7750c12008-04-01 03:39:49 +000016#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer59088392010-01-29 14:42:22 +000017#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000018#include "llvm/Support/Path.h"
Chris Lattnera7750c12008-04-01 03:39:49 +000019#include "llvm/ADT/OwningPtr.h"
20#include "llvm/ADT/SmallString.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000021#include <cstdlib>
Chris Lattner2ee51cb2005-02-15 22:12:10 +000022#include <cstring>
Chris Lattnerc0d0e772005-03-17 04:49:04 +000023#include <cctype>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000026static bool isSignedChar(char C) {
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000027 return (C == '+' || C == '-');
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000028}
29
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000030static bool isExponentChar(char C) {
Chris Lattner44542532005-01-23 03:13:43 +000031 switch (C) {
Chris Lattner8dcd5482005-08-02 00:11:53 +000032 case 'D': // Strange exponential notation.
33 case 'd': // Strange exponential notation.
Chris Lattner44542532005-01-23 03:13:43 +000034 case 'e':
35 case 'E': return true;
36 default: return false;
37 }
38}
39
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000040static bool isNumberChar(char C) {
41 switch (C) {
42 case '0': case '1': case '2': case '3': case '4':
43 case '5': case '6': case '7': case '8': case '9':
44 case '.': return true;
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000045 default: return isSignedChar(C) || isExponentChar(C);
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000046 }
47}
48
Chris Lattnera7750c12008-04-01 03:39:49 +000049static const char *BackupNumber(const char *Pos, const char *FirstChar) {
Chris Lattner44542532005-01-23 03:13:43 +000050 // If we didn't stop in the middle of a number, don't backup.
51 if (!isNumberChar(*Pos)) return Pos;
52
53 // Otherwise, return to the start of the number.
Daniel Dunbard1c82fe2010-06-15 19:20:28 +000054 bool HasPeriod = false;
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000055 while (Pos > FirstChar && isNumberChar(Pos[-1])) {
Daniel Dunbard1c82fe2010-06-15 19:20:28 +000056 // Backup over at most one period.
57 if (Pos[-1] == '.') {
58 if (HasPeriod)
59 break;
60 HasPeriod = true;
61 }
62
Chris Lattner44542532005-01-23 03:13:43 +000063 --Pos;
Lauro Ramos Venancio42f6e452008-01-28 20:02:51 +000064 if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
Lauro Ramos Venancio599ddf92008-01-28 18:23:23 +000065 break;
66 }
Chris Lattner44542532005-01-23 03:13:43 +000067 return Pos;
68}
69
Chris Lattnera7750c12008-04-01 03:39:49 +000070/// EndOfNumber - Return the first character that is not part of the specified
71/// number. This assumes that the buffer is null terminated, so it won't fall
72/// off the end.
73static const char *EndOfNumber(const char *Pos) {
74 while (isNumberChar(*Pos))
75 ++Pos;
76 return Pos;
77}
78
Chris Lattner44542532005-01-23 03:13:43 +000079/// CompareNumbers - compare two numbers, returning true if they are different.
Chris Lattnera7750c12008-04-01 03:39:49 +000080static bool CompareNumbers(const char *&F1P, const char *&F2P,
81 const char *F1End, const char *F2End,
Chris Lattner44542532005-01-23 03:13:43 +000082 double AbsTolerance, double RelTolerance,
83 std::string *ErrorMsg) {
Chris Lattnera7750c12008-04-01 03:39:49 +000084 const char *F1NumEnd, *F2NumEnd;
Misha Brukmanf976c852005-04-21 22:55:34 +000085 double V1 = 0.0, V2 = 0.0;
Chris Lattnerc0d0e772005-03-17 04:49:04 +000086
87 // If one of the positions is at a space and the other isn't, chomp up 'til
88 // the end of the space.
89 while (isspace(*F1P) && F1P != F1End)
90 ++F1P;
91 while (isspace(*F2P) && F2P != F2End)
92 ++F2P;
93
Chris Lattnera7750c12008-04-01 03:39:49 +000094 // If we stop on numbers, compare their difference.
95 if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) {
96 // The diff failed.
Chris Lattner44542532005-01-23 03:13:43 +000097 F1NumEnd = F1P;
98 F2NumEnd = F2P;
Chris Lattnera7750c12008-04-01 03:39:49 +000099 } else {
100 // Note that some ugliness is built into this to permit support for numbers
101 // that use "D" or "d" as their exponential marker, e.g. "1.234D45". This
102 // occurs in 200.sixtrack in spec2k.
103 V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
104 V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
105
106 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
107 // Copy string into tmp buffer to replace the 'D' with an 'e'.
108 SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000109 // Strange exponential notation!
110 StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';
Chris Lattnera7750c12008-04-01 03:39:49 +0000111
112 V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
113 F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
114 }
115
116 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
117 // Copy string into tmp buffer to replace the 'D' with an 'e'.
118 SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000119 // Strange exponential notation!
120 StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';
Chris Lattnera7750c12008-04-01 03:39:49 +0000121
122 V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
123 F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
124 }
Chris Lattner44542532005-01-23 03:13:43 +0000125 }
126
127 if (F1NumEnd == F1P || F2NumEnd == F2P) {
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000128 if (ErrorMsg) {
129 *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
130 *ErrorMsg += F1P[0];
131 *ErrorMsg += "' and '";
132 *ErrorMsg += F2P[0];
133 *ErrorMsg += "'";
134 }
Chris Lattner44542532005-01-23 03:13:43 +0000135 return true;
136 }
137
138 // Check to see if these are inside the absolute tolerance
139 if (AbsTolerance < std::abs(V1-V2)) {
140 // Nope, check the relative tolerance...
141 double Diff;
142 if (V2)
143 Diff = std::abs(V1/V2 - 1.0);
144 else if (V1)
145 Diff = std::abs(V2/V1 - 1.0);
146 else
147 Diff = 0; // Both zero.
148 if (Diff > RelTolerance) {
149 if (ErrorMsg) {
Benjamin Kramer59088392010-01-29 14:42:22 +0000150 raw_string_ostream(*ErrorMsg)
151 << "Compared: " << V1 << " and " << V2 << '\n'
152 << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'
153 << "Out of tolerance: rel/abs: " << RelTolerance << '/'
154 << AbsTolerance;
Chris Lattner44542532005-01-23 03:13:43 +0000155 }
156 return true;
157 }
158 }
159
160 // Otherwise, advance our read pointers to the end of the numbers.
161 F1P = F1NumEnd; F2P = F2NumEnd;
162 return false;
163}
164
Chris Lattner44542532005-01-23 03:13:43 +0000165/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
166/// files match, 1 if they are different, and 2 if there is a file error. This
167/// function differs from DiffFiles in that you can specify an absolete and
168/// relative FP error that is allowed to exist. If you specify a string to fill
169/// in for the error option, it will set the string to an error message if an
170/// error occurs, allowing the caller to distinguish between a failed diff and a
171/// file system error.
172///
Reid Spencerc74b4612007-04-07 18:53:16 +0000173int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
174 const sys::PathWithStatus &FileB,
Chris Lattner44542532005-01-23 03:13:43 +0000175 double AbsTol, double RelTol,
176 std::string *Error) {
Reid Spencer8475ec02007-03-29 19:05:44 +0000177 const sys::FileStatus *FileAStat = FileA.getFileStatus(false, Error);
178 if (!FileAStat)
Chris Lattner252ad032006-07-28 22:03:44 +0000179 return 2;
Reid Spencer8475ec02007-03-29 19:05:44 +0000180 const sys::FileStatus *FileBStat = FileB.getFileStatus(false, Error);
181 if (!FileBStat)
Reid Spencer86279692006-08-22 16:06:27 +0000182 return 2;
183
Chris Lattner252ad032006-07-28 22:03:44 +0000184 // Check for zero length files because some systems croak when you try to
185 // mmap an empty file.
Reid Spencer8475ec02007-03-29 19:05:44 +0000186 size_t A_size = FileAStat->getSize();
187 size_t B_size = FileBStat->getSize();
Chris Lattner252ad032006-07-28 22:03:44 +0000188
189 // If they are both zero sized then they're the same
190 if (A_size == 0 && B_size == 0)
191 return 0;
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000192
Chris Lattner252ad032006-07-28 22:03:44 +0000193 // If only one of them is zero sized then they can't be the same
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000194 if ((A_size == 0 || B_size == 0)) {
195 if (Error)
196 *Error = "Files differ: one is zero-sized, the other isn't";
Chris Lattner252ad032006-07-28 22:03:44 +0000197 return 1;
Reid Spencerbfb8eaf2006-10-18 20:23:52 +0000198 }
Chris Lattner252ad032006-07-28 22:03:44 +0000199
Reid Spencer30493372006-08-23 20:37:59 +0000200 // Now its safe to mmap the files into memory becasue both files
201 // have a non-zero size.
Chris Lattner038112a2008-04-01 18:04:03 +0000202 OwningPtr<MemoryBuffer> F1(MemoryBuffer::getFile(FileA.c_str(), Error));
203 OwningPtr<MemoryBuffer> F2(MemoryBuffer::getFile(FileB.c_str(), Error));
Chris Lattnera7750c12008-04-01 03:39:49 +0000204 if (F1 == 0 || F2 == 0)
Chris Lattner44542532005-01-23 03:13:43 +0000205 return 2;
Chris Lattnera7750c12008-04-01 03:39:49 +0000206
Reid Spencer30493372006-08-23 20:37:59 +0000207 // Okay, now that we opened the files, scan them for the first difference.
Chris Lattnera7750c12008-04-01 03:39:49 +0000208 const char *File1Start = F1->getBufferStart();
209 const char *File2Start = F2->getBufferStart();
210 const char *File1End = F1->getBufferEnd();
211 const char *File2End = F2->getBufferEnd();
212 const char *F1P = File1Start;
213 const char *F2P = File2Start;
Reid Spencer30493372006-08-23 20:37:59 +0000214
Daniel Dunbar71907fb2010-06-15 19:20:30 +0000215 // Are the buffers identical? Common case: Handle this efficiently.
216 if (A_size == B_size &&
217 std::memcmp(File1Start, File2Start, A_size) == 0)
218 return 0;
Reid Spencer30493372006-08-23 20:37:59 +0000219
Daniel Dunbar71907fb2010-06-15 19:20:30 +0000220 // Otherwise, we are done a tolerances are set.
221 if (AbsTol == 0 && RelTol == 0) {
222 if (Error)
223 *Error = "Files differ without tolerance allowance";
224 return 1; // Files different!
Chris Lattner44542532005-01-23 03:13:43 +0000225 }
Reid Spencer30493372006-08-23 20:37:59 +0000226
Reid Spencer30493372006-08-23 20:37:59 +0000227 bool CompareFailed = false;
228 while (1) {
229 // Scan for the end of file or next difference.
230 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
231 ++F1P, ++F2P;
232
233 if (F1P >= File1End || F2P >= File2End) break;
234
235 // Okay, we must have found a difference. Backup to the start of the
236 // current number each stream is at so that we can compare from the
237 // beginning.
238 F1P = BackupNumber(F1P, File1Start);
239 F2P = BackupNumber(F2P, File2Start);
240
241 // Now that we are at the start of the numbers, compare them, exiting if
242 // they don't match.
243 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
244 CompareFailed = true;
245 break;
246 }
247 }
248
249 // Okay, we reached the end of file. If both files are at the end, we
250 // succeeded.
251 bool F1AtEnd = F1P >= File1End;
252 bool F2AtEnd = F2P >= File2End;
253 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
254 // Else, we might have run off the end due to a number: backup and retry.
255 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
256 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
257 F1P = BackupNumber(F1P, File1Start);
258 F2P = BackupNumber(F2P, File2Start);
259
260 // Now that we are at the start of the numbers, compare them, exiting if
261 // they don't match.
262 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
263 CompareFailed = true;
264
265 // If we found the end, we succeeded.
266 if (F1P < File1End || F2P < File2End)
267 CompareFailed = true;
268 }
269
Reid Spencer30493372006-08-23 20:37:59 +0000270 return CompareFailed;
Chris Lattner44542532005-01-23 03:13:43 +0000271}