[PATCH] D47095: [clang-format/ObjC] Correctly parse Objective-C methods with 'class' in name

Ben Hamilton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 18 15:57:09 PDT 2018


benhamilton created this revision.
benhamilton added reviewers: djasper, jolesiak.
Herald added subscribers: cfe-commits, klimek.

Please take a close look at this CL. I haven't touched much of
`UnwrappedLineParser` before, so I may have gotten things wrong.

Previously, clang-format would incorrectly format the following:

  @implementation Foo
  
  - (Class)class {
  }
  
  - (void)foo {
  }
  
  @end

as:

  @implementation Foo
  
  - (Class)class {
  }
  
      - (void)foo {
  }
  
  @end

The problem is whenever `UnwrappedLineParser::parseStructuralElement()`
sees any of the keywords `class`, `struct`, or `enum`, it calls
`parseRecord()` to parse them as a C/C++ record.

This causes subsequent lines to be parsed incorrectly, which
causes them to be indented incorrectly.

In Objective-C/Objective-C++, these keywords are valid selector
components.

This diff fixes the issue by explicitly handling `+` and `-` lines
inside `@implementation` / `@interface` / `@protocol` blocks
and parsing them as Objective-C methods.

Test Plan: New tests added. Ran tests with:

  make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests


Repository:
  rC Clang

https://reviews.llvm.org/D47095

Files:
  lib/Format/UnwrappedLineParser.cpp
  lib/Format/UnwrappedLineParser.h
  unittests/Format/FormatTestObjC.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47095.147610.patch
Type: text/x-patch
Size: 3695 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180518/aaf2254e/attachment.bin>


More information about the cfe-commits mailing list