r221607 - clang-format: [Java] Never treat @interface as annotation.

Nico Weber nicolasweber at gmx.de
Mon Nov 10 08:30:02 PST 2014


Author: nico
Date: Mon Nov 10 10:30:02 2014
New Revision: 221607

URL: http://llvm.org/viewvc/llvm-project?rev=221607&view=rev
Log:
clang-format: [Java] Never treat @interface as annotation.

'@' followed by any keyword can't be an annotation, but @interface is currently
the only combination of '@' and a keyword that's allowed, so limit it to this
case. `@interface Foo` without a leading `public` was misformatted prior to
this patch.

Modified:
    cfe/trunk/lib/Format/FormatToken.h
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestJava.cpp

Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=221607&r1=221606&r2=221607&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Mon Nov 10 10:30:02 2014
@@ -299,6 +299,7 @@ struct FormatToken {
   bool isNot(T Kind) const {
     return Tok.isNot(Kind);
   }
+  bool isNot(IdentifierInfo *II) const { return II != Tok.getIdentifierInfo(); }
 
   bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
 
@@ -544,6 +545,7 @@ struct AdditionalKeywords {
 
     kw_extends = &IdentTable.get("extends");
     kw_implements = &IdentTable.get("implements");
+    kw_interface = &IdentTable.get("interface");
     kw_synchronized = &IdentTable.get("synchronized");
     kw_throws = &IdentTable.get("throws");
 
@@ -566,6 +568,7 @@ struct AdditionalKeywords {
   // Java keywords.
   IdentifierInfo *kw_extends;
   IdentifierInfo *kw_implements;
+  IdentifierInfo *kw_interface;
   IdentifierInfo *kw_synchronized;
   IdentifierInfo *kw_throws;
 

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=221607&r1=221606&r2=221607&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Nov 10 10:30:02 2014
@@ -842,7 +842,8 @@ private:
         // function declaration have been found.
         Current.Type = TT_TrailingAnnotation;
       } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
-                 Current.Previous->is(tok::at)) {
+                 Current.Previous->is(tok::at) &&
+                 Current.isNot(Keywords.kw_interface)) {
         const FormatToken& AtToken = *Current.Previous;
         if (!AtToken.Previous ||
             AtToken.Previous->Type == TT_LeadingJavaAnnotation)

Modified: cfe/trunk/unittests/Format/FormatTestJava.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJava.cpp?rev=221607&r1=221606&r2=221607&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJava.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJava.cpp Mon Nov 10 10:30:02 2014
@@ -87,6 +87,22 @@ TEST_F(FormatTestJava, ClassDeclarations
                "    implements cccccccccccc {\n"
                "}",
                getStyleWithColumns(76));
+  verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n"
+               "  void doStuff(int theStuff);\n"
+               "  void doMoreStuff(int moreStuff);\n"
+               "}");
+  verifyFormat("public interface SomeInterface {\n"
+               "  void doStuff(int theStuff);\n"
+               "  void doMoreStuff(int moreStuff);\n"
+               "}");
+  verifyFormat("@interface SomeInterface {\n"
+               "  void doStuff(int theStuff);\n"
+               "  void doMoreStuff(int moreStuff);\n"
+               "}");
+  verifyFormat("public @interface SomeInterface {\n"
+               "  void doStuff(int theStuff);\n"
+               "  void doMoreStuff(int moreStuff);\n"
+               "}");
 }
 
 TEST_F(FormatTestJava, EnumDeclarations) {





More information about the cfe-commits mailing list