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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171666
91177308-0d34-0410-b5e6-
96231b3b80d8
}
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()) {
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;
addUnwrappedLine();
Line.Level += AddLevels;
- parseLevel();
+ parseLevel(/*HasOpeningBrace=*/true);
Line.Level -= AddLevels;
// FIXME: Add error handling.
private:
bool parseFile();
- bool parseLevel();
+ bool parseLevel(bool HasOpeningBrace);
bool parseBlock(unsigned AddLevels = 1);
void parsePPDirective();
void parsePPDefine();
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(