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 this->FilePath = Entry ? Entry->getName() : InvalidLocation; 117 this->ReplacementRange = Range(DecomposedLocation.second, Length); 118 this->ReplacementText = ReplacementText; 119 } 120 121 // FIXME: This should go into the Lexer, but we need to figure out how 122 // to handle ranges for refactoring in general first - there is no obvious 123 // good way how to integrate this into the Lexer yet. 124 static int getRangeSize(const SourceManager &Sources, 125 const CharSourceRange &Range, 126 const LangOptions &LangOpts) { 127 SourceLocation SpellingBegin = Sources.getSpellingLoc(Range.getBegin()); 128 SourceLocation SpellingEnd = Sources.getSpellingLoc(Range.getEnd()); 129 std::pair<FileID, unsigned> Start = Sources.getDecomposedLoc(SpellingBegin); 130 std::pair<FileID, unsigned> End = Sources.getDecomposedLoc(SpellingEnd); 131 if (Start.first != End.first) return -1; 132 if (Range.isTokenRange()) 133 End.second += Lexer::MeasureTokenLength(SpellingEnd, Sources, LangOpts); 134 return End.second - Start.second; 135 } 136 137 void Replacement::setFromSourceRange(const SourceManager &Sources, 138 const CharSourceRange &Range, 139 StringRef ReplacementText, 140 const LangOptions &LangOpts) { 141 setFromSourceLocation(Sources, Sources.getSpellingLoc(Range.getBegin()), 142 getRangeSize(Sources, Range, LangOpts), 143 ReplacementText); 144 } 145 146 unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) { 147 unsigned NewPosition = Position; 148 for (Replacements::iterator I = Replaces.begin(), E = Replaces.end(); I != E; 149 ++I) { 150 if (I->getOffset() >= Position) 151 break; 152 if (I->getOffset() + I->getLength() > Position) 153 NewPosition += I->getOffset() + I->getLength() - Position; 154 NewPosition += I->getReplacementText().size() - I->getLength(); 155 } 156 return NewPosition; 157 } 158 159 // FIXME: Remove this function when Replacements is implemented as std::vector 160 // instead of std::set. 161 unsigned shiftedCodePosition(const std::vector<Replacement> &Replaces, 162 unsigned Position) { 163 unsigned NewPosition = Position; 164 for (std::vector<Replacement>::const_iterator I = Replaces.begin(), 165 E = Replaces.end(); 166 I != E; ++I) { 167 if (I->getOffset() >= Position) 168 break; 169 if (I->getOffset() + I->getLength() > Position) 170 NewPosition += I->getOffset() + I->getLength() - Position; 171 NewPosition += I->getReplacementText().size() - I->getLength(); 172 } 173 return NewPosition; 174 } 175 176 void deduplicate(std::vector<Replacement> &Replaces, 177 std::vector<Range> &Conflicts) { 178 if (Replaces.empty()) 179 return; 180 181 auto LessNoPath = [](const Replacement &LHS, const Replacement &RHS) { 182 if (LHS.getOffset() != RHS.getOffset()) 183 return LHS.getOffset() < RHS.getOffset(); 184 if (LHS.getLength() != RHS.getLength()) 185 return LHS.getLength() < RHS.getLength(); 186 return LHS.getReplacementText() < RHS.getReplacementText(); 187 }; 188 189 auto EqualNoPath = [](const Replacement &LHS, const Replacement &RHS) { 190 return LHS.getOffset() == RHS.getOffset() && 191 LHS.getLength() == RHS.getLength() && 192 LHS.getReplacementText() == RHS.getReplacementText(); 193 }; 194 195 // Deduplicate. We don't want to deduplicate based on the path as we assume 196 // that all replacements refer to the same file (or are symlinks). 197 std::sort(Replaces.begin(), Replaces.end(), LessNoPath); 198 Replaces.erase(std::unique(Replaces.begin(), Replaces.end(), EqualNoPath), 199 Replaces.end()); 200 201 // Detect conflicts 202 Range ConflictRange(Replaces.front().getOffset(), 203 Replaces.front().getLength()); 204 unsigned ConflictStart = 0; 205 unsigned ConflictLength = 1; 206 for (unsigned i = 1; i < Replaces.size(); ++i) { 207 Range Current(Replaces[i].getOffset(), Replaces[i].getLength()); 208 if (ConflictRange.overlapsWith(Current)) { 209 // Extend conflicted range 210 ConflictRange = Range(ConflictRange.getOffset(), 211 std::max(ConflictRange.getLength(), 212 Current.getOffset() + Current.getLength() - 213 ConflictRange.getOffset())); 214 ++ConflictLength; 215 } else { 216 if (ConflictLength > 1) 217 Conflicts.push_back(Range(ConflictStart, ConflictLength)); 218 ConflictRange = Current; 219 ConflictStart = i; 220 ConflictLength = 1; 221 } 222 } 223 224 if (ConflictLength > 1) 225 Conflicts.push_back(Range(ConflictStart, ConflictLength)); 226 } 227 228 bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) { 229 bool Result = true; 230 for (Replacements::const_iterator I = Replaces.begin(), 231 E = Replaces.end(); 232 I != E; ++I) { 233 if (I->isApplicable()) { 234 Result = I->apply(Rewrite) && Result; 235 } else { 236 Result = false; 237 } 238 } 239 return Result; 240 } 241 242 // FIXME: Remove this function when Replacements is implemented as std::vector 243 // instead of std::set. 244 bool applyAllReplacements(const std::vector<Replacement> &Replaces, 245 Rewriter &Rewrite) { 246 bool Result = true; 247 for (std::vector<Replacement>::const_iterator I = Replaces.begin(), 248 E = Replaces.end(); 249 I != E; ++I) { 250 if (I->isApplicable()) { 251 Result = I->apply(Rewrite) && Result; 252 } else { 253 Result = false; 254 } 255 } 256 return Result; 257 } 258 259 std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { 260 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( 261 new vfs::InMemoryFileSystem); 262 FileManager Files(FileSystemOptions(), InMemoryFileSystem); 263 DiagnosticsEngine Diagnostics( 264 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 265 new DiagnosticOptions); 266 SourceManager SourceMgr(Diagnostics, Files); 267 Rewriter Rewrite(SourceMgr, LangOptions()); 268 InMemoryFileSystem->addFile( 269 "<stdin>", 0, llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>")); 270 FileID ID = SourceMgr.createFileID(Files.getFile("<stdin>"), SourceLocation(), 271 clang::SrcMgr::C_User); 272 for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); 273 I != E; ++I) { 274 Replacement Replace("<stdin>", I->getOffset(), I->getLength(), 275 I->getReplacementText()); 276 if (!Replace.apply(Rewrite)) 277 return ""; 278 } 279 std::string Result; 280 llvm::raw_string_ostream OS(Result); 281 Rewrite.getEditBuffer(ID).write(OS); 282 OS.flush(); 283 return Result; 284 } 285 286 namespace { 287 // Represents a merged replacement, i.e. a replacement consisting of multiple 288 // overlapping replacements from 'First' and 'Second' in mergeReplacements. 289 // 290 // Position projection: 291 // Offsets and lengths of the replacements can generally refer to two different 292 // coordinate spaces. Replacements from 'First' refer to the original text 293 // whereas replacements from 'Second' refer to the text after applying 'First'. 294 // 295 // MergedReplacement always operates in the coordinate space of the original 296 // text, i.e. transforms elements from 'Second' to take into account what was 297 // changed based on the elements from 'First'. 298 // 299 // We can correctly calculate this projection as we look at the replacements in 300 // order of strictly increasing offsets. 301 // 302 // Invariants: 303 // * We always merge elements from 'First' into elements from 'Second' and vice 304 // versa. Within each set, the replacements are non-overlapping. 305 // * We only extend to the right, i.e. merge elements with strictly increasing 306 // offsets. 307 class MergedReplacement { 308 public: 309 MergedReplacement(const Replacement &R, bool MergeSecond, int D) 310 : MergeSecond(MergeSecond), Delta(D), FilePath(R.getFilePath()), 311 Offset(R.getOffset() + (MergeSecond ? 0 : Delta)), Length(R.getLength()), 312 Text(R.getReplacementText()) { 313 Delta += MergeSecond ? 0 : Text.size() - Length; 314 DeltaFirst = MergeSecond ? Text.size() - Length : 0; 315 } 316 317 // Merges the next element 'R' into this merged element. As we always merge 318 // from 'First' into 'Second' or vice versa, the MergedReplacement knows what 319 // set the next element is coming from. 320 void merge(const Replacement &R) { 321 if (MergeSecond) { 322 unsigned REnd = R.getOffset() + Delta + R.getLength(); 323 unsigned End = Offset + Text.size(); 324 if (REnd > End) { 325 Length += REnd - End; 326 MergeSecond = false; 327 } 328 StringRef TextRef = Text; 329 StringRef Head = TextRef.substr(0, R.getOffset() + Delta - Offset); 330 StringRef Tail = TextRef.substr(REnd - Offset); 331 Text = (Head + R.getReplacementText() + Tail).str(); 332 Delta += R.getReplacementText().size() - R.getLength(); 333 } else { 334 unsigned End = Offset + Length; 335 StringRef RText = R.getReplacementText(); 336 StringRef Tail = RText.substr(End - R.getOffset()); 337 Text = (Text + Tail).str(); 338 if (R.getOffset() + RText.size() > End) { 339 Length = R.getOffset() + R.getLength() - Offset; 340 MergeSecond = true; 341 } else { 342 Length += R.getLength() - RText.size(); 343 } 344 DeltaFirst += RText.size() - R.getLength(); 345 } 346 } 347 348 // Returns 'true' if 'R' starts strictly after the MergedReplacement and thus 349 // doesn't need to be merged. 350 bool endsBefore(const Replacement &R) const { 351 if (MergeSecond) 352 return Offset + Text.size() < R.getOffset() + Delta; 353 return Offset + Length < R.getOffset(); 354 } 355 356 // Returns 'true' if an element from the second set should be merged next. 357 bool mergeSecond() const { return MergeSecond; } 358 int deltaFirst() const { return DeltaFirst; } 359 Replacement asReplacement() const { return {FilePath, Offset, Length, Text}; } 360 361 private: 362 bool MergeSecond; 363 364 // Amount of characters that elements from 'Second' need to be shifted by in 365 // order to refer to the original text. 366 int Delta; 367 368 // Sum of all deltas (text-length - length) of elements from 'First' merged 369 // into this element. This is used to update 'Delta' once the 370 // MergedReplacement is completed. 371 int DeltaFirst; 372 373 // Data of the actually merged replacement. FilePath and Offset aren't changed 374 // as the element is only extended to the right. 375 const StringRef FilePath; 376 const unsigned Offset; 377 unsigned Length; 378 std::string Text; 379 }; 380 } // namespace 381 382 Replacements mergeReplacements(const Replacements &First, 383 const Replacements &Second) { 384 if (First.empty() || Second.empty()) 385 return First.empty() ? Second : First; 386 387 // Delta is the amount of characters that replacements from 'Second' need to 388 // be shifted so that their offsets refer to the original text. 389 int Delta = 0; 390 Replacements Result; 391 392 // Iterate over both sets and always add the next element (smallest total 393 // Offset) from either 'First' or 'Second'. Merge that element with 394 // subsequent replacements as long as they overlap. See more details in the 395 // comment on MergedReplacement. 396 for (auto FirstI = First.begin(), SecondI = Second.begin(); 397 FirstI != First.end() || SecondI != Second.end();) { 398 bool NextIsFirst = SecondI == Second.end() || 399 (FirstI != First.end() && 400 FirstI->getOffset() < SecondI->getOffset() + Delta); 401 MergedReplacement Merged(NextIsFirst ? *FirstI : *SecondI, NextIsFirst, 402 Delta); 403 ++(NextIsFirst ? FirstI : SecondI); 404 405 while ((Merged.mergeSecond() && SecondI != Second.end()) || 406 (!Merged.mergeSecond() && FirstI != First.end())) { 407 auto &I = Merged.mergeSecond() ? SecondI : FirstI; 408 if (Merged.endsBefore(*I)) 409 break; 410 Merged.merge(*I); 411 ++I; 412 } 413 Delta -= Merged.deltaFirst(); 414 Result.insert(Merged.asReplacement()); 415 } 416 return Result; 417 } 418 419 } // end namespace tooling 420 } // end namespace clang 421 422