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