[clang] bad error message on incorrect string literal #18079 (PR #81670)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 14 16:53:56 PST 2024


https://github.com/akshaykumars614 updated https://github.com/llvm/llvm-project/pull/81670

>From c2f716ee5f787ec3df63511fd5f565a3deee4d6e Mon Sep 17 00:00:00 2001
From: akshaykumars614 <akshaykumars614 at gmail.com>
Date: Tue, 13 Feb 2024 16:29:51 -0500
Subject: [PATCH 1/6] issue: #18079 (bad errwqor message on incorrect string
 literal)

Fixed the error message for incorrect string literal
---
 clang/include/clang/Basic/DiagnosticLexKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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\"">;

>From ac8b99309b07b6c7114dfbf784a46d2fb5d9dcc4 Mon Sep 17 00:00:00 2001
From: akshaykumars614 <akshaykumars614 at gmail.com>
Date: Wed, 14 Feb 2024 14:04:52 -0500
Subject: [PATCH 2/6] bad error message on incorrect string literal #18079

Introduced new error code for \n delimiter in raw string delimiter. Added testcase to test the same.
---
 clang/include/clang/Basic/DiagnosticLexKinds.td | 3 +++
 clang/lib/Lex/Lexer.cpp                         | 6 ++++--
 clang/test/Lexer/raw-string-dlim-newline.cpp    | 5 +++++
 3 files changed, 12 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Lexer/raw-string-dlim-newline.cpp

diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index c5a2096d02b39d..0bc684a9db5793 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -102,6 +102,9 @@ def err_raw_delim_too_long : Error<
 def err_invalid_char_raw_delim : Error<
   "invalid newline character in raw string delimiter"
   "; use PREFIX( )PREFIX to delimit raw string">;
+def err_invalid_nexline_raw_delim : Error<
+  "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\"">;
 def warn_cxx98_compat_raw_string_literal : Warning<
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index d927f28b47c270..95f39df7a4628a 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -2270,10 +2270,12 @@ 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 {
+      } else if (*PrefixEnd != '\n') {
         Diag(PrefixEnd, diag::err_invalid_char_raw_delim)
           << StringRef(PrefixEnd, 1);
-      }
+      } else {
+        Diag(PrefixEnd, diag::err_invalid_nexline_raw_delim);
+	}
     }
 
     // Search for the next '"' in hopes of salvaging the lexer. Unfortunately,
diff --git a/clang/test/Lexer/raw-string-dlim-newline.cpp b/clang/test/Lexer/raw-string-dlim-newline.cpp
new file mode 100644
index 00000000000000..36939ed864ecae
--- /dev/null
+++ b/clang/test/Lexer/raw-string-dlim-newline.cpp
@@ -0,0 +1,5 @@
+// RUN: not  %clang_cc1 -E %s 2>&1 | grep 'error: invalid newline character in raw string delimiter; use PREFIX( )PREFIX to delimit raw string'
+
+// Introduced new error code err_invalid_nexline_raw_delim for code which has \n as delimiter.
+char const* str1 = R"
+";

>From f8ede1571c5678aa6cdbce7cc0d5b30f191b4e92 Mon Sep 17 00:00:00 2001
From: akshaykumars614 <88362922+akshaykumars614 at users.noreply.github.com>
Date: Wed, 14 Feb 2024 14:15:34 -0500
Subject: [PATCH 3/6] Update clang/include/clang/Basic/DiagnosticLexKinds.td

Co-authored-by: Jon Roelofs <jroelofs at gmail.com>
---
 clang/include/clang/Basic/DiagnosticLexKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 0bc684a9db5793..1db34adf6bee61 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -102,7 +102,7 @@ def err_raw_delim_too_long : Error<
 def err_invalid_char_raw_delim : Error<
   "invalid newline character in raw string delimiter"
   "; use PREFIX( )PREFIX to delimit raw string">;
-def err_invalid_nexline_raw_delim : Error<
+def err_invalid_newline_raw_delim : Error<
   "invalid newline character in raw string delimiter"
   "; use PREFIX( )PREFIX to delimit raw string">;
 def err_unterminated_raw_string : Error<

>From baa768ebc8f5a5359a8921c110a39d3ff8739747 Mon Sep 17 00:00:00 2001
From: akshaykumars614 <88362922+akshaykumars614 at users.noreply.github.com>
Date: Wed, 14 Feb 2024 14:15:49 -0500
Subject: [PATCH 4/6] Update clang/lib/Lex/Lexer.cpp

Co-authored-by: Jon Roelofs <jroelofs at gmail.com>
---
 clang/lib/Lex/Lexer.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 95f39df7a4628a..9ea119cfe61b43 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -2274,8 +2274,8 @@ bool Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr,
         Diag(PrefixEnd, diag::err_invalid_char_raw_delim)
           << StringRef(PrefixEnd, 1);
       } else {
-        Diag(PrefixEnd, diag::err_invalid_nexline_raw_delim);
-	}
+        Diag(PrefixEnd, diag::err_invalid_newline_raw_delim);
+      }
     }
 
     // Search for the next '"' in hopes of salvaging the lexer. Unfortunately,

>From a63987b23045d9a3a54bbf08a86bb6f22d339d32 Mon Sep 17 00:00:00 2001
From: akshaykumars614 <88362922+akshaykumars614 at users.noreply.github.com>
Date: Wed, 14 Feb 2024 14:28:00 -0500
Subject: [PATCH 5/6] Update clang/include/clang/Basic/DiagnosticLexKinds.td

Co-authored-by: Jon Roelofs <jroelofs at gmail.com>
---
 clang/include/clang/Basic/DiagnosticLexKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 1db34adf6bee61..b5164da38904c3 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 newline character in raw string delimiter"
+  "invalid character '%0' in raw string delimiter"
   "; use PREFIX( )PREFIX to delimit raw string">;
 def err_invalid_newline_raw_delim : Error<
   "invalid newline character in raw string delimiter"

>From 790ed39ca8d3adf43020e570aaf4ceafd1c87fbe Mon Sep 17 00:00:00 2001
From: akshaykumars614 <akshaykumars614 at gmail.com>
Date: Wed, 14 Feb 2024 19:53:03 -0500
Subject: [PATCH 6/6] updated Lexer.cpp file

---
 clang/lib/Lex/Lexer.cpp                      | 6 +++---
 clang/test/Lexer/raw-string-dlim-invalid.cpp | 5 +++++
 clang/test/Lexer/raw-string-dlim-newline.cpp | 8 ++++----
 3 files changed, 12 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/Lexer/raw-string-dlim-invalid.cpp

diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 9ea119cfe61b43..c98645993abe07 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -2270,11 +2270,11 @@ 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 == '\n') {
+        Diag(PrefixEnd, diag::err_invalid_newline_raw_delim);
+      } else {
         Diag(PrefixEnd, diag::err_invalid_char_raw_delim)
           << StringRef(PrefixEnd, 1);
-      } else {
-        Diag(PrefixEnd, diag::err_invalid_newline_raw_delim);
       }
     }
 
diff --git a/clang/test/Lexer/raw-string-dlim-invalid.cpp b/clang/test/Lexer/raw-string-dlim-invalid.cpp
new file mode 100644
index 00000000000000..9beea89cd4c839
--- /dev/null
+++ b/clang/test/Lexer/raw-string-dlim-invalid.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -E -fsyntax-only -verify %s
+
+// expected-error at +2{{invalid character ')' character in raw string delimiter; use PREFIX( )PREFIX to delimit raw string}}
+// expected-error at +1{{expected expression}}
+char const *str1 = R")";
diff --git a/clang/test/Lexer/raw-string-dlim-newline.cpp b/clang/test/Lexer/raw-string-dlim-newline.cpp
index 36939ed864ecae..6cbd593120ce91 100644
--- a/clang/test/Lexer/raw-string-dlim-newline.cpp
+++ b/clang/test/Lexer/raw-string-dlim-newline.cpp
@@ -1,5 +1,5 @@
-// RUN: not  %clang_cc1 -E %s 2>&1 | grep 'error: invalid newline character in raw string delimiter; use PREFIX( )PREFIX to delimit raw string'
+// RUN: %clang_cc1 -E -fsyntax-only -verify %s
 
-// Introduced new error code err_invalid_nexline_raw_delim for code which has \n as delimiter.
-char const* str1 = R"
-";
+// expected-error at +2{{invalid newline character in raw string delimiter; use PREFIX( )PREFIX to delimit raw string}}
+// expected-error at +1{{expected expression}}
+char const* str1 = R"";



More information about the cfe-commits mailing list