Skip to content

[MC,ELF] Emit warning if a string constant contains newline char #98060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion llvm/lib/MC/MCParser/AsmLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,6 @@ AsmToken AsmLexer::LexQuote() {
return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
}

// TODO: does gas allow multiline string constants?
while (CurChar != '"') {
if (CurChar == '\\') {
// Allow \", etc.
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/MC/MCParser/AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3033,6 +3033,11 @@ bool AsmParser::parseEscapedString(std::string &Data) {
StringRef Str = getTok().getStringContents();
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
if (Str[i] != '\\') {
if (Str[i] == '\n') {
SMLoc NewlineLoc = SMLoc::getFromPointer(Str.data() + i);
if (Warning(NewlineLoc, "unterminated string; newline inserted"))
return true;
}
Data += Str[i];
continue;
}
Expand Down
55 changes: 55 additions & 0 deletions llvm/test/MC/ELF/warn-newline-in-escaped-string.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// RUN: llvm-mc -filetype=obj -triple x86_64 %s 2>&1 -o /dev/null \
// RUN: | FileCheck %s --implicit-check-not=warning:

.string "abcd\xFFefg
12345678"

// CHECK: [[#@LINE-3]]:21: warning: unterminated string; newline inserted
// CHECK-NEXT: .string "abcd\xFFefg

.ascii "some test ascii

sequence
with
newlines\x0A
"

// CHECK: [[#@LINE-7]]:24: warning: unterminated string; newline inserted
// CHECK-NEXT: .ascii "some test ascii
// CHECK: [[#@LINE-8]]:1: warning: unterminated string; newline inserted
// CHECK: [[#@LINE-8]]:9: warning: unterminated string; newline inserted
// CHECK-NEXT: sequence
// CHECK: [[#@LINE-9]]:5: warning: unterminated string; newline inserted
// CHECK-NEXT: with
// CHECK: [[#@LINE-10]]:13: warning: unterminated string; newline inserted
// CHECK-NEXT: newlines\x0A

.asciz "another test string

with
newline characters


"

// CHECK: [[#@LINE-8]]:28: warning: unterminated string; newline inserted
// CHECK-NEXT: .asciz "another test string
// CHECK: [[#@LINE-9]]:1: warning: unterminated string; newline inserted
// CHECK: [[#@LINE-9]]:5: warning: unterminated string; newline inserted
// CHECK-NEXT: with
// CHECK: [[#@LINE-10]]:19: warning: unterminated string; newline inserted
// CHECK-NEXT: newline characters
// CHECK: [[#@LINE-11]]:1: warning: unterminated string; newline inserted
// CHECK: [[#@LINE-11]]:1: warning: unterminated string; newline inserted

.file "warn-newline
.s"
// CHECK: [[#@LINE-2]]:20: warning: unterminated string; newline inserted

.cv_file 1 "some_an
other_file.s"
// CHECK: [[#@LINE-2]]:20: warning: unterminated string; newline inserted

.ascii "test\nvalid1_string\xFF\n\n\xFF"
.asciz "\n\n\nvalid2_string\x0A"
.string "1234\nvalid3_string\xFF\n\xFF\n"