[PATCH] D89425: [Format/ObjC] Add NS_SWIFT_NAME() and CF_SWIFT_NAME() to WhitespaceSensitiveMacros

Ben Hamilton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 14 13:36:09 PDT 2020


benhamilton created this revision.
benhamilton added reviewers: sammccall, JakeMerdichAMD, curdeius.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
benhamilton requested review of this revision.

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 <https://reviews.llvm.org/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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89425

Files:
  clang/lib/Format/Format.cpp
  clang/unittests/Format/FormatTestObjC.cpp


Index: clang/unittests/Format/FormatTestObjC.cpp
===================================================================
--- clang/unittests/Format/FormatTestObjC.cpp
+++ clang/unittests/Format/FormatTestObjC.cpp
@@ -1024,6 +1024,12 @@
   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"
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -964,6 +964,8 @@
   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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89425.298233.patch
Type: text/x-patch
Size: 1341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201014/a5c51be3/attachment-0001.bin>


More information about the cfe-commits mailing list