aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOwen Pan <owenpiano@gmail.com>2024-04-09 20:00:03 -0700
committerGitHub <noreply@github.com>2024-04-09 20:00:03 -0700
commitbcf849b1e5faea405cfbbd4bc848048651055b25 (patch)
treeb52ce312153941727bb739eb0399781f120ae72e
parent58323de2e5ed0fec81ccfe421488d7fb27ecbe38 (diff)
downloadllvm-bcf849b1e5faea405cfbbd4bc848048651055b25.zip
llvm-bcf849b1e5faea405cfbbd4bc848048651055b25.tar.gz
llvm-bcf849b1e5faea405cfbbd4bc848048651055b25.tar.bz2
[clang-format] instanceof is a keyword only in Java/JavaScript (#88085)
Fixes #87907.
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp3
-rw-r--r--clang/unittests/Format/TokenAnnotatorTest.cpp4
2 files changed, 6 insertions, 1 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 9abd428..628f704 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2354,7 +2354,8 @@ private:
// 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)) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 251e317..c3153cf 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -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);