@@ -83,11 +83,10 @@ DetermineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
8383} // namespace
8484
8585IncludeSorter::IncludeSorter (const SourceManager *SourceMgr,
86- const LangOptions *LangOpts, const FileID FileID,
87- StringRef FileName, IncludeStyle Style)
88- : SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style),
89- CurrentFileID (FileID), CanonicalFile(MakeCanonicalName(FileName, Style)) {
90- }
86+ const FileID FileID, StringRef FileName,
87+ IncludeStyle Style)
88+ : SourceMgr(SourceMgr), Style(Style), CurrentFileID(FileID),
89+ CanonicalFile (MakeCanonicalName(FileName, Style)) {}
9190
9291void IncludeSorter::AddInclude (StringRef FileName, bool IsAngled,
9392 SourceLocation HashLocation,
@@ -175,106 +174,6 @@ Optional<FixItHint> IncludeSorter::CreateIncludeInsertion(StringRef FileName,
175174 IncludeStmt);
176175}
177176
178- std::vector<FixItHint> IncludeSorter::GetEdits () {
179- if (SourceLocations.empty ())
180- return {};
181-
182- typedef std::map<int , std::pair<SourceRange, std::string>>
183- FileLineToSourceEditMap;
184- FileLineToSourceEditMap Edits;
185- auto SourceLocationIterator = SourceLocations.begin ();
186- auto SourceLocationIteratorEnd = SourceLocations.end ();
187-
188- // Compute the Edits that need to be done to each line to add, replace, or
189- // delete inclusions.
190- for (int IncludeKind = 0 ; IncludeKind < IK_InvalidInclude; ++IncludeKind) {
191- std::sort (IncludeBucket[IncludeKind].begin (),
192- IncludeBucket[IncludeKind].end ());
193- for (const auto &IncludeEntry : IncludeBucket[IncludeKind]) {
194- auto &Location = IncludeLocations[IncludeEntry];
195- SourceRangeVector::iterator LocationIterator = Location.begin ();
196- SourceRangeVector::iterator LocationIteratorEnd = Location.end ();
197- SourceRange FirstLocation = *LocationIterator;
198-
199- // If the first occurrence of a particular include is on the current
200- // source line we are examining, leave it alone.
201- if (FirstLocation == *SourceLocationIterator)
202- ++LocationIterator;
203-
204- // Add the deletion Edits for any (remaining) instances of this inclusion,
205- // and remove their Locations from the source Locations to be processed.
206- for (; LocationIterator != LocationIteratorEnd; ++LocationIterator) {
207- int LineNumber =
208- SourceMgr->getSpellingLineNumber (LocationIterator->getBegin ());
209- Edits[LineNumber] = std::make_pair (*LocationIterator, " " );
210- SourceLocationIteratorEnd =
211- std::remove (SourceLocationIterator, SourceLocationIteratorEnd,
212- *LocationIterator);
213- }
214-
215- if (FirstLocation == *SourceLocationIterator) {
216- // Do nothing except move to the next source Location (Location of an
217- // inclusion in the original, unchanged source file).
218- ++SourceLocationIterator;
219- continue ;
220- }
221-
222- // Add (or append to) the replacement text for this line in source file.
223- int LineNumber =
224- SourceMgr->getSpellingLineNumber (SourceLocationIterator->getBegin ());
225- if (Edits.find (LineNumber) == Edits.end ()) {
226- Edits[LineNumber].first =
227- SourceRange (SourceLocationIterator->getBegin ());
228- }
229- StringRef SourceText = Lexer::getSourceText (
230- CharSourceRange::getCharRange (FirstLocation), *SourceMgr, *LangOpts);
231- Edits[LineNumber].second .append (SourceText.data (), SourceText.size ());
232- }
233-
234- // Clear the bucket.
235- IncludeBucket[IncludeKind].clear ();
236- }
237-
238- // Go through the single-line Edits and combine them into blocks of Edits.
239- int CurrentEndLine = 0 ;
240- SourceRange CurrentRange;
241- std::string CurrentText;
242- std::vector<FixItHint> Fixes;
243- for (const auto &LineEdit : Edits) {
244- // If the current edit is on the next line after the previous edit, add it
245- // to the current block edit.
246- if (LineEdit.first == CurrentEndLine + 1 &&
247- CurrentRange.getBegin () != CurrentRange.getEnd ()) {
248- SourceRange EditRange = LineEdit.second .first ;
249- if (EditRange.getBegin () != EditRange.getEnd ()) {
250- ++CurrentEndLine;
251- CurrentRange.setEnd (EditRange.getEnd ());
252- }
253- CurrentText += LineEdit.second .second ;
254- // Otherwise report the current block edit and start a new block.
255- } else {
256- if (CurrentEndLine) {
257- Fixes.push_back (FixItHint::CreateReplacement (
258- CharSourceRange::getCharRange (CurrentRange), CurrentText));
259- }
260-
261- CurrentEndLine = LineEdit.first ;
262- CurrentRange = LineEdit.second .first ;
263- CurrentText = LineEdit.second .second ;
264- }
265- }
266- // Finally, report the current block edit if there is one.
267- if (CurrentEndLine) {
268- Fixes.push_back (FixItHint::CreateReplacement (
269- CharSourceRange::getCharRange (CurrentRange), CurrentText));
270- }
271-
272- // Reset the remaining internal state.
273- SourceLocations.clear ();
274- IncludeLocations.clear ();
275- return Fixes;
276- }
277-
278177llvm::ArrayRef<std::pair<StringRef, IncludeSorter::IncludeStyle>>
279178IncludeSorter::getMapping () {
280179 static constexpr std::pair<StringRef, IncludeSorter::IncludeStyle> Mapping[] =
0 commit comments