From: Daniel Jasper Date: Sun, 2 Nov 2014 21:52:57 +0000 (+0000) Subject: clang-format: [Java] Support generics with "?". X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f11ab95e6c89cc2070a2fc9443b2e8e379c70d83;p=clang clang-format: [Java] Support generics with "?". Before: @Override public Map < String, ? > getAll() { // ... } After: @Override public Map getAll() { // ... } This fixes llvm.org/PR21454. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221109 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index ebcad05d86..a131397fb5 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -64,7 +64,10 @@ private: return true; } if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace, - tok::colon, tok::question)) + tok::colon)) + return false; + if (CurrentToken->is(tok::question) && + Style.Language != FormatStyle::LK_Java) return false; // If a && or || is found and interpreted as a binary operator, this set // of angles is likely part of something like "a < b && c > d". If the @@ -364,6 +367,10 @@ private: } bool parseConditional() { + if (Style.Language == FormatStyle::LK_Java && + CurrentToken->isOneOf(tok::comma, tok::greater)) + return true; // This is a generic "?". + while (CurrentToken) { if (CurrentToken->is(tok::colon)) { CurrentToken->Type = TT_ConditionalExpr; diff --git a/unittests/Format/FormatTestJava.cpp b/unittests/Format/FormatTestJava.cpp index 62b3e92edc..bcbf2831ef 100644 --- a/unittests/Format/FormatTestJava.cpp +++ b/unittests/Format/FormatTestJava.cpp @@ -120,6 +120,10 @@ TEST_F(FormatTestJava, Generics) { verifyFormat("Iterable a;"); verifyFormat("A.doSomething();"); + + verifyFormat("@Override\n" + "public Map getAll() {\n" + "}"); } TEST_F(FormatTestJava, StringConcatenation) {