[llvm] 8f9ca56 - [ObjectYAML][DWARF] Collect diagnostic message when YAMLParser fails.
Xing GUO via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 29 01:09:34 PDT 2020
Author: Xing GUO
Date: 2020-06-29T16:13:53+08:00
New Revision: 8f9ca561a2bd9d373d4a4ad0cd79a118c5abe4fb
URL: https://github.com/llvm/llvm-project/commit/8f9ca561a2bd9d373d4a4ad0cd79a118c5abe4fb
DIFF: https://github.com/llvm/llvm-project/commit/8f9ca561a2bd9d373d4a4ad0cd79a118c5abe4fb.diff
LOG: [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
Added:
llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp
Modified:
llvm/lib/ObjectYAML/DWARFEmitter.cpp
llvm/unittests/ObjectYAML/CMakeLists.txt
Removed:
################################################################################
diff --git a/llvm/lib/ObjectYAML/DWARFEmitter.cpp b/llvm/lib/ObjectYAML/DWARFEmitter.cpp
index b9b8c0e52842..7f8de2c2f30b 100644
--- a/llvm/lib/ObjectYAML/DWARFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DWARFEmitter.cpp
@@ -23,6 +23,7 @@
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/SwapByteOrder.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
@@ -475,13 +476,19 @@ class DIEFixupVisitor : public DWARFYAML::Visitor {
Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
DWARFYAML::emitDebugSections(StringRef YAMLString, bool ApplyFixups,
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;
DI.IsLittleEndian = IsLittleEndian;
YIn >> DI;
if (YIn.error())
- return errorCodeToError(YIn.error());
+ return createStringError(YIn.error(), GeneratedDiag.getMessage());
if (ApplyFixups) {
DIEFixupVisitor DIFixer(DI);
diff --git a/llvm/unittests/ObjectYAML/CMakeLists.txt b/llvm/unittests/ObjectYAML/CMakeLists.txt
index 04a770a46eb3..c20f79265763 100644
--- a/llvm/unittests/ObjectYAML/CMakeLists.txt
+++ b/llvm/unittests/ObjectYAML/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
)
add_llvm_unittest(ObjectYAMLTests
+ DWARFYAMLTest.cpp
ELFYAMLTest.cpp
MinidumpYAMLTest.cpp
YAML2ObjTest.cpp
diff --git a/llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp b/llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp
new file mode 100644
index 000000000000..92a148fffdb9
--- /dev/null
+++ b/llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp
@@ -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'"));
+}
More information about the llvm-commits
mailing list