aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/WhitespaceManager.cpp
blob: d29cd52b0eedc128c62858d51e409a27420d02d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file implements WhitespaceManager class.
///
//===----------------------------------------------------------------------===//

#include "WhitespaceManager.h"
#include "llvm/ADT/STLExtras.h"

namespace clang {
namespace format {

void WhitespaceManager::replaceWhitespace(const AnnotatedToken &Tok,
                                          unsigned NewLines, unsigned Spaces,
                                          unsigned WhitespaceStartColumn) {
  // 2+ newlines mean an empty line separating logic scopes.
  if (NewLines >= 2)
    alignComments();

  // Align line comments if they are trailing or if they continue other
  // trailing comments.
  if (Tok.isTrailingComment()) {
    SourceLocation TokenEndLoc = Tok.FormatTok.getStartOfNonWhitespace()
        .getLocWithOffset(Tok.FormatTok.TokenLength);
    // Remove the comment's trailing whitespace.
    if (Tok.FormatTok.TrailingWhiteSpaceLength != 0)
      Replaces.insert(tooling::Replacement(
          SourceMgr, TokenEndLoc, Tok.FormatTok.TrailingWhiteSpaceLength, ""));

    bool LineExceedsColumnLimit =
        Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength >
        Style.ColumnLimit;
    // Align comment with other comments.
    if ((Tok.Parent != NULL || !Comments.empty()) && !LineExceedsColumnLimit) {
      StoredComment Comment;
      Comment.Tok = Tok.FormatTok;
      Comment.Spaces = Spaces;
      Comment.NewLines = NewLines;
      Comment.MinColumn =
          NewLines > 0 ? Spaces : WhitespaceStartColumn + Spaces;
      Comment.MaxColumn = Style.ColumnLimit - Tok.FormatTok.TokenLength;
      Comment.Untouchable = false;
      Comments.push_back(Comment);
      return;
    }
  }

  // If this line does not have a trailing comment, align the stored comments.
  if (Tok.Children.empty() && !Tok.isTrailingComment())
    alignComments();

  storeReplacement(Tok.FormatTok, getNewLineText(NewLines, Spaces));
}

void WhitespaceManager::replacePPWhitespace(const AnnotatedToken &Tok,
                                            unsigned NewLines, unsigned Spaces,
                                            unsigned WhitespaceStartColumn) {
  storeReplacement(Tok.FormatTok,
                   getNewLineText(NewLines, Spaces, WhitespaceStartColumn));
}

void WhitespaceManager::breakToken(const FormatToken &Tok, unsigned Offset,
                                   unsigned ReplaceChars, StringRef Prefix,
                                   StringRef Postfix, bool InPPDirective,
                                   unsigned Spaces,
                                   unsigned WhitespaceStartColumn) {
  std::string NewLineText;
  if (!InPPDirective)
    NewLineText = getNewLineText(1, Spaces);
  else
    NewLineText = getNewLineText(1, Spaces, WhitespaceStartColumn);
  std::string ReplacementText = (Prefix + NewLineText + Postfix).str();
  SourceLocation Location =
      Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
  Replaces.insert(
      tooling::Replacement(SourceMgr, Location, ReplaceChars, ReplacementText));
}

const tooling::Replacements &WhitespaceManager::generateReplacements() {
  alignComments();
  return Replaces;
}

void WhitespaceManager::addReplacement(const SourceLocation &SourceLoc,
                                       unsigned ReplaceChars, StringRef Text) {
  Replaces.insert(
      tooling::Replacement(SourceMgr, SourceLoc, ReplaceChars, Text));
}

void WhitespaceManager::addUntouchableComment(unsigned Column) {
  StoredComment Comment;
  Comment.MinColumn = Column;
  Comment.MaxColumn = Column;
  Comment.Untouchable = true;
  Comments.push_back(Comment);
}

std::string WhitespaceManager::getNewLineText(unsigned NewLines,
                                              unsigned Spaces) {
  return std::string(NewLines, '\n') + std::string(Spaces, ' ');
}

std::string WhitespaceManager::getNewLineText(unsigned NewLines,
                                              unsigned Spaces,
                                              unsigned WhitespaceStartColumn) {
  std::string NewLineText;
  if (NewLines > 0) {
    unsigned Offset =
        std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn);
    for (unsigned i = 0; i < NewLines; ++i) {
      NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
      NewLineText += "\\\n";
      Offset = 0;
    }
  }
  return NewLineText + std::string(Spaces, ' ');
}

void WhitespaceManager::alignComments() {
  unsigned MinColumn = 0;
  unsigned MaxColumn = UINT_MAX;
  comment_iterator Start = Comments.begin();
  for (comment_iterator I = Start, E = Comments.end(); I != E; ++I) {
    if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) {
      alignComments(Start, I, MinColumn);
      MinColumn = I->MinColumn;
      MaxColumn = I->MaxColumn;
      Start = I;
    } else {
      MinColumn = std::max(MinColumn, I->MinColumn);
      MaxColumn = std::min(MaxColumn, I->MaxColumn);
    }
  }
  alignComments(Start, Comments.end(), MinColumn);
  Comments.clear();
}

void WhitespaceManager::alignComments(comment_iterator I, comment_iterator E,
                                      unsigned Column) {
  while (I != E) {
    if (!I->Untouchable) {
      unsigned Spaces = I->Spaces + Column - I->MinColumn;
      storeReplacement(I->Tok, getNewLineText(I->NewLines, Spaces));
    }
    ++I;
  }
}

void WhitespaceManager::storeReplacement(const FormatToken &Tok,
                                         const std::string Text) {
  // Don't create a replacement, if it does not change anything.
  if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart),
                Tok.WhiteSpaceLength) == Text)
    return;

  Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart,
                                       Tok.WhiteSpaceLength, Text));
}

} // namespace format
} // namespace clang