}
bool parseBrace() {
- // Lines are fine to end with '{'.
- if (CurrentToken == NULL)
- return true;
- ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
- AnnotatedToken *Left = CurrentToken->Parent;
- while (CurrentToken != NULL) {
- if (CurrentToken->is(tok::r_brace)) {
- Left->MatchingParen = CurrentToken;
- CurrentToken->MatchingParen = Left;
- next();
- return true;
+ if (CurrentToken != NULL) {
+ ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
+ AnnotatedToken *Left = CurrentToken->Parent;
+ while (CurrentToken != NULL) {
+ if (CurrentToken->is(tok::r_brace)) {
+ Left->MatchingParen = CurrentToken;
+ CurrentToken->MatchingParen = Left;
+ next();
+ return true;
+ }
+ if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
+ return false;
+ updateParameterCount(Left, CurrentToken);
+ if (!consumeToken())
+ return false;
}
- if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
- return false;
- updateParameterCount(Left, CurrentToken);
- if (!consumeToken())
- return false;
}
+ // No closing "}" found, this probably starts a definition.
+ Line.StartsDefinition = true;
return true;
}
AnnotatedLine(const UnwrappedLine &Line)
: First(Line.Tokens.front()), Level(Line.Level),
InPPDirective(Line.InPPDirective),
- MustBeDeclaration(Line.MustBeDeclaration),
- MightBeFunctionDecl(false) {
+ MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
+ StartsDefinition(false) {
assert(!Line.Tokens.empty());
AnnotatedToken *Current = &First;
for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
: First(Other.First), Type(Other.Type), Level(Other.Level),
InPPDirective(Other.InPPDirective),
MustBeDeclaration(Other.MustBeDeclaration),
- MightBeFunctionDecl(Other.MightBeFunctionDecl) {
+ MightBeFunctionDecl(Other.MightBeFunctionDecl),
+ StartsDefinition(Other.StartsDefinition) {
Last = &First;
while (!Last->Children.empty()) {
Last->Children[0].Parent = Last;
bool InPPDirective;
bool MustBeDeclaration;
bool MightBeFunctionDecl;
+ bool StartsDefinition;
};
inline prec::Level getPrecedence(const AnnotatedToken &Tok) {
TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
EXPECT_EQ("int\n"
"#define A\n"
- "a;",
+ " a;",
format("int\n#define A\na;"));
verifyFormat("functionCallTo(\n"
" someOtherFunction(\n"
"CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
" CFTimeInterval interval, CFOptionFlags flags,\n"
" CFIndex order, CFRunLoopTimerCallBack callout,\n"
- " CFRunLoopTimerContext *context);");
+ " CFRunLoopTimerContext *context) {}");
// Deep nesting somewhat works around our memoization.
verifyFormat(
" Cccccccccccccc cccccccccccccc);");
// 2) break after return type.
- verifyFormat("Aaaaaaaaaaaaaaaaaaaaaaaa\n"
- "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
+ verifyFormat(
+ "Aaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
// 3) break after (.
verifyFormat(
// 4) break before after nested name specifiers.
verifyFormat(
"Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
+ " SomeClasssssssssssssssssssssssssssssssssssssss::\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
// However, there are exceptions, if a sufficient amount of lines can be
// saved.
" Cccccccccccccc cccccccccc);");
verifyFormat(
"Aaaaaaaaaaaaaaaaaa\n"
- "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
- " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
- " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
+ " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
verifyFormat(
"Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
" Cccccccccccccc cccccccccc,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaa));");
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" __attribute__((unused));");
-
- // FIXME: This is bad indentation, but generally hard to distinguish from a
- // function declaration.
verifyFormat(
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- "GUARDED_BY(aaaaaaaaaaaa);");
+ " GUARDED_BY(aaaaaaaaaaaa);");
}
TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
// FIXME: If multiple variables are defined, the "*" needs to move to the new
// line. Also fix indent for breaking after the type, this looks bad.
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
- " *b = bbbbbbbbbbbbbbbbbbb;");
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
+ " *b = bbbbbbbbbbbbbbbbbbb;");
// Not ideal, but pointer-with-type does not allow much here.
verifyGoogleFormat(
}
TEST_F(FormatTest, BreaksLongDeclarations) {
+ verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
+ " AnotherNameForTheLongType;");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
+ " LoooooooooooooooooooooooooooooooooooooooongVariable;");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
+ " LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
+ "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
+
+ // FIXME: Without the comment, this breaks after "(".
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
+ " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
+
verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
" int LoooooooooooooooooooongParam2) {}");
verifyFormat(
" AnotherLongParameterName) {}");
verifyFormat(
"aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
- "aaaaaaaaaaaaaaaaaaaaaaa;");
+ " aaaaaaaaaaaaaaaaaaaaaaa;");
verifyGoogleFormat(
"TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"