}
void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
+ bool SwitchLabelEncountered = false;
do {
switch (FormatTok->Tok.getKind()) {
case tok::comment:
nextToken();
addUnwrappedLine();
break;
+ case tok::kw_default:
+ case tok::kw_case:
+ if (!SwitchLabelEncountered)
+ Line->Level += Style.IndentCaseLabels;
+ SwitchLabelEncountered = true;
+ parseStructuralElement();
+ break;
default:
parseStructuralElement();
break;
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
unsigned AddLevels) {
assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
+ unsigned InitialLevel = Line->Level;
nextToken();
addUnwrappedLine();
parseLevel(/*HasOpeningBrace=*/true);
if (!FormatTok->Tok.is(tok::r_brace)) {
- Line->Level -= AddLevels;
+ Line->Level = InitialLevel;
StructuralError = true;
return;
}
nextToken(); // Munch the closing brace.
- Line->Level -= AddLevels;
+ Line->Level = InitialLevel;
}
void UnwrappedLineParser::parsePPDirective() {
if (FormatTok->Tok.is(tok::l_paren))
parseParens();
if (FormatTok->Tok.is(tok::l_brace)) {
- parseBlock(/*MustBeDeclaration=*/false, Style.IndentCaseLabels ? 2 : 1);
+ parseBlock(/*MustBeDeclaration=*/false, 1);
addUnwrappedLine();
} else {
addUnwrappedLine();
- Line->Level += (Style.IndentCaseLabels ? 2 : 1);
+ ++Line->Level;
parseStructuralElement();
- Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
+ --Line->Level;
}
}
" }\n"
"}");
verifyGoogleFormat("switch (test)\n"
- " ;");
+ " ;");
+
+ verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
+ " case OP_name: \\\n"
+ " return operations::Operation##name\n");
+ verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
+ " // Get the correction operation class.\n"
+ " switch (OpCode) {\n"
+ " CASE(Add);\n"
+ " CASE(Subtract);\n"
+ " default:\n"
+ " return operations::Unknown;\n"
+ " }\n"
+ "#undef OPERATION_CASE\n"
+ "}");
}
TEST_F(FormatTest, FormatsLabels) {