[PATCH] D36768: [test-suite] Add -i option to fpcmp to ignore whitespace changes.

Michael Kruse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 15 14:17:24 PDT 2017


Meinersbur created this revision.

Before this patch, fpcmp could loop forever when comparing two whitespace characters (e.g. '\n' vs. '\r' which typically happens when comparing files that only differ in LF vs. CRLF line endings).

Add a switch '-i' to fpcmp that allows ignoring whitespace changes. This is required for SPEC CPU 2017  511.povray_r which has its reference output stored with CRLF line endings, but the actual output on UNIX system has LF line endings.


https://reviews.llvm.org/D36768

Files:
  tools/fpcmp.c


Index: tools/fpcmp.c
===================================================================
--- tools/fpcmp.c
+++ tools/fpcmp.c
@@ -217,7 +217,8 @@
 
 int diff_files_with_tolerance(const char *path_a, const char *path_b,
                               double absolute_tolerance,
-                              double relative_tolerance) {
+                              double relative_tolerance,
+                              bool ignore_whitespace) {
   /* First, load the file buffers completely into memory. */
   long A_size, B_size;
   char *data_a = load_file(path_a, &A_size);
@@ -246,8 +247,34 @@
 
   while (1) {
     // Scan for the end of file or next difference.
-    while (F1P < File1End && F2P < File2End && *F1P == *F2P)
-      ++F1P, ++F2P;
+    while (F1P < File1End && F2P < File2End) {
+      if (*F1P == *F2P) {
+        ++F1P, ++F2P;
+        continue;
+      }
+
+      // If both chars are whitespace, then we have a
+      // whitespace-only difference (e.g. CR vs. LF).
+      // Without this check, we would be in an infinite loop.
+      if (!ignore_whitespace && isspace(*F1P) && isspace(*F2P))
+        return 1;
+
+      // With whitespace ignored, skip whitespace chars and recheck for
+      // the next difference.
+      if (ignore_whitespace) {
+        if (isspace(*F1P)) {
+          ++F1P;
+          continue;
+        }
+
+        if (isspace(*F2P)) {
+          ++F2P;
+          continue;
+        }
+      }
+
+      break;
+    }
 
     if (F1P >= File1End || F2P >= File2End) break;
 
@@ -296,17 +323,19 @@
 }
 
 void usage() {
-  fprintf(stderr, "usage: %s [-a VALUE] [-r VALUE] <path-A> <path-B>\n\n",
+  fprintf(stderr, "usage: %s [-a VALUE] [-r VALUE] [-i] <path-A> <path-B>\n\n",
           g_program);
   fprintf(stderr, "Compare two files using absolute and relative tolerances\n");
   fprintf(stderr, "when comparing differences between two character\n");
   fprintf(stderr, "which could be real numbers\n");
+  fprintf(stderr, "The -i switch ignores whitespace differences\n");
   exit(2);
 }
 
 int main(int argc, char * const argv[]) {
   double relative_tolerance = 0.0;
   double absolute_tolerance = 0.0;
+  bool ignore_whitespace = false;
   int i;
 
   g_program = argv[0];
@@ -341,6 +370,10 @@
       }
       break;
 
+    case 'i':
+      ignore_whitespace = true;
+      break;
+
     default:
       fprintf(stderr, "error: invalid argument '%s'\n\n", arg);
       usage();
@@ -352,6 +385,6 @@
     usage();
   }
 
-  return diff_files_with_tolerance(argv[i], argv[i + 1],
-                                   absolute_tolerance, relative_tolerance);
+  return diff_files_with_tolerance(argv[i], argv[i + 1], absolute_tolerance,
+                                   relative_tolerance, ignore_whitespace);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36768.111253.patch
Type: text/x-patch
Size: 2773 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170815/03210fa6/attachment.bin>


More information about the llvm-commits mailing list