]> granicus.if.org Git - clang/commitdiff
Automatically munch semicolons after blocks.
authorManuel Klimek <klimek@google.com>
Sat, 12 Oct 2013 22:46:56 +0000 (22:46 +0000)
committerManuel Klimek <klimek@google.com>
Sat, 12 Oct 2013 22:46:56 +0000 (22:46 +0000)
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
lib/Format/UnwrappedLineParser.h
unittests/Format/FormatTest.cpp

index e8503bb332220e0c6a1b1c655aabf1cbc73c5740..949f5a308b2c593a3da997a3f22d3b9afcdf81b9 100644 (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;
index 7a3993ed34d1b07e95d772f9b964c46f1955fbe8..f1f4e57a20b30cfb3f00a829877b44e695859fa9 100644 (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();
index 6e8179a3068ffa3ee12b0239c1f6439ef9dea8ae..afe9c8b1cc2a9557887134f21968c18fe8ff787e 100644 (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