[llvm] [MC] Fix llvm-mc unterminated string constants warning for MacOS and Windows (PR #112995)

Daniel Paoliello via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 18 15:53:12 PDT 2024


https://github.com/dpaoliello created https://github.com/llvm/llvm-project/pull/112995

#98060 introduced a warning for unterminated string constants, however it was only checking for `\n` which means that it wouldn't trigger for MacOS newlines (`\r`), and produced strange results on Windows (always blaming column 1) including having the associated test fail if Git happened to use Windows newlines when creating the file.

This fix for this is to detect both `\r` and `\n`, but don't double-warn for Windows newlines.

>From fbf6c06a6f9a55d128c119a8b83ad76b67972b90 Mon Sep 17 00:00:00 2001
From: "Daniel Paoliello (HE/HIM)" <danpao at microsoft.com>
Date: Fri, 18 Oct 2024 15:48:36 -0700
Subject: [PATCH] Fix llvm-mc unterminated newline warnings for MacOS and
 Windows

---
 llvm/lib/MC/MCParser/AsmParser.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 3f55d8a66bc2ce..4774e5112af535 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -3037,7 +3037,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') {
+      if ((Str[i] == '\n') || (Str[i] == '\r')) {
+        // Don't double-warn for Windows newlines.
+        if ((Str[i] == '\n') && (i > 0) && (Str[i - 1] == '\r'))
+          continue;
+
         SMLoc NewlineLoc = SMLoc::getFromPointer(Str.data() + i);
         if (Warning(NewlineLoc, "unterminated string; newline inserted"))
           return true;



More information about the llvm-commits mailing list