r176435 - Format a line if a range in its leading whitespace was selected.
Daniel Jasper
djasper at google.com
Mon Mar 4 05:43:19 PST 2013
Author: djasper
Date: Mon Mar 4 07:43:19 2013
New Revision: 176435
URL: http://llvm.org/viewvc/llvm-project?rev=176435&view=rev
Log:
Format a line if a range in its leading whitespace was selected.
With [] marking the selected range, clang-format invoked on
[ ] int a;
Would so far not reformat anything. With this patch, it formats a
line if its leading whitespace is touched.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.h
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=176435&r1=176434&r2=176435&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Mar 4 07:43:19 2013
@@ -995,6 +995,9 @@ public:
// Consume and record whitespace until we find a significant token.
while (FormatTok.Tok.is(tok::unknown)) {
unsigned Newlines = Text.count('\n');
+ if (Newlines > 0)
+ FormatTok.LastNewlineOffset =
+ FormatTok.WhiteSpaceLength + Text.rfind('\n') + 1;
unsigned EscapedNewlines = Text.count("\\\n");
FormatTok.NewlinesBefore += Newlines;
FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
@@ -1371,7 +1374,8 @@ private:
const FormatToken *First = &TheLine.First.FormatTok;
const FormatToken *Last = &TheLine.Last->FormatTok;
CharSourceRange LineRange = CharSourceRange::getTokenRange(
- First->Tok.getLocation(), Last->Tok.getLocation());
+ First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset),
+ Last->Tok.getLocation());
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
Ranges[i].getBegin()) &&
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=176435&r1=176434&r2=176435&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Mon Mar 4 07:43:19 2013
@@ -33,8 +33,8 @@ namespace format {
struct FormatToken {
FormatToken()
: NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
- TokenLength(0), IsFirst(false), MustBreakBefore(false) {
- }
+ LastNewlineOffset(0), TokenLength(0), IsFirst(false),
+ MustBreakBefore(false) {}
/// \brief The \c Token.
Token Tok;
@@ -59,6 +59,10 @@ struct FormatToken {
/// the \c Token.
unsigned WhiteSpaceLength;
+ /// \brief The offset just past the last '\n' in this token's leading
+ /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
+ unsigned LastNewlineOffset;
+
/// \brief The length of the non-whitespace parts of the token. This is
/// necessary because we need to handle escaped newlines that are stored
/// with the token.
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176435&r1=176434&r2=176435&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Mar 4 07:43:19 2013
@@ -172,6 +172,28 @@ TEST_F(FormatTest, RemovesTrailingWhites
format("int a; \nint b; ", 0, 0, getLLVMStyle()));
}
+TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
+ EXPECT_EQ("int b;\nint a;",
+ format("int b;\n int a;", 7, 0, getLLVMStyle()));
+ EXPECT_EQ("int b;\n int a;",
+ format("int b;\n int a;", 6, 0, getLLVMStyle()));
+
+ EXPECT_EQ("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ format("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ 26, 0, getLLVMStyleWithColumns(12)));
+ EXPECT_EQ("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ format("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ 25, 0, getLLVMStyleWithColumns(12)));
+}
+
TEST_F(FormatTest, ReformatsMovedLines) {
EXPECT_EQ(
"template <typename T> T *getFETokenInfo() const {\n"
More information about the cfe-commits
mailing list