aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Format/UnwrappedLineParser.cpp17
-rw-r--r--lib/Format/UnwrappedLineParser.h2
-rw-r--r--unittests/Format/FormatTest.cpp8
3 files changed, 19 insertions, 8 deletions
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp
index 72b47503ee..00710827c2 100644
--- a/lib/Format/UnwrappedLineParser.cpp
+++ b/lib/Format/UnwrappedLineParser.cpp
@@ -84,13 +84,13 @@ bool UnwrappedLineParser::parse() {
}
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 @@ bool UnwrappedLineParser::parseLevel() {
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 @@ bool UnwrappedLineParser::parseBlock(unsigned AddLevels) {
addUnwrappedLine();
Line.Level += AddLevels;
- parseLevel();
+ parseLevel(/*HasOpeningBrace=*/true);
Line.Level -= AddLevels;
// FIXME: Add error handling.
diff --git a/lib/Format/UnwrappedLineParser.h b/lib/Format/UnwrappedLineParser.h
index 287143dae2..2308c92fa1 100644
--- a/lib/Format/UnwrappedLineParser.h
+++ b/lib/Format/UnwrappedLineParser.h
@@ -106,7 +106,7 @@ public:
private:
bool parseFile();
- bool parseLevel();
+ bool parseLevel(bool HasOpeningBrace);
bool parseBlock(unsigned AddLevels = 1);
void parsePPDirective();
void parsePPDefine();
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 8d95538b16..28f63aa055 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -474,8 +474,12 @@ TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
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(