Skip to content

Commit 8168669

Browse files
committed
[ClangImporter] Diagnose bad swift_name attributes better
1. Set the diagnostic location to where the attribute was written (or to the Clang decl's source, if the attribute came from API notes) 2. Add a note to contact the owners of the framework to make it clear that the client of the framework didn't do anything wrong. rdar://problem/52736145
1 parent e364552 commit 8168669

File tree

8 files changed

+59
-18
lines changed

8 files changed

+59
-18
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ WARNING(unresolvable_clang_decl,none,
8686
"imported declaration '%0' could not be mapped to '%1'",
8787
(StringRef, StringRef))
8888

89+
NOTE(unresolvable_clang_decl_is_a_framework_bug,none,
90+
"please report this issue to the owners of '%0'",
91+
(StringRef))
92+
8993
WARNING(implicit_bridging_header_imported_from_module,none,
9094
"implicit import of bridging header '%0' via module %1 "
9195
"is deprecated and will be removed in a later version of Swift",

lib/ClangImporter/ClangImporter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ ClangImporter::create(ASTContext &ctx, const ClangImporterOptions &importerOpts,
10001000
std::make_shared<SwiftNameLookupExtension>(
10011001
importer->Impl.BridgingHeaderLookupTable,
10021002
importer->Impl.LookupTables, importer->Impl.SwiftContext,
1003+
importer->Impl.getBufferImporterForDiagnostics(),
10031004
importer->Impl.platformAvailability,
10041005
importer->Impl.InferImportAsMember));
10051006

@@ -1330,7 +1331,8 @@ bool ClangImporter::Implementation::importHeader(
13301331
}
13311332

13321333
// Finalize the lookup table, which may fail.
1333-
finalizeLookupTable(*BridgingHeaderLookupTable, getNameImporter());
1334+
finalizeLookupTable(*BridgingHeaderLookupTable, getNameImporter(),
1335+
getBufferImporterForDiagnostics());
13341336

13351337
// FIXME: What do we do if there was already an error?
13361338
if (!hadError && clangDiags.hasErrorOccurred()) {
@@ -1844,7 +1846,6 @@ ClangImporter::Implementation::Implementation(
18441846
ASTContext &ctx, const ClangImporterOptions &opts,
18451847
DWARFImporterDelegate *dwarfImporterDelegate)
18461848
: SwiftContext(ctx),
1847-
BuffersForDiagnostics(ctx.SourceMgr),
18481849
ImportForwardDeclarations(opts.ImportForwardDeclarations),
18491850
InferImportAsMember(opts.InferImportAsMember),
18501851
DisableSwiftBridgeAttr(opts.DisableSwiftBridgeAttr),
@@ -1853,6 +1854,7 @@ ClangImporter::Implementation::Implementation(
18531854
IsReadingBridgingPCH(false),
18541855
CurrentVersion(ImportNameVersion::fromOptions(ctx.LangOpts)),
18551856
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),
1857+
BuffersForDiagnostics(ctx.SourceMgr),
18561858
platformAvailability(ctx.LangOpts), nameImporter(),
18571859
DWARFImporter(dwarfImporterDelegate) {}
18581860

lib/ClangImporter/ImporterImpl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,15 +1380,18 @@ class SwiftNameLookupExtension : public clang::ModuleFileExtension {
13801380
std::unique_ptr<SwiftLookupTable> &pchLookupTable;
13811381
LookupTableMap &lookupTables;
13821382
ASTContext &swiftCtx;
1383+
ClangSourceBufferImporter &buffersForDiagnostics;
13831384
const PlatformAvailability &availability;
13841385
const bool inferImportAsMember;
13851386

13861387
public:
13871388
SwiftNameLookupExtension(std::unique_ptr<SwiftLookupTable> &pchLookupTable,
13881389
LookupTableMap &tables, ASTContext &ctx,
1390+
ClangSourceBufferImporter &buffersForDiagnostics,
13891391
const PlatformAvailability &avail, bool inferIAM)
13901392
: pchLookupTable(pchLookupTable), lookupTables(tables), swiftCtx(ctx),
1391-
availability(avail), inferImportAsMember(inferIAM) {}
1393+
buffersForDiagnostics(buffersForDiagnostics), availability(avail),
1394+
inferImportAsMember(inferIAM) {}
13921395

13931396
clang::ModuleFileExtensionMetadata getExtensionMetadata() const override;
13941397
llvm::hash_code hashExtension(llvm::hash_code code) const override;

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,19 @@ class SwiftLookupTableWriter : public clang::ModuleFileExtensionWriter {
6767
clang::ASTWriter &Writer;
6868

6969
ASTContext &swiftCtx;
70+
importer::ClangSourceBufferImporter &buffersForDiagnostics;
7071
const PlatformAvailability &availability;
7172
const bool inferImportAsMember;
7273

7374
public:
74-
SwiftLookupTableWriter(clang::ModuleFileExtension *extension,
75-
clang::ASTWriter &writer, ASTContext &ctx,
76-
const PlatformAvailability &avail, bool inferIAM)
77-
: ModuleFileExtensionWriter(extension), Writer(writer), swiftCtx(ctx),
78-
availability(avail), inferImportAsMember(inferIAM) {}
75+
SwiftLookupTableWriter(
76+
clang::ModuleFileExtension *extension, clang::ASTWriter &writer,
77+
ASTContext &ctx,
78+
importer::ClangSourceBufferImporter &buffersForDiagnostics,
79+
const PlatformAvailability &avail, bool inferIAM)
80+
: ModuleFileExtensionWriter(extension), Writer(writer), swiftCtx(ctx),
81+
buffersForDiagnostics(buffersForDiagnostics), availability(avail),
82+
inferImportAsMember(inferIAM) {}
7983

8084
void writeExtensionContents(clang::Sema &sema,
8185
llvm::BitstreamWriter &stream) override;
@@ -1788,8 +1792,9 @@ void importer::addMacrosToLookupTable(SwiftLookupTable &table,
17881792
}
17891793
}
17901794

1791-
void importer::finalizeLookupTable(SwiftLookupTable &table,
1792-
NameImporter &nameImporter) {
1795+
void importer::finalizeLookupTable(
1796+
SwiftLookupTable &table, NameImporter &nameImporter,
1797+
ClangSourceBufferImporter &buffersForDiagnostics) {
17931798
// Resolve any unresolved entries.
17941799
SmallVector<SwiftLookupTable::SingleEntry, 4> unresolved;
17951800
if (table.resolveUnresolvedEntries(unresolved)) {
@@ -1799,9 +1804,22 @@ void importer::finalizeLookupTable(SwiftLookupTable &table,
17991804
auto swiftName = decl->getAttr<clang::SwiftNameAttr>();
18001805

18011806
if (swiftName) {
1802-
nameImporter.getContext().Diags.diagnose(
1803-
SourceLoc(), diag::unresolvable_clang_decl, decl->getNameAsString(),
1804-
swiftName->getName());
1807+
clang::SourceLocation diagLoc = swiftName->getLocation();
1808+
if (!diagLoc.isValid())
1809+
diagLoc = decl->getLocation();
1810+
SourceLoc swiftSourceLoc = buffersForDiagnostics.resolveSourceLocation(
1811+
nameImporter.getClangContext().getSourceManager(), diagLoc);
1812+
1813+
DiagnosticEngine &swiftDiags = nameImporter.getContext().Diags;
1814+
swiftDiags.diagnose(swiftSourceLoc, diag::unresolvable_clang_decl,
1815+
decl->getNameAsString(), swiftName->getName());
1816+
StringRef moduleName =
1817+
nameImporter.getClangContext().getLangOpts().CurrentModule;
1818+
if (!moduleName.empty()) {
1819+
swiftDiags.diagnose(swiftSourceLoc,
1820+
diag::unresolvable_clang_decl_is_a_framework_bug,
1821+
moduleName);
1822+
}
18051823
}
18061824
}
18071825
}
@@ -1842,14 +1860,15 @@ void SwiftLookupTableWriter::populateTable(SwiftLookupTable &table,
18421860
addMacrosToLookupTable(table, nameImporter);
18431861

18441862
// Finalize the lookup table, which may fail.
1845-
finalizeLookupTable(table, nameImporter);
1863+
finalizeLookupTable(table, nameImporter, buffersForDiagnostics);
18461864
};
18471865

18481866
std::unique_ptr<clang::ModuleFileExtensionWriter>
18491867
SwiftNameLookupExtension::createExtensionWriter(clang::ASTWriter &writer) {
1850-
return std::unique_ptr<clang::ModuleFileExtensionWriter>(
1851-
new SwiftLookupTableWriter(this, writer, swiftCtx, availability,
1852-
inferImportAsMember));
1868+
return llvm::make_unique<SwiftLookupTableWriter>(this, writer, swiftCtx,
1869+
buffersForDiagnostics,
1870+
availability,
1871+
inferImportAsMember);
18531872
}
18541873

18551874
std::unique_ptr<clang::ModuleFileExtensionReader>

lib/ClangImporter/SwiftLookupTable.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ class SwiftLookupTable {
520520
};
521521

522522
namespace importer {
523+
class ClangSourceBufferImporter;
523524
class NameImporter;
524525

525526
/// Add the given named declaration as an entry to the given Swift name
@@ -533,7 +534,8 @@ void addMacrosToLookupTable(SwiftLookupTable &table, NameImporter &);
533534

534535
/// Finalize a lookup table, handling any as-yet-unresolved entries
535536
/// and emitting diagnostics if necessary.
536-
void finalizeLookupTable(SwiftLookupTable &table, NameImporter &);
537+
void finalizeLookupTable(SwiftLookupTable &table, NameImporter &,
538+
ClangSourceBufferImporter &buffersForDiagnostics);
537539
}
538540
}
539541

test/ClangImporter/Inputs/custom-modules/ImportAsMember.apinotes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Functions:
1212
Typedefs:
1313
- Name: IAMStruct1APINoteType
1414
SwiftName: Struct1.NewApiNoteType
15+
- Name: IAMBadInnerIntAPINotes
16+
SwiftName: IAMNonexistent.Inner2
1517
SwiftVersions:
1618
- Version: 3
1719
Globals:

test/ClangImporter/Inputs/custom-modules/ImportAsMember.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@ struct IAMMultipleNested {
6868
typedef int MNInnerInt __attribute__((swift_name("IAMMultipleNested.Inner")));
6969
typedef float MNInnerFloat __attribute__((swift_name("IAMMultipleNested.Inner")));
7070

71+
typedef int IAMBadInnerInt
72+
__attribute__((swift_name("IAMNonexistent.Inner")));
73+
// CHECK: ImportAsMember.h:[[@LINE-1]]:{{[0-9]+}}: warning: imported declaration 'IAMBadInnerInt' could not be mapped to 'IAMNonexistent.Inner'
74+
// CHECK: ImportAsMember.h:[[@LINE-2]]:{{[0-9]+}}: note: please report this issue to the owners of 'ImportAsMember'
75+
typedef int IAMBadInnerIntAPINotes;
76+
// CHECK: ImportAsMember.h:[[@LINE-1]]:{{[0-9]+}}: warning: imported declaration 'IAMBadInnerIntAPINotes' could not be mapped to 'IAMNonexistent.Inner2'
77+
// CHECK: ImportAsMember.h:[[@LINE-2]]:{{[0-9]+}}: note: please report this issue to the owners of 'ImportAsMember'
78+
7179
#endif // IMPORT_AS_MEMBER_H

test/ClangImporter/import-as-member.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -typecheck -F %S/Inputs/frameworks -I %S/Inputs/custom-modules %s 2>&1 | %FileCheck %S/Inputs/custom-modules/ImportAsMember.h
12
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -typecheck -F %S/Inputs/frameworks -I %S/Inputs/custom-modules %s -verify
23

34
import ImportAsMember

0 commit comments

Comments
 (0)