Automatically munch semicolons after blocks.

While it is mostly a user error to have the extra semicolon,
formatting it graciously will correctly format in the cases
where we do not fully understand the code (macros).

llvm-svn: 192543
This commit is contained in:
Manuel Klimek 2013-10-12 22:46:56 +00:00
parent 0b48c738e6
commit b212f3baa1
3 changed files with 15 additions and 3 deletions

View File

@ -360,7 +360,8 @@ void UnwrappedLineParser::calculateBraceTypes() {
FormatTok = Tokens->setPosition(StoredPosition);
}
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
bool MunchSemi) {
assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
unsigned InitialLevel = Line->Level;
nextToken();
@ -380,6 +381,8 @@ void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
}
nextToken(); // Munch the closing brace.
if (MunchSemi && FormatTok->Tok.is(tok::semi))
nextToken();
Line->Level = InitialLevel;
}
@ -1160,7 +1163,8 @@ void UnwrappedLineParser::parseRecord() {
Style.BreakBeforeBraces == FormatStyle::BS_Allman)
addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/true);
parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true,
/*MunchSemi=*/false);
}
// We fall through to parsing a structural element afterwards, so
// class A {} n, m;

View File

@ -69,7 +69,8 @@ private:
void reset();
void parseFile();
void parseLevel(bool HasOpeningBrace);
void parseBlock(bool MustBeDeclaration, bool AddLevel = true);
void parseBlock(bool MustBeDeclaration, bool AddLevel = true,
bool MunchSemi = true);
void parseChildBlock();
void parsePPDirective();
void parsePPDefine();

View File

@ -6854,5 +6854,12 @@ TEST_F(FormatTest, SupportsCRLF) {
getGoogleStyle()));
}
TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
verifyFormat("MY_CLASS(C) {\n"
" int i;\n"
" int j;\n"
"};");
}
} // end namespace tooling
} // end namespace clang