blob: 39dbefff5b7083467f5ee52bd410f35613110b0b [file] [log] [blame]
Chris Lattner802b0e22003-08-01 21:16:14 +00001//===- Support/FileUtilities.cpp - File System Utilities ------------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner802b0e22003-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 Spencer7c16caa2004-09-01 22:55:40 +000015#include "llvm/Support/FileUtilities.h"
Chris Lattner314a1412008-04-01 03:39:49 +000016#include "llvm/ADT/SmallString.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000017#include "llvm/Support/ErrorOr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Support/MemoryBuffer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include <cctype>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000021#include <cmath>
22#include <cstdint>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000023#include <cstdlib>
Chris Lattner3e7a1932005-02-15 22:12:10 +000024#include <cstring>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000025#include <memory>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000026#include <system_error>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000027
Chris Lattnerc9499b62003-12-14 21:35:53 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Lauro Ramos Venancio99929d20e2008-01-28 18:23:23 +000030static bool isSignedChar(char C) {
Lauro Ramos Venancio0f54a092008-01-28 20:02:51 +000031 return (C == '+' || C == '-');
Lauro Ramos Venancio99929d20e2008-01-28 18:23:23 +000032}
33
Lauro Ramos Venancio0f54a092008-01-28 20:02:51 +000034static bool isExponentChar(char C) {
Chris Lattner16a43682005-01-23 03:13:43 +000035 switch (C) {
Chris Lattnerfca31ae2005-08-02 00:11:53 +000036 case 'D': // Strange exponential notation.
37 case 'd': // Strange exponential notation.
Chris Lattner16a43682005-01-23 03:13:43 +000038 case 'e':
39 case 'E': return true;
40 default: return false;
41 }
42}
43
Lauro Ramos Venancio99929d20e2008-01-28 18:23:23 +000044static bool isNumberChar(char C) {
45 switch (C) {
46 case '0': case '1': case '2': case '3': case '4':
47 case '5': case '6': case '7': case '8': case '9':
48 case '.': return true;
Lauro Ramos Venancio0f54a092008-01-28 20:02:51 +000049 default: return isSignedChar(C) || isExponentChar(C);
Lauro Ramos Venancio99929d20e2008-01-28 18:23:23 +000050 }
51}
52
Chris Lattner314a1412008-04-01 03:39:49 +000053static const char *BackupNumber(const char *Pos, const char *FirstChar) {
Chris Lattner16a43682005-01-23 03:13:43 +000054 // If we didn't stop in the middle of a number, don't backup.
55 if (!isNumberChar(*Pos)) return Pos;
56
57 // Otherwise, return to the start of the number.
Daniel Dunbarb645fa12010-06-15 19:20:28 +000058 bool HasPeriod = false;
Lauro Ramos Venancio99929d20e2008-01-28 18:23:23 +000059 while (Pos > FirstChar && isNumberChar(Pos[-1])) {
Daniel Dunbarb645fa12010-06-15 19:20:28 +000060 // Backup over at most one period.
61 if (Pos[-1] == '.') {
62 if (HasPeriod)
63 break;
64 HasPeriod = true;
65 }
66
Chris Lattner16a43682005-01-23 03:13:43 +000067 --Pos;
Lauro Ramos Venancio0f54a092008-01-28 20:02:51 +000068 if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
Lauro Ramos Venancio99929d20e2008-01-28 18:23:23 +000069 break;
70 }
Chris Lattner16a43682005-01-23 03:13:43 +000071 return Pos;
72}
73
Chris Lattner314a1412008-04-01 03:39:49 +000074/// EndOfNumber - Return the first character that is not part of the specified
75/// number. This assumes that the buffer is null terminated, so it won't fall
76/// off the end.
77static const char *EndOfNumber(const char *Pos) {
78 while (isNumberChar(*Pos))
79 ++Pos;
80 return Pos;
81}
82
Chris Lattner16a43682005-01-23 03:13:43 +000083/// CompareNumbers - compare two numbers, returning true if they are different.
Chris Lattner314a1412008-04-01 03:39:49 +000084static bool CompareNumbers(const char *&F1P, const char *&F2P,
85 const char *F1End, const char *F2End,
Chris Lattner16a43682005-01-23 03:13:43 +000086 double AbsTolerance, double RelTolerance,
87 std::string *ErrorMsg) {
Chris Lattner314a1412008-04-01 03:39:49 +000088 const char *F1NumEnd, *F2NumEnd;
Misha Brukman10468d82005-04-21 22:55:34 +000089 double V1 = 0.0, V2 = 0.0;
Chris Lattner252f5462005-03-17 04:49:04 +000090
91 // If one of the positions is at a space and the other isn't, chomp up 'til
92 // the end of the space.
Guy Benyei83c74e92013-02-12 21:21:59 +000093 while (isspace(static_cast<unsigned char>(*F1P)) && F1P != F1End)
Chris Lattner252f5462005-03-17 04:49:04 +000094 ++F1P;
Guy Benyei83c74e92013-02-12 21:21:59 +000095 while (isspace(static_cast<unsigned char>(*F2P)) && F2P != F2End)
Chris Lattner252f5462005-03-17 04:49:04 +000096 ++F2P;
97
Chris Lattner314a1412008-04-01 03:39:49 +000098 // If we stop on numbers, compare their difference.
99 if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) {
100 // The diff failed.
Chris Lattner16a43682005-01-23 03:13:43 +0000101 F1NumEnd = F1P;
102 F2NumEnd = F2P;
Chris Lattner314a1412008-04-01 03:39:49 +0000103 } else {
104 // Note that some ugliness is built into this to permit support for numbers
105 // that use "D" or "d" as their exponential marker, e.g. "1.234D45". This
106 // occurs in 200.sixtrack in spec2k.
107 V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
108 V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
109
110 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
111 // Copy string into tmp buffer to replace the 'D' with an 'e'.
112 SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1);
Evan Cheng86cb3182008-05-05 18:30:58 +0000113 // Strange exponential notation!
114 StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';
Michael J. Spencerdb97c0b2010-12-09 17:37:32 +0000115
Chris Lattner314a1412008-04-01 03:39:49 +0000116 V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
117 F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
118 }
Michael J. Spencerdb97c0b2010-12-09 17:37:32 +0000119
Chris Lattner314a1412008-04-01 03:39:49 +0000120 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
121 // Copy string into tmp buffer to replace the 'D' with an 'e'.
122 SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1);
Evan Cheng86cb3182008-05-05 18:30:58 +0000123 // Strange exponential notation!
124 StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';
Michael J. Spencerdb97c0b2010-12-09 17:37:32 +0000125
Chris Lattner314a1412008-04-01 03:39:49 +0000126 V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
127 F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
128 }
Chris Lattner16a43682005-01-23 03:13:43 +0000129 }
130
131 if (F1NumEnd == F1P || F2NumEnd == F2P) {
Reid Spencer309e03a2006-10-18 20:23:52 +0000132 if (ErrorMsg) {
133 *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
134 *ErrorMsg += F1P[0];
135 *ErrorMsg += "' and '";
136 *ErrorMsg += F2P[0];
137 *ErrorMsg += "'";
138 }
Chris Lattner16a43682005-01-23 03:13:43 +0000139 return true;
140 }
141
142 // Check to see if these are inside the absolute tolerance
143 if (AbsTolerance < std::abs(V1-V2)) {
144 // Nope, check the relative tolerance...
145 double Diff;
146 if (V2)
147 Diff = std::abs(V1/V2 - 1.0);
148 else if (V1)
149 Diff = std::abs(V2/V1 - 1.0);
150 else
151 Diff = 0; // Both zero.
152 if (Diff > RelTolerance) {
153 if (ErrorMsg) {
Benjamin Kramerb17c5862010-01-29 14:42:22 +0000154 raw_string_ostream(*ErrorMsg)
155 << "Compared: " << V1 << " and " << V2 << '\n'
156 << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'
157 << "Out of tolerance: rel/abs: " << RelTolerance << '/'
158 << AbsTolerance;
Chris Lattner16a43682005-01-23 03:13:43 +0000159 }
160 return true;
161 }
162 }
163
164 // Otherwise, advance our read pointers to the end of the numbers.
165 F1P = F1NumEnd; F2P = F2NumEnd;
166 return false;
167}
168
Chris Lattner16a43682005-01-23 03:13:43 +0000169/// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
170/// files match, 1 if they are different, and 2 if there is a file error. This
171/// function differs from DiffFiles in that you can specify an absolete and
172/// relative FP error that is allowed to exist. If you specify a string to fill
173/// in for the error option, it will set the string to an error message if an
174/// error occurs, allowing the caller to distinguish between a failed diff and a
175/// file system error.
176///
Rafael Espindola280e5cb2013-06-13 20:41:00 +0000177int llvm::DiffFilesWithTolerance(StringRef NameA,
178 StringRef NameB,
Chris Lattner16a43682005-01-23 03:13:43 +0000179 double AbsTol, double RelTol,
180 std::string *Error) {
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000181 // Now its safe to mmap the files into memory because both files
Reid Spencerd62a9632006-08-23 20:37:59 +0000182 // have a non-zero size.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000183 ErrorOr<std::unique_ptr<MemoryBuffer>> F1OrErr = MemoryBuffer::getFile(NameA);
184 if (std::error_code EC = F1OrErr.getError()) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000185 if (Error)
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000186 *Error = EC.message();
Chris Lattner16a43682005-01-23 03:13:43 +0000187 return 2;
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000188 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000189 MemoryBuffer &F1 = *F1OrErr.get();
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000190
191 ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB);
192 if (std::error_code EC = F2OrErr.getError()) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000193 if (Error)
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000194 *Error = EC.message();
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000195 return 2;
196 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000197 MemoryBuffer &F2 = *F2OrErr.get();
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000198
Reid Spencerd62a9632006-08-23 20:37:59 +0000199 // Okay, now that we opened the files, scan them for the first difference.
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000200 const char *File1Start = F1.getBufferStart();
201 const char *File2Start = F2.getBufferStart();
202 const char *File1End = F1.getBufferEnd();
203 const char *File2End = F2.getBufferEnd();
Chris Lattner314a1412008-04-01 03:39:49 +0000204 const char *F1P = File1Start;
205 const char *F2P = File2Start;
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000206 uint64_t A_size = F1.getBufferSize();
207 uint64_t B_size = F2.getBufferSize();
Reid Spencerd62a9632006-08-23 20:37:59 +0000208
Daniel Dunbare22295e2010-06-15 19:20:30 +0000209 // Are the buffers identical? Common case: Handle this efficiently.
210 if (A_size == B_size &&
211 std::memcmp(File1Start, File2Start, A_size) == 0)
212 return 0;
Reid Spencerd62a9632006-08-23 20:37:59 +0000213
Daniel Dunbare22295e2010-06-15 19:20:30 +0000214 // Otherwise, we are done a tolerances are set.
215 if (AbsTol == 0 && RelTol == 0) {
216 if (Error)
217 *Error = "Files differ without tolerance allowance";
218 return 1; // Files different!
Chris Lattner16a43682005-01-23 03:13:43 +0000219 }
Reid Spencerd62a9632006-08-23 20:37:59 +0000220
Reid Spencerd62a9632006-08-23 20:37:59 +0000221 bool CompareFailed = false;
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000222 while (true) {
Reid Spencerd62a9632006-08-23 20:37:59 +0000223 // Scan for the end of file or next difference.
Richard Trieu7a083812016-02-18 22:09:30 +0000224 while (F1P < File1End && F2P < File2End && *F1P == *F2P) {
225 ++F1P;
226 ++F2P;
227 }
Reid Spencerd62a9632006-08-23 20:37:59 +0000228
229 if (F1P >= File1End || F2P >= File2End) break;
230
231 // Okay, we must have found a difference. Backup to the start of the
232 // current number each stream is at so that we can compare from the
233 // beginning.
234 F1P = BackupNumber(F1P, File1Start);
235 F2P = BackupNumber(F2P, File2Start);
236
237 // Now that we are at the start of the numbers, compare them, exiting if
238 // they don't match.
239 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
240 CompareFailed = true;
241 break;
242 }
243 }
244
245 // Okay, we reached the end of file. If both files are at the end, we
246 // succeeded.
247 bool F1AtEnd = F1P >= File1End;
248 bool F2AtEnd = F2P >= File2End;
249 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
250 // Else, we might have run off the end due to a number: backup and retry.
251 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
252 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
253 F1P = BackupNumber(F1P, File1Start);
254 F2P = BackupNumber(F2P, File2Start);
255
256 // Now that we are at the start of the numbers, compare them, exiting if
257 // they don't match.
258 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
259 CompareFailed = true;
260
261 // If we found the end, we succeeded.
262 if (F1P < File1End || F2P < File2End)
263 CompareFailed = true;
264 }
265
Reid Spencerd62a9632006-08-23 20:37:59 +0000266 return CompareFailed;
Chris Lattner16a43682005-01-23 03:13:43 +0000267}