Skip to content

[MC] Fix llvm-mc unterminated string constants warning for Windows #112995

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 1 commit into from
Oct 21, 2024

Conversation

dpaoliello
Copy link
Contributor

@dpaoliello dpaoliello commented Oct 18, 2024

#98060 introduced a warning for unterminated string constants, however it was only checking for \n which means that it 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.

@llvmbot llvmbot added the mc Machine (object) code label Oct 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2024

@llvm/pr-subscribers-mc

Author: Daniel Paoliello (dpaoliello)

Changes

#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 --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;

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Classic MacOS \r seems not relevant and not detected in newer code

@dpaoliello dpaoliello changed the title [MC] Fix llvm-mc unterminated string constants warning for MacOS and Windows [MC] Fix llvm-mc unterminated string constants warning for Windows Oct 20, 2024
@dpaoliello
Copy link
Contributor Author

Classic MacOS \r seems not relevant and not detected in newer code

Oh, huh, I didn't realize that MacOS changed newline character. Shows the last time I used it...

Updated title and description.

@dpaoliello dpaoliello merged commit 8ae39c8 into llvm:main Oct 21, 2024
10 checks passed
@dpaoliello dpaoliello deleted the warncrlf branch October 21, 2024 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants