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