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
+
+@protocol A
+// CHECK: @protocol A
+ @optional
+// CHECK: {{^ @optional}}
+- (void)f;
+// CHECK: {{^ - \(void\)f;}}
+@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;


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
+
+@protocol A
+// CHECK: @protocol A
+ @optional
+// CHECK: {{^ @optional}}
+- (void)f;
+// CHECK: {{^ - \(void\)f;}}
+@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;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to