diff options
| author | Ben Hamilton <benhamilton@google.com> | 2018-03-22 03:23:53 +0000 | 
|---|---|---|
| committer | Ben Hamilton <benhamilton@google.com> | 2018-03-22 03:23:53 +0000 | 
| commit | 5f91134344114826644b920d6fd5ec5b88ca47cb (patch) | |
| tree | 390b115857499e95de5bbbf020b4d783fe97cbb6 | |
| parent | 97608445b12e93b95111031492d8a46a97f33db3 (diff) | |
| download | bcm5719-llvm-5f91134344114826644b920d6fd5ec5b88ca47cb.tar.gz bcm5719-llvm-5f91134344114826644b920d6fd5ec5b88ca47cb.zip  | |
[clang-format] Don't insert space between r_paren and 'new' in ObjC decl
Summary:
Previously, clang-format would insert a space between
the closing parenthesis and 'new' in the following valid Objective-C
declaration:
  + (instancetype)new;
This was because 'new' is treated as a keyword, not an identifier.
TokenAnnotator::spaceRequiredBefore() already handled the case where
r_paren came before an identifier, so this diff extends it to
handle r_paren before 'new'.
Test Plan: New tests added. Ran tests with:
  % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests
Reviewers: djasper, jolesiak, stephanemoore
Reviewed By: djasper, jolesiak, stephanemoore
Subscribers: stephanemoore, klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D44692
llvm-svn: 328174
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 6 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTestObjC.cpp | 12 | 
2 files changed, 16 insertions, 2 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 7734b7e9a2b..681e790f877 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2615,8 +2615,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,    if (Line.Type == LT_ObjCMethodDecl) {      if (Left.is(TT_ObjCMethodSpecifier))        return true; -    if (Left.is(tok::r_paren) && Right.is(tok::identifier)) -      // Don't space between ')' and <id> +    if (Left.is(tok::r_paren) && Right.isOneOf(tok::identifier, tok::kw_new)) +      // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a +      // keyword in Objective-C, and '+ (instancetype)new;' is a standard class +      // method declaration.        return false;    }    if (Line.Type == LT_ObjCProperty && diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index 35c4d886386..51443c0f393 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -514,6 +514,7 @@ TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {                 "    evenLongerKeyword:(float)theInterval\n"                 "                error:(NSError **)theError {\n"                 "}"); +  verifyFormat("+ (instancetype)new;\n");    Style.ColumnLimit = 60;    verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"                 "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" @@ -914,6 +915,17 @@ TEST_F(FormatTestObjC, ObjCForIn) {                 "     }]) {\n}");  } +TEST_F(FormatTestObjC, ObjCNew) { +  verifyFormat("+ (instancetype)new {\n" +               "  return nil;\n" +               "}\n"); +  verifyFormat("+ (instancetype)myNew {\n" +               "  return [self new];\n" +               "}\n"); +  verifyFormat("SEL NewSelector(void) { return @selector(new); }\n"); +  verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n"); +} +  TEST_F(FormatTestObjC, ObjCLiterals) {    verifyFormat("@\"String\"");    verifyFormat("@1");  | 

