[PATCH] D121259: [clang] Fix CodeGenAction for LLVM IR MemBuffers

Ryan Senanayake via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 8 15:13:37 PST 2022


RSenApps created this revision.
RSenApps added reviewers: jlebar, mkuper, dcastagna, shangwuyao.
Herald added a project: All.
RSenApps requested review of this revision.
Herald added a project: clang.

Replaces use of getCurrentFile with getCurrentFileOrBufferName
in CodeGenAction. This avoids an assertion error or an incorrect
name chosen for the output file when assertions are disabled.
This error previously occurred when the FrontendInputFile was a
MemoryBuffer instead of a file.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121259

Files:
  clang/lib/CodeGen/CodeGenAction.cpp
  clang/unittests/Frontend/CodeGenActionTest.cpp


Index: clang/unittests/Frontend/CodeGenActionTest.cpp
===================================================================
--- clang/unittests/Frontend/CodeGenActionTest.cpp
+++ clang/unittests/Frontend/CodeGenActionTest.cpp
@@ -59,4 +59,21 @@
   EXPECT_TRUE(Success);
 }
 
+TEST(CodeGenTest, CodeGenFromIRMemBuffer) {
+  auto Invocation = std::make_shared<CompilerInvocation>();
+  std::unique_ptr<MemoryBuffer> MemBuffer =
+      MemoryBuffer::getMemBuffer("", "test.ll");
+  Invocation->getFrontendOpts().Inputs.push_back(
+      FrontendInputFile(*MemBuffer, Language::LLVM_IR));
+  Invocation->getFrontendOpts().ProgramAction = frontend::EmitLLVMOnly;
+  Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
+  CompilerInstance Compiler;
+  Compiler.setInvocation(std::move(Invocation));
+  Compiler.createDiagnostics();
+  EXPECT_TRUE(Compiler.hasDiagnostics());
+
+  EmitLLVMOnlyAction Action;
+  bool Success = Compiler.ExecuteAction(Action);
+  EXPECT_TRUE(Success);
+}
 }
Index: clang/lib/CodeGen/CodeGenAction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenAction.cpp
+++ clang/lib/CodeGen/CodeGenAction.cpp
@@ -1113,7 +1113,7 @@
   auto &CodeGenOpts = CI.getCodeGenOpts();
   auto &Diagnostics = CI.getDiagnostics();
   std::unique_ptr<raw_pwrite_stream> OS =
-      GetOutputStream(CI, getCurrentFile(), BA);
+      GetOutputStream(CI, getCurrentFileOrBufferName(), BA);
   if (BA != Backend_EmitNothing && !OS)
     return;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121259.413953.patch
Type: text/x-patch
Size: 1503 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220308/8d471ba7/attachment.bin>


More information about the cfe-commits mailing list