llvm.org GIT mirror llvm / 8e6d2bb
Make fpcmp handle running off of the beginning or end of the file correctly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14259 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 15 years ago
1 changed file(s) with 35 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
103103 F1P = F1NumEnd; F2P = F2NumEnd;
104104 }
105105
106 // PadFileIfNeeded - If the files are not identical, we will have to be doing
107 // numeric comparisons in here. There are bad cases involved where we (i.e.,
108 // strtod) might run off the beginning or end of the file if it starts or ends
109 // with a number. Because of this, if needed, we pad the file so that it starts
110 // and ends with a null character.
111 static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
112 if (isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
113 unsigned FileLen = FileEnd-FileStart;
114 char *NewFile = new char[FileLen+2];
115 NewFile[0] = 0; // Add null padding
116 NewFile[FileLen+1] = 0; // Add null padding
117 memcpy(NewFile+1, FileStart, FileLen);
118 FP = NewFile+(FP-FileStart)+1;
119 FileStart = NewFile+1;
120 FileEnd = FileStart+FileLen;
121 }
122 }
106123
107124 int main(int argc, char **argv) {
108125 cl::ParseCommandLineOptions(argc, argv);
118135 char *File2End = File2Start+File2Len;
119136 char *F1P = File1Start;
120137 char *F2P = File2Start;
138
139 // Scan for the end of file or first difference.
140 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
141 ++F1P, ++F2P;
142
143 // Common case: identifical files.
144 if (F1P == File1End && F2P == File2End) return 0;
145
146 // If the files need padding, do so now.
147 PadFileIfNeeded(File1Start, File1End, F1P);
148 PadFileIfNeeded(File2Start, File2End, F2P);
121149
122150 while (1) {
123 // Scan for the end of file or first difference.
151 // Scan for the end of file or next difference.
124152 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
125153 ++F1P, ++F2P;
126154
139167
140168 // Okay, we reached the end of file. If both files are at the end, we
141169 // succeeded.
142 if (F1P >= File1End && F2P >= File2End) return 0;
170 bool F1AtEnd = F1P >= File1End;
171 bool F2AtEnd = F2P >= File2End;
172 if (F1AtEnd & F2AtEnd) return 0;
143173
144 // Otherwise, we might have run off the end due to a number, backup and retry.
174 // Otherwise, we might have run off the end due to a number: backup and retry.
175 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
176 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
145177 F1P = BackupNumber(F1P, File1Start);
146178 F2P = BackupNumber(F2P, File2Start);
147179