Skip to content

Commit 989c73d

Browse files
committed
Ensure that buffers containing Clang swift_attr attributes are parsed as attributes
Previously, they were being parsed as top-level code, which would cause errors because there are no definitions. Introduce a new GeneratedSourceInfo kind to mark the purpose of these buffers so the parser can handle them appropriately.
1 parent 29315e6 commit 989c73d

File tree

15 files changed

+45
-5
lines changed

15 files changed

+45
-5
lines changed

include/swift/Basic/BasicBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedGeneratedSourceFileKind {
445445
BridgedGeneratedSourceFileKindReplacedFunctionBody,
446446
BridgedGeneratedSourceFileKindPrettyPrinted,
447447
BridgedGeneratedSourceFileKindDefaultArgument,
448+
BridgedGeneratedSourceFileKindAttribute,
448449

449450
BridgedGeneratedSourceFileKindNone,
450451
};

include/swift/Basic/SourceManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class GeneratedSourceInfo {
6565

6666
/// The expansion of default argument at caller side
6767
DefaultArgument,
68+
69+
/// A Swift attribute expressed in C headers.
70+
Attribute,
6871
} kind;
6972

7073
static StringRef kindToString(GeneratedSourceInfo::Kind kind) {
@@ -80,6 +83,8 @@ class GeneratedSourceInfo {
8083
return "PrettyPrinted";
8184
case DefaultArgument:
8285
return "DefaultArgument";
86+
case Attribute:
87+
return "Attribute";
8388
}
8489
llvm_unreachable("Invalid kind");
8590
}

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4535,6 +4535,7 @@ void ASTMangler::appendMacroExpansionContext(
45354535
case GeneratedSourceInfo::PrettyPrinted:
45364536
case GeneratedSourceInfo::ReplacedFunctionBody:
45374537
case GeneratedSourceInfo::DefaultArgument:
4538+
case GeneratedSourceInfo::Attribute:
45384539
return appendMacroExpansionLoc();
45394540
}
45404541

@@ -4586,6 +4587,7 @@ void ASTMangler::appendMacroExpansionContext(
45864587
case GeneratedSourceInfo::PrettyPrinted:
45874588
case GeneratedSourceInfo::ReplacedFunctionBody:
45884589
case GeneratedSourceInfo::DefaultArgument:
4590+
case GeneratedSourceInfo::Attribute:
45894591
llvm_unreachable("Exited above");
45904592
}
45914593

lib/AST/Decl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12075,6 +12075,7 @@ MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf(
1207512075
case GeneratedSourceInfo::PrettyPrinted:
1207612076
case GeneratedSourceInfo::ReplacedFunctionBody:
1207712077
case GeneratedSourceInfo::DefaultArgument:
12078+
case GeneratedSourceInfo::Attribute:
1207812079
return origDC;
1207912080
}
1208012081
}

lib/AST/DiagnosticEngine.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
13361336
#include "swift/Basic/MacroRoles.def"
13371337
case GeneratedSourceInfo::PrettyPrinted:
13381338
case GeneratedSourceInfo::DefaultArgument:
1339+
case GeneratedSourceInfo::Attribute:
13391340
fixIts = {};
13401341
break;
13411342
case GeneratedSourceInfo::ReplacedFunctionBody:
@@ -1381,6 +1382,7 @@ getGeneratedSourceInfoMacroName(const GeneratedSourceInfo &info) {
13811382
case GeneratedSourceInfo::PrettyPrinted:
13821383
case GeneratedSourceInfo::ReplacedFunctionBody:
13831384
case GeneratedSourceInfo::DefaultArgument:
1385+
case GeneratedSourceInfo::Attribute:
13841386
return DeclName();
13851387
}
13861388
}
@@ -1438,6 +1440,7 @@ DiagnosticEngine::getGeneratedSourceBufferNotes(SourceLoc loc) {
14381440
}
14391441

14401442
case GeneratedSourceInfo::PrettyPrinted:
1443+
case GeneratedSourceInfo::Attribute:
14411444
break;
14421445

14431446
case GeneratedSourceInfo::DefaultArgument:

lib/AST/Module.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ ModuleDecl::getOriginalLocation(SourceLoc loc) const {
862862
// replaced function bodies. The body is actually different code to the
863863
// original file.
864864
case GeneratedSourceInfo::PrettyPrinted:
865+
case GeneratedSourceInfo::Attribute:
865866
// No original location, return the original buffer/location
866867
return {startBufferID, startLoc};
867868
}
@@ -1148,6 +1149,7 @@ std::optional<MacroRole> SourceFile::getFulfilledMacroRole() const {
11481149
case GeneratedSourceInfo::ReplacedFunctionBody:
11491150
case GeneratedSourceInfo::PrettyPrinted:
11501151
case GeneratedSourceInfo::DefaultArgument:
1152+
case GeneratedSourceInfo::Attribute:
11511153
return std::nullopt;
11521154
}
11531155
}

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public func parseSourceFile(
122122
.preambleMacroExpansion,
123123
.replacedFunctionBody,
124124
.prettyPrinted,
125-
.defaultArgument:
125+
.defaultArgument,
126+
.attribute:
126127
parsed = Syntax(SourceFileSyntax.parse(from: &parser))
127128

128129
case .declarationMacroExpansion,

lib/Basic/SourceLoc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ StringRef SourceManager::getIdentifierForBuffer(
306306
// We only care about macros, so skip everything else.
307307
if (generatedInfo->kind == GeneratedSourceInfo::ReplacedFunctionBody ||
308308
generatedInfo->kind == GeneratedSourceInfo::PrettyPrinted ||
309-
generatedInfo->kind == GeneratedSourceInfo::DefaultArgument)
309+
generatedInfo->kind == GeneratedSourceInfo::DefaultArgument ||
310+
generatedInfo->kind == GeneratedSourceInfo::Attribute)
310311
return buffer->getBufferIdentifier();
311312

312313
if (generatedInfo->onDiskBufferCopyFileName.empty()) {
@@ -400,6 +401,7 @@ void SourceManager::setGeneratedSourceInfo(
400401
#include "swift/Basic/MacroRoles.def"
401402
case GeneratedSourceInfo::PrettyPrinted:
402403
case GeneratedSourceInfo::DefaultArgument:
404+
case GeneratedSourceInfo::Attribute:
403405
break;
404406

405407
case GeneratedSourceInfo::ReplacedFunctionBody:

lib/ClangImporter/ImportDecl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8205,6 +8205,16 @@ unsigned ClangImporter::Implementation::getClangSwiftAttrSourceBuffer(
82058205
auto &sourceMgr = SwiftContext.SourceMgr;
82068206
auto bufferID = sourceMgr.addMemBufferCopy(attributeText);
82078207
ClangSwiftAttrSourceBuffers.insert({attributeText, bufferID});
8208+
8209+
// Note that this is for an attribute.
8210+
sourceMgr.setGeneratedSourceInfo(
8211+
bufferID,
8212+
{
8213+
GeneratedSourceInfo::Attribute,
8214+
CharSourceRange(),
8215+
sourceMgr.getRangeForBuffer(bufferID)
8216+
}
8217+
);
82088218
return bufferID;
82098219
}
82108220

lib/IDE/Utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ adjustMacroExpansionWhitespace(GeneratedSourceInfo::Kind kind,
661661
case GeneratedSourceInfo::ReplacedFunctionBody:
662662
case GeneratedSourceInfo::PrettyPrinted:
663663
case GeneratedSourceInfo::DefaultArgument:
664+
case GeneratedSourceInfo::Attribute:
664665
return expandedCode;
665666
}
666667
}

0 commit comments

Comments
 (0)