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