From aabfb2712e180bb11e5dfc4f2a273890a6c5da66 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Sat, 12 Oct 2013 22:46:56 +0000 Subject: [PATCH] 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). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@192543 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineParser.cpp | 8 ++++++-- lib/Format/UnwrappedLineParser.h | 3 ++- unittests/Format/FormatTest.cpp | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index e8503bb332..949f5a308b 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -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; diff --git a/lib/Format/UnwrappedLineParser.h b/lib/Format/UnwrappedLineParser.h index 7a3993ed34..f1f4e57a20 100644 --- a/lib/Format/UnwrappedLineParser.h +++ b/lib/Format/UnwrappedLineParser.h @@ -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(); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 6e8179a306..afe9c8b1cc 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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 -- 2.40.0