]> granicus.if.org Git - clang/commitdiff
clang-format: Make short case labels work with #ifs
authorDaniel Jasper <djasper@google.com>
Sun, 23 Nov 2014 21:45:03 +0000 (21:45 +0000)
committerDaniel Jasper <djasper@google.com>
Sun, 23 Nov 2014 21:45:03 +0000 (21:45 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@222642 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 6518b246a8892edce184a1ff6a3d773560de5eb6..8a04571a8cb05f656d707edad3edbe12b2c10ef4 100644 (file)
@@ -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,
index 58680e073438e6d79a25e10b0dde9f6bdfc825e5..b7ffd958286b977ff1513e4e5a145ce41dfd1a19 100644 (file)
@@ -753,6 +753,12 @@ TEST_F(FormatTest, ShortCaseLabels) {
                "default: y = 1; break;\n"
                "}",
                Style);
+  verifyFormat("switch (a) {\n"
+               "#if FOO\n"
+               "case 0: return 0;\n"
+               "#endif\n"
+               "}",
+               Style);
   verifyFormat("switch (a) {\n"
                "case 1: {\n"
                "}\n"