summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2012-12-10 16:34:48 +0000
committerAlexander Kornienko <alexfh@google.com>2012-12-10 16:34:48 +0000
commit2ca766f32a775dd2d20c88d338728bb847651bde (patch)
tree6f27885a00c188cd7782aadf4eb4c70d3f585632
parentdd1ff5ece09e0c82d04e038596320b9aad09efd7 (diff)
downloadbcm5719-llvm-2ca766f32a775dd2d20c88d338728bb847651bde.tar.gz
bcm5719-llvm-2ca766f32a775dd2d20c88d338728bb847651bde.zip
Clang-format: error recovery for access specifiers
Reviewers: klimek Reviewed By: klimek CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D198 llvm-svn: 169738
-rw-r--r--clang/lib/Format/Format.cpp5
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp4
-rw-r--r--clang/unittests/Format/FormatTest.cpp24
3 files changed, 26 insertions, 7 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 3bedc6a4cd5..7f1131ca6f5 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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;
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index a225f3b3e78..8545f336b86 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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();
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d00da7ffee1..79cefc46637 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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("{");
OpenPOWER on IntegriCloud