gollvm: refactor unit test remastering code

Move some of the functionality that supports auto-remastering
of unit test results out of its current location in BackendCore
and make it part of TestUtils. This change is in preparation for
using it more widely in the unit tests (for example, in the
driver tests).

Change-Id: I9b2cb516483c3e8f3d5183367efb09d9dbcd1363
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/255048
Trust: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/unittests/BackendCore/TestUtils.cpp b/unittests/BackendCore/TestUtils.cpp
index e84e51a..bfca6c1 100644
--- a/unittests/BackendCore/TestUtils.cpp
+++ b/unittests/BackendCore/TestUtils.cpp
@@ -394,8 +394,6 @@
     , lineNum_(1)
     , finished_(false)
     , returnAdded_(false)
-    , emitDumpFilesOnDiff_(false)
-    , findOrphanBBs_(true)
 {
   // establish initial file so as to make verifier happy
   be_->linemap()->start_file("unit_testing.go", 1);
@@ -407,14 +405,6 @@
     entryBlock_ = be()->block(func_, nullptr, emptyVarList_, loc_, loc_);
     curBlock_ = be()->block(func_, nullptr, emptyVarList_, loc_, loc_);
   }
-
-  // debugging
-  if (getenv("GOLLVM_UNITTESTS_BACKENDCORE_EMITDUMPFILES"))
-    emitDumpFilesOnDiff_ = true;
-  if (getenv("GOLLVM_UNITTESTS_EMIT_REMASTER_SCRIPT")) {
-    emitRemasterScript_ = true;
-    emitDumpFilesOnDiff_ = true;
-  }
 }
 
 FcnTestHarness::~FcnTestHarness()
@@ -582,7 +572,7 @@
   std::string actual(repr(st));
   bool equal = difftokens(expected, actual, reason);
   if (! equal)
-    complainOnNequal(reason, ed, actual, emitDumpFilesOnDiff_, emitRemasterScript_);
+    complainOnNequal(reason, ed, actual);
   return equal;
 }
 
@@ -593,7 +583,7 @@
   std::string actual(repr(val));
   bool equal = difftokens(expected, actual, reason);
   if (! equal)
-    complainOnNequal(reason, ed, actual, emitDumpFilesOnDiff_, emitRemasterScript_);
+    complainOnNequal(reason, ed, actual);
   return equal;
 }
 
@@ -604,7 +594,7 @@
   std::string actual(repr(curBlock_));
   bool equal = difftokens(expected, actual, reason);
   if (! equal)
-    complainOnNequal(reason, ed, actual, emitDumpFilesOnDiff_, emitRemasterScript_);
+    complainOnNequal(reason, ed, actual);
   return equal;
 }
 
@@ -633,7 +623,7 @@
   std::string actual(repr(node));
   bool equal = difftokens(expected, actual, reason);
   if (! equal)
-    complainOnNequal(reason, ed, actual, emitDumpFilesOnDiff_, emitRemasterScript_);
+    complainOnNequal(reason, ed, actual);
   return equal;
 }
 
diff --git a/unittests/DriverUtils/DriverUtilsTests.cpp b/unittests/DriverUtils/DriverUtilsTests.cpp
index 4556235..b06306c 100644
--- a/unittests/DriverUtils/DriverUtilsTests.cpp
+++ b/unittests/DriverUtils/DriverUtilsTests.cpp
@@ -223,7 +223,7 @@
   std::string actual(cand.toString());
   bool equal = difftokens(ed.content, actual, reason);
   if (! equal)
-    complainOnNequal(reason, ed, actual, false, false);
+    complainOnNequal(reason, ed, actual);
   return equal;
 }
 
diff --git a/unittests/GoDumpSpec/ParserTests.cpp b/unittests/GoDumpSpec/ParserTests.cpp
index 48299a2..ed1c018 100644
--- a/unittests/GoDumpSpec/ParserTests.cpp
+++ b/unittests/GoDumpSpec/ParserTests.cpp
@@ -58,7 +58,7 @@
   std::string reason;
   bool equal = difftokens(expected, actual, reason);
   if (! equal)
-    complainOnNequal(reason, ed, actual, false, false);
+    complainOnNequal(reason, ed, actual);
   return equal;
 }
 
diff --git a/unittests/GoDumpSpec/TokenizerTests.cpp b/unittests/GoDumpSpec/TokenizerTests.cpp
index 13bb242..d2aeca3 100644
--- a/unittests/GoDumpSpec/TokenizerTests.cpp
+++ b/unittests/GoDumpSpec/TokenizerTests.cpp
@@ -57,7 +57,7 @@
   std::string actual(dump(t));
   bool equal = difftokens(expected, actual, reason);
   if (! equal)
-    complainOnNequal(reason, ed, actual, false, false);
+    complainOnNequal(reason, ed, actual);
   return equal;
 }
 
diff --git a/unittests/TestUtils/DiffUtils.cpp b/unittests/TestUtils/DiffUtils.cpp
index 40a100e..b3295f4 100644
--- a/unittests/TestUtils/DiffUtils.cpp
+++ b/unittests/TestUtils/DiffUtils.cpp
@@ -161,21 +161,29 @@
 
 void complainOnNequal(const std::string &reason,
                       const ExpectedDump &ed,
-                      const std::string &actual,
-                      bool emitDump,
-                      bool emitRemaster)
+                      const std::string &actual)
 {
+  bool emitDumpFilesOnDiff = false;
+  bool emitRemasterScript = false;
+
+  if (getenv("GOLLVM_UNITTESTS_BACKENDCORE_EMITDUMPFILES") != nullptr)
+    emitDumpFilesOnDiff = true;
+  if (getenv("GOLLVM_UNITTESTS_BACKENDCORE_EMITDUMPFILES") != nullptr) {
+    emitRemasterScript = true;
+    emitDumpFilesOnDiff = true;
+  }
+
   std::cerr << reason << "\n";
   const std::string &expected = ed.content;
   std::cerr << "expected dump:\n" << expected << "\n";
   std::cerr << "actual dump:\n" << actual << "\n";
   unsigned mls = macroLineAtStart() ? 1 : 0;
-  if (emitDump) {
+  if (emitDumpFilesOnDiff) {
     static unsigned filecount;
     static FILE *outfp; // script
     emitStringToDumpFile("expected", filecount, expected);
     emitStringToDumpFile("actual", filecount, actual);
-    if (emitRemaster) {
+    if (emitRemasterScript) {
       // HACK: no explicit close for this file. Assume that it will
       // be closed when the unit test finishes running.
       if (outfp == nullptr) {
diff --git a/unittests/TestUtils/DiffUtils.h b/unittests/TestUtils/DiffUtils.h
index 9888ac3..fd98c62 100644
--- a/unittests/TestUtils/DiffUtils.h
+++ b/unittests/TestUtils/DiffUtils.h
@@ -53,13 +53,11 @@
 unsigned countinstances(const std::string &text, const std::string &pat);
 
 // Issue an error message to std:cerr to report the fact that the expected dump
-// in 'ed' does not match the actual dump in 'actual'; emit actual/expected
-// debug dump files if 'emitDump' is true; emit script to perform output
-// remastering if 'emitRemasterScript' is true.
+// in 'ed' does not match the actual dump in 'actual'. Depending on environment
+// variable settings, may also emit dump files and/or remaster script.
 void complainOnNequal(const std::string &reason,
                       const ExpectedDump &ed,
-                      const std::string &actual,
-                      bool emitDump, bool emitRemasterScript);
+                      const std::string &actual);
 
 }