Skip to content

Commit aa4c548

Browse files
committed
Clang importer: switch swift_attr attribute text cache over to be module-sensitive
We need different buffers for each imported module that has swift_attr attributes, so cache them appropriately.
1 parent 989c73d commit aa4c548

File tree

6 files changed

+32
-41
lines changed

6 files changed

+32
-41
lines changed

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,11 +1440,11 @@ DiagnosticEngine::getGeneratedSourceBufferNotes(SourceLoc loc) {
14401440
}
14411441

14421442
case GeneratedSourceInfo::PrettyPrinted:
1443-
case GeneratedSourceInfo::Attribute:
14441443
break;
14451444

14461445
case GeneratedSourceInfo::DefaultArgument:
14471446
case GeneratedSourceInfo::ReplacedFunctionBody:
1447+
case GeneratedSourceInfo::Attribute:
14481448
return childNotes;
14491449
}
14501450

lib/Basic/SourceLoc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ StringRef SourceManager::getIdentifierForBuffer(
303303
if (ForceGeneratedSourceToDisk) {
304304
if (const GeneratedSourceInfo *generatedInfo =
305305
getGeneratedSourceInfo(bufferID)) {
306-
// We only care about macros, so skip everything else.
306+
// We only care about macro expansion buffers, so skip everything else.
307307
if (generatedInfo->kind == GeneratedSourceInfo::ReplacedFunctionBody ||
308308
generatedInfo->kind == GeneratedSourceInfo::PrettyPrinted ||
309309
generatedInfo->kind == GeneratedSourceInfo::DefaultArgument ||

lib/ClangImporter/ImportDecl.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8194,39 +8194,38 @@ bool importer::hasSameUnderlyingType(const clang::Type *a,
81948194
return a == b->getTypeForDecl();
81958195
}
81968196

8197-
unsigned ClangImporter::Implementation::getClangSwiftAttrSourceBuffer(
8198-
StringRef attributeText) {
8199-
auto known = ClangSwiftAttrSourceBuffers.find(attributeText);
8200-
if (known != ClangSwiftAttrSourceBuffers.end())
8201-
return known->second;
8197+
SourceFile &ClangImporter::Implementation::getClangSwiftAttrSourceFile(
8198+
ModuleDecl &module,
8199+
StringRef attributeText
8200+
) {
8201+
auto &sourceFiles = ClangSwiftAttrSourceFiles[attributeText];
82028202

8203-
// Create a new buffer with a copy of the attribute text, so we don't need to
8204-
// rely on Clang keeping it around.
8203+
// Check whether we've already created a source file.
8204+
for (auto sourceFile : sourceFiles) {
8205+
if (sourceFile->getParentModule() == &module)
8206+
return *sourceFile;
8207+
}
8208+
8209+
// Create a new buffer with a copy of the attribute text,
8210+
// so we don't need to rely on Clang keeping it around.
82058211
auto &sourceMgr = SwiftContext.SourceMgr;
82068212
auto bufferID = sourceMgr.addMemBufferCopy(attributeText);
8207-
ClangSwiftAttrSourceBuffers.insert({attributeText, bufferID});
82088213

82098214
// Note that this is for an attribute.
82108215
sourceMgr.setGeneratedSourceInfo(
82118216
bufferID,
82128217
{
82138218
GeneratedSourceInfo::Attribute,
82148219
CharSourceRange(),
8215-
sourceMgr.getRangeForBuffer(bufferID)
8220+
sourceMgr.getRangeForBuffer(bufferID),
8221+
&module
82168222
}
82178223
);
8218-
return bufferID;
8219-
}
8220-
8221-
SourceFile &ClangImporter::Implementation::getClangSwiftAttrSourceFile(
8222-
ModuleDecl &module, unsigned bufferID) {
8223-
auto known = ClangSwiftAttrSourceFiles.find(&module);
8224-
if (known != ClangSwiftAttrSourceFiles.end())
8225-
return *known->second;
82268224

8225+
// Create the source file.
82278226
auto sourceFile = new (SwiftContext)
82288227
SourceFile(module, SourceFileKind::Library, bufferID);
8229-
ClangSwiftAttrSourceFiles.insert({&module, sourceFile});
8228+
sourceFiles.push_back(sourceFile);
82308229

82318230
return *sourceFile;
82328231
}
@@ -8402,17 +8401,15 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
84028401
continue;
84038402
}
84048403

8405-
// Dig out a buffer with the attribute text.
8406-
unsigned bufferID = getClangSwiftAttrSourceBuffer(
8407-
swiftAttr->getAttribute());
8408-
84098404
// Dig out a source file we can use for parsing.
84108405
auto &sourceFile = getClangSwiftAttrSourceFile(
8411-
*MappedDecl->getDeclContext()->getParentModule(), bufferID);
8406+
*MappedDecl->getDeclContext()->getParentModule(),
8407+
swiftAttr->getAttribute());
84128408

84138409
// Spin up a parser.
84148410
swift::Parser parser(
8415-
bufferID, sourceFile, &SwiftContext.Diags, nullptr, nullptr);
8411+
sourceFile.getBufferID(), sourceFile, &SwiftContext.Diags,
8412+
nullptr, nullptr);
84168413
// Prime the lexer.
84178414
parser.consumeTokenWithoutFeedingReceiver();
84188415

lib/ClangImporter/ImporterImpl.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,11 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
538538
/// Clang arguments used to create the Clang invocation.
539539
std::vector<std::string> ClangArgs;
540540

541-
/// Mapping from Clang swift_attr attribute text to the Swift source buffer
542-
/// IDs that contain that attribute text. These are re-used when parsing the
543-
/// Swift attributes on import.
544-
llvm::StringMap<unsigned> ClangSwiftAttrSourceBuffers;
545-
546-
/// Mapping from modules in which a Clang swift_attr attribute occurs, to be
547-
/// used when parsing the attribute text.
548-
llvm::SmallDenseMap<ModuleDecl *, SourceFile *> ClangSwiftAttrSourceFiles;
541+
/// Mapping from Clang swift_attr attribute text to the Swift source file(s)
542+
/// that contain that attribute text.
543+
///
544+
/// These are re-used when parsing the Swift attributes on import.
545+
llvm::StringMap<llvm::TinyPtrVector<SourceFile *>> ClangSwiftAttrSourceFiles;
549546

550547
public:
551548
/// The Swift lookup table for the bridging header.
@@ -1058,13 +1055,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
10581055
/// Map a Clang identifier name to its imported Swift equivalent.
10591056
StringRef getSwiftNameFromClangName(StringRef name);
10601057

1061-
/// Retrieve the Swift source buffer ID that corresponds to the given
1062-
/// swift_attr attribute text, creating one if necessary.
1063-
unsigned getClangSwiftAttrSourceBuffer(StringRef attributeText);
1064-
10651058
/// Retrieve the placeholder source file for use in parsing Swift attributes
10661059
/// in the given module.
1067-
SourceFile &getClangSwiftAttrSourceFile(ModuleDecl &module, unsigned bufferID);
1060+
SourceFile &getClangSwiftAttrSourceFile(ModuleDecl &module, StringRef attributeText);
10681061

10691062
/// Utility function to import Clang attributes from a source Swift decl to
10701063
/// synthesized Swift decl.

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
296296
if (!ForceGeneratedSourceToDisk) {
297297
auto BufferID = SM.findBufferContainingLoc(SL);
298298
if (auto generatedInfo = SM.getGeneratedSourceInfo(BufferID)) {
299-
// We only care about macros, so skip everything else.
299+
// We only care about macro expansion buffers,
300+
// so skip everything else.
300301
if (generatedInfo->kind != GeneratedSourceInfo::ReplacedFunctionBody &&
301302
generatedInfo->kind != GeneratedSourceInfo::PrettyPrinted &&
302303
generatedInfo->kind != GeneratedSourceInfo::DefaultArgument &&

test/Macros/print_clang_expand_on_imported.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Build the macro library to give us access to AddAsync.
88
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/macro_library.swiftmodule %S/Inputs/macro_library.swift -module-name macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)
99

10-
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -print-module -print-implicit-attrs -source-filename %s -module-to-print=CompletionHandlerGlobals -I %t -function-definitions=false -load-plugin-library %t/%target-library-name(MacroDefinition) -import-module macro_library -enable-experimental-feature MacrosOnImports > %t/imported.printed.txt 2> %t/stderr.log
10+
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -typecheck -print-module -print-implicit-attrs -source-filename %s -module-to-print=CompletionHandlerGlobals -I %t -function-definitions=false -load-plugin-library %t/%target-library-name(MacroDefinition) -import-module macro_library -enable-experimental-feature MacrosOnImports > %t/imported.printed.txt 2> %t/stderr.log
1111
// RUN: %FileCheck -input-file %t/imported.printed.txt %s
1212
// RUN: echo "" >> %t/stderr.log
1313
// RUN: %FileCheck -check-prefix DIAGS -input-file %t/stderr.log %s

0 commit comments

Comments
 (0)