1 //===--- Replacement.cpp - Framework for clang refactoring tools ----------===//
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 //
10 //  Implements classes to support/store refactorings.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Tooling/Core/Replacement.h"
15 
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/DiagnosticIDs.h"
18 #include "clang/Basic/DiagnosticOptions.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Lex/Lexer.h"
22 #include "clang/Rewrite/Core/Rewriter.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/raw_os_ostream.h"
26 
27 namespace clang {
28 namespace tooling {
29 
30 static const char * const InvalidLocation = "";
31 
32 Replacement::Replacement()
33   : FilePath(InvalidLocation) {}
34 
35 Replacement::Replacement(StringRef FilePath, unsigned Offset, unsigned Length,
36                          StringRef ReplacementText)
37     : FilePath(FilePath), ReplacementRange(Offset, Length),
38       ReplacementText(ReplacementText) {}
39 
40 Replacement::Replacement(const SourceManager &Sources, SourceLocation Start,
41                          unsigned Length, StringRef ReplacementText) {
42   setFromSourceLocation(Sources, Start, Length, ReplacementText);
43 }
44 
45 Replacement::Replacement(const SourceManager &Sources,
46                          const CharSourceRange &Range,
47                          StringRef ReplacementText,
48                          const LangOptions &LangOpts) {
49   setFromSourceRange(Sources, Range, ReplacementText, LangOpts);
50 }
51 
52 bool Replacement::isApplicable() const {
53   return FilePath != InvalidLocation;
54 }
55 
56 bool Replacement::apply(Rewriter &Rewrite) const {
57   SourceManager &SM = Rewrite.getSourceMgr();
58   const FileEntry *Entry = SM.getFileManager().getFile(FilePath);
59   if (!Entry)
60     return false;
61 
62   FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
63   const SourceLocation Start =
64     SM.getLocForStartOfFile(ID).
65     getLocWithOffset(ReplacementRange.getOffset());
66   // ReplaceText returns false on success.
67   // ReplaceText only fails if the source location is not a file location, in
68   // which case we already returned false earlier.
69   bool RewriteSucceeded = !Rewrite.ReplaceText(
70       Start, ReplacementRange.getLength(), ReplacementText);
71   assert(RewriteSucceeded);
72   return RewriteSucceeded;
73 }
74 
75 std::string Replacement::toString() const {
76   std::string Result;
77   llvm::raw_string_ostream Stream(Result);
78   Stream << FilePath << ": " << ReplacementRange.getOffset() << ":+"
79          << ReplacementRange.getLength() << ":\"" << ReplacementText << "\"";
80   return Stream.str();
81 }
82 
83 bool operator<(const Replacement &LHS, const Replacement &RHS) {
84   if (LHS.getOffset() != RHS.getOffset())
85     return LHS.getOffset() < RHS.getOffset();
86 
87   // Apply longer replacements first, specifically so that deletions are
88   // executed before insertions. It is (hopefully) never the intention to
89   // delete parts of newly inserted code.
90   if (LHS.getLength() != RHS.getLength())
91     return LHS.getLength() > RHS.getLength();
92 
93   if (LHS.getFilePath() != RHS.getFilePath())
94     return LHS.getFilePath() < RHS.getFilePath();
95   return LHS.getReplacementText() < RHS.getReplacementText();
96 }
97 
98 bool operator==(const Replacement &LHS, const Replacement &RHS) {
99   return LHS.getOffset() == RHS.getOffset() &&
100          LHS.getLength() == RHS.getLength() &&
101          LHS.getFilePath() == RHS.getFilePath() &&
102          LHS.getReplacementText() == RHS.getReplacementText();
103 }
104 
105 void Replacement::setFromSourceLocation(const SourceManager &Sources,
106                                         SourceLocation Start, unsigned Length,
107                                         StringRef ReplacementText) {
108   const std::pair<FileID, unsigned> DecomposedLocation =
109       Sources.getDecomposedLoc(Start);
110   const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first);
111   this->FilePath = Entry ? Entry->getName() : InvalidLocation;
112   this->ReplacementRange = Range(DecomposedLocation.second, Length);
113   this->ReplacementText = ReplacementText;
114 }
115 
116 // FIXME: This should go into the Lexer, but we need to figure out how
117 // to handle ranges for refactoring in general first - there is no obvious
118 // good way how to integrate this into the Lexer yet.
119 static int getRangeSize(const SourceManager &Sources,
120                         const CharSourceRange &Range,
121                         const LangOptions &LangOpts) {
122   SourceLocation SpellingBegin = Sources.getSpellingLoc(Range.getBegin());
123   SourceLocation SpellingEnd = Sources.getSpellingLoc(Range.getEnd());
124   std::pair<FileID, unsigned> Start = Sources.getDecomposedLoc(SpellingBegin);
125   std::pair<FileID, unsigned> End = Sources.getDecomposedLoc(SpellingEnd);
126   if (Start.first != End.first) return -1;
127   if (Range.isTokenRange())
128     End.second += Lexer::MeasureTokenLength(SpellingEnd, Sources, LangOpts);
129   return End.second - Start.second;
130 }
131 
132 void Replacement::setFromSourceRange(const SourceManager &Sources,
133                                      const CharSourceRange &Range,
134                                      StringRef ReplacementText,
135                                      const LangOptions &LangOpts) {
136   setFromSourceLocation(Sources, Sources.getSpellingLoc(Range.getBegin()),
137                         getRangeSize(Sources, Range, LangOpts),
138                         ReplacementText);
139 }
140 
141 template <typename T>
142 unsigned shiftedCodePositionInternal(const T &Replaces, unsigned Position) {
143   unsigned Offset = 0;
144   for (const auto& R : Replaces) {
145     if (R.getOffset() + R.getLength() <= Position) {
146       Offset += R.getReplacementText().size() - R.getLength();
147       continue;
148     }
149     if (R.getOffset() < Position &&
150         R.getOffset() + R.getReplacementText().size() <= Position) {
151       Position = R.getOffset() + R.getReplacementText().size() - 1;
152     }
153     break;
154   }
155   return Position + Offset;
156 }
157 
158 unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) {
159   return shiftedCodePositionInternal(Replaces, Position);
160 }
161 
162 // FIXME: Remove this function when Replacements is implemented as std::vector
163 // instead of std::set.
164 unsigned shiftedCodePosition(const std::vector<Replacement> &Replaces,
165                              unsigned Position) {
166   return shiftedCodePositionInternal(Replaces, Position);
167 }
168 
169 void deduplicate(std::vector<Replacement> &Replaces,
170                  std::vector<Range> &Conflicts) {
171   if (Replaces.empty())
172     return;
173 
174   auto LessNoPath = [](const Replacement &LHS, const Replacement &RHS) {
175     if (LHS.getOffset() != RHS.getOffset())
176       return LHS.getOffset() < RHS.getOffset();
177     if (LHS.getLength() != RHS.getLength())
178       return LHS.getLength() < RHS.getLength();
179     return LHS.getReplacementText() < RHS.getReplacementText();
180   };
181 
182   auto EqualNoPath = [](const Replacement &LHS, const Replacement &RHS) {
183     return LHS.getOffset() == RHS.getOffset() &&
184            LHS.getLength() == RHS.getLength() &&
185            LHS.getReplacementText() == RHS.getReplacementText();
186   };
187 
188   // Deduplicate. We don't want to deduplicate based on the path as we assume
189   // that all replacements refer to the same file (or are symlinks).
190   std::sort(Replaces.begin(), Replaces.end(), LessNoPath);
191   Replaces.erase(std::unique(Replaces.begin(), Replaces.end(), EqualNoPath),
192                  Replaces.end());
193 
194   // Detect conflicts
195   Range ConflictRange(Replaces.front().getOffset(),
196                       Replaces.front().getLength());
197   unsigned ConflictStart = 0;
198   unsigned ConflictLength = 1;
199   for (unsigned i = 1; i < Replaces.size(); ++i) {
200     Range Current(Replaces[i].getOffset(), Replaces[i].getLength());
201     if (ConflictRange.overlapsWith(Current)) {
202       // Extend conflicted range
203       ConflictRange = Range(ConflictRange.getOffset(),
204                             std::max(ConflictRange.getLength(),
205                                      Current.getOffset() + Current.getLength() -
206                                          ConflictRange.getOffset()));
207       ++ConflictLength;
208     } else {
209       if (ConflictLength > 1)
210         Conflicts.push_back(Range(ConflictStart, ConflictLength));
211       ConflictRange = Current;
212       ConflictStart = i;
213       ConflictLength = 1;
214     }
215   }
216 
217   if (ConflictLength > 1)
218     Conflicts.push_back(Range(ConflictStart, ConflictLength));
219 }
220 
221 bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) {
222   bool Result = true;
223   for (Replacements::const_iterator I = Replaces.begin(),
224                                     E = Replaces.end();
225        I != E; ++I) {
226     if (I->isApplicable()) {
227       Result = I->apply(Rewrite) && Result;
228     } else {
229       Result = false;
230     }
231   }
232   return Result;
233 }
234 
235 // FIXME: Remove this function when Replacements is implemented as std::vector
236 // instead of std::set.
237 bool applyAllReplacements(const std::vector<Replacement> &Replaces,
238                           Rewriter &Rewrite) {
239   bool Result = true;
240   for (std::vector<Replacement>::const_iterator I = Replaces.begin(),
241                                                 E = Replaces.end();
242        I != E; ++I) {
243     if (I->isApplicable()) {
244       Result = I->apply(Rewrite) && Result;
245     } else {
246       Result = false;
247     }
248   }
249   return Result;
250 }
251 
252 std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) {
253   if (Replaces.empty()) return Code;
254 
255   IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
256       new vfs::InMemoryFileSystem);
257   FileManager Files(FileSystemOptions(), InMemoryFileSystem);
258   DiagnosticsEngine Diagnostics(
259       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
260       new DiagnosticOptions);
261   SourceManager SourceMgr(Diagnostics, Files);
262   Rewriter Rewrite(SourceMgr, LangOptions());
263   InMemoryFileSystem->addFile(
264       "<stdin>", 0, llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>"));
265   FileID ID = SourceMgr.createFileID(Files.getFile("<stdin>"), SourceLocation(),
266                                      clang::SrcMgr::C_User);
267   for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end();
268        I != E; ++I) {
269     Replacement Replace("<stdin>", I->getOffset(), I->getLength(),
270                         I->getReplacementText());
271     if (!Replace.apply(Rewrite))
272       return "";
273   }
274   std::string Result;
275   llvm::raw_string_ostream OS(Result);
276   Rewrite.getEditBuffer(ID).write(OS);
277   OS.flush();
278   return Result;
279 }
280 
281 std::vector<Range> calculateChangedRanges(const Replacements &Replaces) {
282   std::vector<Range> ChangedRanges;
283   int Shift = 0;
284   for (const Replacement &R : Replaces) {
285     unsigned Offset = R.getOffset() + Shift;
286     unsigned Length = R.getReplacementText().size();
287     Shift += Length - R.getLength();
288     ChangedRanges.push_back(Range(Offset, Length));
289   }
290   return ChangedRanges;
291 }
292 
293 namespace {
294 // Represents a merged replacement, i.e. a replacement consisting of multiple
295 // overlapping replacements from 'First' and 'Second' in mergeReplacements.
296 //
297 // Position projection:
298 // Offsets and lengths of the replacements can generally refer to two different
299 // coordinate spaces. Replacements from 'First' refer to the original text
300 // whereas replacements from 'Second' refer to the text after applying 'First'.
301 //
302 // MergedReplacement always operates in the coordinate space of the original
303 // text, i.e. transforms elements from 'Second' to take into account what was
304 // changed based on the elements from 'First'.
305 //
306 // We can correctly calculate this projection as we look at the replacements in
307 // order of strictly increasing offsets.
308 //
309 // Invariants:
310 // * We always merge elements from 'First' into elements from 'Second' and vice
311 //   versa. Within each set, the replacements are non-overlapping.
312 // * We only extend to the right, i.e. merge elements with strictly increasing
313 //   offsets.
314 class MergedReplacement {
315 public:
316   MergedReplacement(const Replacement &R, bool MergeSecond, int D)
317       : MergeSecond(MergeSecond), Delta(D), FilePath(R.getFilePath()),
318         Offset(R.getOffset() + (MergeSecond ? 0 : Delta)), Length(R.getLength()),
319         Text(R.getReplacementText()) {
320     Delta += MergeSecond ? 0 : Text.size() - Length;
321     DeltaFirst = MergeSecond ? Text.size() - Length : 0;
322   }
323 
324   // Merges the next element 'R' into this merged element. As we always merge
325   // from 'First' into 'Second' or vice versa, the MergedReplacement knows what
326   // set the next element is coming from.
327   void merge(const Replacement &R) {
328     if (MergeSecond) {
329       unsigned REnd = R.getOffset() + Delta + R.getLength();
330       unsigned End = Offset + Text.size();
331       if (REnd > End) {
332         Length += REnd - End;
333         MergeSecond = false;
334       }
335       StringRef TextRef = Text;
336       StringRef Head = TextRef.substr(0, R.getOffset() + Delta - Offset);
337       StringRef Tail = TextRef.substr(REnd - Offset);
338       Text = (Head + R.getReplacementText() + Tail).str();
339       Delta += R.getReplacementText().size() - R.getLength();
340     } else {
341       unsigned End = Offset + Length;
342       StringRef RText = R.getReplacementText();
343       StringRef Tail = RText.substr(End - R.getOffset());
344       Text = (Text + Tail).str();
345       if (R.getOffset() + RText.size() > End) {
346         Length = R.getOffset() + R.getLength() - Offset;
347         MergeSecond = true;
348       } else {
349         Length += R.getLength() - RText.size();
350       }
351       DeltaFirst += RText.size() - R.getLength();
352     }
353   }
354 
355   // Returns 'true' if 'R' starts strictly after the MergedReplacement and thus
356   // doesn't need to be merged.
357   bool endsBefore(const Replacement &R) const {
358     if (MergeSecond)
359       return Offset + Text.size() < R.getOffset() + Delta;
360     return Offset + Length < R.getOffset();
361   }
362 
363   // Returns 'true' if an element from the second set should be merged next.
364   bool mergeSecond() const { return MergeSecond; }
365   int deltaFirst() const { return DeltaFirst; }
366   Replacement asReplacement() const { return {FilePath, Offset, Length, Text}; }
367 
368 private:
369   bool MergeSecond;
370 
371   // Amount of characters that elements from 'Second' need to be shifted by in
372   // order to refer to the original text.
373   int Delta;
374 
375   // Sum of all deltas (text-length - length) of elements from 'First' merged
376   // into this element. This is used to update 'Delta' once the
377   // MergedReplacement is completed.
378   int DeltaFirst;
379 
380   // Data of the actually merged replacement. FilePath and Offset aren't changed
381   // as the element is only extended to the right.
382   const StringRef FilePath;
383   const unsigned Offset;
384   unsigned Length;
385   std::string Text;
386 };
387 } // namespace
388 
389 std::map<std::string, Replacements>
390 groupReplacementsByFile(const Replacements &Replaces) {
391   std::map<std::string, Replacements> FileToReplaces;
392   for (const auto &Replace : Replaces) {
393     FileToReplaces[Replace.getFilePath()].insert(Replace);
394   }
395   return FileToReplaces;
396 }
397 
398 Replacements mergeReplacements(const Replacements &First,
399                                const Replacements &Second) {
400   if (First.empty() || Second.empty())
401     return First.empty() ? Second : First;
402 
403   // Delta is the amount of characters that replacements from 'Second' need to
404   // be shifted so that their offsets refer to the original text.
405   int Delta = 0;
406   Replacements Result;
407 
408   // Iterate over both sets and always add the next element (smallest total
409   // Offset) from either 'First' or 'Second'. Merge that element with
410   // subsequent replacements as long as they overlap. See more details in the
411   // comment on MergedReplacement.
412   for (auto FirstI = First.begin(), SecondI = Second.begin();
413        FirstI != First.end() || SecondI != Second.end();) {
414     bool NextIsFirst = SecondI == Second.end() ||
415                        (FirstI != First.end() &&
416                         FirstI->getOffset() < SecondI->getOffset() + Delta);
417     MergedReplacement Merged(NextIsFirst ? *FirstI : *SecondI, NextIsFirst,
418                              Delta);
419     ++(NextIsFirst ? FirstI : SecondI);
420 
421     while ((Merged.mergeSecond() && SecondI != Second.end()) ||
422            (!Merged.mergeSecond() && FirstI != First.end())) {
423       auto &I = Merged.mergeSecond() ? SecondI : FirstI;
424       if (Merged.endsBefore(*I))
425         break;
426       Merged.merge(*I);
427       ++I;
428     }
429     Delta -= Merged.deltaFirst();
430     Result.insert(Merged.asReplacement());
431   }
432   return Result;
433 }
434 
435 } // end namespace tooling
436 } // end namespace clang
437 
438