r176245 - Improve formatting of #defines.
Daniel Jasper
djasper at google.com
Thu Feb 28 03:05:57 PST 2013
Author: djasper
Date: Thu Feb 28 05:05:57 2013
New Revision: 176245
URL: http://llvm.org/viewvc/llvm-project?rev=176245&view=rev
Log:
Improve formatting of #defines.
Two improvements:
1) Always leave at least one space before "\". Otherwise is can look bad
and there is a risk of unwillingly joining to characters to a different
token.
2) Use the full column limit for single-line #defines.
Fixes llvm.org/PR15148
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=176245&r1=176244&r2=176245&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Feb 28 05:05:57 2013
@@ -273,7 +273,7 @@ public:
///
/// \returns The column after the last token in the last line of the
/// \c UnwrappedLine.
- unsigned format() {
+ unsigned format(const AnnotatedLine *NextLine) {
// Initialize state dependent on indent.
LineState State;
State.Column = FirstIndent;
@@ -295,7 +295,11 @@ public:
moveStateToNextToken(State, /*DryRun=*/ false);
// If everything fits on a single line, just put it there.
- if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) {
+ unsigned ColumnLimit = Style.ColumnLimit;
+ if (NextLine && NextLine->InPPDirective &&
+ !NextLine->First.FormatTok.HasUnescapedNewline)
+ ColumnLimit = getColumnLimit();
+ if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
while (State.NextToken != NULL) {
addTokenToState(false, false, State);
}
@@ -749,7 +753,7 @@ private:
}
unsigned getColumnLimit() {
- return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0);
+ return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
}
/// \brief An edge in the solution space from \c Previous->State to \c State,
@@ -1112,7 +1116,8 @@ public:
UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
TheLine.First, Whitespaces,
StructuralError);
- PreviousEndOfLineColumn = Formatter.format();
+ PreviousEndOfLineColumn =
+ Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
IndentForLevel[TheLine.Level] = LevelIndent;
PreviousLineWasTouched = true;
} else {
@@ -1192,7 +1197,7 @@ private:
if (I->Last->Type == TT_LineComment)
return;
- unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
+ unsigned Limit = Style.ColumnLimit - Indent;
// If we already exceed the column limit, we set 'Limit' to 0. The different
// tryMerge..() functions can then decide whether to still do merging.
Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176245&r1=176244&r2=176245&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Feb 28 05:05:57 2013
@@ -857,29 +857,24 @@ TEST_F(FormatTest, EndOfFileEndsPPDirect
}
TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
- // If the macro fits in one line, we still do not get the full
- // line, as only the next line decides whether we need an escaped newline and
- // thus use the last column.
- verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
-
- verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
- verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
+ verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
+ verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
+ // FIXME: We never break before the macro name.
+ verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
verifyFormat("#define A A\n#define A A");
verifyFormat("#define A(X) A\n#define A A");
- verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
- verifyFormat("#define Something \\\n"
- " Other",
- getLLVMStyleWithColumns(23));
+ verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
+ verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
}
TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
EXPECT_EQ("// some comment\n"
"#include \"a.h\"\n"
- "#define A(A,\\\n"
- " B)\n"
+ "#define A( \\\n"
+ " A, B)\n"
"#include \"b.h\"\n"
"// some comment\n",
format(" // some comment\n"
More information about the cfe-commits
mailing list