Skip to content

Commit bcf849b

Browse files
authored
[clang-format] instanceof is a keyword only in Java/JavaScript (#88085)
Fixes #87907.
1 parent 58323de commit bcf849b

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,8 @@ class AnnotatingParser {
23542354
// Line.MightBeFunctionDecl can only be true after the parentheses of a
23552355
// function declaration have been found. In this case, 'Current' is a
23562356
// trailing token of this declaration and thus cannot be a name.
2357-
if (Current.is(Keywords.kw_instanceof)) {
2357+
if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
2358+
Current.is(Keywords.kw_instanceof)) {
23582359
Current.setType(TT_BinaryOperator);
23592360
} else if (isStartOfName(Current) &&
23602361
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
17691769
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
17701770
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
17711771

1772+
Tokens = annotate("void instanceof();");
1773+
ASSERT_EQ(Tokens.size(), 6u);
1774+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
1775+
17721776
Tokens = annotate("int iso_time(time_t);");
17731777
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
17741778
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);

0 commit comments

Comments
 (0)