[llvm] Emit warning if a string constant contains newline char. (PR #98060)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 8 11:06:34 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mc

Author: Dmitriy Chestnykh (chestnykh)

<details>
<summary>Changes</summary>

GAS emits warning about newline in the string constant so make the same behaviour.

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


2 Files Affected:

- (modified) llvm/lib/MC/MCParser/AsmLexer.cpp (+6-1) 
- (added) llvm/test/MC/ELF/warn-newline-in-string-constant.s (+6) 


``````````diff
diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp
index e08404ae0ad92f..eda7aedd9f5d3d 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -18,6 +18,7 @@
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCParser/MCAsmLexer.h"
+#include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/SMLoc.h"
 #include "llvm/Support/SaveAndRestore.h"
@@ -646,13 +647,17 @@ AsmToken AsmLexer::LexQuote() {
     return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
   }
 
-  // TODO: does gas allow multiline string constants?
+  // gas doesn't allow multiline string constants
+  // and emits a warning if a string constant contains newline character.
   while (CurChar != '"') {
     if (CurChar == '\\') {
       // Allow \", etc.
       CurChar = getNextChar();
     }
 
+    if (CurChar == '\n')
+      outs() << "Warning: unterminated string; newline inserted\n";
+
     if (CurChar == EOF)
       return ReturnError(TokStart, "unterminated string constant");
 
diff --git a/llvm/test/MC/ELF/warn-newline-in-string-constant.s b/llvm/test/MC/ELF/warn-newline-in-string-constant.s
new file mode 100644
index 00000000000000..e126db30ee47a4
--- /dev/null
+++ b/llvm/test/MC/ELF/warn-newline-in-string-constant.s
@@ -0,0 +1,6 @@
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o %t | FileCheck %s
+
+.string "abcdefg
+12345678"
+
+// CHECK: Warning: unterminated string; newline inserted

``````````

</details>


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


More information about the llvm-commits mailing list