1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a family of utility functions which are useful for doing
11 // various things with files.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/FileUtilities.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
26 #include <system_error>
30 static bool isSignedChar(char C
) {
31 return (C
== '+' || C
== '-');
34 static bool isExponentChar(char C
) {
36 case 'D': // Strange exponential notation.
37 case 'd': // Strange exponential notation.
39 case 'E': return true;
40 default: return false;
44 static bool isNumberChar(char 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;
49 default: return isSignedChar(C
) || isExponentChar(C
);
53 static const char *BackupNumber(const char *Pos
, const char *FirstChar
) {
54 // If we didn't stop in the middle of a number, don't backup.
55 if (!isNumberChar(*Pos
)) return Pos
;
57 // Otherwise, return to the start of the number.
58 bool HasPeriod
= false;
59 while (Pos
> FirstChar
&& isNumberChar(Pos
[-1])) {
60 // Backup over at most one period.
68 if (Pos
> FirstChar
&& isSignedChar(Pos
[0]) && !isExponentChar(Pos
[-1]))
74 /// 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
77 static const char *EndOfNumber(const char *Pos
) {
78 while (isNumberChar(*Pos
))
83 /// CompareNumbers - compare two numbers, returning true if they are different.
84 static bool CompareNumbers(const char *&F1P
, const char *&F2P
,
85 const char *F1End
, const char *F2End
,
86 double AbsTolerance
, double RelTolerance
,
87 std::string
*ErrorMsg
) {
88 const char *F1NumEnd
, *F2NumEnd
;
89 double V1
= 0.0, V2
= 0.0;
91 // If one of the positions is at a space and the other isn't, chomp up 'til
92 // the end of the space.
93 while (isspace(static_cast<unsigned char>(*F1P
)) && F1P
!= F1End
)
95 while (isspace(static_cast<unsigned char>(*F2P
)) && F2P
!= F2End
)
98 // If we stop on numbers, compare their difference.
99 if (!isNumberChar(*F1P
) || !isNumberChar(*F2P
)) {
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
));
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);
113 // Strange exponential notation!
114 StrTmp
[static_cast<unsigned>(F1NumEnd
-F1P
)] = 'e';
116 V1
= strtod(&StrTmp
[0], const_cast<char**>(&F1NumEnd
));
117 F1NumEnd
= F1P
+ (F1NumEnd
-&StrTmp
[0]);
120 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);
123 // Strange exponential notation!
124 StrTmp
[static_cast<unsigned>(F2NumEnd
-F2P
)] = 'e';
126 V2
= strtod(&StrTmp
[0], const_cast<char**>(&F2NumEnd
));
127 F2NumEnd
= F2P
+ (F2NumEnd
-&StrTmp
[0]);
131 if (F1NumEnd
== F1P
|| F2NumEnd
== F2P
) {
133 *ErrorMsg
= "FP Comparison failed, not a numeric difference between '";
135 *ErrorMsg
+= "' and '";
142 // Check to see if these are inside the absolute tolerance
143 if (AbsTolerance
< std::abs(V1
-V2
)) {
144 // Nope, check the relative tolerance...
147 Diff
= std::abs(V1
/V2
- 1.0);
149 Diff
= std::abs(V2
/V1
- 1.0);
151 Diff
= 0; // Both zero.
152 if (Diff
> RelTolerance
) {
154 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
<< '/'
164 // Otherwise, advance our read pointers to the end of the numbers.
165 F1P
= F1NumEnd
; F2P
= F2NumEnd
;
169 /// 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.
177 int llvm::DiffFilesWithTolerance(StringRef NameA
,
179 double AbsTol
, double RelTol
,
180 std::string
*Error
) {
181 // Now its safe to mmap the files into memory because both files
182 // have a non-zero size.
183 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> F1OrErr
= MemoryBuffer::getFile(NameA
);
184 if (std::error_code EC
= F1OrErr
.getError()) {
186 *Error
= EC
.message();
189 MemoryBuffer
&F1
= *F1OrErr
.get();
191 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> F2OrErr
= MemoryBuffer::getFile(NameB
);
192 if (std::error_code EC
= F2OrErr
.getError()) {
194 *Error
= EC
.message();
197 MemoryBuffer
&F2
= *F2OrErr
.get();
199 // Okay, now that we opened the files, scan them for the first difference.
200 const char *File1Start
= F1
.getBufferStart();
201 const char *File2Start
= F2
.getBufferStart();
202 const char *File1End
= F1
.getBufferEnd();
203 const char *File2End
= F2
.getBufferEnd();
204 const char *F1P
= File1Start
;
205 const char *F2P
= File2Start
;
206 uint64_t A_size
= F1
.getBufferSize();
207 uint64_t B_size
= F2
.getBufferSize();
209 // Are the buffers identical? Common case: Handle this efficiently.
210 if (A_size
== B_size
&&
211 std::memcmp(File1Start
, File2Start
, A_size
) == 0)
214 // Otherwise, we are done a tolerances are set.
215 if (AbsTol
== 0 && RelTol
== 0) {
217 *Error
= "Files differ without tolerance allowance";
218 return 1; // Files different!
221 bool CompareFailed
= false;
223 // Scan for the end of file or next difference.
224 while (F1P
< File1End
&& F2P
< File2End
&& *F1P
== *F2P
) {
229 if (F1P
>= File1End
|| F2P
>= File2End
) break;
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
234 F1P
= BackupNumber(F1P
, File1Start
);
235 F2P
= BackupNumber(F2P
, File2Start
);
237 // Now that we are at the start of the numbers, compare them, exiting if
239 if (CompareNumbers(F1P
, F2P
, File1End
, File2End
, AbsTol
, RelTol
, Error
)) {
240 CompareFailed
= true;
245 // Okay, we reached the end of file. If both files are at the end, we
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
);
256 // Now that we are at the start of the numbers, compare them, exiting if
258 if (CompareNumbers(F1P
, F2P
, File1End
, File2End
, AbsTol
, RelTol
, Error
))
259 CompareFailed
= true;
261 // If we found the end, we succeeded.
262 if (F1P
< File1End
|| F2P
< File2End
)
263 CompareFailed
= true;
266 return CompareFailed
;