Add ability to test parsing of bitcode records in Subzero.

Extends the NaCl bitcode munger so that the PNaClTranslator parser
can be applied to the defined sequence of record values.

BUG=None
R=jvoung@chromium.org, stichnot@chromium.org

Review URL: https://codereview.chromium.org/800883006
diff --git a/src/IceClFlags.h b/src/IceClFlags.h
index 68255fb..b444dc0 100644
--- a/src/IceClFlags.h
+++ b/src/IceClFlags.h
@@ -27,6 +27,7 @@
         UseSandboxing(false), PhiEdgeSplit(false), DecorateAsm(false),
         DumpStats(false), AllowUninitializedGlobals(false),
         TimeEachFunction(false), DisableIRGeneration(false),
+        AllowErrorRecovery(false), GenerateUnitTestMessages(false),
         DefaultGlobalPrefix(""), DefaultFunctionPrefix(""), TimingFocusOn(""),
         VerboseFocusOn(""), TranslateOnly("") {}
   bool DisableInternal;
@@ -43,6 +44,8 @@
   bool AllowUninitializedGlobals;
   bool TimeEachFunction;
   bool DisableIRGeneration;
+  bool AllowErrorRecovery;
+  bool GenerateUnitTestMessages;
   IceString DefaultGlobalPrefix;
   IceString DefaultFunctionPrefix;
   IceString TimingFocusOn;
diff --git a/src/PNaClTranslator.cpp b/src/PNaClTranslator.cpp
index 9be47f5..482844f 100644
--- a/src/PNaClTranslator.cpp
+++ b/src/PNaClTranslator.cpp
@@ -37,12 +37,6 @@
 namespace {
 using namespace llvm;
 
-// TODO(kschimpf) Remove error recovery once implementation complete.
-static cl::opt<bool> AllowErrorRecovery(
-    "allow-pnacl-reader-error-recovery",
-    cl::desc("Allow error recovery when reading PNaCl bitcode."),
-    cl::init(false));
-
 // Models elements in the list of types defined in the types block.
 // These elements can be undefined, a (simple) type, or a function type
 // signature. Note that an extended type is undefined on construction.
@@ -440,7 +434,7 @@
   ErrorStatus = true;
   ++NumErrors;
   NaClBitcodeParser::Error(Message);
-  if (!AllowErrorRecovery)
+  if (!Translator.getFlags().AllowErrorRecovery)
     report_fatal_error("Unable to continue");
   return true;
 }
@@ -612,10 +606,11 @@
   StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8),
                           static_cast<unsigned>(Bit % 8)) << ") ";
   // Note: If dump routines have been turned off, the error messages
-  // will not be readable. Hence, replace with simple error.
-  if (ALLOW_DUMP)
+  // will not be readable. Hence, replace with simple error. We also
+  // use the simple form for unit tests.
+  if (ALLOW_DUMP && !getFlags().GenerateUnitTestMessages) {
     StrBuf << Message;
-  else {
+  } else {
     StrBuf << "Invalid " << getBlockName() << " record: <" << Record.GetCode();
     for (const uint64_t Val : Record.GetValues()) {
       StrBuf << " " << Val;
@@ -2967,6 +2962,11 @@
   }
 
   std::unique_ptr<MemoryBuffer> MemBuf(ErrOrFile.get().release());
+  translateBuffer(IRFilename, MemBuf.get());
+}
+
+void PNaClTranslator::translateBuffer(const std::string &IRFilename,
+                                      MemoryBuffer *MemBuf) {
   if (MemBuf->getBufferSize() % 4 != 0) {
     errs() << IRFilename
            << ": Bitcode stream should be a multiple of 4 bytes in length.\n";
diff --git a/src/PNaClTranslator.h b/src/PNaClTranslator.h
index dea7d43..5738f11 100644
--- a/src/PNaClTranslator.h
+++ b/src/PNaClTranslator.h
@@ -19,6 +19,10 @@
 
 #include "IceTranslator.h"
 
+namespace llvm {
+class MemoryBuffer;
+} // end of namespace llvm
+
 namespace Ice {
 
 class PNaClTranslator : public Translator {
@@ -28,10 +32,15 @@
 public:
   PNaClTranslator(GlobalContext *Ctx, const ClFlags &Flags)
       : Translator(Ctx, Flags) {}
+
   // Reads the PNaCl bitcode file and translates to ICE, which is then
   // converted to machine code. Sets ErrorStatus to true if any
   // errors occurred.
   void translate(const std::string &IRFilename);
+
+  // Reads MemBuf, assuming it is the PNaCl bitcode contents of IRFilename.
+  void translateBuffer(const std::string &IRFilename,
+                       llvm::MemoryBuffer *MemBuf);
 };
 
 } // end of namespace Ice
diff --git a/src/llvm2ice.cpp b/src/llvm2ice.cpp
index 53c90ee..3b1813d 100644
--- a/src/llvm2ice.cpp
+++ b/src/llvm2ice.cpp
@@ -167,6 +167,10 @@
                 cl::desc("Build ICE instructions when reading bitcode"),
                 cl::init(true));
 
+static cl::opt<bool> AllowErrorRecovery(
+    "allow-pnacl-reader-error-recovery",
+    cl::desc("Allow error recovery when reading PNaCl bitcode."),
+    cl::init(false));
 
 static cl::opt<bool>
 LLVMVerboseErrors(
@@ -297,6 +301,7 @@
   Flags.VerboseFocusOn = VerboseFocusOn;
   Flags.TranslateOnly = TranslateOnly;
   Flags.DisableIRGeneration = DisableIRGeneration;
+  Flags.AllowErrorRecovery = AllowErrorRecovery;
 
   // Force -build-on-read=0 for .ll files.
   const std::string LLSuffix = ".ll";