1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

[ObjectYAML][DWARF] Collect diagnostic message when YAMLParser fails.

Before this patch, the diagnostic message is printed to `errs()` directly, which makes it difficult to use `FailedWithMessage()` in unit testing.
In this patch, we add a custom error handler for YAMLParser, which helps collect diagnostic messages and make it easy to use `FailedWithMessage()` to check error messages.

Reviewed By: jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D82630
This commit is contained in:
Xing GUO 2020-06-29 15:35:12 +08:00
parent e668706cd2
commit 81f234a2c6
3 changed files with 59 additions and 2 deletions

View File

@ -23,6 +23,7 @@
#include "llvm/Support/LEB128.h" #include "llvm/Support/LEB128.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/SwapByteOrder.h" #include "llvm/Support/SwapByteOrder.h"
#include "llvm/Support/YAMLTraits.h" #include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
@ -475,13 +476,19 @@ private:
Expected<StringMap<std::unique_ptr<MemoryBuffer>>> Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
DWARFYAML::emitDebugSections(StringRef YAMLString, bool ApplyFixups, DWARFYAML::emitDebugSections(StringRef YAMLString, bool ApplyFixups,
bool IsLittleEndian) { bool IsLittleEndian) {
yaml::Input YIn(YAMLString); auto CollectDiagnostic = [](const SMDiagnostic &Diag, void *DiagContext) {
*static_cast<SMDiagnostic *>(DiagContext) = Diag;
};
SMDiagnostic GeneratedDiag;
yaml::Input YIn(YAMLString, /*Ctxt=*/nullptr, CollectDiagnostic,
&GeneratedDiag);
DWARFYAML::Data DI; DWARFYAML::Data DI;
DI.IsLittleEndian = IsLittleEndian; DI.IsLittleEndian = IsLittleEndian;
YIn >> DI; YIn >> DI;
if (YIn.error()) if (YIn.error())
return errorCodeToError(YIn.error()); return createStringError(YIn.error(), GeneratedDiag.getMessage());
if (ApplyFixups) { if (ApplyFixups) {
DIEFixupVisitor DIFixer(DI); DIEFixupVisitor DIFixer(DI);

View File

@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
) )
add_llvm_unittest(ObjectYAMLTests add_llvm_unittest(ObjectYAMLTests
DWARFYAMLTest.cpp
ELFYAMLTest.cpp ELFYAMLTest.cpp
MinidumpYAMLTest.cpp MinidumpYAMLTest.cpp
YAML2ObjTest.cpp YAML2ObjTest.cpp

View File

@ -0,0 +1,49 @@
//===- DWARFYAMLTest.cpp - Tests for DWARFYAML.cpp ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ObjectYAML/DWARFYAML.h"
#include "llvm/ObjectYAML/DWARFEmitter.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
using namespace llvm;
TEST(DebugAddrSection, TestParseDebugAddrYAML) {
StringRef Yaml = R"(
debug_addr:
- Format: DWARF64
Length: 0x1234
Version: 5
)";
auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml);
EXPECT_THAT_EXPECTED(SectionsOrErr, Succeeded());
}
TEST(DebugAddrSection, TestMissingVersion) {
StringRef Yaml = R"(
debug_addr:
- Format: DWARF64
Length: 0x1234
)";
auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml);
EXPECT_THAT_ERROR(SectionsOrErr.takeError(),
FailedWithMessage("missing required key 'Version'"));
}
TEST(DebugAddrSection, TestUnexpectedKey) {
StringRef Yaml = R"(
debug_addr:
- Format: DWARF64
Length: 0x1234
Version: 5
Blah: unexpected
)";
auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml);
EXPECT_THAT_ERROR(SectionsOrErr.takeError(),
FailedWithMessage("unknown key 'Blah'"));
}