From: Daniel Jasper Date: Sun, 23 Nov 2014 21:45:03 +0000 (+0000) Subject: clang-format: Make short case labels work with #ifs X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fb768e1bdab31e04f255861b93bdf641b98cbaed;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@222642 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 6518b246a8..8a04571a8c 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -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, diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 58680e0734..b7ffd95828 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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"