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.

llvm-svn: 221109

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

index ebcad05..a131397 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 62b3e92..bcbf283 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) {