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
This commit is contained in:
Daniel Jasper 2014-11-23 21:45:03 +00:00
parent 325e486f9b
commit 79f226e780
2 changed files with 9 additions and 0 deletions

View 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,

View 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"