r222642 - clang-format: Make short case labels work with #ifs

Daniel Jasper djasper at google.com
Sun Nov 23 13:45:03 PST 2014


Author: djasper
Date: Sun Nov 23 15:45:03 2014
New Revision: 222642

URL: http://llvm.org/viewvc/llvm-project?rev=222642&view=rev
Log:
clang-format: Make short case labels work with #ifs

Before:
  switch (a) {
  #if FOO
  case 0: return 0; #endif
  }

After:
  switch (a) {
  #if FOO
  case 0: return 0;
  #endif
  }

This fixed llvm.org/PR21544.

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=222642&r1=222641&r2=222642&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Sun Nov 23 15:45:03 2014
@@ -742,10 +742,13 @@ private:
       return 0;
     unsigned NumStmts = 0;
     unsigned Length = 0;
+    bool InPPDirective = I[0]->InPPDirective;
     for (; NumStmts < 3; ++NumStmts) {
       if (I + 1 + NumStmts == E)
         break;
       const AnnotatedLine *Line = I[1 + NumStmts];
+      if (Line->InPPDirective != InPPDirective)
+        break;
       if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
         break;
       if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=222642&r1=222641&r2=222642&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Nov 23 15:45:03 2014
@@ -754,6 +754,12 @@ TEST_F(FormatTest, ShortCaseLabels) {
                "}",
                Style);
   verifyFormat("switch (a) {\n"
+               "#if FOO\n"
+               "case 0: return 0;\n"
+               "#endif\n"
+               "}",
+               Style);
+  verifyFormat("switch (a) {\n"
                "case 1: {\n"
                "}\n"
                "case 2: {\n"





More information about the cfe-commits mailing list