[cfe-commits] r172065 - in /cfe/trunk: include/clang/Format/Format.h lib/Format/Format.cpp test/Index/comment-c-decls.c test/Index/format-comment-cdecls.c unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Thu Jan 10 05:08:12 PST 2013


Author: djasper
Date: Thu Jan 10 07:08:12 2013
New Revision: 172065

URL: http://llvm.org/viewvc/llvm-project?rev=172065&view=rev
Log:
Improvements to function type and ObjC block formatting.

Before: int (^myBlock) (int) = ^(int num) {}
        A<void ()>;
	int (*b)(int);

After:  int (^myBlock)(int) = ^(int num) {}
        A<void()>;
	int(*b)(int);

For function types and function pointer types, this patch only makes
the behavior consistent (for types that are keywords and other types).
For the latter function pointer type declarations, we'll probably
want to add a space after "int".

Also added LangOpts.Bool = 1, so we handle "A<bool()>" appropriately
Moved the LangOpts-settings to a public place for use by tests
and clang-format binary.

Modified:
    cfe/trunk/include/clang/Format/Format.h
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/test/Index/comment-c-decls.c
    cfe/trunk/test/Index/format-comment-cdecls.c
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=172065&r1=172064&r2=172065&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Thu Jan 10 07:08:12 2013
@@ -78,6 +78,9 @@
                                SourceManager &SourceMgr,
                                std::vector<CharSourceRange> Ranges);
 
+/// \brief Returns the \c LangOpts that the formatter expects you to set.
+LangOptions getFormattingLangOpts();
+
 } // end namespace format
 } // end namespace clang
 

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172065&r1=172064&r2=172065&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jan 10 07:08:12 2013
@@ -34,6 +34,7 @@
   TT_CtorInitializerColon,
   TT_DirectorySeparator,
   TT_LineComment,
+  TT_ObjCBlockLParen,
   TT_ObjCMethodSpecifier,
   TT_OverloadedOperator,
   TT_PointerOrReference,
@@ -627,6 +628,8 @@
     }
 
     bool parseParens() {
+      if (CurrentToken != NULL && CurrentToken->is(tok::caret))
+        CurrentToken->Parent->Type = TT_ObjCBlockLParen;
       while (CurrentToken != NULL) {
         if (CurrentToken->is(tok::r_paren)) {
           next();
@@ -995,7 +998,8 @@
     if (Left.is(tok::at) &&
         (Right.is(tok::identifier) || Right.is(tok::string_literal) ||
          Right.is(tok::char_constant) || Right.is(tok::numeric_constant) ||
-         Right.is(tok::l_paren) || Right.is(tok::l_brace)))
+         Right.is(tok::l_paren) || Right.is(tok::l_brace) ||
+         Right.is(tok::kw_true) || Right.is(tok::kw_false)))
       return false;
     if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less))
       return false;
@@ -1024,8 +1028,7 @@
     if (Right.is(tok::l_paren)) {
       return Left.is(tok::kw_if) || Left.is(tok::kw_for) ||
              Left.is(tok::kw_while) || Left.is(tok::kw_switch) ||
-             (Left.isNot(tok::identifier) && Left.isNot(tok::kw_sizeof) &&
-              Left.isNot(tok::kw_typeof) && Left.isNot(tok::kw_alignof));
+             Left.is(tok::kw_return) || Left.is(tok::kw_catch);
     }
     if (Left.is(tok::at) &&
         Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword)
@@ -1050,11 +1053,11 @@
         return false;
     }
 
-    if (Tok.Type == TT_CtorInitializerColon)
+    if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
       return true;
     if (Tok.Type == TT_OverloadedOperator)
       return Tok.is(tok::identifier) || Tok.is(tok::kw_new) ||
-             Tok.is(tok::kw_delete);
+             Tok.is(tok::kw_delete) || Tok.is(tok::kw_bool);
     if (Tok.Parent->Type == TT_OverloadedOperator)
       return false;
     if (Tok.is(tok::colon))
@@ -1290,5 +1293,15 @@
   return formatter.format();
 }
 
+LangOptions getFormattingLangOpts() {
+  LangOptions LangOpts;
+  LangOpts.CPlusPlus = 1;
+  LangOpts.CPlusPlus11 = 1;
+  LangOpts.Bool = 1;
+  LangOpts.ObjC1 = 1;
+  LangOpts.ObjC2 = 1;
+  return LangOpts;
+}
+
 } // namespace format
 } // namespace clang

Modified: cfe/trunk/test/Index/comment-c-decls.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/comment-c-decls.c?rev=172065&r1=172064&r2=172065&view=diff
==============================================================================
--- cfe/trunk/test/Index/comment-c-decls.c (original)
+++ cfe/trunk/test/Index/comment-c-decls.c Thu Jan 10 07:08:12 2013
@@ -95,10 +95,10 @@
  *\brief block declaration
 */
 int (^Block) (int i, int j);
-// CHECK: <Declaration>int (^Block) (int, int)</Declaration>
+// CHECK: <Declaration>int (^Block)(int, int)</Declaration>
 
 /**
  *\brief block declaration
 */
 int (^Block1) (int i, int j) = ^(int i, int j) { return i + j; };
-// CHECK: <Declaration>int (^Block1) (int, int) = ^(int i, int j) {\n}</Declaration>
+// CHECK: <Declaration>int (^Block1)(int, int) = ^(int i, int j) {\n}</Declaration>

Modified: cfe/trunk/test/Index/format-comment-cdecls.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/format-comment-cdecls.c?rev=172065&r1=172064&r2=172065&view=diff
==============================================================================
--- cfe/trunk/test/Index/format-comment-cdecls.c (original)
+++ cfe/trunk/test/Index/format-comment-cdecls.c Thu Jan 10 07:08:12 2013
@@ -90,10 +90,10 @@
  *\brief block declaration
 */
 int (^Block) (int i, int j);
-// CHECK: <Declaration>int (^Block) (int, int)</Declaration>
+// CHECK: <Declaration>int (^Block)(int, int)</Declaration>
 
 /**
  *\brief block declaration
 */
 int (^Block1) (int i, int j) = ^(int i, int j) { return i + j; };
-// CHECK: <Declaration>int (^Block1) (int, int) = ^(int i, int j) {\n}</Declaration>
+// CHECK: <Declaration>int (^Block1)(int, int) = ^(int i, int j) {\n}</Declaration>

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172065&r1=172064&r2=172065&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jan 10 07:08:12 2013
@@ -26,12 +26,8 @@
     std::vector<CharSourceRange> Ranges(
         1,
         CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
-    LangOptions LangOpts;
-    LangOpts.CPlusPlus = 1;
-    LangOpts.CPlusPlus11 = 1;
-    LangOpts.ObjC1 = 1;
-    LangOpts.ObjC2 = 1;
-    Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts);
+    Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
+              getFormattingLangOpts());
     tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
                                              Ranges);
     EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
@@ -1029,6 +1025,15 @@
   verifyGoogleFormat("int a = b ? *c : *d;");
 }
 
+TEST_F(FormatTest, FormatsFunctionTypes) {
+  // FIXME: Determine the cases that need a space after the return type and fix.
+  verifyFormat("A<bool()> a;");
+  verifyFormat("A<SomeType()> a;");
+  verifyFormat("A<void(*)(int, std::string)> a;");
+
+  verifyFormat("int(*func)(void *);");
+}
+
 TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
   verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
                "                  int LoooooooooooooooongParam2) {\n}");
@@ -1177,8 +1182,8 @@
 }
 
 TEST_F(FormatTest, FormatObjCBlocks) {
-  verifyFormat("int (^Block) (int, int);");
-  verifyFormat("int (^Block1) (int, int) = ^(int i, int j)");
+  verifyFormat("int (^Block)(int, int);");
+  verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
 }
 
 TEST_F(FormatTest, FormatObjCInterface) {





More information about the cfe-commits mailing list