[PATCH] D82630: [ObjectYAML][DWARF] Collect diagnostic message when YAMLParser fails.

Xing GUO via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 29 01:34:46 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG8f9ca561a2bd: [ObjectYAML][DWARF] Collect diagnostic message when YAMLParser fails. (authored by Higuoxing).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82630/new/

https://reviews.llvm.org/D82630

Files:
  llvm/lib/ObjectYAML/DWARFEmitter.cpp
  llvm/unittests/ObjectYAML/CMakeLists.txt
  llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp


Index: llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp
===================================================================
--- /dev/null
+++ 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'"));
+}
Index: llvm/unittests/ObjectYAML/CMakeLists.txt
===================================================================
--- llvm/unittests/ObjectYAML/CMakeLists.txt
+++ llvm/unittests/ObjectYAML/CMakeLists.txt
@@ -4,6 +4,7 @@
   )
 
 add_llvm_unittest(ObjectYAMLTests
+  DWARFYAMLTest.cpp
   ELFYAMLTest.cpp
   MinidumpYAMLTest.cpp
   YAML2ObjTest.cpp
Index: llvm/lib/ObjectYAML/DWARFEmitter.cpp
===================================================================
--- llvm/lib/ObjectYAML/DWARFEmitter.cpp
+++ 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 @@
 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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82630.274007.patch
Type: text/x-patch
Size: 3287 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200629/d415e62b/attachment.bin>


More information about the llvm-commits mailing list