@@ -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
@@ -1864,13 +1868,15 @@ bool Lexer::tryLexConflictMarker(bool EatNewline) {
1864
1868
return false ;
1865
1869
}
1866
1870
1867
- bool Lexer::lexUnknown () {
1871
+ bool Lexer::lexUnknown (bool EmitDiagnosticsIfToken ) {
1868
1872
const char *Tmp = CurPtr - 1 ;
1869
1873
1870
1874
if (advanceIfValidContinuationOfIdentifier (Tmp, BufferEnd)) {
1871
1875
// If this is a valid identifier continuation, but not a valid identifier
1872
1876
// start, attempt to recover by eating more continuation characters.
1873
- diagnose (CurPtr - 1 , diag::lex_invalid_identifier_start_character);
1877
+ if (EmitDiagnosticsIfToken) {
1878
+ diagnose (CurPtr - 1 , diag::lex_invalid_identifier_start_character);
1879
+ }
1874
1880
while (advanceIfValidContinuationOfIdentifier (Tmp, BufferEnd))
1875
1881
;
1876
1882
CurPtr = Tmp;
@@ -1886,25 +1892,30 @@ bool Lexer::lexUnknown() {
1886
1892
return false ; // Skip presumed whitespace.
1887
1893
} else if (Codepoint == 0x0000201D ) {
1888
1894
// If this is an end curly quote, just diagnose it with a fixit hint.
1889
- diagnose (CurPtr - 1 , diag::lex_invalid_curly_quote)
1890
- .fixItReplaceChars (getSourceLoc (CurPtr - 1 ), getSourceLoc (Tmp), " \" " );
1895
+ if (EmitDiagnosticsIfToken) {
1896
+ diagnose (CurPtr - 1 , diag::lex_invalid_curly_quote)
1897
+ .fixItReplaceChars (getSourceLoc (CurPtr - 1 ), getSourceLoc (Tmp), " \" " );
1898
+ }
1891
1899
CurPtr = Tmp;
1892
1900
return true ;
1893
1901
} else if (Codepoint == 0x0000201C ) {
1894
1902
auto EndPtr = Tmp;
1895
1903
// If this is a start curly quote, do a fuzzy match of a string literal
1896
1904
// to improve recovery.
1897
- if (auto Tmp2 = findEndOfCurlyQuoteStringLiteral (Tmp))
1905
+ if (auto Tmp2 =
1906
+ findEndOfCurlyQuoteStringLiteral (Tmp, EmitDiagnosticsIfToken))
1898
1907
Tmp = Tmp2;
1899
1908
1900
1909
// Note, we intentionally diagnose the end quote before the start quote,
1901
1910
// so that the IDE suggests fixing the end quote before the start quote.
1902
1911
// This, in turn, works better with our error recovery because we won't
1903
1912
// diagnose an end curly quote in the middle of a straight quoted
1904
1913
// literal.
1905
- diagnose (CurPtr - 1 , diag::lex_invalid_curly_quote)
1906
- .fixItReplaceChars (getSourceLoc (CurPtr - 1 ), getSourceLoc (EndPtr),
1907
- " \" " );
1914
+ if (EmitDiagnosticsIfToken) {
1915
+ diagnose (CurPtr - 1 , diag::lex_invalid_curly_quote)
1916
+ .fixItReplaceChars (getSourceLoc (CurPtr - 1 ), getSourceLoc (EndPtr),
1917
+ " \" " );
1918
+ }
1908
1919
CurPtr = Tmp;
1909
1920
return true ;
1910
1921
}
@@ -2167,7 +2178,7 @@ void Lexer::lexImpl() {
2167
2178
if (advanceIfValidStartOfOperator (Tmp, BufferEnd))
2168
2179
return lexOperatorIdentifier ();
2169
2180
2170
- bool ShouldTokenize = lexUnknown ();
2181
+ bool ShouldTokenize = lexUnknown (/* EmitDiagnosticsIfToken= */ true );
2171
2182
if (ShouldTokenize) {
2172
2183
return formToken (tok::unknown, TokStart);
2173
2184
}
0 commit comments