llvm-cov: Use ErrorOr rather than an error_code* (NFC)
authorJustin Bogner <mail@justinbogner.com>
Mon, 8 Sep 2014 21:04:00 +0000 (21:04 +0000)
committerJustin Bogner <mail@justinbogner.com>
Mon, 8 Sep 2014 21:04:00 +0000 (21:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217404 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ProfileData/CoverageMapping.h
lib/ProfileData/CoverageMapping.cpp
tools/llvm-cov/CodeCoverage.cpp

index 51b4ccd98279c64ee11969f463f6a6cea877bbfe..04b8085384c85a1e5e03a3bfa27afa59fcd03f31 100644 (file)
@@ -16,6 +16,7 @@
 #define LLVM_PROFILEDATA_COVERAGEMAPPING_H_
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/raw_ostream.h"
 #include <system_error>
 
@@ -186,13 +187,9 @@ public:
   void dump(const Counter &C, llvm::raw_ostream &OS) const;
   void dump(const Counter &C) const { dump(C, llvm::outs()); }
 
-  /// \brief Return the number of times that a region of code
-  /// associated with this counter was executed.
-  int64_t evaluate(const Counter &C, std::error_code *Error) const;
-  int64_t evaluate(const Counter &C, std::error_code &Error) const {
-    Error.clear();
-    return evaluate(C, &Error);
-  }
+  /// \brief Return the number of times that a region of code associated with
+  /// this counter was executed.
+  ErrorOr<int64_t> evaluate(const Counter &C) const;
 };
 
 } // end namespace coverage
index f9becdd5b771c5a311a9a06f76c2d79df4ac53d0..54965fe77d805d8f3c137b2315ffc864bd4f89d1 100644 (file)
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ProfileData/CoverageMapping.h"
+#include "llvm/Support/ErrorHandling.h"
 
 using namespace llvm;
 using namespace coverage;
@@ -110,40 +111,32 @@ void CounterMappingContext::dump(const Counter &C,
   }
   if (CounterValues.empty())
     return;
-  std::error_code Error;
-  auto Value = evaluate(C, Error);
-  if (Error)
+  ErrorOr<int64_t> Value = evaluate(C);
+  if (!Value)
     return;
-  OS << '[' << Value << ']';
+  OS << '[' << *Value << ']';
 }
 
-int64_t CounterMappingContext::evaluate(const Counter &C,
-                                        std::error_code *EC) const {
+ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
   switch (C.getKind()) {
   case Counter::Zero:
     return 0;
   case Counter::CounterValueReference:
-    if (C.getCounterID() >= CounterValues.size()) {
-      if (EC)
-        *EC = std::make_error_code(std::errc::argument_out_of_domain);
-      break;
-    }
+    if (C.getCounterID() >= CounterValues.size())
+      return std::errc::argument_out_of_domain;
     return CounterValues[C.getCounterID()];
   case Counter::Expression: {
-    if (C.getExpressionID() >= Expressions.size()) {
-      if (EC)
-        *EC = std::make_error_code(std::errc::argument_out_of_domain);
-      break;
-    }
+    if (C.getExpressionID() >= Expressions.size())
+      return std::errc::argument_out_of_domain;
     const auto &E = Expressions[C.getExpressionID()];
-    auto LHS = evaluate(E.LHS, EC);
-    if (EC && *EC)
-      return 0;
-    auto RHS = evaluate(E.RHS, EC);
-    if (EC && *EC)
-      return 0;
-    return E.Kind == CounterExpression::Subtract ? LHS - RHS : LHS + RHS;
+    ErrorOr<int64_t> LHS = evaluate(E.LHS);
+    if (!LHS)
+      return LHS;
+    ErrorOr<int64_t> RHS = evaluate(E.RHS);
+    if (!RHS)
+      return RHS;
+    return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
   }
   }
-  return 0;
+  llvm_unreachable("Unhandled CounterKind");
 }
index a19a22bc19299a904e1024dc4e7fdf53b50259c1..001d155acc7ba2bad99aaf0919397a861543723d 100644 (file)
@@ -411,10 +411,10 @@ bool CodeCoverageTool::load() {
         }
         outs() << "\n";
       }
-      std::error_code Error;
-      Function.MappingRegions.push_back(
-          MappingRegion(R, Ctx.evaluate(R.Count, Error)));
-      if (Error && !RegionError) {
+      ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(R.Count);
+      if (ExecutionCount) {
+        Function.MappingRegions.push_back(MappingRegion(R, *ExecutionCount));
+      } else if (!RegionError) {
         colored_ostream(errs(), raw_ostream::RED)
             << "error: Regions and counters don't match in a function '"
             << Function.Name << "' (re-run the instrumented binary).";