This is an archive of the discontinued LLVM Phabricator instance.

[Format/ObjC] Add NS_SWIFT_NAME() and CF_SWIFT_NAME() to WhitespaceSensitiveMacros
ClosedPublic

Authored by benhamilton on Oct 14 2020, 1:36 PM.

Details

Summary

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

Diff Detail

Event Timeline

benhamilton created this revision.Oct 14 2020, 1:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2020, 1:36 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
benhamilton requested review of this revision.Oct 14 2020, 1:36 PM
sammccall accepted this revision.Oct 14 2020, 2:35 PM
This revision is now accepted and ready to land.Oct 14 2020, 2:35 PM
MyDeveloperDay added a subscriber: MyDeveloperDay.EditedMay 15 2021, 4:46 AM

After this change, this

FOUNDATION_EXPORT NSString *const kFIRInstanceIDScopeFirebaseMessaging
    NS_SWIFT_NAME(InstanceIDScopeFirebaseMessaging) DEPRECATED_ATTRIBUTE;

typedef NS_ENUM(NSInteger, FIRHTTPMethod) {
  /** HTTP Method GET */
  FIRHTTPMethodGET NS_SWIFT_NAME(get),
  /** HTTP Method PUT */
  FIRHTTPMethodPUT NS_SWIFT_NAME(put),
  /** HTTP Method POST */
  FIRHTTPMethodPOST NS_SWIFT_NAME(post),
  /** HTTP Method DELETE */
  FIRHTTPMethodDELETE NS_SWIFT_NAME(delete),
  /** HTTP Method HEAD */
  FIRHTTPMethodHEAD NS_SWIFT_NAME(head),
  /** HTTP Method PATCH */
  FIRHTTPMethodPATCH NS_SWIFT_NAME(patch),
  /** HTTP Method OPTIONS */
  FIRHTTPMethodOPTIONS NS_SWIFT_NAME(options),
  /** HTTP Method TRACE */
  FIRHTTPMethodTRACE NS_SWIFT_NAME(trace),
  /** HTTP Method CONNECT */
  FIRHTTPMethodCONNECT NS_SWIFT_NAME(connect)
} NS_SWIFT_NAME(HTTPMethod);

is formatted as

FOUNDATION_EXPORT NSString *const
    kFIRInstanceIDScopeFirebaseMessaging NS_SWIFT_NAME(InstanceIDScopeFirebaseMe
ssaging) DEPRECATED_ATTRIBUTE;

typedef NS_ENUM(NSInteger, FIRHTTPMethod) {
  /** HTTP Method GET */
  FIRHTTPMethodGET NS_SWIFT_NAME(get),
      /** HTTP Method PUT */
      FIRHTTPMethodPUT NS_SWIFT_NAME(put),
          /** HTTP Method POST */
          FIRHTTPMethodPOST NS_SWIFT_NAME(post),
              /** HTTP Method DELETE */
              FIRHTTPMethodDELETE NS_SWIFT_NAME(delete),
                  /** HTTP Method HEAD */
                  FIRHTTPMethodHEAD NS_SWIFT_NAME(head),
                      /** HTTP Method PATCH */
                      FIRHTTPMethodPATCH NS_SWIFT_NAME(patch),
                          /** HTTP Method OPTIONS */
                          FIRHTTPMethodOPTIONS NS_SWIFT_NAME(options),
                              /** HTTP Method TRACE */
                              FIRHTTPMethodTRACE NS_SWIFT_NAME(trace),
                                  /** HTTP Method CONNECT */
                                  FIRHTTPMethodCONNECT NS_SWIFT_NAME(connect)
                              } NS_SWIFT_NAME(HTTPMethod);

This is logged as https://bugs.llvm.org/show_bug.cgi?id=49995

I bisected to these two sha's to see this change after I noticed this was logged, I assumed this might have been related to another potential change, but turns out to be this. (sorry!)

MyDeveloperDay added a project: Restricted Project.May 15 2021, 5:57 AM