[PATCH] Don't merge constructors with initializer lists to one line in WebKit style.
Alexander Kornienko
alexfh at google.com
Mon Dec 23 09:37:15 PST 2013
Change MustBreakBefore outside of tryFitMultipleLinesInOne.
Hi djasper,
http://llvm-reviews.chandlerc.com/D2455
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D2455?vs=6223&id=6241#toc
Files:
lib/Format/Format.cpp
unittests/Format/FormatTest.cpp
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -475,10 +475,12 @@
LineJoiner(const FormatStyle &Style) : Style(Style) {}
/// \brief Calculates how many lines can be merged into 1 starting at \p I.
- unsigned
- tryFitMultipleLinesInOne(unsigned Indent,
- SmallVectorImpl<AnnotatedLine *>::const_iterator I,
- SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
+ /// If the lines should not be merged, but skipped instead, sets
+ /// \p SkipLines to \c true.
+ unsigned tryFitMultipleLinesInOne(
+ unsigned Indent, SmallVectorImpl<AnnotatedLine *>::const_iterator I,
+ SmallVectorImpl<AnnotatedLine *>::const_iterator E, bool &SkipLines) {
+ SkipLines = false;
// We can never merge stuff if there are trailing line comments.
AnnotatedLine *TheLine = *I;
if (TheLine->Last->Type == TT_LineComment)
@@ -510,17 +512,26 @@
}
if (I[1]->First->Type == TT_FunctionLBrace &&
Style.BreakBeforeBraces != FormatStyle::BS_Attach) {
+ if (!Style.AllowShortFunctionsOnASingleLine)
+ return 0;
+ // The only well-known style using this option is WebKit, and it also
+ // doesn't allow merging constructors to a single line, if there's an
+ // initializer list.
+ if (Style.BreakConstructorInitializersBeforeComma) {
+ for (FormatToken *Tok = TheLine->First; Tok != NULL; Tok = Tok->Next)
+ if (Tok->Type == TT_CtorInitializerColon) {
+ SkipLines = true;
+ return 2;
+ }
+ }
// Reduce the column limit by the number of spaces we need to insert
// around braces.
Limit = Limit > 3 ? Limit - 3 : 0;
- unsigned MergedLines = 0;
- if (Style.AllowShortFunctionsOnASingleLine) {
- MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
- // If we managed to merge the block, count the function header, which is
- // on a separate line.
- if (MergedLines > 0)
- ++MergedLines;
- }
+ unsigned MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
+ // If we managed to merge the block, count the function header, which is
+ // on a separate line.
+ if (MergedLines > 0)
+ ++MergedLines;
return MergedLines;
}
if (TheLine->First->is(tok::kw_if)) {
@@ -682,7 +693,14 @@
Indent += Offset;
// Merge multiple lines if possible.
- unsigned MergedLines = Joiner.tryFitMultipleLinesInOne(Indent, I, E);
+ bool SkipLines = false;
+ unsigned MergedLines =
+ Joiner.tryFitMultipleLinesInOne(Indent, I, E, SkipLines);
+ if (SkipLines) {
+ for (unsigned i = 0; i < MergedLines && I + i + 1 != E; ++i)
+ I[i + 1]->First->MustBreakBefore = true;
+ MergedLines = 0;
+ }
if (MergedLines > 0 && Style.ColumnLimit == 0) {
// Disallow line merging if there is a break at the start of one of the
// input lines.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2455.2.patch
Type: text/x-patch
Size: 3082 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131223/1e314dab/attachment.bin>
More information about the cfe-commits
mailing list