Skip to content

Commit

Permalink
[Format/ObjC] Add NS_SWIFT_NAME() and CF_SWIFT_NAME() to WhitespaceSe…
Browse files Browse the repository at this point in the history
…nsitiveMacros

The argument passed to the preprocessor macros `NS_SWIFT_NAME(x)` and
`CF_SWIFT_NAME(x)` is stringified before passing to
`__attribute__((swift_name("x")))`.

ClangFormat didn't know about this stringification, so its custom parser
tried to parse the argument(s) passed to the macro as if they were
normal function arguments.

That means ClangFormat currently incorrectly inserts whitespace
between `NS_SWIFT_NAME` arguments with colons and dots, so:

```
extern UIWindow *MainWindow(void) NS_SWIFT_NAME(getter:MyHelper.mainWindow());
```

becomes:

```
extern UIWindow *MainWindow(void) NS_SWIFT_NAME(getter : MyHelper.mainWindow());
```

which clang treats as a parser error:

```
error: 'swift_name' attribute has invalid identifier for context name [-Werror,-Wswift-name-attribute]
```

Thankfully, D82620 recently added the ability to treat specific macros
as "whitespace sensitive", meaning their arguments are implicitly
treated as strings (so whitespace is not added anywhere inside).

This diff adds `NS_SWIFT_NAME` and `CF_SWIFT_NAME` to
`WhitespaceSensitiveMacros` so their arguments are implicitly treated
as whitespace-sensitive.

Test Plan:
  New tests added. Ran tests with:
  % ninja FormatTests && ./tools/clang/unittests/Format/FormatTests

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D89425
  • Loading branch information
bhamiltoncx committed Oct 14, 2020
1 parent 0ff9116 commit e7b4fee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.WhitespaceSensitiveMacros.push_back("STRINGIZE");
LLVMStyle.WhitespaceSensitiveMacros.push_back("PP_STRINGIZE");
LLVMStyle.WhitespaceSensitiveMacros.push_back("BOOST_PP_STRINGIZE");
LLVMStyle.WhitespaceSensitiveMacros.push_back("NS_SWIFT_NAME");
LLVMStyle.WhitespaceSensitiveMacros.push_back("CF_SWIFT_NAME");

// Defaults that differ when not C++.
if (Language == FormatStyle::LK_TableGen) {
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/FormatTestObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,12 @@ TEST_F(FormatTestObjC, ObjCSnippets) {
verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
verifyFormat("@property(assign, getter=isEditable) BOOL editable;");

verifyFormat("extern UIWindow *MainWindow(void) "
"NS_SWIFT_NAME(getter:MyHelper.mainWindow());");

verifyFormat("extern UIWindow *MainWindow(void) "
"CF_SWIFT_NAME(getter:MyHelper.mainWindow());");

Style.ColumnLimit = 50;
verifyFormat("@interface Foo\n"
"- (void)doStuffWithFoo:(id)name\n"
Expand Down

0 comments on commit e7b4fee

Please sign in to comment.