[cfe-commits] r171666 - in /cfe/trunk: lib/Format/UnwrappedLineParser.cpp lib/Format/UnwrappedLineParser.h unittests/Format/FormatTest.cpp

Manuel Klimek klimek at google.com
Sun Jan 6 12:07:31 PST 2013


Author: klimek
Date: Sun Jan  6 14:07:31 2013
New Revision: 171666

URL: http://llvm.org/viewvc/llvm-project?rev=171666&view=rev
Log:
Fixes handling of unbalances braces.

If we find an unexpected closing brace, we must not stop parsing, as
we'd otherwise not layout anything beyond that point.

If we find a structural error on the highest level we'll not re-indent
anyway, but we'll still want to format within unwrapped lines.

Needed to introduce a differentiation between an expected and unexpected
closing brace.

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=171666&r1=171665&r2=171666&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Sun Jan  6 14:07:31 2013
@@ -84,13 +84,13 @@
 }
 
 bool UnwrappedLineParser::parseFile() {
-  bool Error = parseLevel();
+  bool Error = parseLevel(/*HasOpeningBrace=*/false);
   // Make sure to format the remaining tokens.
   addUnwrappedLine();
   return Error;
 }
 
-bool UnwrappedLineParser::parseLevel() {
+bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
   bool Error = false;
   do {
     switch (FormatTok.Tok.getKind()) {
@@ -103,8 +103,15 @@
       addUnwrappedLine();
       break;
     case tok::r_brace:
-      // Stray '}' is an error.
-      return true;
+      if (HasOpeningBrace) {
+        return false;
+      } else {
+        // Stray '}' is an error.
+        Error = true;
+        nextToken();
+        addUnwrappedLine();
+      }
+      break;
     default:
       parseStatement();
       break;
@@ -120,7 +127,7 @@
   addUnwrappedLine();
 
   Line.Level += AddLevels;
-  parseLevel();
+  parseLevel(/*HasOpeningBrace=*/true);
   Line.Level -= AddLevels;
 
   // FIXME: Add error handling.

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=171666&r1=171665&r2=171666&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Sun Jan  6 14:07:31 2013
@@ -106,7 +106,7 @@
 
 private:
   bool parseFile();
-  bool parseLevel();
+  bool parseLevel(bool HasOpeningBrace);
   bool parseBlock(unsigned AddLevels = 1);
   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=171666&r1=171665&r2=171666&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Jan  6 14:07:31 2013
@@ -474,8 +474,12 @@
   verifyFormat("{\n  {\n    a #c;\n  }\n}");
 }
 
-// FIXME: write test for unbalanced braces in macros...
-// FIXME: test # inside a normal statement (like {#define A b})
+TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
+  EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
+            format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
+  EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
+            format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
+}
 
 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
   EXPECT_EQ(





More information about the cfe-commits mailing list