@@ -1739,8 +1739,9 @@ void Lexer::lexStringLiteral() {
1739
1739
// / string literal, diagnose the problem and return a pointer to the end of the
1740
1740
// / entire string literal. This helps us avoid parsing the body of the string
1741
1741
// / as program tokens, which will only lead to massive confusion.
1742
- const char *Lexer::findEndOfCurlyQuoteStringLiteral (const char *Body) {
1743
-
1742
+ const char *Lexer::findEndOfCurlyQuoteStringLiteral (const char *Body,
1743
+ bool EmitDiagnostics) {
1744
+
1744
1745
while (true ) {
1745
1746
// Don't bother with string interpolations.
1746
1747
if (*Body == ' \\ ' && *(Body + 1 ) == ' (' )
@@ -1752,7 +1753,7 @@ const char *Lexer::findEndOfCurlyQuoteStringLiteral(const char *Body) {
1752
1753
1753
1754
// Get the next character.
1754
1755
const char *CharStart = Body;
1755
- unsigned CharValue = lexCharacter (Body, ' \0 ' , false );
1756
+ unsigned CharValue = lexCharacter (Body, ' \0 ' , /* EmitDiagnostics= */ false );
1756
1757
// If the character was incorrectly encoded, give up.
1757
1758
if (CharValue == ~1U ) return nullptr ;
1758
1759
@@ -1764,8 +1765,11 @@ const char *Lexer::findEndOfCurlyQuoteStringLiteral(const char *Body) {
1764
1765
// If we found an ending curly quote (common since this thing started with
1765
1766
// an opening curly quote) diagnose it with a fixit and then return.
1766
1767
if (CharValue == 0x0000201D ) {
1767
- diagnose (CharStart, diag::lex_invalid_curly_quote)
1768
- .fixItReplaceChars (getSourceLoc (CharStart), getSourceLoc (Body), " \" " );
1768
+ if (EmitDiagnostics) {
1769
+ diagnose (CharStart, diag::lex_invalid_curly_quote)
1770
+ .fixItReplaceChars (getSourceLoc (CharStart), getSourceLoc (Body),
1771
+ " \" " );
1772
+ }
1769
1773
return Body;
1770
1774
}
1771
1775
@@ -1875,15 +1879,19 @@ Lexer::NulCharacterKind Lexer::getNulCharacterKind(const char *Ptr) const {
1875
1879
return NulCharacterKind::Embedded;
1876
1880
}
1877
1881
1878
- bool Lexer::lexInvalidCharacters (const char *&Ptr) {
1882
+ bool Lexer::lexInvalidCharacters (const char *&Ptr, bool InLexTrivia) {
1883
+ // in lexTrivia, diagnose only when its should not be tokenize.
1884
+
1879
1885
assert (Ptr != nullptr );
1880
1886
1881
1887
const char *const StartPtr = Ptr;
1882
1888
1883
1889
if (advanceIfValidContinuationOfIdentifier (Ptr, BufferEnd)) {
1884
1890
// If this is a valid identifier continuation, but not a valid identifier
1885
1891
// start, attempt to recover by eating more continuation characters.
1886
- diagnose (StartPtr, diag::lex_invalid_identifier_start_character);
1892
+ if (!InLexTrivia) {
1893
+ diagnose (StartPtr, diag::lex_invalid_identifier_start_character);
1894
+ }
1887
1895
while (advanceIfValidContinuationOfIdentifier (Ptr, BufferEnd))
1888
1896
;
1889
1897
return true ;
@@ -1900,18 +1908,21 @@ bool Lexer::lexInvalidCharacters(const char *&Ptr) {
1900
1908
1901
1909
if (codepoint == 0x0000201D ) {
1902
1910
// If this is an end curly quote, just diagnose it with a fixit hint.
1903
- diagnose (CurPtr - 1 , diag::lex_invalid_curly_quote)
1904
- .fixItReplaceChars (getSourceLoc (StartPtr), getSourceLoc (Ptr), " \" " );
1911
+ if (!InLexTrivia) {
1912
+ diagnose (CurPtr - 1 , diag::lex_invalid_curly_quote)
1913
+ .fixItReplaceChars (getSourceLoc (StartPtr), getSourceLoc (Ptr), " \" " );
1914
+ }
1905
1915
return true ;
1906
1916
}
1907
1917
1908
1918
if (codepoint == 0x0000201C ) {
1909
1919
const char *const LeftQuoteEndPtr = Ptr;
1920
+ bool EmitDiagnostics = !InLexTrivia;
1910
1921
1911
1922
// If this is a start curly quote, do a fuzzy match of a string literal
1912
1923
// to improve recovery.
1913
1924
if (const char *const RightQuoteEndPtr =
1914
- findEndOfCurlyQuoteStringLiteral (Ptr)) {
1925
+ findEndOfCurlyQuoteStringLiteral (Ptr, EmitDiagnostics )) {
1915
1926
Ptr = RightQuoteEndPtr;
1916
1927
}
1917
1928
@@ -1920,10 +1931,11 @@ bool Lexer::lexInvalidCharacters(const char *&Ptr) {
1920
1931
// This, in turn, works better with our error recovery because we won't
1921
1932
// diagnose an end curly quote in the middle of a straight quoted
1922
1933
// literal.
1923
- diagnose (StartPtr, diag::lex_invalid_curly_quote)
1924
- .fixItReplaceChars (getSourceLoc (StartPtr),
1925
- getSourceLoc (LeftQuoteEndPtr), " \" " );
1926
-
1934
+ if (EmitDiagnostics) {
1935
+ diagnose (StartPtr, diag::lex_invalid_curly_quote)
1936
+ .fixItReplaceChars (getSourceLoc (StartPtr),
1937
+ getSourceLoc (LeftQuoteEndPtr), " \" " );
1938
+ }
1927
1939
return true ;
1928
1940
}
1929
1941
@@ -2174,7 +2186,7 @@ void Lexer::lexImpl() {
2174
2186
if (advanceIfValidStartOfOperator (tmp, BufferEnd))
2175
2187
return lexOperatorIdentifier ();
2176
2188
2177
- bool ShouldTokenize = lexInvalidCharacters (tmp);
2189
+ bool ShouldTokenize = lexInvalidCharacters (tmp, /* InLexTrivia= */ false );
2178
2190
CurPtr = tmp;
2179
2191
2180
2192
if (ShouldTokenize) {
0 commit comments