Revert "Retry^2 "[ProfileData] (llvm) Use Error in InstrProf and Coverage, NFC""

This reverts commit r269694. MSVC says:

error C2086: 'char llvm::ProfErrorInfoBase<enum llvm::instrprof_error>::ID' : redefinition

llvm-svn: 269700
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index d45e6af..5cdf962 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -47,50 +47,38 @@
   ::exit(1);
 }
 
-static void exitWithError(Error E, StringRef Whence = "") {
-  if (E.isA<InstrProfError>()) {
-    handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
-      instrprof_error instrError = IPE.get();
-      StringRef Hint = "";
-      if (instrError == instrprof_error::unrecognized_format) {
-        // Hint for common error of forgetting -sample for sample profiles.
-        Hint = "Perhaps you forgot to use the -sample option?";
-      }
-      exitWithError(IPE.message(), Whence, Hint);
-    });
+static void exitWithErrorCode(const std::error_code &Error,
+                              StringRef Whence = "") {
+  if (Error.category() == instrprof_category()) {
+    instrprof_error instrError = static_cast<instrprof_error>(Error.value());
+    if (instrError == instrprof_error::unrecognized_format) {
+      // Hint for common error of forgetting -sample for sample profiles.
+      exitWithError(Error.message(), Whence,
+                    "Perhaps you forgot to use the -sample option?");
+    }
   }
-
-  exitWithError(toString(std::move(E)), Whence);
-}
-
-static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
-  exitWithError(EC.message(), Whence);
+  exitWithError(Error.message(), Whence);
 }
 
 namespace {
 enum ProfileKinds { instr, sample };
 }
 
-static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
+static void handleMergeWriterError(std::error_code &Error,
+                                   StringRef WhenceFile = "",
                                    StringRef WhenceFunction = "",
                                    bool ShowHint = true) {
   if (!WhenceFile.empty())
     errs() << WhenceFile << ": ";
   if (!WhenceFunction.empty())
     errs() << WhenceFunction << ": ";
-
-  auto IPE = instrprof_error::success;
-  E = handleErrors(std::move(E),
-                   [&IPE](std::unique_ptr<InstrProfError> E) -> Error {
-                     IPE = E->get();
-                     return Error(std::move(E));
-                   });
-  errs() << toString(std::move(E)) << "\n";
+  errs() << Error.message() << "\n";
 
   if (ShowHint) {
     StringRef Hint = "";
-    if (IPE != instrprof_error::success) {
-      switch (IPE) {
+    if (Error.category() == instrprof_category()) {
+      instrprof_error instrError = static_cast<instrprof_error>(Error.value());
+      switch (instrError) {
       case instrprof_error::hash_mismatch:
       case instrprof_error::count_mismatch:
       case instrprof_error::value_site_count_mismatch:
@@ -132,11 +120,11 @@
     exitWithErrorCode(EC, OutputFilename);
 
   InstrProfWriter Writer(OutputSparse);
-  SmallSet<instrprof_error, 4> WriterErrorCodes;
+  SmallSet<std::error_code, 4> WriterErrorCodes;
   for (const auto &Input : Inputs) {
     auto ReaderOrErr = InstrProfReader::create(Input.Filename);
-    if (Error E = ReaderOrErr.takeError())
-      exitWithError(std::move(E), Input.Filename);
+    if (std::error_code ec = ReaderOrErr.getError())
+      exitWithErrorCode(ec, Input.Filename);
 
     auto Reader = std::move(ReaderOrErr.get());
     bool IsIRProfile = Reader->isIRLevelProfile();
@@ -144,16 +132,14 @@
       exitWithError("Merge IR generated profile with Clang generated profile.");
 
     for (auto &I : *Reader) {
-      if (Error E = Writer.addRecord(std::move(I), Input.Weight)) {
+      if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) {
         // Only show hint the first time an error occurs.
-        instrprof_error IPE = InstrProfError::take(std::move(E));
-        bool firstTime = WriterErrorCodes.insert(IPE).second;
-        handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
-                               I.Name, firstTime);
+        bool firstTime = WriterErrorCodes.insert(EC).second;
+        handleMergeWriterError(EC, Input.Filename, I.Name, firstTime);
       }
     }
     if (Reader->hasError())
-      exitWithError(Reader->getError(), Input.Filename);
+      exitWithErrorCode(Reader->getError(), Input.Filename);
   }
   if (OutputFormat == PF_Text)
     Writer.writeText(Output);
@@ -201,7 +187,7 @@
       sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
       if (Result != sampleprof_error::success) {
         std::error_code EC = make_error_code(Result);
-        handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
+        handleMergeWriterError(EC, Input.Filename, FName);
       }
     }
   }
@@ -282,8 +268,8 @@
     Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
   }
   InstrProfSummary PS(Cutoffs);
-  if (Error E = ReaderOrErr.takeError())
-    exitWithError(std::move(E), Filename);
+  if (std::error_code EC = ReaderOrErr.getError())
+    exitWithErrorCode(EC, Filename);
 
   auto Reader = std::move(ReaderOrErr.get());
   bool IsIRInstr = Reader->isIRLevelProfile();
@@ -349,7 +335,7 @@
   }
 
   if (Reader->hasError())
-    exitWithError(Reader->getError(), Filename);
+    exitWithErrorCode(Reader->getError(), Filename);
 
   if (ShowCounts && TextFormat)
     return 0;