Skip to content

[clang-format] Insert a space between a keyword and a literal #93632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,29 @@ struct FormatToken {
return isOneOf(tok::star, tok::amp, tok::ampamp);
}

bool isCppAlternativeOperatorKeyword() const {
assert(!TokenText.empty());
if (!isalpha(TokenText[0]))
return false;

switch (Tok.getKind()) {
case tok::ampamp:
case tok::ampequal:
case tok::amp:
case tok::pipe:
case tok::tilde:
case tok::exclaim:
case tok::exclaimequal:
case tok::pipepipe:
case tok::pipeequal:
case tok::caret:
case tok::caretequal:
return true;
default:
return false;
}
}

bool isUnaryOperator() const {
switch (Tok.getKind()) {
case tok::plus:
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4846,8 +4846,12 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Right.is(TT_TemplateOpener)) {
return true;
}
if (Left.Tok.getIdentifierInfo() && Right.is(tok::numeric_constant))
// C++ Core Guidelines suppression tag, e.g. `[[suppress(type.5)]]`.
if (Left.is(tok::identifier) && Right.is(tok::numeric_constant))
return Right.TokenText[0] != '.';
// `Left` is a keyword (including C++ alternative operator) or identifier.
if (Left.Tok.getIdentifierInfo() && Right.Tok.isLiteral())
return true;
} else if (Style.isProto()) {
if (Right.is(tok::period) &&
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
Expand Down
9 changes: 1 addition & 8 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1410,13 +1410,6 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
}
}

static bool isAltOperator(const FormatToken &Tok) {
return isalpha(Tok.TokenText[0]) &&
Tok.isOneOf(tok::ampamp, tok::ampequal, tok::amp, tok::pipe,
tok::tilde, tok::exclaim, tok::exclaimequal, tok::pipepipe,
tok::pipeequal, tok::caret, tok::caretequal);
}

void UnwrappedLineParser::parseStructuralElement(
const FormatToken *OpeningBrace, IfStmtKind *IfKind,
FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
Expand Down Expand Up @@ -1699,7 +1692,7 @@ void UnwrappedLineParser::parseStructuralElement(
for (const bool InRequiresExpression =
OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
!eof();) {
if (IsCpp && isAltOperator(*FormatTok)) {
if (IsCpp && FormatTok->isCppAlternativeOperatorKeyword()) {
if (auto *Next = Tokens->peekNextToken(/*SkipComment=*/true);
Next && Next->isBinaryOperator()) {
FormatTok->Tok.setKind(tok::identifier);
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27452,6 +27452,12 @@ TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
Style);
}

TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
verifyFormat("return .5;");
verifyFormat("return not '5';");
verifyFormat("return sizeof \"5\";");
}

} // namespace
} // namespace test
} // namespace format
Expand Down
Loading