[all-commits] [llvm/llvm-project] e7b4fe: [Format/ObjC] Add NS_SWIFT_NAME() and CF_SWIFT_NAM...

Ben Hamilton (Ben Gertzfield) via All-commits all-commits at lists.llvm.org
Wed Oct 14 14:44:13 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: e7b4feea8e1bf520b34ad8c116abab6677344b74
      https://github.com/llvm/llvm-project/commit/e7b4feea8e1bf520b34ad8c116abab6677344b74
  Author: Ben Hamilton <benhamilton at google.com>
  Date:   2020-10-14 (Wed, 14 Oct 2020)

  Changed paths:
    M clang/lib/Format/Format.cpp
    M clang/unittests/Format/FormatTestObjC.cpp

  Log Message:
  -----------
  [Format/ObjC] Add NS_SWIFT_NAME() and CF_SWIFT_NAME() to WhitespaceSensitiveMacros

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




More information about the All-commits mailing list