Skip to content

Commit d258fa7

Browse files
committed
AST: Simplify the interface of DiagnosticEngine::getBestAddImportFixItLoc().
For clarity, it should just take a `SourceFile`.
1 parent 6ea2dd8 commit d258fa7

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,6 @@ namespace swift {
475475

476476
static const char *fixItStringFor(const FixItID id);
477477

478-
/// Get the best location where an 'import' fixit might be offered.
479-
SourceLoc getBestAddImportFixItLoc(const Decl *Member) const;
480-
481478
/// Add a token-based replacement fix-it to the currently-active
482479
/// diagnostic.
483480
template <typename... ArgTypes>
@@ -1195,11 +1192,7 @@ namespace swift {
11951192
SourceLoc getDefaultDiagnosticLoc() const {
11961193
return bufferIndirectlyCausingDiagnostic;
11971194
}
1198-
SourceLoc getBestAddImportFixItLoc(const Decl *Member,
1199-
SourceFile *sourceFile) const;
1200-
SourceLoc getBestAddImportFixItLoc(const Decl *Member) const {
1201-
return getBestAddImportFixItLoc(Member, nullptr);
1202-
}
1195+
SourceLoc getBestAddImportFixItLoc(SourceFile *sf) const;
12031196
};
12041197

12051198
inline SourceManager &InFlightDiagnostic::getSourceManager() {

lib/AST/DiagnosticEngine.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,12 @@ InFlightDiagnostic::fixItReplaceChars(SourceLoc Start, SourceLoc End,
313313
return *this;
314314
}
315315

316-
SourceLoc
317-
DiagnosticEngine::getBestAddImportFixItLoc(const Decl *Member,
318-
SourceFile *sourceFile) const {
316+
SourceLoc DiagnosticEngine::getBestAddImportFixItLoc(SourceFile *SF) const {
319317
auto &SM = SourceMgr;
320318

321319
SourceLoc bestLoc;
322-
323-
auto SF =
324-
sourceFile ? sourceFile : Member->getDeclContext()->getParentSourceFile();
325-
if (!SF) {
320+
if (!SF)
326321
return bestLoc;
327-
}
328322

329323
for (auto item : SF->getTopLevelItems()) {
330324
// If we found an import declaration, we want to insert after it.
@@ -356,19 +350,21 @@ DiagnosticEngine::getBestAddImportFixItLoc(const Decl *Member,
356350

357351
InFlightDiagnostic &InFlightDiagnostic::fixItAddImport(StringRef ModuleName) {
358352
assert(IsActive && "Cannot modify an inactive diagnostic");
359-
auto Member = Engine->ActiveDiagnostic->getDecl();
360-
SourceLoc bestLoc = Engine->getBestAddImportFixItLoc(Member);
353+
auto decl = Engine->ActiveDiagnostic->getDecl();
354+
if (!decl)
355+
return *this;
361356

362-
if (bestLoc.isValid()) {
363-
llvm::SmallString<64> importText;
364-
importText += "import ";
365-
importText += ModuleName;
366-
importText += "\n";
357+
auto bestLoc = Engine->getBestAddImportFixItLoc(
358+
decl->getDeclContext()->getOutermostParentSourceFile());
359+
if (!bestLoc.isValid())
360+
return *this;
367361

368-
return fixItInsert(bestLoc, importText);
369-
}
362+
llvm::SmallString<64> importText;
363+
importText += "import ";
364+
importText += ModuleName;
365+
importText += "\n";
370366

371-
return *this;
367+
return fixItInsert(bestLoc, importText);
372368
}
373369

374370
InFlightDiagnostic &

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ diagnoseAndFixMissingImportForMember(const ValueDecl *decl, SourceFile *sf,
990990
diagnoseMissingImportsForMember(decl, modulesToImport, sf, loc);
991991

992992
auto &ctx = sf->getASTContext();
993-
SourceLoc bestLoc = ctx.Diags.getBestAddImportFixItLoc(decl, sf);
993+
SourceLoc bestLoc = ctx.Diags.getBestAddImportFixItLoc(sf);
994994
if (!bestLoc.isValid())
995995
return;
996996

0 commit comments

Comments
 (0)