diff options
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 19 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 16 |
2 files changed, 29 insertions, 6 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 77f98bffda2..939a301e9d7 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -213,6 +213,7 @@ void UnwrappedLineParser::parseFile() { } void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { + bool SwitchLabelEncountered = false; do { switch (FormatTok->Tok.getKind()) { case tok::comment: @@ -232,6 +233,13 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { nextToken(); addUnwrappedLine(); break; + case tok::kw_default: + case tok::kw_case: + if (!SwitchLabelEncountered) + Line->Level += Style.IndentCaseLabels; + SwitchLabelEncountered = true; + parseStructuralElement(); + break; default: parseStructuralElement(); break; @@ -314,6 +322,7 @@ void UnwrappedLineParser::calculateBraceTypes() { void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels) { assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); + unsigned InitialLevel = Line->Level; nextToken(); addUnwrappedLine(); @@ -324,13 +333,13 @@ void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, 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() { @@ -865,13 +874,13 @@ void UnwrappedLineParser::parseSwitch() { 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; } } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 9f41491924e..f4ebc7159aa 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -544,7 +544,21 @@ TEST_F(FormatTest, FormatsSwitchStatement) { " }\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) { |