]> granicus.if.org Git - clang/commitdiff
clang-format: [Java] Never treat @interface as annotation.
authorNico Weber <nicolasweber@gmx.de>
Mon, 10 Nov 2014 16:30:02 +0000 (16:30 +0000)
committerNico Weber <nicolasweber@gmx.de>
Mon, 10 Nov 2014 16:30:02 +0000 (16:30 +0000)
'@' 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.

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

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

index c2cc3856024ab817483eb928891107aa811f0250..0ef563b73a7fdc6058248f40fbd04ebdf0fac410 100644 (file)
@@ -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;
 
index d00e648706b436fc847e5baef413469e160f809f..62ec93f3234e4bdd97b9ed62ce8f1e27f83f4e59 100644 (file)
@@ -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)
index c51566715dd4fb56627acca85c088984503673fa..44a7910e9892794ed19210942b342191d5a56b18 100644 (file)
@@ -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) {