From: Daniel Jasper Date: Sun, 23 Nov 2014 21:34:25 +0000 (+0000) Subject: clang-format: [Java] Treat 'instanceof' like other binary operators. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0389421cac43a0014e15eabcb4af30eac6921dfd;p=clang clang-format: [Java] Treat 'instanceof' like other binary operators. This fixes llvm.org/PR21436. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@222641 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 84e1858a7b..05b05a531d 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -532,6 +532,7 @@ struct AdditionalKeywords { kw_extends = &IdentTable.get("extends"); kw_final = &IdentTable.get("final"); kw_implements = &IdentTable.get("implements"); + kw_instanceof = &IdentTable.get("instanceof"); kw_interface = &IdentTable.get("interface"); kw_synchronized = &IdentTable.get("synchronized"); kw_throws = &IdentTable.get("throws"); @@ -557,6 +558,7 @@ struct AdditionalKeywords { IdentifierInfo *kw_extends; IdentifierInfo *kw_final; IdentifierInfo *kw_implements; + IdentifierInfo *kw_instanceof; IdentifierInfo *kw_interface; IdentifierInfo *kw_synchronized; IdentifierInfo *kw_throws; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index e36a16a54a..aa6a8f537c 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -795,8 +795,10 @@ 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 (isStartOfName(Current) && - (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) { + if (Current.is(Keywords.kw_instanceof)) { + Current.Type = TT_BinaryOperator; + } else if (isStartOfName(Current) && + (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) { Contexts.back().FirstStartOfName = &Current; Current.Type = TT_StartOfName; } else if (Current.is(tok::kw_auto)) { diff --git a/unittests/Format/FormatTestJava.cpp b/unittests/Format/FormatTestJava.cpp index ba91f1677b..5a45b80f3b 100644 --- a/unittests/Format/FormatTestJava.cpp +++ b/unittests/Format/FormatTestJava.cpp @@ -54,6 +54,17 @@ TEST_F(FormatTestJava, NoAlternativeOperatorNames) { verifyFormat("someObject.and();"); } +TEST_F(FormatTestJava, FormatsInstanceOfLikeOperators) { + FormatStyle Style = getStyleWithColumns(50); + verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " instanceof bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", + Style); + Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; + verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa instanceof\n" + " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", + Style); +} + TEST_F(FormatTestJava, ClassDeclarations) { verifyFormat("public class SomeClass {\n" " private int a;\n"