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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mc

Author: Daniel Paoliello (dpaoliello)

<details>
<summary>Changes</summary>

#<!-- -->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.

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


1 Files Affected:

- (modified) llvm/lib/MC/MCParser/AsmParser.cpp (+5-1) 


``````````diff
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;

``````````

</details>


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


More information about the llvm-commits mailing list