[PATCH] D138658: [DebugInfo] Protect DIFile::Source against empty string

Jonas Hahnfeld via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 24 04:46:12 PST 2022


Hahnfeld created this revision.
Hahnfeld added reviewers: dblaikie, echristo, aprantl.
Herald added a project: All.
Hahnfeld requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

`getCanonicalMDString()` returns a `nullptr` for empty strings, which tripped over the `getSource()` method.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138658

Files:
  llvm/include/llvm/IR/DebugInfoMetadata.h
  llvm/unittests/IR/DebugInfoTest.cpp
  llvm/unittests/IR/MetadataTest.cpp


Index: llvm/unittests/IR/MetadataTest.cpp
===================================================================
--- llvm/unittests/IR/MetadataTest.cpp
+++ llvm/unittests/IR/MetadataTest.cpp
@@ -2221,6 +2221,20 @@
   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
 }
 
+TEST_F(DIFileTest, EmptySource) {
+  DIFile *N = DIFile::get(Context, "file", "dir");
+  EXPECT_EQ(None, N->getSource());
+
+  Optional<DIFile::ChecksumInfo<StringRef>> Checksum = None;
+  N = DIFile::get(Context, "file", "dir", Checksum, /*Source=*/None);
+  EXPECT_EQ(None, N->getSource());
+
+  // Test that passing an empty string gives the same result.
+  N = DIFile::get(Context, "file", "dir", Checksum,
+                  /*Source=*/Optional<StringRef>(""));
+  EXPECT_EQ(None, N->getSource());
+}
+
 TEST_F(DIFileTest, ScopeGetFile) {
   // Ensure that DIScope::getFile() returns itself.
   DIScope *N = DIFile::get(Context, "file", "dir");
Index: llvm/unittests/IR/DebugInfoTest.cpp
===================================================================
--- llvm/unittests/IR/DebugInfoTest.cpp
+++ llvm/unittests/IR/DebugInfoTest.cpp
@@ -190,6 +190,24 @@
   EXPECT_TRUE(isa<UndefValue>(DVIs[0]->getValue(0)));
 }
 
+TEST(DIBuiler, CreateFile) {
+  LLVMContext Ctx;
+  std::unique_ptr<Module> M(new Module("MyModule", Ctx));
+  DIBuilder DIB(*M);
+
+  DIFile *F = DIB.createFile("main.c", "/");
+  EXPECT_EQ(None, F->getSource());
+
+  Optional<DIFile::ChecksumInfo<StringRef>> Checksum = None;
+  F = DIB.createFile("main.c", "/", Checksum, /*Source=*/None);
+  EXPECT_EQ(None, F->getSource());
+
+  // Test that passing an empty string gives the same result.
+  F = DIB.createFile("main.c", "/", Checksum,
+                     /*Source=*/Optional<StringRef>(""));
+  EXPECT_EQ(None, F->getSource());
+}
+
 TEST(DIBuilder, CreateFortranArrayTypeWithAttributes) {
   LLVMContext Ctx;
   std::unique_ptr<Module> M(new Module("MyModule", Ctx));
Index: llvm/include/llvm/IR/DebugInfoMetadata.h
===================================================================
--- llvm/include/llvm/IR/DebugInfoMetadata.h
+++ llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -613,12 +613,13 @@
     Optional<ChecksumInfo<MDString *>> MDChecksum;
     if (CS)
       MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
-    return getImpl(
-        Context, getCanonicalMDString(Context, Filename),
-        getCanonicalMDString(Context, Directory), MDChecksum,
-        Source ? Optional<MDString *>(getCanonicalMDString(Context, *Source))
-               : None,
-        Storage, ShouldCreate);
+    Optional<MDString *> MDSource = None;
+    if (Source)
+      if (MDString *S = getCanonicalMDString(Context, *Source))
+        MDSource = S;
+    return getImpl(Context, getCanonicalMDString(Context, Filename),
+                   getCanonicalMDString(Context, Directory), MDChecksum,
+                   MDSource, Storage, ShouldCreate);
   }
   static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
                          MDString *Directory,
@@ -654,7 +655,10 @@
     return StringRefChecksum;
   }
   Optional<StringRef> getSource() const {
-    return Source ? Optional<StringRef>((*Source)->getString()) : None;
+    if (!Source)
+      return None;
+    assert(*Source && "nullptr in Optional?");
+    return Optional<StringRef>((*Source)->getString());
   }
 
   MDString *getRawFilename() const { return getOperandAs<MDString>(0); }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138658.477746.patch
Type: text/x-patch
Size: 3443 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221124/aec94cdc/attachment.bin>


More information about the llvm-commits mailing list