[llvm] [llvm-pdbutil] Print error on invalid YAML (PR #207528)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 4 09:54:41 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo

Author: Nerixyz (Nerixyz)

<details>
<summary>Changes</summary>

>From https://github.com/llvm/llvm-project/pull/207058#discussion_r3522204565: We should print out an error if we encounter invalid YAML.

With this PR, the error is printed. I included the diagnostics here, because they provide better error messages and allow you to locate the error easier.

---
Full diff: https://github.com/llvm/llvm-project/pull/207528.diff


2 Files Affected:

- (added) llvm/test/tools/llvm-pdbutil/invalid-yaml.test (+10) 
- (modified) llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp (+9-1) 


``````````diff
diff --git a/llvm/test/tools/llvm-pdbutil/invalid-yaml.test b/llvm/test/tools/llvm-pdbutil/invalid-yaml.test
new file mode 100644
index 0000000000000..e650a3d5e72bc
--- /dev/null
+++ b/llvm/test/tools/llvm-pdbutil/invalid-yaml.test
@@ -0,0 +1,10 @@
+## Test that we produce an error if the YAML input is invalid.
+# RUN: not llvm-pdbutil yaml2pdb %s --pdb=%t.pdb 2>&1 | FileCheck %s
+
+---
+- A list is not valid
+# CHECK: invalid-yaml.test:[[@LINE-1]]:1: error: not a mapping
+# CHECK-NEXT: - A list
+# CHECK-NEXT: ^
+# CHECK-NEXT: llvm-pdbutil: failed to parse YAML input: invalid argument
+...
diff --git a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
index 77aed01ad498f..553866f4e9bc4 100644
--- a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
+++ b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
@@ -819,16 +819,24 @@ static void yamlToPdb(StringRef Path, unsigned DocNum) {
 
   std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
 
-  llvm::yaml::Input In(Buffer->getBuffer());
+  llvm::yaml::Input In(
+      Buffer->getMemBufferRef(), nullptr,
+      [](const SMDiagnostic &Diag, void *) { Diag.print(nullptr, errs()); });
+
   for (unsigned CurrentDoc = 1; CurrentDoc < DocNum; ++CurrentDoc) {
     if (!In.nextDocument())
       ExitOnErr(createFileError(
           Path, createStringErrorV("cannot find the {0}{1} document", DocNum,
                                    getOrdinalSuffix(DocNum))));
   }
+
   pdb::yaml::PdbObject YamlObj(Allocator);
   In >> YamlObj;
 
+  if (std::error_code EC = In.error())
+    ExitOnErr(
+        createStringErrorV("failed to parse YAML input: {0}", EC.message()));
+
   PDBFileBuilder Builder(Allocator);
 
   uint32_t BlockSize = 4096;

``````````

</details>


https://github.com/llvm/llvm-project/pull/207528


More information about the llvm-commits mailing list