From 79f226e780449291f5905fac05c0ae864068a28d Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Sun, 23 Nov 2014 21:45:03 +0000 Subject: [PATCH] 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. llvm-svn: 222642 --- clang/lib/Format/Format.cpp | 3 +++ clang/unittests/Format/FormatTest.cpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 6518b246a889..8a04571a8cb0 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 58680e073438..b7ffd958286b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/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" -- 2.34.1