aeubanks created this revision.
Herald added a project: All.
aeubanks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

With D117142 <https://reviews.llvm.org/D117142>, we would now format

  struct A {
  #define A
    void f() { a(); }
  #endif
  };

into

  struct A {
  #ifdef A
    void f() {
      a();
    }
  #endif
  };

because we were looking for the record lbrace without skipping preprocess lines.

Fixes https://github.com/llvm/llvm-project/issues/54901.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123737

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -12766,6 +12766,13 @@
                "};",
                MergeInlineOnly);
 
+  verifyFormat("class C {\n"
+               "#ifdef A\n"
+               "  int f() { return 42; }\n"
+               "#endif\n"
+               "};",
+               MergeInlineOnly);
+
   // Also verify behavior when BraceWrapping.AfterFunction = true
   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
   MergeInlineOnly.BraceWrapping.AfterFunction = true;
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -308,7 +308,7 @@
           // Find the last line with lower level.
           auto J = I - 1;
           for (; J != AnnotatedLines.begin(); --J)
-            if ((*J)->Level < TheLine->Level)
+            if (!(*J)->InPPDirective && (*J)->Level < TheLine->Level)
               break;
           if ((*J)->Level >= TheLine->Level)
             return false;


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -12766,6 +12766,13 @@
                "};",
                MergeInlineOnly);
 
+  verifyFormat("class C {\n"
+               "#ifdef A\n"
+               "  int f() { return 42; }\n"
+               "#endif\n"
+               "};",
+               MergeInlineOnly);
+
   // Also verify behavior when BraceWrapping.AfterFunction = true
   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
   MergeInlineOnly.BraceWrapping.AfterFunction = true;
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -308,7 +308,7 @@
           // Find the last line with lower level.
           auto J = I - 1;
           for (; J != AnnotatedLines.begin(); --J)
-            if ((*J)->Level < TheLine->Level)
+            if (!(*J)->InPPDirective && (*J)->Level < TheLine->Level)
               break;
           if ((*J)->Level >= TheLine->Level)
             return false;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to