Inline Asm: Ensure buffer is newline terminated to match how the text is printed.
authorDaniel Dunbar <daniel@zuster.org>
Fri, 14 May 2010 04:31:50 +0000 (04:31 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Fri, 14 May 2010 04:31:50 +0000 (04:31 +0000)
 - This is a hack, but I can't decide the best place to handle this. Chris?

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@103765 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
test/Other/inline-asm-newline-terminator.ll [new file with mode: 0644]

index 37d10e5c4ccd71bdc4cc3a9f31f3b6cd0c29835f..ba6fed2a78ba7eeb66601f0bfd1ac6ea7d89306b 100644 (file)
@@ -53,6 +53,17 @@ void AsmPrinter::EmitInlineAsm(StringRef Str, unsigned LocCookie) const {
   }
   
   SourceMgr SrcMgr;
+
+  // Ensure the buffer is newline terminated.
+  char *TmpString = 0;
+  if (Str.back() != '\n') {
+    TmpString = new char[Str.size() + 2];
+    memcpy(TmpString, Str.data(), Str.size());
+    TmpString[Str.size()] = '\n';
+    TmpString[Str.size() + 1] = 0;
+    isNullTerminated = true;
+    Str = TmpString;
+  }
   
   // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
   LLVMContext &LLVMCtx = MMI->getModule()->getContext();
@@ -84,6 +95,9 @@ void AsmPrinter::EmitInlineAsm(StringRef Str, unsigned LocCookie) const {
                        /*NoFinalize*/ true);
   if (Res && !HasDiagHandler)
     report_fatal_error("Error parsing inline asm\n");
+
+  if (TmpString)
+    delete[] TmpString;
 }
 
 
diff --git a/test/Other/inline-asm-newline-terminator.ll b/test/Other/inline-asm-newline-terminator.ll
new file mode 100644 (file)
index 0000000..af93cc0
--- /dev/null
@@ -0,0 +1,6 @@
+; RUN: llc -filetype=obj -o - < %s
+
+; ModuleID = 't.c'
+target triple = "x86_64-apple-darwin10.0.0"
+
+module asm ".desc _f0, 0x10"