From c21265f9f8a7fcabeb895b61237e8decc758aaef Mon Sep 17 00:00:00 2001 From: Samuel Osewa Date: Sun, 15 May 2022 14:24:23 +0100 Subject: [PATCH] Changed error message for for Unicode character in non-unicode string literal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kamil ƚliwak --- liblangutil/Scanner.cpp | 7 ++++++- liblangutil/Scanner.h | 3 +++ test/liblangutil/Scanner.cpp | 2 +- .../string/control_character_returned_in_string.sol | 7 +++++++ .../syntaxTests/string/string_unicode_without_prefix.sol | 2 +- ..._override.sol => unicode_string_direction_override.yul} | 0 6 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 test/libsolidity/syntaxTests/string/control_character_returned_in_string.sol rename test/libyul/yulSyntaxTests/invalid/{unicode_string_direction_override.sol => unicode_string_direction_override.yul} (100%) diff --git a/liblangutil/Scanner.cpp b/liblangutil/Scanner.cpp index fede6d3f8..819dafaf5 100644 --- a/liblangutil/Scanner.cpp +++ b/liblangutil/Scanner.cpp @@ -76,6 +76,7 @@ string to_string(ScannerError _errorCode) case ScannerError::IllegalHexDigit: return "Hexadecimal digit missing or invalid."; case ScannerError::IllegalCommentTerminator: return "Expected multi-line comment-terminator."; case ScannerError::IllegalEscapeSequence: return "Invalid escape sequence."; + case ScannerError::UnicodeCharacterInNonUnicodeString: return "Invalid character in string. If you are trying to use Unicode characters, use a unicode\"...\" string literal."; case ScannerError::IllegalCharacterInString: return "Invalid character in string."; case ScannerError::IllegalStringEndQuote: return "Expected string end-quote."; case ScannerError::IllegalNumberSeparator: return "Invalid use of number separator '_'."; @@ -844,7 +845,11 @@ Token Scanner::scanString(bool const _isUnicode) // We are using a manual range and not isprint() to avoid // any potential complications with locale. if (!_isUnicode && (static_cast(c) <= 0x1f || static_cast(c) >= 0x7f)) - return setError(ScannerError::IllegalCharacterInString); + { + if (m_kind == ScannerKind::Yul) + return setError(ScannerError::IllegalCharacterInString); + return setError(ScannerError::UnicodeCharacterInNonUnicodeString); + } addLiteralChar(c); } } diff --git a/liblangutil/Scanner.h b/liblangutil/Scanner.h index e9957850c..c45a2ec24 100644 --- a/liblangutil/Scanner.h +++ b/liblangutil/Scanner.h @@ -81,6 +81,7 @@ enum class ScannerError IllegalHexDigit, IllegalCommentTerminator, IllegalEscapeSequence, + UnicodeCharacterInNonUnicodeString, IllegalCharacterInString, IllegalStringEndQuote, IllegalNumberSeparator, @@ -160,6 +161,8 @@ public: /// Called by the parser during FunctionDefinition parsing to clear the current comment void clearCurrentCommentLiteral() { m_skippedComments[Current].literal.clear(); } + ScannerKind scannerKind() const { return m_kind; } + ///@} ///@{ diff --git a/test/liblangutil/Scanner.cpp b/test/liblangutil/Scanner.cpp index 51d051236..c8a88ec82 100644 --- a/test/liblangutil/Scanner.cpp +++ b/test/liblangutil/Scanner.cpp @@ -123,7 +123,7 @@ BOOST_AUTO_TEST_CASE(string_nonprintable) if (v == '\n' || v == '\v' || v == '\f' || v == '\r') BOOST_CHECK_EQUAL(scanner.currentError(), ScannerError::IllegalStringEndQuote); else - BOOST_CHECK_EQUAL(scanner.currentError(), ScannerError::IllegalCharacterInString); + BOOST_CHECK_EQUAL(scanner.currentError(),ScannerError::UnicodeCharacterInNonUnicodeString); BOOST_CHECK_EQUAL(scanner.currentLiteral(), ""); } } diff --git a/test/libsolidity/syntaxTests/string/control_character_returned_in_string.sol b/test/libsolidity/syntaxTests/string/control_character_returned_in_string.sol new file mode 100644 index 000000000..981f5fdd5 --- /dev/null +++ b/test/libsolidity/syntaxTests/string/control_character_returned_in_string.sol @@ -0,0 +1,7 @@ +contract test { + function f() public pure returns (string memory) { + return "; + } +} +// ---- +// ParserError 8936: (86-88): Invalid character in string. If you are trying to use Unicode characters, use a unicode"..." string literal. diff --git a/test/libsolidity/syntaxTests/string/string_unicode_without_prefix.sol b/test/libsolidity/syntaxTests/string/string_unicode_without_prefix.sol index a204e84e5..581d9dd7d 100644 --- a/test/libsolidity/syntaxTests/string/string_unicode_without_prefix.sol +++ b/test/libsolidity/syntaxTests/string/string_unicode_without_prefix.sol @@ -4,4 +4,4 @@ contract test { } } // ---- -// ParserError 8936: (86-88): Invalid character in string. +// ParserError 8936: (86-88): Invalid character in string. If you are trying to use Unicode characters, use a unicode"..." string literal. diff --git a/test/libyul/yulSyntaxTests/invalid/unicode_string_direction_override.sol b/test/libyul/yulSyntaxTests/invalid/unicode_string_direction_override.yul similarity index 100% rename from test/libyul/yulSyntaxTests/invalid/unicode_string_direction_override.sol rename to test/libyul/yulSyntaxTests/invalid/unicode_string_direction_override.yul