Skip to content

Commit

Permalink
[clang-format] fix PR38557 - comments between "default" and ':' cause…
Browse files Browse the repository at this point in the history
…s the case label to be treated as an identifier

Summary:
The Bug was reported and fixed by Owen Pan. See the original bug report here: https://bugs.llvm.org/show_bug.cgi?id=38557

Patch by Owen Pan!

Reviewers: krasimir, djasper, klimek

Reviewed By: klimek

Subscribers: JonasToth, cfe-commits

Differential Revision: https://reviews.llvm.org/D50697

llvm-svn: 340624
  • Loading branch information
JonasToth committed Aug 24, 2018
1 parent 4636deb commit 90d2aa2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Expand Up @@ -350,7 +350,10 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
break;
case tok::kw_default: {
unsigned StoredPosition = Tokens->getPosition();
FormatToken *Next = Tokens->getNextToken();
FormatToken *Next;
do {
Next = Tokens->getNextToken();
} while (Next && Next->is(tok::comment));
FormatTok = Tokens->setPosition(StoredPosition);
if (Next && Next->isNot(tok::colon)) {
// default not followed by ':' is not a case label; treat it like
Expand Down
16 changes: 16 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -1145,6 +1145,22 @@ TEST_F(FormatTest, ShortCaseLabels) {
" break;\n"
"}",
Style);
Style.ColumnLimit = 80;
Style.AllowShortCaseLabelsOnASingleLine = false;
Style.IndentCaseLabels = true;
EXPECT_EQ("switch (n) {\n"
" default /*comments*/:\n"
" return true;\n"
" case 0:\n"
" return false;\n"
"}",
format("switch (n) {\n"
"default/*comments*/:\n"
" return true;\n"
"case 0:\n"
" return false;\n"
"}",
Style));
}

TEST_F(FormatTest, FormatsLabels) {
Expand Down

0 comments on commit 90d2aa2

Please sign in to comment.