r192543 - Automatically munch semicolons after blocks.
Manuel Klimek
klimek at google.com
Sat Oct 12 15:46:56 PDT 2013
Author: klimek
Date: Sat Oct 12 17:46:56 2013
New Revision: 192543
URL: http://llvm.org/viewvc/llvm-project?rev=192543&view=rev
Log:
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).
Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.h
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=192543&r1=192542&r2=192543&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Sat Oct 12 17:46:56 2013
@@ -360,7 +360,8 @@ void UnwrappedLineParser::calculateBrace
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(boo
}
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;
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=192543&r1=192542&r2=192543&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Sat Oct 12 17:46:56 2013
@@ -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();
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=192543&r1=192542&r2=192543&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sat Oct 12 17:46:56 2013
@@ -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
More information about the cfe-commits
mailing list