diff options
author | Owen Pan <owenpiano@gmail.com> | 2025-07-10 18:14:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-10 18:14:45 -0700 |
commit | cb52efb8936c0da0a03958daa95d45eaaf8806fb (patch) | |
tree | 8c7813a8e023e469a2da4698f295c363bcebf714 /clang/lib/Format/FormatTokenLexer.cpp | |
parent | 7704f817d0a60596c4c8883c8a8ece67f0a8255a (diff) | |
download | llvm-cb52efb8936c0da0a03958daa95d45eaaf8806fb.zip llvm-cb52efb8936c0da0a03958daa95d45eaaf8806fb.tar.gz llvm-cb52efb8936c0da0a03958daa95d45eaaf8806fb.tar.bz2 |
[clang-format] Split line comments separated by backslashes (#147648)
Fixes #147341
Diffstat (limited to 'clang/lib/Format/FormatTokenLexer.cpp')
-rw-r--r-- | clang/lib/Format/FormatTokenLexer.cpp | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 40b62b2..d8ee5cb 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -1329,6 +1329,8 @@ FormatToken *FormatTokenLexer::getNextToken() { if (FormatTok->is(tok::unknown)) FormatTok->setType(TT_ImplicitStringLiteral); + const bool IsCpp = Style.isCpp(); + // JavaScript and Java do not allow to escape the end of the line with a // backslash. Backslashes are syntax errors in plain source, but can occur in // comments. When a single line comment ends with a \, it'll cause the next @@ -1336,16 +1338,17 @@ FormatToken *FormatTokenLexer::getNextToken() { // finds comments that contain a backslash followed by a line break, truncates // the comment token at the backslash, and resets the lexer to restart behind // the backslash. - if ((Style.isJavaScript() || Style.isJava()) && FormatTok->is(tok::comment) && - FormatTok->TokenText.starts_with("//")) { - size_t BackslashPos = FormatTok->TokenText.find('\\'); - while (BackslashPos != StringRef::npos) { - if (BackslashPos + 1 < FormatTok->TokenText.size() && - FormatTok->TokenText[BackslashPos + 1] == '\n') { - truncateToken(BackslashPos + 1); + if (const auto Text = FormatTok->TokenText; + Text.starts_with("//") && + (IsCpp || Style.isJavaScript() || Style.isJava())) { + assert(FormatTok->is(tok::comment)); + for (auto Pos = Text.find('\\'); Pos++ != StringRef::npos; + Pos = Text.find('\\', Pos)) { + if (Pos < Text.size() && Text[Pos] == '\n' && + (!IsCpp || Text.substr(Pos + 1).ltrim().starts_with("//"))) { + truncateToken(Pos); break; } - BackslashPos = FormatTok->TokenText.find('\\', BackslashPos + 1); } } @@ -1450,7 +1453,7 @@ FormatToken *FormatTokenLexer::getNextToken() { Column = FormatTok->LastLineColumnWidth; } - if (Style.isCpp()) { + if (IsCpp) { auto *Identifier = FormatTok->Tok.getIdentifierInfo(); auto it = Macros.find(Identifier); if ((Tokens.empty() || !Tokens.back()->Tok.getIdentifierInfo() || |