- try {
- // Map in the files into memory.
- sys::MappedFile F1(FileA);
- sys::MappedFile F2(FileB);
- F1.map();
- F2.map();
-
- // Okay, now that we opened the files, scan them for the first difference.
- char *File1Start = F1.charBase();
- char *File2Start = F2.charBase();
- char *File1End = File1Start+F1.size();
- char *File2End = File2Start+F2.size();
- char *F1P = File1Start;
- char *F2P = File2Start;
-
- // Scan for the end of file or first difference.
+ // Now its safe to mmap the files into memory because both files
+ // have a non-zero size.
+ ErrorOr<std::unique_ptr<MemoryBuffer>> F1OrErr = MemoryBuffer::getFile(NameA);
+ if (std::error_code EC = F1OrErr.getError()) {
+ if (Error)
+ *Error = EC.message();
+ return 2;
+ }
+ std::unique_ptr<MemoryBuffer> F1 = std::move(F1OrErr.get());
+
+ ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB);
+ if (std::error_code EC = F2OrErr.getError()) {
+ if (Error)
+ *Error = EC.message();
+ return 2;
+ }
+ std::unique_ptr<MemoryBuffer> F2 = std::move(F2OrErr.get());
+
+ // Okay, now that we opened the files, scan them for the first difference.
+ const char *File1Start = F1->getBufferStart();
+ const char *File2Start = F2->getBufferStart();
+ const char *File1End = F1->getBufferEnd();
+ const char *File2End = F2->getBufferEnd();
+ const char *F1P = File1Start;
+ const char *F2P = File2Start;
+ uint64_t A_size = F1->getBufferSize();
+ uint64_t B_size = F2->getBufferSize();
+
+ // Are the buffers identical? Common case: Handle this efficiently.
+ if (A_size == B_size &&
+ std::memcmp(File1Start, File2Start, A_size) == 0)
+ return 0;
+
+ // Otherwise, we are done a tolerances are set.
+ if (AbsTol == 0 && RelTol == 0) {
+ if (Error)
+ *Error = "Files differ without tolerance allowance";
+ return 1; // Files different!
+ }
+
+ bool CompareFailed = false;
+ while (1) {
+ // Scan for the end of file or next difference.