Index: llvm/include/llvm/IR/DebugInfoMetadata.h =================================================================== --- llvm/include/llvm/IR/DebugInfoMetadata.h +++ llvm/include/llvm/IR/DebugInfoMetadata.h @@ -613,12 +613,13 @@ Optional> MDChecksum; if (CS) MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value)); - return getImpl( - Context, getCanonicalMDString(Context, Filename), - getCanonicalMDString(Context, Directory), MDChecksum, - Source ? Optional(getCanonicalMDString(Context, *Source)) - : None, - Storage, ShouldCreate); + Optional 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 getSource() const { - return Source ? Optional((*Source)->getString()) : None; + if (!Source) + return None; + assert(*Source && "nullptr in Optional?"); + return Optional((*Source)->getString()); } MDString *getRawFilename() const { return getOperandAs(0); } Index: llvm/unittests/IR/DebugInfoTest.cpp =================================================================== --- llvm/unittests/IR/DebugInfoTest.cpp +++ llvm/unittests/IR/DebugInfoTest.cpp @@ -190,6 +190,24 @@ EXPECT_TRUE(isa(DVIs[0]->getValue(0))); } +TEST(DIBuiler, CreateFile) { + LLVMContext Ctx; + std::unique_ptr M(new Module("MyModule", Ctx)); + DIBuilder DIB(*M); + + DIFile *F = DIB.createFile("main.c", "/"); + EXPECT_EQ(None, F->getSource()); + + Optional> 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("")); + EXPECT_EQ(None, F->getSource()); +} + TEST(DIBuilder, CreateFortranArrayTypeWithAttributes) { LLVMContext Ctx; std::unique_ptr M(new Module("MyModule", Ctx)); 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> 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("")); + EXPECT_EQ(None, N->getSource()); +} + TEST_F(DIFileTest, ScopeGetFile) { // Ensure that DIScope::getFile() returns itself. DIScope *N = DIFile::get(Context, "file", "dir");