Skip to content

Commit c5fdb5c

Browse files
authored
[clang-format] Insert a space between a keyword and a literal (#93632)
Fixes #93603.
1 parent 461cc86 commit c5fdb5c

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,29 @@ struct FormatToken {
727727
return isOneOf(tok::star, tok::amp, tok::ampamp);
728728
}
729729

730+
bool isCppAlternativeOperatorKeyword() const {
731+
assert(!TokenText.empty());
732+
if (!isalpha(TokenText[0]))
733+
return false;
734+
735+
switch (Tok.getKind()) {
736+
case tok::ampamp:
737+
case tok::ampequal:
738+
case tok::amp:
739+
case tok::pipe:
740+
case tok::tilde:
741+
case tok::exclaim:
742+
case tok::exclaimequal:
743+
case tok::pipepipe:
744+
case tok::pipeequal:
745+
case tok::caret:
746+
case tok::caretequal:
747+
return true;
748+
default:
749+
return false;
750+
}
751+
}
752+
730753
bool isUnaryOperator() const {
731754
switch (Tok.getKind()) {
732755
case tok::plus:

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4846,8 +4846,12 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
48464846
Right.is(TT_TemplateOpener)) {
48474847
return true;
48484848
}
4849-
if (Left.Tok.getIdentifierInfo() && Right.is(tok::numeric_constant))
4849+
// C++ Core Guidelines suppression tag, e.g. `[[suppress(type.5)]]`.
4850+
if (Left.is(tok::identifier) && Right.is(tok::numeric_constant))
48504851
return Right.TokenText[0] != '.';
4852+
// `Left` is a keyword (including C++ alternative operator) or identifier.
4853+
if (Left.Tok.getIdentifierInfo() && Right.Tok.isLiteral())
4854+
return true;
48514855
} else if (Style.isProto()) {
48524856
if (Right.is(tok::period) &&
48534857
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,13 +1410,6 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
14101410
}
14111411
}
14121412

1413-
static bool isAltOperator(const FormatToken &Tok) {
1414-
return isalpha(Tok.TokenText[0]) &&
1415-
Tok.isOneOf(tok::ampamp, tok::ampequal, tok::amp, tok::pipe,
1416-
tok::tilde, tok::exclaim, tok::exclaimequal, tok::pipepipe,
1417-
tok::pipeequal, tok::caret, tok::caretequal);
1418-
}
1419-
14201413
void UnwrappedLineParser::parseStructuralElement(
14211414
const FormatToken *OpeningBrace, IfStmtKind *IfKind,
14221415
FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
@@ -1699,7 +1692,7 @@ void UnwrappedLineParser::parseStructuralElement(
16991692
for (const bool InRequiresExpression =
17001693
OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
17011694
!eof();) {
1702-
if (IsCpp && isAltOperator(*FormatTok)) {
1695+
if (IsCpp && FormatTok->isCppAlternativeOperatorKeyword()) {
17031696
if (auto *Next = Tokens->peekNextToken(/*SkipComment=*/true);
17041697
Next && Next->isBinaryOperator()) {
17051698
FormatTok->Tok.setKind(tok::identifier);

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27452,6 +27452,12 @@ TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
2745227452
Style);
2745327453
}
2745427454

27455+
TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
27456+
verifyFormat("return .5;");
27457+
verifyFormat("return not '5';");
27458+
verifyFormat("return sizeof \"5\";");
27459+
}
27460+
2745527461
} // namespace
2745627462
} // namespace test
2745727463
} // namespace format

0 commit comments

Comments
 (0)