diff options
author | Manuel Klimek <klimek@google.com> | 2013-01-02 16:30:12 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2013-01-02 16:30:12 +0000 |
commit | a080a187fa7e538da3212c7d5e678e4b7ae03253 (patch) | |
tree | 5a2c54db0a6a2ec4e1764cea7c7080d6898b8d84 /lib/Format/Format.cpp | |
parent | ef5b9c3d38a1f6d0921591cb2749e529a8cc4a2e (diff) |
Fixes use of unescaped newlines when formatting preprocessor directives.
This is the first step towards handling preprocessor directives. This
patch only fixes the most pressing issue, namely correctly escaping
newlines for tokens within a sequence of a preprocessor directive.
The next step will be to fix incorrect format decisions on #define
directives.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171393 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r-- | lib/Format/Format.cpp | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 60b2f56745..8ea95c4862 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -434,9 +434,21 @@ private: /// each \c FormatToken. void replaceWhitespace(const FormatToken &Tok, unsigned NewLines, unsigned Spaces) { + std::string NewLineText; + if (!Line.InPPDirective) { + NewLineText = std::string(NewLines, '\n'); + } else if (NewLines > 0) { + unsigned Offset = + SourceMgr.getSpellingColumnNumber(Tok.WhiteSpaceStart) - 1; + for (unsigned i = 0; i < NewLines; ++i) { + NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' '); + NewLineText += "\\\n"; + Offset = 0; + } + } Replaces.insert(tooling::Replacement( SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength, - std::string(NewLines, '\n') + std::string(Spaces, ' '))); + NewLineText + std::string(Spaces, ' '))); } /// \brief Add a new line and the required indent before the first Token @@ -634,6 +646,9 @@ public: next(); if (Index >= Tokens.size()) return; + // It is the responsibility of the UnwrappedLineParser to make sure + // this sequence is not produced inside an unwrapped line. + assert(Tokens[Index].Tok.getIdentifierInfo() != NULL); switch (Tokens[Index].Tok.getIdentifierInfo()->getPPKeywordID()) { case tok::pp_include: case tok::pp_import: @@ -969,7 +984,10 @@ public: // Consume and record whitespace until we find a significant token. while (FormatTok.Tok.is(tok::unknown)) { - FormatTok.NewlinesBefore += tokenText(FormatTok.Tok).count('\n'); + StringRef Text = tokenText(FormatTok.Tok); + FormatTok.NewlinesBefore += Text.count('\n'); + FormatTok.HasUnescapedNewline = + Text.count("\\\n") != FormatTok.NewlinesBefore; FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); if (FormatTok.Tok.is(tok::eof)) |