xref: /minix3/external/bsd/llvm/dist/clang/lib/Format/WhitespaceManager.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc ///
10f4a2713aSLionel Sambuc /// \file
11f4a2713aSLionel Sambuc /// \brief This file implements WhitespaceManager class.
12f4a2713aSLionel Sambuc ///
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "WhitespaceManager.h"
16f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc namespace clang {
19f4a2713aSLionel Sambuc namespace format {
20f4a2713aSLionel Sambuc 
21*0a6a1f1dSLionel Sambuc bool WhitespaceManager::Change::IsBeforeInFile::
operator ()(const Change & C1,const Change & C2) const22*0a6a1f1dSLionel Sambuc operator()(const Change &C1, const Change &C2) const {
23f4a2713aSLionel Sambuc   return SourceMgr.isBeforeInTranslationUnit(
24f4a2713aSLionel Sambuc       C1.OriginalWhitespaceRange.getBegin(),
25f4a2713aSLionel Sambuc       C2.OriginalWhitespaceRange.getBegin());
26f4a2713aSLionel Sambuc }
27f4a2713aSLionel Sambuc 
Change(bool CreateReplacement,const SourceRange & OriginalWhitespaceRange,unsigned IndentLevel,int Spaces,unsigned StartOfTokenColumn,unsigned NewlinesBefore,StringRef PreviousLinePostfix,StringRef CurrentLinePrefix,tok::TokenKind Kind,bool ContinuesPPDirective)28f4a2713aSLionel Sambuc WhitespaceManager::Change::Change(
29f4a2713aSLionel Sambuc     bool CreateReplacement, const SourceRange &OriginalWhitespaceRange,
30*0a6a1f1dSLionel Sambuc     unsigned IndentLevel, int Spaces, unsigned StartOfTokenColumn,
31f4a2713aSLionel Sambuc     unsigned NewlinesBefore, StringRef PreviousLinePostfix,
32f4a2713aSLionel Sambuc     StringRef CurrentLinePrefix, tok::TokenKind Kind, bool ContinuesPPDirective)
33f4a2713aSLionel Sambuc     : CreateReplacement(CreateReplacement),
34f4a2713aSLionel Sambuc       OriginalWhitespaceRange(OriginalWhitespaceRange),
35f4a2713aSLionel Sambuc       StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
36f4a2713aSLionel Sambuc       PreviousLinePostfix(PreviousLinePostfix),
37f4a2713aSLionel Sambuc       CurrentLinePrefix(CurrentLinePrefix), Kind(Kind),
38f4a2713aSLionel Sambuc       ContinuesPPDirective(ContinuesPPDirective), IndentLevel(IndentLevel),
39f4a2713aSLionel Sambuc       Spaces(Spaces) {}
40f4a2713aSLionel Sambuc 
reset()41f4a2713aSLionel Sambuc void WhitespaceManager::reset() {
42f4a2713aSLionel Sambuc   Changes.clear();
43f4a2713aSLionel Sambuc   Replaces.clear();
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
replaceWhitespace(FormatToken & Tok,unsigned Newlines,unsigned IndentLevel,unsigned Spaces,unsigned StartOfTokenColumn,bool InPPDirective)46f4a2713aSLionel Sambuc void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
47f4a2713aSLionel Sambuc                                           unsigned IndentLevel, unsigned Spaces,
48f4a2713aSLionel Sambuc                                           unsigned StartOfTokenColumn,
49f4a2713aSLionel Sambuc                                           bool InPPDirective) {
50f4a2713aSLionel Sambuc   if (Tok.Finalized)
51f4a2713aSLionel Sambuc     return;
52f4a2713aSLionel Sambuc   Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue;
53f4a2713aSLionel Sambuc   Changes.push_back(Change(true, Tok.WhitespaceRange, IndentLevel, Spaces,
54f4a2713aSLionel Sambuc                            StartOfTokenColumn, Newlines, "", "",
55f4a2713aSLionel Sambuc                            Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst));
56f4a2713aSLionel Sambuc }
57f4a2713aSLionel Sambuc 
addUntouchableToken(const FormatToken & Tok,bool InPPDirective)58f4a2713aSLionel Sambuc void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
59f4a2713aSLionel Sambuc                                             bool InPPDirective) {
60f4a2713aSLionel Sambuc   if (Tok.Finalized)
61f4a2713aSLionel Sambuc     return;
62f4a2713aSLionel Sambuc   Changes.push_back(Change(false, Tok.WhitespaceRange, /*IndentLevel=*/0,
63f4a2713aSLionel Sambuc                            /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore,
64f4a2713aSLionel Sambuc                            "", "", Tok.Tok.getKind(),
65f4a2713aSLionel Sambuc                            InPPDirective && !Tok.IsFirst));
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
replaceWhitespaceInToken(const FormatToken & Tok,unsigned Offset,unsigned ReplaceChars,StringRef PreviousPostfix,StringRef CurrentPrefix,bool InPPDirective,unsigned Newlines,unsigned IndentLevel,int Spaces)68f4a2713aSLionel Sambuc void WhitespaceManager::replaceWhitespaceInToken(
69f4a2713aSLionel Sambuc     const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
70f4a2713aSLionel Sambuc     StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
71*0a6a1f1dSLionel Sambuc     unsigned Newlines, unsigned IndentLevel, int Spaces) {
72f4a2713aSLionel Sambuc   if (Tok.Finalized)
73f4a2713aSLionel Sambuc     return;
74*0a6a1f1dSLionel Sambuc   SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
75f4a2713aSLionel Sambuc   Changes.push_back(Change(
76*0a6a1f1dSLionel Sambuc       true, SourceRange(Start, Start.getLocWithOffset(ReplaceChars)),
77*0a6a1f1dSLionel Sambuc       IndentLevel, Spaces, std::max(0, Spaces), Newlines, PreviousPostfix,
78*0a6a1f1dSLionel Sambuc       CurrentPrefix,
79f4a2713aSLionel Sambuc       // If we don't add a newline this change doesn't start a comment. Thus,
80f4a2713aSLionel Sambuc       // when we align line comments, we don't need to treat this change as one.
81f4a2713aSLionel Sambuc       // FIXME: We still need to take this change in account to properly
82f4a2713aSLionel Sambuc       // calculate the new length of the comment and to calculate the changes
83f4a2713aSLionel Sambuc       // for which to do the alignment when aligning comments.
84*0a6a1f1dSLionel Sambuc       Tok.is(TT_LineComment) && Newlines > 0 ? tok::comment : tok::unknown,
85f4a2713aSLionel Sambuc       InPPDirective && !Tok.IsFirst));
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc 
generateReplacements()88f4a2713aSLionel Sambuc const tooling::Replacements &WhitespaceManager::generateReplacements() {
89f4a2713aSLionel Sambuc   if (Changes.empty())
90f4a2713aSLionel Sambuc     return Replaces;
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc   std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
93f4a2713aSLionel Sambuc   calculateLineBreakInformation();
94f4a2713aSLionel Sambuc   alignTrailingComments();
95f4a2713aSLionel Sambuc   alignEscapedNewlines();
96f4a2713aSLionel Sambuc   generateChanges();
97f4a2713aSLionel Sambuc 
98f4a2713aSLionel Sambuc   return Replaces;
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc 
calculateLineBreakInformation()101f4a2713aSLionel Sambuc void WhitespaceManager::calculateLineBreakInformation() {
102f4a2713aSLionel Sambuc   Changes[0].PreviousEndOfTokenColumn = 0;
103f4a2713aSLionel Sambuc   for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
104f4a2713aSLionel Sambuc     unsigned OriginalWhitespaceStart =
105f4a2713aSLionel Sambuc         SourceMgr.getFileOffset(Changes[i].OriginalWhitespaceRange.getBegin());
106f4a2713aSLionel Sambuc     unsigned PreviousOriginalWhitespaceEnd = SourceMgr.getFileOffset(
107f4a2713aSLionel Sambuc         Changes[i - 1].OriginalWhitespaceRange.getEnd());
108f4a2713aSLionel Sambuc     Changes[i - 1].TokenLength = OriginalWhitespaceStart -
109f4a2713aSLionel Sambuc                                  PreviousOriginalWhitespaceEnd +
110f4a2713aSLionel Sambuc                                  Changes[i].PreviousLinePostfix.size() +
111f4a2713aSLionel Sambuc                                  Changes[i - 1].CurrentLinePrefix.size();
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc     Changes[i].PreviousEndOfTokenColumn =
114f4a2713aSLionel Sambuc         Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc     Changes[i - 1].IsTrailingComment =
117f4a2713aSLionel Sambuc         (Changes[i].NewlinesBefore > 0 || Changes[i].Kind == tok::eof) &&
118f4a2713aSLionel Sambuc         Changes[i - 1].Kind == tok::comment;
119f4a2713aSLionel Sambuc   }
120f4a2713aSLionel Sambuc   // FIXME: The last token is currently not always an eof token; in those
121f4a2713aSLionel Sambuc   // cases, setting TokenLength of the last token to 0 is wrong.
122f4a2713aSLionel Sambuc   Changes.back().TokenLength = 0;
123f4a2713aSLionel Sambuc   Changes.back().IsTrailingComment = Changes.back().Kind == tok::comment;
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc   const WhitespaceManager::Change *LastBlockComment = nullptr;
126*0a6a1f1dSLionel Sambuc   for (auto &Change : Changes) {
127*0a6a1f1dSLionel Sambuc     Change.StartOfBlockComment = nullptr;
128*0a6a1f1dSLionel Sambuc     Change.IndentationOffset = 0;
129*0a6a1f1dSLionel Sambuc     if (Change.Kind == tok::comment) {
130*0a6a1f1dSLionel Sambuc       LastBlockComment = &Change;
131*0a6a1f1dSLionel Sambuc     } else if (Change.Kind == tok::unknown) {
132*0a6a1f1dSLionel Sambuc       if ((Change.StartOfBlockComment = LastBlockComment))
133*0a6a1f1dSLionel Sambuc         Change.IndentationOffset =
134*0a6a1f1dSLionel Sambuc             Change.StartOfTokenColumn -
135*0a6a1f1dSLionel Sambuc             Change.StartOfBlockComment->StartOfTokenColumn;
136*0a6a1f1dSLionel Sambuc     } else {
137*0a6a1f1dSLionel Sambuc       LastBlockComment = nullptr;
138*0a6a1f1dSLionel Sambuc     }
139*0a6a1f1dSLionel Sambuc   }
140f4a2713aSLionel Sambuc }
141f4a2713aSLionel Sambuc 
alignTrailingComments()142f4a2713aSLionel Sambuc void WhitespaceManager::alignTrailingComments() {
143f4a2713aSLionel Sambuc   unsigned MinColumn = 0;
144f4a2713aSLionel Sambuc   unsigned MaxColumn = UINT_MAX;
145f4a2713aSLionel Sambuc   unsigned StartOfSequence = 0;
146f4a2713aSLionel Sambuc   bool BreakBeforeNext = false;
147f4a2713aSLionel Sambuc   unsigned Newlines = 0;
148f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
149*0a6a1f1dSLionel Sambuc     if (Changes[i].StartOfBlockComment)
150*0a6a1f1dSLionel Sambuc       continue;
151f4a2713aSLionel Sambuc     Newlines += Changes[i].NewlinesBefore;
152*0a6a1f1dSLionel Sambuc     if (!Changes[i].IsTrailingComment)
153*0a6a1f1dSLionel Sambuc       continue;
154*0a6a1f1dSLionel Sambuc 
155*0a6a1f1dSLionel Sambuc     unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
156*0a6a1f1dSLionel Sambuc     unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
157*0a6a1f1dSLionel Sambuc     if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
158*0a6a1f1dSLionel Sambuc       ChangeMaxColumn -= 2;
159f4a2713aSLionel Sambuc     // If this comment follows an } in column 0, it probably documents the
160f4a2713aSLionel Sambuc     // closing of a namespace and we don't want to align it.
161f4a2713aSLionel Sambuc     bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
162f4a2713aSLionel Sambuc                                   Changes[i - 1].Kind == tok::r_brace &&
163f4a2713aSLionel Sambuc                                   Changes[i - 1].StartOfTokenColumn == 0;
164f4a2713aSLionel Sambuc     bool WasAlignedWithStartOfNextLine = false;
165f4a2713aSLionel Sambuc     if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
166*0a6a1f1dSLionel Sambuc       unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
167*0a6a1f1dSLionel Sambuc           Changes[i].OriginalWhitespaceRange.getEnd());
168f4a2713aSLionel Sambuc       for (unsigned j = i + 1; j != e; ++j) {
169f4a2713aSLionel Sambuc         if (Changes[j].Kind != tok::comment) { // Skip over comments.
170*0a6a1f1dSLionel Sambuc           unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
171*0a6a1f1dSLionel Sambuc               Changes[j].OriginalWhitespaceRange.getEnd());
172f4a2713aSLionel Sambuc           // The start of the next token was previously aligned with the
173f4a2713aSLionel Sambuc           // start of this comment.
174f4a2713aSLionel Sambuc           WasAlignedWithStartOfNextLine =
175*0a6a1f1dSLionel Sambuc               CommentColumn == NextColumn ||
176*0a6a1f1dSLionel Sambuc               CommentColumn == NextColumn + Style.IndentWidth;
177f4a2713aSLionel Sambuc           break;
178f4a2713aSLionel Sambuc         }
179f4a2713aSLionel Sambuc       }
180f4a2713aSLionel Sambuc     }
181f4a2713aSLionel Sambuc     if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
182f4a2713aSLionel Sambuc       alignTrailingComments(StartOfSequence, i, MinColumn);
183f4a2713aSLionel Sambuc       MinColumn = ChangeMinColumn;
184f4a2713aSLionel Sambuc       MaxColumn = ChangeMinColumn;
185f4a2713aSLionel Sambuc       StartOfSequence = i;
186f4a2713aSLionel Sambuc     } else if (BreakBeforeNext || Newlines > 1 ||
187f4a2713aSLionel Sambuc                (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
188f4a2713aSLionel Sambuc                // Break the comment sequence if the previous line did not end
189f4a2713aSLionel Sambuc                // in a trailing comment.
190f4a2713aSLionel Sambuc                (Changes[i].NewlinesBefore == 1 && i > 0 &&
191f4a2713aSLionel Sambuc                 !Changes[i - 1].IsTrailingComment) ||
192f4a2713aSLionel Sambuc                WasAlignedWithStartOfNextLine) {
193f4a2713aSLionel Sambuc       alignTrailingComments(StartOfSequence, i, MinColumn);
194f4a2713aSLionel Sambuc       MinColumn = ChangeMinColumn;
195f4a2713aSLionel Sambuc       MaxColumn = ChangeMaxColumn;
196f4a2713aSLionel Sambuc       StartOfSequence = i;
197f4a2713aSLionel Sambuc     } else {
198f4a2713aSLionel Sambuc       MinColumn = std::max(MinColumn, ChangeMinColumn);
199f4a2713aSLionel Sambuc       MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
200f4a2713aSLionel Sambuc     }
201f4a2713aSLionel Sambuc     BreakBeforeNext =
202f4a2713aSLionel Sambuc         (i == 0) || (Changes[i].NewlinesBefore > 1) ||
203f4a2713aSLionel Sambuc         // Never start a sequence with a comment at the beginning of
204f4a2713aSLionel Sambuc         // the line.
205f4a2713aSLionel Sambuc         (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
206f4a2713aSLionel Sambuc     Newlines = 0;
207f4a2713aSLionel Sambuc   }
208f4a2713aSLionel Sambuc   alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
209f4a2713aSLionel Sambuc }
210f4a2713aSLionel Sambuc 
alignTrailingComments(unsigned Start,unsigned End,unsigned Column)211f4a2713aSLionel Sambuc void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
212f4a2713aSLionel Sambuc                                               unsigned Column) {
213f4a2713aSLionel Sambuc   for (unsigned i = Start; i != End; ++i) {
214*0a6a1f1dSLionel Sambuc     int Shift = 0;
215f4a2713aSLionel Sambuc     if (Changes[i].IsTrailingComment) {
216*0a6a1f1dSLionel Sambuc       Shift = Column - Changes[i].StartOfTokenColumn;
217f4a2713aSLionel Sambuc     }
218*0a6a1f1dSLionel Sambuc     if (Changes[i].StartOfBlockComment) {
219*0a6a1f1dSLionel Sambuc       Shift = Changes[i].IndentationOffset +
220*0a6a1f1dSLionel Sambuc               Changes[i].StartOfBlockComment->StartOfTokenColumn -
221*0a6a1f1dSLionel Sambuc               Changes[i].StartOfTokenColumn;
222*0a6a1f1dSLionel Sambuc     }
223*0a6a1f1dSLionel Sambuc     assert(Shift >= 0);
224*0a6a1f1dSLionel Sambuc     Changes[i].Spaces += Shift;
225*0a6a1f1dSLionel Sambuc     if (i + 1 != End)
226*0a6a1f1dSLionel Sambuc       Changes[i + 1].PreviousEndOfTokenColumn += Shift;
227*0a6a1f1dSLionel Sambuc     Changes[i].StartOfTokenColumn += Shift;
228f4a2713aSLionel Sambuc   }
229f4a2713aSLionel Sambuc }
230f4a2713aSLionel Sambuc 
alignEscapedNewlines()231f4a2713aSLionel Sambuc void WhitespaceManager::alignEscapedNewlines() {
232f4a2713aSLionel Sambuc   unsigned MaxEndOfLine =
233f4a2713aSLionel Sambuc       Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
234f4a2713aSLionel Sambuc   unsigned StartOfMacro = 0;
235f4a2713aSLionel Sambuc   for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
236f4a2713aSLionel Sambuc     Change &C = Changes[i];
237f4a2713aSLionel Sambuc     if (C.NewlinesBefore > 0) {
238f4a2713aSLionel Sambuc       if (C.ContinuesPPDirective) {
239f4a2713aSLionel Sambuc         MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
240f4a2713aSLionel Sambuc       } else {
241f4a2713aSLionel Sambuc         alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
242f4a2713aSLionel Sambuc         MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
243f4a2713aSLionel Sambuc         StartOfMacro = i;
244f4a2713aSLionel Sambuc       }
245f4a2713aSLionel Sambuc     }
246f4a2713aSLionel Sambuc   }
247f4a2713aSLionel Sambuc   alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
248f4a2713aSLionel Sambuc }
249f4a2713aSLionel Sambuc 
alignEscapedNewlines(unsigned Start,unsigned End,unsigned Column)250f4a2713aSLionel Sambuc void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
251f4a2713aSLionel Sambuc                                              unsigned Column) {
252f4a2713aSLionel Sambuc   for (unsigned i = Start; i < End; ++i) {
253f4a2713aSLionel Sambuc     Change &C = Changes[i];
254f4a2713aSLionel Sambuc     if (C.NewlinesBefore > 0) {
255f4a2713aSLionel Sambuc       assert(C.ContinuesPPDirective);
256f4a2713aSLionel Sambuc       if (C.PreviousEndOfTokenColumn + 1 > Column)
257f4a2713aSLionel Sambuc         C.EscapedNewlineColumn = 0;
258f4a2713aSLionel Sambuc       else
259f4a2713aSLionel Sambuc         C.EscapedNewlineColumn = Column;
260f4a2713aSLionel Sambuc     }
261f4a2713aSLionel Sambuc   }
262f4a2713aSLionel Sambuc }
263f4a2713aSLionel Sambuc 
generateChanges()264f4a2713aSLionel Sambuc void WhitespaceManager::generateChanges() {
265f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
266f4a2713aSLionel Sambuc     const Change &C = Changes[i];
267f4a2713aSLionel Sambuc     if (C.CreateReplacement) {
268f4a2713aSLionel Sambuc       std::string ReplacementText = C.PreviousLinePostfix;
269f4a2713aSLionel Sambuc       if (C.ContinuesPPDirective)
270f4a2713aSLionel Sambuc         appendNewlineText(ReplacementText, C.NewlinesBefore,
271f4a2713aSLionel Sambuc                           C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
272f4a2713aSLionel Sambuc       else
273f4a2713aSLionel Sambuc         appendNewlineText(ReplacementText, C.NewlinesBefore);
274*0a6a1f1dSLionel Sambuc       appendIndentText(ReplacementText, C.IndentLevel, std::max(0, C.Spaces),
275*0a6a1f1dSLionel Sambuc                        C.StartOfTokenColumn - std::max(0, C.Spaces));
276f4a2713aSLionel Sambuc       ReplacementText.append(C.CurrentLinePrefix);
277f4a2713aSLionel Sambuc       storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
278f4a2713aSLionel Sambuc     }
279f4a2713aSLionel Sambuc   }
280f4a2713aSLionel Sambuc }
281f4a2713aSLionel Sambuc 
storeReplacement(const SourceRange & Range,StringRef Text)282f4a2713aSLionel Sambuc void WhitespaceManager::storeReplacement(const SourceRange &Range,
283f4a2713aSLionel Sambuc                                          StringRef Text) {
284f4a2713aSLionel Sambuc   unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
285f4a2713aSLionel Sambuc                               SourceMgr.getFileOffset(Range.getBegin());
286f4a2713aSLionel Sambuc   // Don't create a replacement, if it does not change anything.
287f4a2713aSLionel Sambuc   if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
288f4a2713aSLionel Sambuc                 WhitespaceLength) == Text)
289f4a2713aSLionel Sambuc     return;
290f4a2713aSLionel Sambuc   Replaces.insert(tooling::Replacement(
291f4a2713aSLionel Sambuc       SourceMgr, CharSourceRange::getCharRange(Range), Text));
292f4a2713aSLionel Sambuc }
293f4a2713aSLionel Sambuc 
appendNewlineText(std::string & Text,unsigned Newlines)294f4a2713aSLionel Sambuc void WhitespaceManager::appendNewlineText(std::string &Text,
295f4a2713aSLionel Sambuc                                           unsigned Newlines) {
296f4a2713aSLionel Sambuc   for (unsigned i = 0; i < Newlines; ++i)
297f4a2713aSLionel Sambuc     Text.append(UseCRLF ? "\r\n" : "\n");
298f4a2713aSLionel Sambuc }
299f4a2713aSLionel Sambuc 
appendNewlineText(std::string & Text,unsigned Newlines,unsigned PreviousEndOfTokenColumn,unsigned EscapedNewlineColumn)300f4a2713aSLionel Sambuc void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
301f4a2713aSLionel Sambuc                                           unsigned PreviousEndOfTokenColumn,
302f4a2713aSLionel Sambuc                                           unsigned EscapedNewlineColumn) {
303f4a2713aSLionel Sambuc   if (Newlines > 0) {
304f4a2713aSLionel Sambuc     unsigned Offset =
305f4a2713aSLionel Sambuc         std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn);
306f4a2713aSLionel Sambuc     for (unsigned i = 0; i < Newlines; ++i) {
307f4a2713aSLionel Sambuc       Text.append(std::string(EscapedNewlineColumn - Offset - 1, ' '));
308f4a2713aSLionel Sambuc       Text.append(UseCRLF ? "\\\r\n" : "\\\n");
309f4a2713aSLionel Sambuc       Offset = 0;
310f4a2713aSLionel Sambuc     }
311f4a2713aSLionel Sambuc   }
312f4a2713aSLionel Sambuc }
313f4a2713aSLionel Sambuc 
appendIndentText(std::string & Text,unsigned IndentLevel,unsigned Spaces,unsigned WhitespaceStartColumn)314f4a2713aSLionel Sambuc void WhitespaceManager::appendIndentText(std::string &Text,
315f4a2713aSLionel Sambuc                                          unsigned IndentLevel, unsigned Spaces,
316f4a2713aSLionel Sambuc                                          unsigned WhitespaceStartColumn) {
317f4a2713aSLionel Sambuc   switch (Style.UseTab) {
318f4a2713aSLionel Sambuc   case FormatStyle::UT_Never:
319f4a2713aSLionel Sambuc     Text.append(std::string(Spaces, ' '));
320f4a2713aSLionel Sambuc     break;
321f4a2713aSLionel Sambuc   case FormatStyle::UT_Always: {
322f4a2713aSLionel Sambuc     unsigned FirstTabWidth =
323f4a2713aSLionel Sambuc         Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
324f4a2713aSLionel Sambuc     // Indent with tabs only when there's at least one full tab.
325f4a2713aSLionel Sambuc     if (FirstTabWidth + Style.TabWidth <= Spaces) {
326f4a2713aSLionel Sambuc       Spaces -= FirstTabWidth;
327f4a2713aSLionel Sambuc       Text.append("\t");
328f4a2713aSLionel Sambuc     }
329f4a2713aSLionel Sambuc     Text.append(std::string(Spaces / Style.TabWidth, '\t'));
330f4a2713aSLionel Sambuc     Text.append(std::string(Spaces % Style.TabWidth, ' '));
331f4a2713aSLionel Sambuc     break;
332f4a2713aSLionel Sambuc   }
333f4a2713aSLionel Sambuc   case FormatStyle::UT_ForIndentation:
334f4a2713aSLionel Sambuc     if (WhitespaceStartColumn == 0) {
335f4a2713aSLionel Sambuc       unsigned Indentation = IndentLevel * Style.IndentWidth;
336f4a2713aSLionel Sambuc       // This happens, e.g. when a line in a block comment is indented less than
337f4a2713aSLionel Sambuc       // the first one.
338f4a2713aSLionel Sambuc       if (Indentation > Spaces)
339f4a2713aSLionel Sambuc         Indentation = Spaces;
340f4a2713aSLionel Sambuc       unsigned Tabs = Indentation / Style.TabWidth;
341f4a2713aSLionel Sambuc       Text.append(std::string(Tabs, '\t'));
342f4a2713aSLionel Sambuc       Spaces -= Tabs * Style.TabWidth;
343f4a2713aSLionel Sambuc     }
344f4a2713aSLionel Sambuc     Text.append(std::string(Spaces, ' '));
345f4a2713aSLionel Sambuc     break;
346f4a2713aSLionel Sambuc   }
347f4a2713aSLionel Sambuc }
348f4a2713aSLionel Sambuc 
349f4a2713aSLionel Sambuc } // namespace format
350f4a2713aSLionel Sambuc } // namespace clang
351