[llvm] Emit warning if a string constant contains newline char. (PR #98060)
Dmitriy Chestnykh via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 8 11:06:00 PDT 2024
https://github.com/chestnykh created https://github.com/llvm/llvm-project/pull/98060
GAS emits warning about newline in the string constant so make the same behaviour.
>From d885712d2d24a77addc17973dbcd24c7ddbd186e Mon Sep 17 00:00:00 2001
From: Dmitry Chestnykh <dm.chestnykh at gmail.com>
Date: Mon, 8 Jul 2024 21:06:19 +0300
Subject: [PATCH] Emit warning if a string constant contains newline char.
GAS emits warning about newline in the string constant
so make the same behaviour.
---
llvm/lib/MC/MCParser/AsmLexer.cpp | 7 ++++++-
llvm/test/MC/ELF/warn-newline-in-string-constant.s | 6 ++++++
2 files changed, 12 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/MC/ELF/warn-newline-in-string-constant.s
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
More information about the llvm-commits
mailing list