LLVM API Documentation
00001 //===- Support/FileUtilities.cpp - File System Utilities ------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a family of utility functions which are useful for doing 00011 // various things with files. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Support/FileUtilities.h" 00016 #include "llvm/System/Path.h" 00017 #include "llvm/System/MappedFile.h" 00018 #include "llvm/ADT/StringExtras.h" 00019 #include <cmath> 00020 #include <cstring> 00021 #include <cctype> 00022 using namespace llvm; 00023 00024 static bool isNumberChar(char C) { 00025 switch (C) { 00026 case '0': case '1': case '2': case '3': case '4': 00027 case '5': case '6': case '7': case '8': case '9': 00028 case '.': case '+': case '-': 00029 case 'D': // Strange exponential notation. 00030 case 'd': // Strange exponential notation. 00031 case 'e': 00032 case 'E': return true; 00033 default: return false; 00034 } 00035 } 00036 00037 static char *BackupNumber(char *Pos, char *FirstChar) { 00038 // If we didn't stop in the middle of a number, don't backup. 00039 if (!isNumberChar(*Pos)) return Pos; 00040 00041 // Otherwise, return to the start of the number. 00042 while (Pos > FirstChar && isNumberChar(Pos[-1])) 00043 --Pos; 00044 return Pos; 00045 } 00046 00047 /// CompareNumbers - compare two numbers, returning true if they are different. 00048 static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End, 00049 double AbsTolerance, double RelTolerance, 00050 std::string *ErrorMsg) { 00051 char *F1NumEnd, *F2NumEnd; 00052 double V1 = 0.0, V2 = 0.0; 00053 00054 // If one of the positions is at a space and the other isn't, chomp up 'til 00055 // the end of the space. 00056 while (isspace(*F1P) && F1P != F1End) 00057 ++F1P; 00058 while (isspace(*F2P) && F2P != F2End) 00059 ++F2P; 00060 00061 // If we stop on numbers, compare their difference. Note that some ugliness 00062 // is built into this to permit support for numbers that use "D" or "d" as 00063 // their exponential marker, e.g. "1.234D45". This occurs in 200.sixtrack in 00064 // spec2k. 00065 if (isNumberChar(*F1P) && isNumberChar(*F2P)) { 00066 bool isDNotation; 00067 do { 00068 isDNotation = false; 00069 V1 = strtod(F1P, &F1NumEnd); 00070 V2 = strtod(F2P, &F2NumEnd); 00071 00072 if (*F1NumEnd == 'D' || *F1NumEnd == 'd') { 00073 *F1NumEnd = 'e'; // Strange exponential notation! 00074 isDNotation = true; 00075 } 00076 if (*F2NumEnd == 'D' || *F2NumEnd == 'd') { 00077 *F2NumEnd = 'e'; // Strange exponential notation! 00078 isDNotation = true; 00079 } 00080 } while (isDNotation); 00081 } else { 00082 // Otherwise, the diff failed. 00083 F1NumEnd = F1P; 00084 F2NumEnd = F2P; 00085 } 00086 00087 if (F1NumEnd == F1P || F2NumEnd == F2P) { 00088 if (ErrorMsg) *ErrorMsg = "Comparison failed, not a numeric difference."; 00089 return true; 00090 } 00091 00092 // Check to see if these are inside the absolute tolerance 00093 if (AbsTolerance < std::abs(V1-V2)) { 00094 // Nope, check the relative tolerance... 00095 double Diff; 00096 if (V2) 00097 Diff = std::abs(V1/V2 - 1.0); 00098 else if (V1) 00099 Diff = std::abs(V2/V1 - 1.0); 00100 else 00101 Diff = 0; // Both zero. 00102 if (Diff > RelTolerance) { 00103 if (ErrorMsg) { 00104 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) + 00105 ": diff = " + ftostr(Diff) + "\n"; 00106 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) + 00107 "/" + ftostr(AbsTolerance); 00108 } 00109 return true; 00110 } 00111 } 00112 00113 // Otherwise, advance our read pointers to the end of the numbers. 00114 F1P = F1NumEnd; F2P = F2NumEnd; 00115 return false; 00116 } 00117 00118 // PadFileIfNeeded - If the files are not identical, we will have to be doing 00119 // numeric comparisons in here. There are bad cases involved where we (i.e., 00120 // strtod) might run off the beginning or end of the file if it starts or ends 00121 // with a number. Because of this, if needed, we pad the file so that it starts 00122 // and ends with a null character. 00123 static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) { 00124 if (FileStart-FileEnd < 2 || 00125 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) { 00126 unsigned FileLen = FileEnd-FileStart; 00127 char *NewFile = new char[FileLen+2]; 00128 NewFile[0] = 0; // Add null padding 00129 NewFile[FileLen+1] = 0; // Add null padding 00130 memcpy(NewFile+1, FileStart, FileLen); 00131 FP = NewFile+(FP-FileStart)+1; 00132 FileStart = NewFile+1; 00133 FileEnd = FileStart+FileLen; 00134 } 00135 } 00136 00137 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the 00138 /// files match, 1 if they are different, and 2 if there is a file error. This 00139 /// function differs from DiffFiles in that you can specify an absolete and 00140 /// relative FP error that is allowed to exist. If you specify a string to fill 00141 /// in for the error option, it will set the string to an error message if an 00142 /// error occurs, allowing the caller to distinguish between a failed diff and a 00143 /// file system error. 00144 /// 00145 int llvm::DiffFilesWithTolerance(const sys::Path &FileA, 00146 const sys::Path &FileB, 00147 double AbsTol, double RelTol, 00148 std::string *Error) { 00149 try { 00150 // Check for zero length files because some systems croak when you try to 00151 // mmap an empty file. 00152 size_t A_size = FileA.getSize(); 00153 size_t B_size = FileB.getSize(); 00154 00155 // If they are both zero sized then they're the same 00156 if (A_size == 0 && B_size == 0) 00157 return 0; 00158 // If only one of them is zero sized then they can't be the same 00159 if ((A_size == 0 || B_size == 0)) 00160 return 1; 00161 00162 // Now its safe to mmap the files into memory becasue both files 00163 // have a non-zero size. 00164 sys::MappedFile F1(FileA); 00165 sys::MappedFile F2(FileB); 00166 F1.map(); 00167 F2.map(); 00168 00169 // Okay, now that we opened the files, scan them for the first difference. 00170 char *File1Start = F1.charBase(); 00171 char *File2Start = F2.charBase(); 00172 char *File1End = File1Start+A_size; 00173 char *File2End = File2Start+B_size; 00174 char *F1P = File1Start; 00175 char *F2P = File2Start; 00176 00177 if (A_size == B_size) { 00178 // Are the buffers identical? 00179 if (std::memcmp(File1Start, File2Start, A_size) == 0) 00180 return 0; 00181 00182 if (AbsTol == 0 && RelTol == 0) 00183 return 1; // Files different! 00184 } 00185 00186 char *OrigFile1Start = File1Start; 00187 char *OrigFile2Start = File2Start; 00188 00189 // If the files need padding, do so now. 00190 PadFileIfNeeded(File1Start, File1End, F1P); 00191 PadFileIfNeeded(File2Start, File2End, F2P); 00192 00193 bool CompareFailed = false; 00194 while (1) { 00195 // Scan for the end of file or next difference. 00196 while (F1P < File1End && F2P < File2End && *F1P == *F2P) 00197 ++F1P, ++F2P; 00198 00199 if (F1P >= File1End || F2P >= File2End) break; 00200 00201 // Okay, we must have found a difference. Backup to the start of the 00202 // current number each stream is at so that we can compare from the 00203 // beginning. 00204 F1P = BackupNumber(F1P, File1Start); 00205 F2P = BackupNumber(F2P, File2Start); 00206 00207 // Now that we are at the start of the numbers, compare them, exiting if 00208 // they don't match. 00209 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) { 00210 CompareFailed = true; 00211 break; 00212 } 00213 } 00214 00215 // Okay, we reached the end of file. If both files are at the end, we 00216 // succeeded. 00217 bool F1AtEnd = F1P >= File1End; 00218 bool F2AtEnd = F2P >= File2End; 00219 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) { 00220 // Else, we might have run off the end due to a number: backup and retry. 00221 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P; 00222 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P; 00223 F1P = BackupNumber(F1P, File1Start); 00224 F2P = BackupNumber(F2P, File2Start); 00225 00226 // Now that we are at the start of the numbers, compare them, exiting if 00227 // they don't match. 00228 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) 00229 CompareFailed = true; 00230 00231 // If we found the end, we succeeded. 00232 if (F1P < File1End || F2P < File2End) 00233 CompareFailed = true; 00234 } 00235 00236 if (OrigFile1Start != File1Start) 00237 delete[] (File1Start-1); // Back up past null byte 00238 if (OrigFile2Start != File2Start) 00239 delete[] (File2Start-1); // Back up past null byte 00240 return CompareFailed; 00241 } catch (const std::string &Msg) { 00242 if (Error) *Error = Msg; 00243 return 2; 00244 } catch (...) { 00245 *Error = "Unknown Exception Occurred"; 00246 return 2; 00247 } 00248 }