Skip to content

Commit

Permalink
[clang-format] instanceof is a keyword only in Java/JavaScript (#88085)
Browse files Browse the repository at this point in the history
Fixes #87907.
  • Loading branch information
owenca committed Apr 10, 2024
1 parent 58323de commit bcf849b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,8 @@ class AnnotatingParser {
// Line.MightBeFunctionDecl can only be true after the parentheses of a
// function declaration have been found. In this case, 'Current' is a
// trailing token of this declaration and thus cannot be a name.
if (Current.is(Keywords.kw_instanceof)) {
if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
Current.is(Keywords.kw_instanceof)) {
Current.setType(TT_BinaryOperator);
} else if (isStartOfName(Current) &&
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);

Tokens = annotate("void instanceof();");
ASSERT_EQ(Tokens.size(), 6u);
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);

Tokens = annotate("int iso_time(time_t);");
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
Expand Down

0 comments on commit bcf849b

Please sign in to comment.