]> granicus.if.org Git - clang/commitdiff
clang-format: [Java] Support generics with "?".
authorDaniel Jasper <djasper@google.com>
Sun, 2 Nov 2014 21:52:57 +0000 (21:52 +0000)
committerDaniel Jasper <djasper@google.com>
Sun, 2 Nov 2014 21:52:57 +0000 (21:52 +0000)
Before:
  @Override
  public Map < String,
          ? > getAll() {
    // ...
  }

After:
  @Override
  public Map<String, ?> getAll() {
    // ...
  }

This fixes llvm.org/PR21454.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221109 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJava.cpp

index ebcad05d863eaaaf092e9461f3fec7ed6e041c81..a131397fb52207fab3f7ab24792d837618c77ff0 100644 (file)
@@ -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;
index 62b3e92edc103b24cb8b5e7fbc28b0a0a8ef42b2..bcbf2831efa5181719efe8bed73e041200c258f4 100644 (file)
@@ -120,6 +120,10 @@ TEST_F(FormatTestJava, Generics) {
   verifyFormat("Iterable<? extends SomeObject> a;");
 
   verifyFormat("A.<B>doSomething();");
+
+  verifyFormat("@Override\n"
+               "public Map<String, ?> getAll() {\n"
+               "}");
 }
 
 TEST_F(FormatTestJava, StringConcatenation) {