Skip to content

Commit 8f9ca56

Browse files
committed
[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
1 parent 42de94f commit 8f9ca56

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

llvm/lib/ObjectYAML/DWARFEmitter.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Support/LEB128.h"
2424
#include "llvm/Support/MathExtras.h"
2525
#include "llvm/Support/MemoryBuffer.h"
26+
#include "llvm/Support/SourceMgr.h"
2627
#include "llvm/Support/SwapByteOrder.h"
2728
#include "llvm/Support/YAMLTraits.h"
2829
#include "llvm/Support/raw_ostream.h"
@@ -475,13 +476,19 @@ class DIEFixupVisitor : public DWARFYAML::Visitor {
475476
Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
476477
DWARFYAML::emitDebugSections(StringRef YAMLString, bool ApplyFixups,
477478
bool IsLittleEndian) {
478-
yaml::Input YIn(YAMLString);
479+
auto CollectDiagnostic = [](const SMDiagnostic &Diag, void *DiagContext) {
480+
*static_cast<SMDiagnostic *>(DiagContext) = Diag;
481+
};
482+
483+
SMDiagnostic GeneratedDiag;
484+
yaml::Input YIn(YAMLString, /*Ctxt=*/nullptr, CollectDiagnostic,
485+
&GeneratedDiag);
479486

480487
DWARFYAML::Data DI;
481488
DI.IsLittleEndian = IsLittleEndian;
482489
YIn >> DI;
483490
if (YIn.error())
484-
return errorCodeToError(YIn.error());
491+
return createStringError(YIn.error(), GeneratedDiag.getMessage());
485492

486493
if (ApplyFixups) {
487494
DIEFixupVisitor DIFixer(DI);

llvm/unittests/ObjectYAML/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
44
)
55

66
add_llvm_unittest(ObjectYAMLTests
7+
DWARFYAMLTest.cpp
78
ELFYAMLTest.cpp
89
MinidumpYAMLTest.cpp
910
YAML2ObjTest.cpp
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===- DWARFYAMLTest.cpp - Tests for DWARFYAML.cpp ------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/ObjectYAML/DWARFYAML.h"
10+
#include "llvm/ObjectYAML/DWARFEmitter.h"
11+
#include "llvm/Testing/Support/Error.h"
12+
#include "gtest/gtest.h"
13+
14+
using namespace llvm;
15+
16+
TEST(DebugAddrSection, TestParseDebugAddrYAML) {
17+
StringRef Yaml = R"(
18+
debug_addr:
19+
- Format: DWARF64
20+
Length: 0x1234
21+
Version: 5
22+
)";
23+
auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml);
24+
EXPECT_THAT_EXPECTED(SectionsOrErr, Succeeded());
25+
}
26+
27+
TEST(DebugAddrSection, TestMissingVersion) {
28+
StringRef Yaml = R"(
29+
debug_addr:
30+
- Format: DWARF64
31+
Length: 0x1234
32+
)";
33+
auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml);
34+
EXPECT_THAT_ERROR(SectionsOrErr.takeError(),
35+
FailedWithMessage("missing required key 'Version'"));
36+
}
37+
38+
TEST(DebugAddrSection, TestUnexpectedKey) {
39+
StringRef Yaml = R"(
40+
debug_addr:
41+
- Format: DWARF64
42+
Length: 0x1234
43+
Version: 5
44+
Blah: unexpected
45+
)";
46+
auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml);
47+
EXPECT_THAT_ERROR(SectionsOrErr.takeError(),
48+
FailedWithMessage("unknown key 'Blah'"));
49+
}

0 commit comments

Comments
 (0)