clang  6.0.0svn
AtomicChange.cpp
Go to the documentation of this file.
1 //===--- AtomicChange.cpp - AtomicChange implementation -----------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 #include "llvm/Support/YAMLTraits.h"
13 #include <string>
14 
15 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::AtomicChange)
16 
17 namespace {
18 /// \brief Helper to (de)serialize an AtomicChange since we don't have direct
19 /// access to its data members.
20 /// Data members of a normalized AtomicChange can be directly mapped from/to
21 /// YAML string.
22 struct NormalizedAtomicChange {
23  NormalizedAtomicChange() = default;
24 
25  NormalizedAtomicChange(const llvm::yaml::IO &) {}
26 
27  // This converts AtomicChange's internal implementation of the replacements
28  // set to a vector of replacements.
29  NormalizedAtomicChange(const llvm::yaml::IO &,
31  : Key(E.getKey()), FilePath(E.getFilePath()), Error(E.getError()),
32  InsertedHeaders(E.getInsertedHeaders()),
33  RemovedHeaders(E.getRemovedHeaders()),
34  Replaces(E.getReplacements().begin(), E.getReplacements().end()) {}
35 
36  // This is not expected to be called but needed for template instantiation.
37  clang::tooling::AtomicChange denormalize(const llvm::yaml::IO &) {
38  llvm_unreachable("Do not convert YAML to AtomicChange directly with '>>'. "
39  "Use AtomicChange::convertFromYAML instead.");
40  }
41  std::string Key;
42  std::string FilePath;
43  std::string Error;
44  std::vector<std::string> InsertedHeaders;
45  std::vector<std::string> RemovedHeaders;
46  std::vector<clang::tooling::Replacement> Replaces;
47 };
48 } // anonymous namespace
49 
50 namespace llvm {
51 namespace yaml {
52 
53 /// \brief Specialized MappingTraits to describe how an AtomicChange is
54 /// (de)serialized.
55 template <> struct MappingTraits<NormalizedAtomicChange> {
56  static void mapping(IO &Io, NormalizedAtomicChange &Doc) {
57  Io.mapRequired("Key", Doc.Key);
58  Io.mapRequired("FilePath", Doc.FilePath);
59  Io.mapRequired("Error", Doc.Error);
60  Io.mapRequired("InsertedHeaders", Doc.InsertedHeaders);
61  Io.mapRequired("RemovedHeaders", Doc.RemovedHeaders);
62  Io.mapRequired("Replacements", Doc.Replaces);
63  }
64 };
65 
66 /// \brief Specialized MappingTraits to describe how an AtomicChange is
67 /// (de)serialized.
68 template <> struct MappingTraits<clang::tooling::AtomicChange> {
69  static void mapping(IO &Io, clang::tooling::AtomicChange &Doc) {
70  MappingNormalization<NormalizedAtomicChange, clang::tooling::AtomicChange>
71  Keys(Io, Doc);
72  Io.mapRequired("Key", Keys->Key);
73  Io.mapRequired("FilePath", Keys->FilePath);
74  Io.mapRequired("Error", Keys->Error);
75  Io.mapRequired("InsertedHeaders", Keys->InsertedHeaders);
76  Io.mapRequired("RemovedHeaders", Keys->RemovedHeaders);
77  Io.mapRequired("Replacements", Keys->Replaces);
78  }
79 };
80 
81 } // end namespace yaml
82 } // end namespace llvm
83 
84 namespace clang {
85 namespace tooling {
86 namespace {
87 
88 // Returns true if there is any line that violates \p ColumnLimit in range
89 // [Start, End].
90 bool violatesColumnLimit(llvm::StringRef Code, unsigned ColumnLimit,
91  unsigned Start, unsigned End) {
92  auto StartPos = Code.rfind('\n', Start);
93  StartPos = (StartPos == llvm::StringRef::npos) ? 0 : StartPos + 1;
94 
95  auto EndPos = Code.find("\n", End);
96  if (EndPos == llvm::StringRef::npos)
97  EndPos = Code.size();
98 
100  Code.substr(StartPos, EndPos - StartPos).split(Lines, '\n');
101  for (llvm::StringRef Line : Lines)
102  if (Line.size() > ColumnLimit)
103  return true;
104  return false;
105 }
106 
107 std::vector<Range>
108 getRangesForFormating(llvm::StringRef Code, unsigned ColumnLimit,
109  ApplyChangesSpec::FormatOption Format,
110  const clang::tooling::Replacements &Replaces) {
111  // kNone suppresses formatting entirely.
112  if (Format == ApplyChangesSpec::kNone)
113  return {};
114  std::vector<clang::tooling::Range> Ranges;
115  // This works assuming that replacements are ordered by offset.
116  // FIXME: use `getAffectedRanges()` to calculate when it does not include '\n'
117  // at the end of an insertion in affected ranges.
118  int Offset = 0;
119  for (const clang::tooling::Replacement &R : Replaces) {
120  int Start = R.getOffset() + Offset;
121  int End = Start + R.getReplacementText().size();
122  if (!R.getReplacementText().empty() &&
123  R.getReplacementText().back() == '\n' && R.getLength() == 0 &&
124  R.getOffset() > 0 && R.getOffset() <= Code.size() &&
125  Code[R.getOffset() - 1] == '\n')
126  // If we are inserting at the start of a line and the replacement ends in
127  // a newline, we don't need to format the subsequent line.
128  --End;
129  Offset += R.getReplacementText().size() - R.getLength();
130 
131  if (Format == ApplyChangesSpec::kAll ||
132  violatesColumnLimit(Code, ColumnLimit, Start, End))
133  Ranges.emplace_back(Start, End - Start);
134  }
135  return Ranges;
136 }
137 
138 inline llvm::Error make_string_error(const llvm::Twine &Message) {
139  return llvm::make_error<llvm::StringError>(Message,
140  llvm::inconvertibleErrorCode());
141 }
142 
143 // Creates replacements for inserting/deleting #include headers.
145 createReplacementsForHeaders(llvm::StringRef FilePath, llvm::StringRef Code,
147  const format::FormatStyle &Style) {
148  // Create header insertion/deletion replacements to be cleaned up
149  // (i.e. converted to real insertion/deletion replacements).
150  Replacements HeaderReplacements;
151  for (const auto &Change : Changes) {
152  for (llvm::StringRef Header : Change.getInsertedHeaders()) {
153  std::string EscapedHeader =
154  Header.startswith("<") || Header.startswith("\"")
155  ? Header.str()
156  : ("\"" + Header + "\"").str();
157  std::string ReplacementText = "#include " + EscapedHeader;
158  // Offset UINT_MAX and length 0 indicate that the replacement is a header
159  // insertion.
160  llvm::Error Err = HeaderReplacements.add(
161  tooling::Replacement(FilePath, UINT_MAX, 0, ReplacementText));
162  if (Err)
163  return std::move(Err);
164  }
165  for (const std::string &Header : Change.getRemovedHeaders()) {
166  // Offset UINT_MAX and length 1 indicate that the replacement is a header
167  // deletion.
168  llvm::Error Err =
169  HeaderReplacements.add(Replacement(FilePath, UINT_MAX, 1, Header));
170  if (Err)
171  return std::move(Err);
172  }
173  }
174 
175  // cleanupAroundReplacements() converts header insertions/deletions into
176  // actual replacements that add/remove headers at the right location.
177  return clang::format::cleanupAroundReplacements(Code, HeaderReplacements,
178  Style);
179 }
180 
181 // Combine replacements in all Changes as a `Replacements`. This ignores the
182 // file path in all replacements and replaces them with \p FilePath.
184 combineReplacementsInChanges(llvm::StringRef FilePath,
186  Replacements Replaces;
187  for (const auto &Change : Changes)
188  for (const auto &R : Change.getReplacements())
189  if (auto Err = Replaces.add(Replacement(
190  FilePath, R.getOffset(), R.getLength(), R.getReplacementText())))
191  return std::move(Err);
192  return Replaces;
193 }
194 
195 } // end namespace
196 
197 AtomicChange::AtomicChange(const SourceManager &SM,
198  SourceLocation KeyPosition) {
199  const FullSourceLoc FullKeyPosition(KeyPosition, SM);
200  std::pair<FileID, unsigned> FileIDAndOffset =
201  FullKeyPosition.getSpellingLoc().getDecomposedLoc();
202  const FileEntry *FE = SM.getFileEntryForID(FileIDAndOffset.first);
203  assert(FE && "Cannot create AtomicChange with invalid location.");
204  FilePath = FE->getName();
205  Key = FilePath + ":" + std::to_string(FileIDAndOffset.second);
206 }
207 
208 AtomicChange::AtomicChange(std::string Key, std::string FilePath,
209  std::string Error,
210  std::vector<std::string> InsertedHeaders,
211  std::vector<std::string> RemovedHeaders,
213  : Key(std::move(Key)), FilePath(std::move(FilePath)),
214  Error(std::move(Error)), InsertedHeaders(std::move(InsertedHeaders)),
215  RemovedHeaders(std::move(RemovedHeaders)), Replaces(std::move(Replaces)) {
216 }
217 
218 bool AtomicChange::operator==(const AtomicChange &Other) const {
219  if (Key != Other.Key || FilePath != Other.FilePath || Error != Other.Error)
220  return false;
221  if (!(Replaces == Other.Replaces))
222  return false;
223  // FXIME: Compare header insertions/removals.
224  return true;
225 }
226 
227 std::string AtomicChange::toYAMLString() {
228  std::string YamlContent;
229  llvm::raw_string_ostream YamlContentStream(YamlContent);
230 
231  llvm::yaml::Output YAML(YamlContentStream);
232  YAML << *this;
233  YamlContentStream.flush();
234  return YamlContent;
235 }
236 
237 AtomicChange AtomicChange::convertFromYAML(llvm::StringRef YAMLContent) {
238  NormalizedAtomicChange NE;
239  llvm::yaml::Input YAML(YAMLContent);
240  YAML >> NE;
241  AtomicChange E(NE.Key, NE.FilePath, NE.Error, NE.InsertedHeaders,
242  NE.RemovedHeaders, tooling::Replacements());
243  for (const auto &R : NE.Replaces) {
244  llvm::Error Err = E.Replaces.add(R);
245  if (Err)
246  llvm_unreachable(
247  "Failed to add replacement when Converting YAML to AtomicChange.");
248  llvm::consumeError(std::move(Err));
249  }
250  return E;
251 }
252 
253 llvm::Error AtomicChange::replace(const SourceManager &SM,
254  const CharSourceRange &Range,
255  llvm::StringRef ReplacementText) {
256  return Replaces.add(Replacement(SM, Range, ReplacementText));
257 }
258 
259 llvm::Error AtomicChange::replace(const SourceManager &SM, SourceLocation Loc,
260  unsigned Length, llvm::StringRef Text) {
261  return Replaces.add(Replacement(SM, Loc, Length, Text));
262 }
263 
264 llvm::Error AtomicChange::insert(const SourceManager &SM, SourceLocation Loc,
265  llvm::StringRef Text, bool InsertAfter) {
266  if (Text.empty())
267  return llvm::Error::success();
268  Replacement R(SM, Loc, 0, Text);
269  llvm::Error Err = Replaces.add(R);
270  if (Err) {
271  return llvm::handleErrors(
272  std::move(Err), [&](const ReplacementError &RE) -> llvm::Error {
273  if (RE.get() != replacement_error::insert_conflict)
274  return llvm::make_error<ReplacementError>(RE);
275  unsigned NewOffset = Replaces.getShiftedCodePosition(R.getOffset());
276  if (!InsertAfter)
277  NewOffset -=
278  RE.getExistingReplacement()->getReplacementText().size();
279  Replacement NewR(R.getFilePath(), NewOffset, 0, Text);
280  Replaces = Replaces.merge(Replacements(NewR));
281  return llvm::Error::success();
282  });
283  }
284  return llvm::Error::success();
285 }
286 
287 void AtomicChange::addHeader(llvm::StringRef Header) {
288  InsertedHeaders.push_back(Header);
289 }
290 
291 void AtomicChange::removeHeader(llvm::StringRef Header) {
292  RemovedHeaders.push_back(Header);
293 }
294 
296 applyAtomicChanges(llvm::StringRef FilePath, llvm::StringRef Code,
298  const ApplyChangesSpec &Spec) {
299  llvm::Expected<Replacements> HeaderReplacements =
300  createReplacementsForHeaders(FilePath, Code, Changes, Spec.Style);
301  if (!HeaderReplacements)
302  return make_string_error(
303  "Failed to create replacements for header changes: " +
304  llvm::toString(HeaderReplacements.takeError()));
305 
307  combineReplacementsInChanges(FilePath, Changes);
308  if (!Replaces)
309  return make_string_error("Failed to combine replacements in all changes: " +
310  llvm::toString(Replaces.takeError()));
311 
312  Replacements AllReplaces = std::move(*Replaces);
313  for (const auto &R : *HeaderReplacements) {
314  llvm::Error Err = AllReplaces.add(R);
315  if (Err)
316  return make_string_error(
317  "Failed to combine existing replacements with header replacements: " +
318  llvm::toString(std::move(Err)));
319  }
320 
321  if (Spec.Cleanup) {
322  llvm::Expected<Replacements> CleanReplaces =
323  format::cleanupAroundReplacements(Code, AllReplaces, Spec.Style);
324  if (!CleanReplaces)
325  return make_string_error("Failed to cleanup around replacements: " +
326  llvm::toString(CleanReplaces.takeError()));
327  AllReplaces = std::move(*CleanReplaces);
328  }
329 
330  // Apply all replacements.
331  llvm::Expected<std::string> ChangedCode =
332  applyAllReplacements(Code, AllReplaces);
333  if (!ChangedCode)
334  return make_string_error("Failed to apply all replacements: " +
335  llvm::toString(ChangedCode.takeError()));
336 
337  // Sort inserted headers. This is done even if other formatting is turned off
338  // as incorrectly sorted headers are always just wrong, it's not a matter of
339  // taste.
340  Replacements HeaderSortingReplacements = format::sortIncludes(
341  Spec.Style, *ChangedCode, AllReplaces.getAffectedRanges(), FilePath);
342  ChangedCode = applyAllReplacements(*ChangedCode, HeaderSortingReplacements);
343  if (!ChangedCode)
344  return make_string_error(
345  "Failed to apply replacements for sorting includes: " +
346  llvm::toString(ChangedCode.takeError()));
347 
348  AllReplaces = AllReplaces.merge(HeaderSortingReplacements);
349 
350  std::vector<Range> FormatRanges = getRangesForFormating(
351  *ChangedCode, Spec.Style.ColumnLimit, Spec.Format, AllReplaces);
352  if (!FormatRanges.empty()) {
353  Replacements FormatReplacements =
354  format::reformat(Spec.Style, *ChangedCode, FormatRanges, FilePath);
355  ChangedCode = applyAllReplacements(*ChangedCode, FormatReplacements);
356  if (!ChangedCode)
357  return make_string_error(
358  "Failed to apply replacements for formatting changed code: " +
359  llvm::toString(ChangedCode.takeError()));
360  }
361  return ChangedCode;
362 }
363 
364 } // end namespace tooling
365 } // end namespace clang
const llvm::Optional< Replacement > & getExistingReplacement() const
Definition: Replacement.h:178
StringRef getFilePath() const
Accessors.
Definition: Replacement.h:118
bool operator==(CanQual< T > x, CanQual< U > y)
replacement_error get() const
Definition: Replacement.h:170
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName, unsigned *Cursor=nullptr)
Returns the replacements necessary to sort all #include blocks that are affected by Ranges...
Definition: Format.cpp:1600
static void mapping(IO &Io, clang::tooling::AtomicChange &Doc)
Maintains a set of replacements that are conflict-free.
Definition: Replacement.h:205
std::vector< Range > getAffectedRanges() const
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite)
Apply all replacements in Replaces to the Rewriter Rewrite.
uint32_t Offset
Definition: CacheTokens.cpp:43
AvailabilityChange Changes[NumAvailabilitySlots]
Definition: AttributeList.h:57
#define UINT_MAX
Definition: limits.h:72
unsigned ColumnLimit
The column limit.
Definition: Format.h:840
A text replacement.
Definition: Replacement.h:81
A source range independent of the SourceManager.
Definition: Replacement.h:42
Carries extra error information in replacement-related llvm::Error, e.g.
Definition: Replacement.h:152
SourceLocation End
Represents a character-granular source range.
std::pair< FileID, unsigned > getDecomposedLoc() const
Decompose the specified location into a raw FileID + Offset pair.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
const AnnotatedLine * Line
const SourceManager & SM
Definition: Format.cpp:1337
llvm::Expected< tooling::Replacements > cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces, const FormatStyle &Style)
Returns the replacements corresponding to applying Replaces and cleaning up the code after that on su...
Definition: Format.cpp:1935
Encodes a location in the source.
StringRef getName() const
Definition: FileManager.h:84
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>", FormattingAttemptStatus *Status=nullptr)
Reformats the given Ranges in Code.
Definition: Format.cpp:2018
llvm::Expected< std::string > applyAtomicChanges(llvm::StringRef FilePath, llvm::StringRef Code, llvm::ArrayRef< AtomicChange > Changes, const ApplyChangesSpec &Spec)
Applies all AtomicChanges in Changes to the Code.
llvm::Error make_string_error(const llvm::Twine &Message)
Definition: Format.cpp:503
Dataflow Directional Tag Classes.
unsigned getOffset() const
Definition: Replacement.h:119
std::string toString(const til::SExpr *E)
unsigned getShiftedCodePosition(unsigned Position) const
const Expr * Replacement
Definition: AttributeList.h:59
llvm::Error add(const Replacement &R)
Adds a new replacement R to the current set of replacements.
FullSourceLoc getSpellingLoc() const
A SourceLocation and its associated SourceManager.
LLVM_NODISCARD Replacements merge(const Replacements &Replaces) const
Merges Replaces into the current replacements.
static void mapping(IO &Io, NormalizedAtomicChange &Doc)
An atomic change is used to create and group a set of source edits, e.g.
Definition: AtomicChange.h:37
StringRef Text
Definition: Format.cpp:1346
This file defines the structure of a YAML document for serializing replacements.
This class handles loading and caching of source files into memory.
const FormatStyle & Style