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