diff options
Diffstat (limited to 'clang/lib/Format/WhitespaceManager.cpp')
| -rw-r--r-- | clang/lib/Format/WhitespaceManager.cpp | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 50ff8582d39..802145e72d8 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -28,41 +28,44 @@ WhitespaceManager::Change::IsBeforeInFile::operator()(const Change &C1, WhitespaceManager::Change::Change( bool CreateReplacement, const SourceRange &OriginalWhitespaceRange, - unsigned Spaces, unsigned StartOfTokenColumn, unsigned NewlinesBefore, - StringRef PreviousLinePostfix, StringRef CurrentLinePrefix, - tok::TokenKind Kind, bool ContinuesPPDirective) + unsigned IndentLevel, unsigned Spaces, unsigned StartOfTokenColumn, + unsigned NewlinesBefore, StringRef PreviousLinePostfix, + StringRef CurrentLinePrefix, tok::TokenKind Kind, bool ContinuesPPDirective) : CreateReplacement(CreateReplacement), OriginalWhitespaceRange(OriginalWhitespaceRange), StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore), PreviousLinePostfix(PreviousLinePostfix), CurrentLinePrefix(CurrentLinePrefix), Kind(Kind), - ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces) {} + ContinuesPPDirective(ContinuesPPDirective), IndentLevel(IndentLevel), + Spaces(Spaces) {} void WhitespaceManager::replaceWhitespace(const FormatToken &Tok, - unsigned Newlines, unsigned Spaces, + unsigned Newlines, + unsigned IndentLevel, unsigned Spaces, unsigned StartOfTokenColumn, bool InPPDirective) { - Changes.push_back(Change(true, Tok.WhitespaceRange, Spaces, + Changes.push_back(Change(true, Tok.WhitespaceRange, IndentLevel, Spaces, StartOfTokenColumn, Newlines, "", "", Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst)); } void WhitespaceManager::addUntouchableToken(const FormatToken &Tok, bool InPPDirective) { - Changes.push_back(Change(false, Tok.WhitespaceRange, /*Spaces=*/0, - Tok.OriginalColumn, Tok.NewlinesBefore, "", "", - Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst)); + Changes.push_back(Change(false, Tok.WhitespaceRange, /*IndentLevel=*/0, + /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore, + "", "", Tok.Tok.getKind(), + InPPDirective && !Tok.IsFirst)); } void WhitespaceManager::replaceWhitespaceInToken( const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars, StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective, - unsigned Newlines, unsigned Spaces) { + unsigned Newlines, unsigned IndentLevel, unsigned Spaces) { Changes.push_back(Change( true, SourceRange(Tok.getStartOfNonWhitespace().getLocWithOffset(Offset), Tok.getStartOfNonWhitespace().getLocWithOffset( Offset + ReplaceChars)), - Spaces, Spaces, Newlines, PreviousPostfix, CurrentPrefix, + IndentLevel, Spaces, Spaces, Newlines, PreviousPostfix, CurrentPrefix, // If we don't add a newline this change doesn't start a comment. Thus, // when we align line comments, we don't need to treat this change as one. // FIXME: We still need to take this change in account to properly @@ -225,7 +228,8 @@ void WhitespaceManager::generateChanges() { C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn); else appendNewlineText(ReplacementText, C.NewlinesBefore); - appendIndentText(ReplacementText, C.Spaces, C.StartOfTokenColumn - C.Spaces); + appendIndentText(ReplacementText, C.IndentLevel, C.Spaces, + C.StartOfTokenColumn - C.Spaces); ReplacementText.append(C.CurrentLinePrefix); storeReplacement(C.OriginalWhitespaceRange, ReplacementText); } @@ -264,11 +268,14 @@ void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines, } } -void WhitespaceManager::appendIndentText(std::string &Text, unsigned Spaces, +void WhitespaceManager::appendIndentText(std::string &Text, + unsigned IndentLevel, unsigned Spaces, unsigned WhitespaceStartColumn) { - if (!Style.UseTab) { + switch (Style.UseTab) { + case FormatStyle::UT_Never: Text.append(std::string(Spaces, ' ')); - } else { + break; + case FormatStyle::UT_Always: { unsigned FirstTabWidth = Style.TabWidth - WhitespaceStartColumn % Style.TabWidth; // Indent with tabs only when there's at least one full tab. @@ -278,6 +285,19 @@ void WhitespaceManager::appendIndentText(std::string &Text, unsigned Spaces, } Text.append(std::string(Spaces / Style.TabWidth, '\t')); Text.append(std::string(Spaces % Style.TabWidth, ' ')); + break; + } + case FormatStyle::UT_ForIndentation: + if (WhitespaceStartColumn == 0) { + unsigned Indentation = IndentLevel * Style.IndentWidth; + if (Indentation > Spaces) + Indentation = Spaces; + unsigned Tabs = Indentation / Style.TabWidth; + Text.append(std::string(Tabs, '\t')); + Spaces -= Tabs * Style.TabWidth; + } + Text.append(std::string(Spaces, ' ')); + break; } } |

