r187118 - clang-format: Fix switch/case interaction with macros.
Daniel Jasper
djasper at google.com
Thu Jul 25 04:31:57 PDT 2013
Author: djasper
Date: Thu Jul 25 06:31:57 2013
New Revision: 187118
URL: http://llvm.org/viewvc/llvm-project?rev=187118&view=rev
Log:
clang-format: Fix switch/case interaction with macros.
Before:
#define OPERATION_CASE(name) \
case OP_name: \
return operations::Operation##name
switch (OpCode) {
CASE(Add);
CASE(Subtract);
default:
return operations::Unknown;
}
After:
#define OPERATION_CASE(name) \
case OP_name: \
return operations::Operation##name;
switch (OpCode) {
CASE(Add);
CASE(Subtract);
default:
return operations::Unknown;
}
Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=187118&r1=187117&r2=187118&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Thu Jul 25 06:31:57 2013
@@ -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(boo
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::calculateBrace
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(boo
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;
}
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=187118&r1=187117&r2=187118&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jul 25 06:31:57 2013
@@ -544,7 +544,21 @@ TEST_F(FormatTest, FormatsSwitchStatemen
" }\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) {
More information about the cfe-commits
mailing list