[llvm] Fix missed 'else' brunch in loadTargetInfo. (PR #192923)

Nikita Terentev via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 20 02:19:18 PDT 2026


https://github.com/Seraphimt created https://github.com/llvm/llvm-project/pull/192923

Second try to close issue #170117.
The first was merged but it caused fail tests for linker error because missing change in CMakeList and it was reverted. This PR full include #188578 + necessary edit in CMakeList.
@CarlosAlbertoEnciso Could you review, please? 

>From da5e7900c04d8865dc28844480d5f7437cf07d8b Mon Sep 17 00:00:00 2001
From: Seraphimt <svet58585 at mail.ru>
Date: Wed, 15 Apr 2026 15:12:22 +0300
Subject: [PATCH] Fix missed 'else' brunch.

---
 .../LogicalView/Readers/LVCodeViewReader.cpp  |  8 +-
 .../LogicalView/Readers/LVDWARFReader.cpp     |  8 +-
 .../DebugInfo/LogicalView/CMakeLists.txt      |  1 +
 .../DebugInfo/LogicalView/DWARFReaderTest.cpp | 76 ++++++++++++++++++-
 4 files changed, 82 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/DebugInfo/LogicalView/Readers/LVCodeViewReader.cpp b/llvm/lib/DebugInfo/LogicalView/Readers/LVCodeViewReader.cpp
index 2ff70816b4bf1..95ce3fd02ad8c 100644
--- a/llvm/lib/DebugInfo/LogicalView/Readers/LVCodeViewReader.cpp
+++ b/llvm/lib/DebugInfo/LogicalView/Readers/LVCodeViewReader.cpp
@@ -1183,13 +1183,11 @@ Error LVCodeViewReader::loadTargetInfo(const ObjectFile &Obj) {
   TT.setOS(Triple::UnknownOS);
 
   // Features to be passed to target/subtarget
-  Expected<SubtargetFeatures> Features = Obj.getFeatures();
   SubtargetFeatures FeaturesValue;
-  if (!Features) {
+  if (Expected<SubtargetFeatures> Features = Obj.getFeatures())
+    FeaturesValue = std::move(*Features);
+  else
     consumeError(Features.takeError());
-    FeaturesValue = SubtargetFeatures();
-  }
-  FeaturesValue = *Features;
 
   StringRef CPU;
   if (auto OptCPU = Obj.tryGetCPUName())
diff --git a/llvm/lib/DebugInfo/LogicalView/Readers/LVDWARFReader.cpp b/llvm/lib/DebugInfo/LogicalView/Readers/LVDWARFReader.cpp
index 772d821dcda81..9e0d5389e87bd 100644
--- a/llvm/lib/DebugInfo/LogicalView/Readers/LVDWARFReader.cpp
+++ b/llvm/lib/DebugInfo/LogicalView/Readers/LVDWARFReader.cpp
@@ -962,13 +962,11 @@ Error LVDWARFReader::loadTargetInfo(const ObjectFile &Obj) {
   Triple TT = Obj.makeTriple();
 
   // Features to be passed to target/subtarget
-  Expected<SubtargetFeatures> Features = Obj.getFeatures();
   SubtargetFeatures FeaturesValue;
-  if (!Features) {
+  if (Expected<SubtargetFeatures> Features = Obj.getFeatures())
+    FeaturesValue = std::move(*Features);
+  else
     consumeError(Features.takeError());
-    FeaturesValue = SubtargetFeatures();
-  }
-  FeaturesValue = *Features;
 
   StringRef CPU;
   if (auto OptCPU = Obj.tryGetCPUName())
diff --git a/llvm/unittests/DebugInfo/LogicalView/CMakeLists.txt b/llvm/unittests/DebugInfo/LogicalView/CMakeLists.txt
index 42a4b72229483..a8fd83e9b7b6e 100644
--- a/llvm/unittests/DebugInfo/LogicalView/CMakeLists.txt
+++ b/llvm/unittests/DebugInfo/LogicalView/CMakeLists.txt
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
   MC
   MCDisassembler
   Object
+  ObjectYAML
   TargetParser
   )
 
diff --git a/llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp b/llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp
index fb728c8c22e77..5a5497dc78720 100644
--- a/llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp
+++ b/llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp
@@ -13,11 +13,13 @@
 #include "llvm/DebugInfo/LogicalView/Core/LVType.h"
 #include "llvm/DebugInfo/LogicalView/LVReaderHandler.h"
 #include "llvm/MC/TargetRegistry.h"
+#include "llvm/ObjectYAML/yaml2obj.h"
 #include "llvm/Support/COM.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/YAMLTraits.h"
 #include "llvm/Testing/Support/Error.h"
 
 #include "gtest/gtest.h"
@@ -55,7 +57,8 @@ std::unique_ptr<LVReader> createReader(LVReaderHandler &ReaderHandler,
                                        SmallString<128> &InputsDir,
                                        StringRef Filename) {
   SmallString<128> ObjectName(InputsDir);
-  llvm::sys::path::append(ObjectName, Filename);
+  if (Filename != "")
+    llvm::sys::path::append(ObjectName, Filename);
 
   Expected<std::unique_ptr<LVReader>> ReaderOrErr =
       ReaderHandler.createReader(std::string(ObjectName));
@@ -416,4 +419,75 @@ TEST(LogicalViewTest, DWARFReader) {
   compareElements(InputsDir);
 }
 
+bool CreateTempBrokenFile(SmallString<128> &TempPath) {
+  // create broken, but syntax correct Yaml file
+  // 0x41 - version of format
+  // 0x05000000 - length of section, false equal to 5 byte
+  StringRef Yaml = R"(
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_RISCV
+Sections:
+  - Name:            .riscv.attributes
+    Type:            SHT_RISCV_ATTRIBUTES
+    Content:         4105000000
+DWARF:
+  debug_abbrev:
+    - Table:
+        - Code:            1
+          Tag:             DW_TAG_compile_unit
+          Children:        DW_CHILDREN_no
+)";
+
+  int FD;
+  sys::fs::createTemporaryFile("test-broken-dwarf", "", FD, TempPath);
+  raw_fd_ostream OS(FD, /*shouldClose=*/true);
+
+  llvm::yaml::Input YIn(Yaml);
+  const bool Success = llvm::yaml::convertYAML(
+      YIn, OS, [](const Twine &Msg) { FAIL() << Msg.str(); });
+  if (!Success)
+    return false;
+  OS.flush();
+
+  return true;
+}
+
+TEST(LogicalViewTest, DWARFReader_ReadBrokenFile) {
+  // Initialize targets and assembly printers/parsers.
+  llvm::InitializeAllTargetInfos();
+  llvm::InitializeAllTargetMCs();
+  InitializeAllDisassemblers();
+
+  llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
+
+  // This test requires a x86-registered-target.
+  Triple TT;
+  TT.setArch(Triple::riscv64);
+  TT.setVendor(Triple::UnknownVendor);
+  TT.setOS(Triple::UnknownOS);
+
+  std::string TargetLookupError;
+  if (!TargetRegistry::lookupTarget(TT, TargetLookupError))
+    GTEST_SKIP();
+
+  // Reader options.
+  LVOptions ReaderOptions;
+  std::vector<std::string> Objects;
+  ScopedPrinter W(outs());
+  LVReaderHandler ReaderHandler(Objects, W, ReaderOptions);
+
+  SmallString<128> TempPath;
+  if (!CreateTempBrokenFile(TempPath))
+    GTEST_SKIP();
+
+  // Check logical elements properties.
+  std::unique_ptr<LVReader> Reader = createReader(ReaderHandler, TempPath, "");
+
+  sys::fs::remove(TempPath);
+}
+
 } // namespace



More information about the llvm-commits mailing list