[cfe-commits] [PATCH] Clang-format: error recovery for access specifiers

Alexander Kornienko alexfh at google.com
Mon Dec 10 08:29:41 PST 2012


Hi klimek,

http://llvm-reviews.chandlerc.com/D198

Files:
  lib/Format/Format.cpp
  lib/Format/UnwrappedLineParser.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -371,8 +371,9 @@
     if (Newlines == 0 && Offset != 0)
       Newlines = 1;
     unsigned Indent = Line.Level * 2;
-    if (Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) ||
-        Token.Tok.is(tok::kw_private))
+    if ((Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) ||
+         Token.Tok.is(tok::kw_private)) &&
+        static_cast<int>(Indent) + Style.AccessModifierOffset >= 0)
       Indent += Style.AccessModifierOffset;
     replaceWhitespace(Token, Newlines, Indent);
     return Indent;
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -311,7 +311,9 @@
 
 void UnwrappedLineParser::parseAccessSpecifier() {
   nextToken();
-  nextToken();
+  // Otherwise, we don't know what it is, and we'd better keep the next token.
+  if (FormatTok.Tok.is(tok::colon))
+    nextToken();
   addUnwrappedLine();
 }
 
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -498,10 +498,26 @@
 // Error recovery tests.
 //===----------------------------------------------------------------------===//
 
-//TEST_F(FormatTest, IncorrectDerivedClass) {
-//  verifyFormat("public B {\n"
-//               "};");
-//}
+TEST_F(FormatTest, IncorrectAccessSpecifier) {
+  verifyFormat("public:");
+  verifyFormat("class A {\n"
+               "public\n"
+               "  void f() {\n"
+               "  }\n"
+               "};");
+  verifyFormat("public\n"
+               "int qwerty;");
+  verifyFormat("public\n"
+               "B {\n"
+               "};");
+  verifyFormat("public\n"
+               "{\n"
+               "};");
+  verifyFormat("public\n"
+               "B {\n"
+               "  int x;\n"
+               "};");
+}
 
 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D198.1.patch
Type: text/x-patch
Size: 2167 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121210/5fd93c21/attachment.bin>


More information about the cfe-commits mailing list