[lib/ObjectYAML] - Cleanup the private interface of ELFState<ELFT>. NFCI.
[llvm-complete.git] / lib / Support / FileUtilities.cpp
blob62eb7bfda195d93c84502ec948e4b934c2db7afd
1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a family of utility functions which are useful for doing
10 // various things with files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/FileUtilities.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/ErrorOr.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cctype>
20 #include <cmath>
21 #include <cstdint>
22 #include <cstdlib>
23 #include <cstring>
24 #include <memory>
25 #include <system_error>
27 using namespace llvm;
29 static bool isSignedChar(char C) {
30 return (C == '+' || C == '-');
33 static bool isExponentChar(char C) {
34 switch (C) {
35 case 'D': // Strange exponential notation.
36 case 'd': // Strange exponential notation.
37 case 'e':
38 case 'E': return true;
39 default: return false;
43 static bool isNumberChar(char C) {
44 switch (C) {
45 case '0': case '1': case '2': case '3': case '4':
46 case '5': case '6': case '7': case '8': case '9':
47 case '.': return true;
48 default: return isSignedChar(C) || isExponentChar(C);
52 static const char *BackupNumber(const char *Pos, const char *FirstChar) {
53 // If we didn't stop in the middle of a number, don't backup.
54 if (!isNumberChar(*Pos)) return Pos;
56 // Otherwise, return to the start of the number.
57 bool HasPeriod = false;
58 while (Pos > FirstChar && isNumberChar(Pos[-1])) {
59 // Backup over at most one period.
60 if (Pos[-1] == '.') {
61 if (HasPeriod)
62 break;
63 HasPeriod = true;
66 --Pos;
67 if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
68 break;
70 return Pos;
73 /// EndOfNumber - Return the first character that is not part of the specified
74 /// number. This assumes that the buffer is null terminated, so it won't fall
75 /// off the end.
76 static const char *EndOfNumber(const char *Pos) {
77 while (isNumberChar(*Pos))
78 ++Pos;
79 return Pos;
82 /// CompareNumbers - compare two numbers, returning true if they are different.
83 static bool CompareNumbers(const char *&F1P, const char *&F2P,
84 const char *F1End, const char *F2End,
85 double AbsTolerance, double RelTolerance,
86 std::string *ErrorMsg) {
87 const char *F1NumEnd, *F2NumEnd;
88 double V1 = 0.0, V2 = 0.0;
90 // If one of the positions is at a space and the other isn't, chomp up 'til
91 // the end of the space.
92 while (isspace(static_cast<unsigned char>(*F1P)) && F1P != F1End)
93 ++F1P;
94 while (isspace(static_cast<unsigned char>(*F2P)) && F2P != F2End)
95 ++F2P;
97 // If we stop on numbers, compare their difference.
98 if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) {
99 // The diff failed.
100 F1NumEnd = F1P;
101 F2NumEnd = F2P;
102 } else {
103 // Note that some ugliness is built into this to permit support for numbers
104 // that use "D" or "d" as their exponential marker, e.g. "1.234D45". This
105 // occurs in 200.sixtrack in spec2k.
106 V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
107 V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
109 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
110 // Copy string into tmp buffer to replace the 'D' with an 'e'.
111 SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1);
112 // Strange exponential notation!
113 StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';
115 V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
116 F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
119 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
120 // Copy string into tmp buffer to replace the 'D' with an 'e'.
121 SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1);
122 // Strange exponential notation!
123 StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';
125 V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
126 F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
130 if (F1NumEnd == F1P || F2NumEnd == F2P) {
131 if (ErrorMsg) {
132 *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
133 *ErrorMsg += F1P[0];
134 *ErrorMsg += "' and '";
135 *ErrorMsg += F2P[0];
136 *ErrorMsg += "'";
138 return true;
141 // Check to see if these are inside the absolute tolerance
142 if (AbsTolerance < std::abs(V1-V2)) {
143 // Nope, check the relative tolerance...
144 double Diff;
145 if (V2)
146 Diff = std::abs(V1/V2 - 1.0);
147 else if (V1)
148 Diff = std::abs(V2/V1 - 1.0);
149 else
150 Diff = 0; // Both zero.
151 if (Diff > RelTolerance) {
152 if (ErrorMsg) {
153 raw_string_ostream(*ErrorMsg)
154 << "Compared: " << V1 << " and " << V2 << '\n'
155 << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'
156 << "Out of tolerance: rel/abs: " << RelTolerance << '/'
157 << AbsTolerance;
159 return true;
163 // Otherwise, advance our read pointers to the end of the numbers.
164 F1P = F1NumEnd; F2P = F2NumEnd;
165 return false;
168 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
169 /// files match, 1 if they are different, and 2 if there is a file error. This
170 /// function differs from DiffFiles in that you can specify an absolete and
171 /// relative FP error that is allowed to exist. If you specify a string to fill
172 /// in for the error option, it will set the string to an error message if an
173 /// error occurs, allowing the caller to distinguish between a failed diff and a
174 /// file system error.
176 int llvm::DiffFilesWithTolerance(StringRef NameA,
177 StringRef NameB,
178 double AbsTol, double RelTol,
179 std::string *Error) {
180 // Now its safe to mmap the files into memory because both files
181 // have a non-zero size.
182 ErrorOr<std::unique_ptr<MemoryBuffer>> F1OrErr = MemoryBuffer::getFile(NameA);
183 if (std::error_code EC = F1OrErr.getError()) {
184 if (Error)
185 *Error = EC.message();
186 return 2;
188 MemoryBuffer &F1 = *F1OrErr.get();
190 ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB);
191 if (std::error_code EC = F2OrErr.getError()) {
192 if (Error)
193 *Error = EC.message();
194 return 2;
196 MemoryBuffer &F2 = *F2OrErr.get();
198 // Okay, now that we opened the files, scan them for the first difference.
199 const char *File1Start = F1.getBufferStart();
200 const char *File2Start = F2.getBufferStart();
201 const char *File1End = F1.getBufferEnd();
202 const char *File2End = F2.getBufferEnd();
203 const char *F1P = File1Start;
204 const char *F2P = File2Start;
205 uint64_t A_size = F1.getBufferSize();
206 uint64_t B_size = F2.getBufferSize();
208 // Are the buffers identical? Common case: Handle this efficiently.
209 if (A_size == B_size &&
210 std::memcmp(File1Start, File2Start, A_size) == 0)
211 return 0;
213 // Otherwise, we are done a tolerances are set.
214 if (AbsTol == 0 && RelTol == 0) {
215 if (Error)
216 *Error = "Files differ without tolerance allowance";
217 return 1; // Files different!
220 bool CompareFailed = false;
221 while (true) {
222 // Scan for the end of file or next difference.
223 while (F1P < File1End && F2P < File2End && *F1P == *F2P) {
224 ++F1P;
225 ++F2P;
228 if (F1P >= File1End || F2P >= File2End) break;
230 // Okay, we must have found a difference. Backup to the start of the
231 // current number each stream is at so that we can compare from the
232 // beginning.
233 F1P = BackupNumber(F1P, File1Start);
234 F2P = BackupNumber(F2P, File2Start);
236 // Now that we are at the start of the numbers, compare them, exiting if
237 // they don't match.
238 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
239 CompareFailed = true;
240 break;
244 // Okay, we reached the end of file. If both files are at the end, we
245 // succeeded.
246 bool F1AtEnd = F1P >= File1End;
247 bool F2AtEnd = F2P >= File2End;
248 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
249 // Else, we might have run off the end due to a number: backup and retry.
250 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
251 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
252 F1P = BackupNumber(F1P, File1Start);
253 F2P = BackupNumber(F2P, File2Start);
255 // Now that we are at the start of the numbers, compare them, exiting if
256 // they don't match.
257 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
258 CompareFailed = true;
260 // If we found the end, we succeeded.
261 if (F1P < File1End || F2P < File2End)
262 CompareFailed = true;
265 return CompareFailed;