diff options
author | Vedant Kumar <vsk@apple.com> | 2016-05-13 21:50:56 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2016-05-13 21:50:56 +0000 |
commit | db1d476217336be689feb3d16ec541be69838d27 (patch) | |
tree | e41e1306a365b7533734d61b5441f91df7f24db4 /unittests | |
parent | 81f1df869ef4640550f101a6f492989b6d4895d0 (diff) |
Retry "[ProfileData] (llvm) Use Error in InstrProf and Coverage, NFC"
Transition InstrProf and Coverage over to the stricter Error/Expected
interface.
Changes since the initial commit:
- Fix error message printing in llvm-profdata.
- Check errors in loadTestingFormat() + annotateAllFunctions().
- Defer error handling in InstrProfIterator to InstrProfReader.
Differential Revision: http://reviews.llvm.org/D19901
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269491 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/ProfileData/CoverageMappingTest.cpp | 18 | ||||
-rw-r--r-- | unittests/ProfileData/InstrProfTest.cpp | 88 |
2 files changed, 58 insertions, 48 deletions
diff --git a/unittests/ProfileData/CoverageMappingTest.cpp b/unittests/ProfileData/CoverageMappingTest.cpp index 81e9cb13233..53b40ebae85 100644 --- a/unittests/ProfileData/CoverageMappingTest.cpp +++ b/unittests/ProfileData/CoverageMappingTest.cpp @@ -20,11 +20,11 @@ using namespace llvm; using namespace coverage; -static ::testing::AssertionResult NoError(std::error_code EC) { - if (!EC) +static ::testing::AssertionResult NoError(Error E) { + if (!E) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error " << EC.value() - << ": " << EC.message(); + return ::testing::AssertionFailure() << "error: " << toString(std::move(E)) + << "\n"; } namespace llvm { @@ -70,14 +70,14 @@ struct CoverageMappingReaderMock : CoverageMappingReader { CoverageMappingReaderMock(ArrayRef<OutputFunctionCoverageData> Functions) : Functions(Functions) {} - std::error_code readNextRecord(CoverageMappingRecord &Record) override { + Error readNextRecord(CoverageMappingRecord &Record) override { if (Functions.empty()) - return coveragemap_error::eof; + return make_error<CoverageMapError>(coveragemap_error::eof); Functions.front().fillCoverageMappingRecord(Record); Functions = Functions.slice(1); - return coveragemap_error::success; + return Error::success(); } }; @@ -190,7 +190,7 @@ struct CoverageMappingTest : ::testing::Test { void readProfCounts() { auto Profile = ProfileWriter.writeBuffer(); auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); - ASSERT_TRUE(NoError(ReaderOrErr.getError())); + ASSERT_TRUE(NoError(ReaderOrErr.takeError())); ProfileReader = std::move(ReaderOrErr.get()); } @@ -200,7 +200,7 @@ struct CoverageMappingTest : ::testing::Test { CoverageMappingReaderMock CovReader(OutputFunctions); auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader); - ASSERT_TRUE(NoError(CoverageOrErr.getError())); + ASSERT_TRUE(NoError(CoverageOrErr.takeError())); LoadedCoverage = std::move(CoverageOrErr.get()); } }; diff --git a/unittests/ProfileData/InstrProfTest.cpp b/unittests/ProfileData/InstrProfTest.cpp index 2d9a2218e55..9ef3487ba18 100644 --- a/unittests/ProfileData/InstrProfTest.cpp +++ b/unittests/ProfileData/InstrProfTest.cpp @@ -19,19 +19,24 @@ using namespace llvm; -static ::testing::AssertionResult NoError(std::error_code EC) { - if (!EC) +static ::testing::AssertionResult NoError(Error E) { + if (!E) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error " << EC.value() - << ": " << EC.message(); + return ::testing::AssertionFailure() << "error: " << toString(std::move(E)) + << "\n"; } -static ::testing::AssertionResult ErrorEquals(std::error_code Expected, - std::error_code Found) { +static ::testing::AssertionResult ErrorEquals(instrprof_error Expected, + Error E) { + instrprof_error Found; + std::string FoundMsg; + handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { + Found = IPE.get(); + FoundMsg = IPE.message(); + }); if (Expected == Found) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error " << Found.value() - << ": " << Found.message(); + return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n"; } namespace { @@ -44,7 +49,7 @@ struct InstrProfTest : ::testing::Test { void readProfile(std::unique_ptr<MemoryBuffer> Profile) { auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); - ASSERT_TRUE(NoError(ReaderOrErr.getError())); + ASSERT_TRUE(NoError(ReaderOrErr.takeError())); Reader = std::move(ReaderOrErr.get()); } }; @@ -90,23 +95,23 @@ TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(2U, R->Counts.size()); ASSERT_EQ(1U, R->Counts[0]); ASSERT_EQ(2U, R->Counts[1]); R = Reader->getInstrProfRecord("foo", 0x1235); - ASSERT_TRUE(NoError(R.getError())); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(2U, R->Counts.size()); ASSERT_EQ(3U, R->Counts[0]); ASSERT_EQ(4U, R->Counts[1]); R = Reader->getInstrProfRecord("foo", 0x5678); - ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError())); + ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.takeError())); R = Reader->getInstrProfRecord("bar", 0x1234); - ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError())); + ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.takeError())); } TEST_P(MaybeSparseInstrProfTest, get_function_counts) { @@ -128,12 +133,11 @@ TEST_P(MaybeSparseInstrProfTest, get_function_counts) { ASSERT_EQ(3U, Counts[0]); ASSERT_EQ(4U, Counts[1]); - std::error_code EC; - EC = Reader->getFunctionCounts("foo", 0x5678, Counts); - ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC)); + Error E1 = Reader->getFunctionCounts("foo", 0x5678, Counts); + ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, std::move(E1))); - EC = Reader->getFunctionCounts("bar", 0x1234, Counts); - ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC)); + Error E2 = Reader->getFunctionCounts("bar", 0x1234, Counts); + ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, std::move(E2))); } // Profile data is copied from general.proftext @@ -235,8 +239,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -266,8 +270,8 @@ TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) { NoError(Writer.addRecord(std::move(Record))); auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); LLVMContext Ctx; std::unique_ptr<Module> M(new Module("MyModule", Ctx)); @@ -378,8 +382,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -431,8 +435,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) { // Set big endian input. Reader->setValueProfDataEndianness(support::big); - ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -513,8 +517,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -566,23 +570,27 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) { InstrProfRecord Record1("foo", 0x1234, {1}); auto Result1 = Writer.addRecord(std::move(Record1)); - ASSERT_EQ(Result1, instrprof_error::success); + ASSERT_EQ(InstrProfError::take(std::move(Result1)), + instrprof_error::success); // Verify counter overflow. InstrProfRecord Record2("foo", 0x1234, {Max}); auto Result2 = Writer.addRecord(std::move(Record2)); - ASSERT_EQ(Result2, instrprof_error::counter_overflow); + ASSERT_EQ(InstrProfError::take(std::move(Result2)), + instrprof_error::counter_overflow); InstrProfRecord Record3(bar, 0x9012, {8}); auto Result3 = Writer.addRecord(std::move(Record3)); - ASSERT_EQ(Result3, instrprof_error::success); + ASSERT_EQ(InstrProfError::take(std::move(Result3)), + instrprof_error::success); InstrProfRecord Record4("baz", 0x5678, {3, 4}); Record4.reserveSites(IPVK_IndirectCallTarget, 1); InstrProfValueData VD4[] = {{uint64_t(bar), 1}}; Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr); auto Result4 = Writer.addRecord(std::move(Record4)); - ASSERT_EQ(Result4, instrprof_error::success); + ASSERT_EQ(InstrProfError::take(std::move(Result4)), + instrprof_error::success); // Verify value data counter overflow. InstrProfRecord Record5("baz", 0x5678, {5, 6}); @@ -590,19 +598,21 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) { InstrProfValueData VD5[] = {{uint64_t(bar), Max}}; Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr); auto Result5 = Writer.addRecord(std::move(Record5)); - ASSERT_EQ(Result5, instrprof_error::counter_overflow); + ASSERT_EQ(InstrProfError::take(std::move(Result5)), + instrprof_error::counter_overflow); auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); // Verify saturation of counts. - ErrorOr<InstrProfRecord> ReadRecord1 = + Expected<InstrProfRecord> ReadRecord1 = Reader->getInstrProfRecord("foo", 0x1234); - ASSERT_TRUE(NoError(ReadRecord1.getError())); + ASSERT_TRUE(NoError(ReadRecord1.takeError())); ASSERT_EQ(Max, ReadRecord1->Counts[0]); - ErrorOr<InstrProfRecord> ReadRecord2 = + Expected<InstrProfRecord> ReadRecord2 = Reader->getInstrProfRecord("baz", 0x5678); + ASSERT_TRUE(bool(ReadRecord2)); ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget)); std::unique_ptr<InstrProfValueData[]> VD = ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0); @@ -647,8 +657,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); std::unique_ptr<InstrProfValueData[]> VD( R->getValueForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget)); |