[PATCH] D50535: Fix selective formatting of ObjC scope

Arnaud Coomans via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 9 14:21:29 PDT 2018


acoomans created this revision.
acoomans added a reviewer: cfe-commits.

ObjC scopes gets formatted beyond the `@end` directive, when formatting specific lines (`clang-format -lines=x:x`).

This changeset fixes formatting of `@end` the same way that `r_braces` are.


Repository:
  rC Clang

https://reviews.llvm.org/D50535

Files:
  lib/Format/UnwrappedLineFormatter.cpp
  test/Format/adjust-indent-objc.m
  unittests/Format/FormatTestSelective.cpp


Index: unittests/Format/FormatTestSelective.cpp
===================================================================
--- unittests/Format/FormatTestSelective.cpp
+++ unittests/Format/FormatTestSelective.cpp
@@ -584,6 +584,20 @@
              15, 0));
 }
 
+TEST_F(FormatTestSelective, StopFormattingWhenLeavingObjCScope) {
+    EXPECT_EQ("@protocol A\n"
+              " - (void)f;\n"
+              " - (void)g;\n"
+              "@end\n"
+              "MACRO",
+              format("@protocol A\n"
+                     " - (void)f;\n"
+                     "- (void)g;\n"  // Format here.
+                     "@end\n"
+                     "MACRO",
+                     25, 0));
+}
+
 TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
   Style = getGoogleStyle(FormatStyle::LK_JavaScript);
   EXPECT_EQ(
Index: test/Format/adjust-indent-objc.m
===================================================================
--- /dev/null
+++ test/Format/adjust-indent-objc.m
@@ -0,0 +1,13 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s | clang-format -lines=4:4 \
+// RUN:   | FileCheck -strict-whitespace %s
+
+ at protocol A
+// CHECK: @protocol A
+ @optional
+// CHECK: {{^ @optional}}
+- (void)f;
+// CHECK: {{^ - \(void\)f;}}
+ at end
+// CHECK: {{^@end}}
+MACRO
+// CHECK: {{^MACRO}}
\ No newline at end of file
Index: lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- lib/Format/UnwrappedLineFormatter.cpp
+++ lib/Format/UnwrappedLineFormatter.cpp
@@ -1042,6 +1042,10 @@
         (TheLine.Level == RangeMinLevel && !PreviousRBrace &&
          !TheLine.startsWith(tok::r_brace));
 
+    if (TheLine.First->Next && TheLine.First->Next->Tok.isObjCAtKeyword(tok::objc_end)) {
+      ContinueFormatting = false;
+    }
+
     bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
                           Indent != TheLine.First->OriginalColumn;
     bool ShouldFormat = TheLine.Affected || FixIndentation;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50535.160006.patch
Type: text/x-patch
Size: 1976 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180809/0c12bfcf/attachment.bin>


More information about the cfe-commits mailing list