clang-format: [Java] Ignore C++-specific keywords
authorDaniel Jasper <djasper@google.com>
Wed, 19 Nov 2014 14:11:11 +0000 (14:11 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 19 Nov 2014 14:11:11 +0000 (14:11 +0000)
Before:
  public void union
  (Object o);
  public void struct
  (Object o);
  public void delete (Object o);

After:
  public void union(Object o);
  public void struct(Object o);
  public void delete(Object o);

Patch by Harry Terkelsen, thank you!

llvm-svn: 222357

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

index fe52fb5..729ca97 100644 (file)
@@ -1688,6 +1688,11 @@ private:
       IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
       FormatTok->Tok.setIdentifierInfo(&Info);
       FormatTok->Tok.setKind(Info.getTokenID());
+      if (Style.Language == FormatStyle::LK_Java &&
+          FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) {
+        FormatTok->Tok.setKind(tok::identifier);
+        FormatTok->Tok.setIdentifierInfo(nullptr);
+      }
     } else if (FormatTok->Tok.is(tok::greatergreater)) {
       FormatTok->Tok.setKind(tok::greater);
       FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
index 57a0d51..c47cfa9 100644 (file)
@@ -275,5 +275,11 @@ TEST_F(FormatTestJava, MethodDeclarations) {
                getStyleWithColumns(40));
 }
 
+TEST_F(FormatTestJava, CppKeywords) {
+  verifyFormat("public void union(Type a, Type b);");
+  verifyFormat("public void struct(Object o);");
+  verifyFormat("public void delete(Object o);");
+}
+
 } // end namespace tooling
 } // end namespace clang