]> granicus.if.org Git - clang/commitdiff
Clang-format: error recovery for access specifiers
authorAlexander Kornienko <alexfh@google.com>
Mon, 10 Dec 2012 16:34:48 +0000 (16:34 +0000)
committerAlexander Kornienko <alexfh@google.com>
Mon, 10 Dec 2012 16:34:48 +0000 (16:34 +0000)
Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D198

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169738 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 3bedc6a4cd5f7941a506c855a9572025d93b0540..7f1131ca6f532620a84027ec84659f3b07685b27 100644 (file)
@@ -371,8 +371,9 @@ private:
     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 a225f3b3e78f06eef80a983c2b8b18b59df2a885..8545f336b86f0efe825831165e789a579590450a 100644 (file)
@@ -311,7 +311,9 @@ void UnwrappedLineParser::parseSwitch() {
 
 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 d00da7ffee1114068945436cf771f4d0f5eb02df..79cefc46637bfc31575de4144d46f4344934a8fb 100644 (file)
@@ -498,10 +498,26 @@ TEST_F(FormatTest, HandlesIncludeDirectives) {
 // 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("{");