jaredgrubb created this revision. jaredgrubb added reviewers: djasper, egorzhdan, benhamilton. jaredgrubb added a project: clang-format. Herald added a project: All. jaredgrubb requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
The ObjC-block detection code only supports a single token as the return type. Add support to detect pointers, too (ObjC has lots of object-pointers). For example, using `BasedOnStyle: WebKit`, the following is stable output: int* p = ^int*(void) { // return nullptr; } (); After the patch: int* p = ^int*(void) { // return nullptr; }(); Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D146434 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp clang/unittests/Format/FormatTestObjC.cpp Index: clang/unittests/Format/FormatTestObjC.cpp =================================================================== --- clang/unittests/Format/FormatTestObjC.cpp +++ clang/unittests/Format/FormatTestObjC.cpp @@ -994,6 +994,20 @@ verifyFormat("int (^foo[kNumEntries])(char, float);"); verifyFormat("int (^foo[kNumEntries + 10])(char, float);"); verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);"); + + verifyFormat("int *p = ^int *() { //\n" + " return nullptr;\n" + "}();"); + + verifyFormat("int * (^p)(void) = ^int *(void) { //\n" + " return nullptr;\n" + "};"); + + // WebKit forces function braces onto a newline, but blocks should not. + Style = getWebKitStyle(); + verifyFormat("int* p = ^int*() { //\n" + " return nullptr;\n" + "}();"); } TEST_F(FormatTestObjC, ObjCSnippets) { Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -22175,7 +22175,8 @@ " }\n" " }\n" "});"); - verifyFormat("Block b = ^int *(A *a, B *b) {}"); + verifyFormat("Block b = ^int *(A *a, B *b) {\n" + "};"); verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" "};"); Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1773,12 +1773,18 @@ break; case tok::caret: nextToken(); + // Block return type. if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isSimpleTypeSpecifier()) { nextToken(); + // Return types: pointers are ok too. + while (FormatTok->is(tok::star)) + nextToken(); } + // Block argument list. if (FormatTok->is(tok::l_paren)) parseParens(); + // Block body. if (FormatTok->is(tok::l_brace)) parseChildBlock(); break;
Index: clang/unittests/Format/FormatTestObjC.cpp =================================================================== --- clang/unittests/Format/FormatTestObjC.cpp +++ clang/unittests/Format/FormatTestObjC.cpp @@ -994,6 +994,20 @@ verifyFormat("int (^foo[kNumEntries])(char, float);"); verifyFormat("int (^foo[kNumEntries + 10])(char, float);"); verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);"); + + verifyFormat("int *p = ^int *() { //\n" + " return nullptr;\n" + "}();"); + + verifyFormat("int * (^p)(void) = ^int *(void) { //\n" + " return nullptr;\n" + "};"); + + // WebKit forces function braces onto a newline, but blocks should not. + Style = getWebKitStyle(); + verifyFormat("int* p = ^int*() { //\n" + " return nullptr;\n" + "}();"); } TEST_F(FormatTestObjC, ObjCSnippets) { Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -22175,7 +22175,8 @@ " }\n" " }\n" "});"); - verifyFormat("Block b = ^int *(A *a, B *b) {}"); + verifyFormat("Block b = ^int *(A *a, B *b) {\n" + "};"); verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" "};"); Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1773,12 +1773,18 @@ break; case tok::caret: nextToken(); + // Block return type. if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isSimpleTypeSpecifier()) { nextToken(); + // Return types: pointers are ok too. + while (FormatTok->is(tok::star)) + nextToken(); } + // Block argument list. if (FormatTok->is(tok::l_paren)) parseParens(); + // Block body. if (FormatTok->is(tok::l_brace)) parseChildBlock(); break;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits