Skip to content
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

bad error message on incorrect string literal #18079 #81670

Merged
merged 8 commits into from
Feb 16, 2024

Conversation

akshaykumars614
Copy link
Contributor

@akshaykumars614 akshaykumars614 commented Feb 13, 2024

(bad error message on incorrect string literal)

Fixed the error message for incorrect string literal

before:

test.cpp:1:19: error: invalid character '
' character in raw string delimiter; use PREFIX( )PREFIX to delimit raw string
char const* a = R"
                  ^

now:

test.cpp:1:19: error: invalid newline character in raw string delimiter; use PREFIX( )PREFIX to delimit raw string
    1 | char const* a = R"
      |                   ^

(bad errwqor message on incorrect string literal)

Fixed the error message for incorrect string literal
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 13, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 13, 2024

@llvm/pr-subscribers-clang

Author: None (akshaykumars614)

Changes

(bad error message on incorrect string literal)

Fixed the error message for incorrect string literal

before:

test.cpp:1:19: error: invalid character '
' character in raw string delimiter; use PREFIX( )PREFIX to delimit raw string
char const* a = R"
^

now:

test.cpp:1:19: error: invalid newline character in raw string delimiter; use PREFIX( )PREFIX to delimit raw string
1 | char const* a = R"
| ^


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

1 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticLexKinds.td (+1-1)
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 75ca2fa16d3485..c5a2096d02b39d 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -100,7 +100,7 @@ def err_raw_delim_too_long : Error<
   "raw string delimiter longer than 16 characters"
   "; use PREFIX( )PREFIX to delimit raw string">;
 def err_invalid_char_raw_delim : Error<
-  "invalid character '%0' character in raw string delimiter"
+  "invalid newline character in raw string delimiter"
   "; use PREFIX( )PREFIX to delimit raw string">;
 def err_unterminated_raw_string : Error<
   "raw string missing terminating delimiter )%0\"">;

@akshaykumars614 akshaykumars614 removed the request for review from shafik February 13, 2024 21:47
@akshaykumars614
Copy link
Contributor Author

I am new to this project. Please let me know if any files need to be added.

I don't believe any testing or test suite changes are required for this change.

@jroelofs
Copy link
Contributor

I am new to this project. Please let me know if any files need to be added.

I don't believe any testing or test suite changes are required for this change.

This particular change will break this usage of that diagnostic, which expects that format argument:

Diag(PrefixEnd, diag::err_invalid_char_raw_delim)
<< StringRef(PrefixEnd, 1);

The change needs to account for cases where the character should still be printed, and when it isn't printable or would have strange formatting. And then it should be accompanied by a test that verifies the new behavior.

@akshaykumars614
Copy link
Contributor Author

akshaykumars614 commented Feb 14, 2024

Got it. Thank you for helping me. I will look upon it further. Now should I close this pull request or add the updated commit here itself?

@jroelofs
Copy link
Contributor

Feel free to keep it open and push more commits to the branch.

Introduced new error code for \n delimiter in raw string delimiter. Added testcase to test the same.
Copy link

github-actions bot commented Feb 14, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

clang/include/clang/Basic/DiagnosticLexKinds.td Outdated Show resolved Hide resolved
clang/lib/Lex/Lexer.cpp Outdated Show resolved Hide resolved
clang/test/Lexer/raw-string-dlim-newline.cpp Outdated Show resolved Hide resolved
akshaykumars614 and others added 2 commits February 14, 2024 14:15
Co-authored-by: Jon Roelofs <jroelofs@gmail.com>
Co-authored-by: Jon Roelofs <jroelofs@gmail.com>
clang/lib/Lex/Lexer.cpp Outdated Show resolved Hide resolved
@jroelofs jroelofs requested a review from shafik February 14, 2024 19:24
Copy link
Contributor

@jroelofs jroelofs left a comment

Choose a reason for hiding this comment

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

LGTM. Let me know if you need me to press the "merge" button for you.

@akshaykumars614
Copy link
Contributor Author

Thank you for reviewing:)

@akshaykumars614 akshaykumars614 merged commit cc23574 into llvm:main Feb 16, 2024
3 of 4 checks passed
Copy link

@akshaykumars614 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@ldrumm
Copy link
Contributor

ldrumm commented May 1, 2024

Should we be looking for \r as well?

If the testcase is checked out with DOS line endings (e.g. on a windows machine), then the test clang/test/Lexer/raw-string-dlim-invalid.cpp fails

I don't know what the C++ spec says, but I think this diagnostic should work for both common end of line styles.

Something like this:

diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index c98645993abe..e2ab928c04ee 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -2270,7 +2270,7 @@ bool Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr,
       const char *PrefixEnd = &CurPtr[PrefixLen];
       if (PrefixLen == 16) {
         Diag(PrefixEnd, diag::err_raw_delim_too_long);
-      } else if (*PrefixEnd == '\n') {
+      } else if ((*PrefixEnd == '\r' && PrefixEnd[1] == '\n') || *PrefixEnd == '\n') {
         Diag(PrefixEnd, diag::err_invalid_newline_raw_delim);
       } else {
         Diag(PrefixEnd, diag::err_invalid_char_raw_delim)

?

@cor3ntin
Copy link
Contributor

cor3ntin commented May 1, 2024

It would be better to do, in all cases

 Diag(PrefixEnd, diag::err_invalid_char_raw_delim) <<  escapeCStyle<EscapeChar::Single>(*PrefixEnd);

(and remove the err_invalid_newline_raw_delim diagnostic altogether)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants