Special versions of the dep graph and scheduled for SMS for superblocks.
[llvm-complete.git] / lib / Support / FileUtilities.cpp
blobdd296f30a2279b451d29a18f5d3de0c8bbb1b16f
1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/System/Path.h"
17 #include "llvm/System/MappedFile.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include <cmath>
20 #include <cstring>
21 #include <cctype>
22 using namespace llvm;
24 static bool isNumberChar(char C) {
25 switch (C) {
26 case '0': case '1': case '2': case '3': case '4':
27 case '5': case '6': case '7': case '8': case '9':
28 case '.': case '+': case '-':
29 case 'e':
30 case 'E': return true;
31 default: return false;
35 static char *BackupNumber(char *Pos, char *FirstChar) {
36 // If we didn't stop in the middle of a number, don't backup.
37 if (!isNumberChar(*Pos)) return Pos;
39 // Otherwise, return to the start of the number.
40 while (Pos > FirstChar && isNumberChar(Pos[-1]))
41 --Pos;
42 return Pos;
45 /// CompareNumbers - compare two numbers, returning true if they are different.
46 static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End,
47 double AbsTolerance, double RelTolerance,
48 std::string *ErrorMsg) {
49 char *F1NumEnd, *F2NumEnd;
50 double V1 = 0.0, V2 = 0.0;
52 // If one of the positions is at a space and the other isn't, chomp up 'til
53 // the end of the space.
54 while (isspace(*F1P) && F1P != F1End)
55 ++F1P;
56 while (isspace(*F2P) && F2P != F2End)
57 ++F2P;
59 // If we stop on numbers, compare their difference.
60 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
61 V1 = strtod(F1P, &F1NumEnd);
62 V2 = strtod(F2P, &F2NumEnd);
63 } else {
64 // Otherwise, the diff failed.
65 F1NumEnd = F1P;
66 F2NumEnd = F2P;
69 if (F1NumEnd == F1P || F2NumEnd == F2P) {
70 if (ErrorMsg) *ErrorMsg = "Comparison failed, not a numeric difference.";
71 return true;
74 // Check to see if these are inside the absolute tolerance
75 if (AbsTolerance < std::abs(V1-V2)) {
76 // Nope, check the relative tolerance...
77 double Diff;
78 if (V2)
79 Diff = std::abs(V1/V2 - 1.0);
80 else if (V1)
81 Diff = std::abs(V2/V1 - 1.0);
82 else
83 Diff = 0; // Both zero.
84 if (Diff > RelTolerance) {
85 if (ErrorMsg) {
86 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) +
87 ": diff = " + ftostr(Diff) + "\n";
88 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) +
89 "/" + ftostr(AbsTolerance);
91 return true;
95 // Otherwise, advance our read pointers to the end of the numbers.
96 F1P = F1NumEnd; F2P = F2NumEnd;
97 return false;
100 // PadFileIfNeeded - If the files are not identical, we will have to be doing
101 // numeric comparisons in here. There are bad cases involved where we (i.e.,
102 // strtod) might run off the beginning or end of the file if it starts or ends
103 // with a number. Because of this, if needed, we pad the file so that it starts
104 // and ends with a null character.
105 static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
106 if (FileStart-FileEnd < 2 ||
107 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
108 unsigned FileLen = FileEnd-FileStart;
109 char *NewFile = new char[FileLen+2];
110 NewFile[0] = 0; // Add null padding
111 NewFile[FileLen+1] = 0; // Add null padding
112 memcpy(NewFile+1, FileStart, FileLen);
113 FP = NewFile+(FP-FileStart)+1;
114 FileStart = NewFile+1;
115 FileEnd = FileStart+FileLen;
119 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
120 /// files match, 1 if they are different, and 2 if there is a file error. This
121 /// function differs from DiffFiles in that you can specify an absolete and
122 /// relative FP error that is allowed to exist. If you specify a string to fill
123 /// in for the error option, it will set the string to an error message if an
124 /// error occurs, allowing the caller to distinguish between a failed diff and a
125 /// file system error.
127 int llvm::DiffFilesWithTolerance(const sys::Path &FileA,
128 const sys::Path &FileB,
129 double AbsTol, double RelTol,
130 std::string *Error) {
131 try {
132 // Check for zero length files because some systems croak when you try to
133 // mmap an empty file.
134 size_t A_size = FileA.getSize();
135 size_t B_size = FileB.getSize();
137 // If they are both zero sized then they're the same
138 if (A_size == 0 && B_size == 0)
139 return 0;
140 // If only one of them is zero sized then they can't be the same
141 if ((A_size == 0 || B_size == 0))
142 return 1;
144 // Now its safe to mmap the files into memory becasue both files
145 // have a non-zero size.
146 sys::MappedFile F1(FileA);
147 sys::MappedFile F2(FileB);
148 F1.map();
149 F2.map();
151 // Okay, now that we opened the files, scan them for the first difference.
152 char *File1Start = F1.charBase();
153 char *File2Start = F2.charBase();
154 char *File1End = File1Start+A_size;
155 char *File2End = File2Start+B_size;
156 char *F1P = File1Start;
157 char *F2P = File2Start;
159 if (A_size == B_size) {
160 // Are the buffers identical?
161 if (std::memcmp(File1Start, File2Start, A_size) == 0)
162 return 0;
164 if (AbsTol == 0 && RelTol == 0)
165 return 1; // Files different!
168 char *OrigFile1Start = File1Start;
169 char *OrigFile2Start = File2Start;
171 // If the files need padding, do so now.
172 PadFileIfNeeded(File1Start, File1End, F1P);
173 PadFileIfNeeded(File2Start, File2End, F2P);
175 bool CompareFailed = false;
176 while (1) {
177 // Scan for the end of file or next difference.
178 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
179 ++F1P, ++F2P;
181 if (F1P >= File1End || F2P >= File2End) break;
183 // Okay, we must have found a difference. Backup to the start of the
184 // current number each stream is at so that we can compare from the
185 // beginning.
186 F1P = BackupNumber(F1P, File1Start);
187 F2P = BackupNumber(F2P, File2Start);
189 // Now that we are at the start of the numbers, compare them, exiting if
190 // they don't match.
191 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
192 CompareFailed = true;
193 break;
197 // Okay, we reached the end of file. If both files are at the end, we
198 // succeeded.
199 bool F1AtEnd = F1P >= File1End;
200 bool F2AtEnd = F2P >= File2End;
201 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
202 // Else, we might have run off the end due to a number: backup and retry.
203 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
204 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
205 F1P = BackupNumber(F1P, File1Start);
206 F2P = BackupNumber(F2P, File2Start);
208 // Now that we are at the start of the numbers, compare them, exiting if
209 // they don't match.
210 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
211 CompareFailed = true;
213 // If we found the end, we succeeded.
214 if (F1P < File1End || F2P < File2End)
215 CompareFailed = true;
218 if (OrigFile1Start != File1Start)
219 delete[] (File1Start-1); // Back up past null byte
220 if (OrigFile2Start != File2Start)
221 delete[] (File2Start-1); // Back up past null byte
222 return CompareFailed;
223 } catch (const std::string &Msg) {
224 if (Error) *Error = Msg;
225 return 2;