Skip to content

Commit f802b67

Browse files
authored
Merge pull request swiftlang#77580 from DougGregor/swift-attr-parsing-fixes
Ensure that buffers containing Clang swift_attr attributes are parsed as attributes
2 parents 6aa9d43 + 75aae22 commit f802b67

34 files changed

+238
-71
lines changed

include/swift/AST/ASTScope.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,8 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
808808
protected:
809809
void printSpecifics(llvm::raw_ostream &out) const override;
810810

811+
bool lookupLocalsOrMembers(DeclConsumer) const override;
812+
811813
public:
812814
Decl *getDecl() const { return decl; }
813815

include/swift/AST/NameLookup.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,15 @@ void lookupVisibleMemberDecls(VisibleDeclConsumer &Consumer,
528528
bool includeProtocolExtensionMembers,
529529
GenericSignature genericSig = GenericSignature());
530530

531+
/// Determine the module-scope context from which lookup should proceed.
532+
///
533+
/// In the common case, module-scope context is the source file in which
534+
/// the declaration context is nested. However, when declaration context is
535+
/// part of an imported Clang declaration context, it won't be nested within a
536+
/// source file. Rather, the source file will be on the side, and will be
537+
/// provided here because it contains information about the available imports.
538+
DeclContext *getModuleScopeLookupContext(DeclContext *dc);
539+
531540
namespace namelookup {
532541

533542
/// Add semantic members to \p type before attempting a semantic lookup.

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
}

include/swift/Subsystems.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ namespace swift {
120120
/// This walks the AST to resolve imports.
121121
void performImportResolution(SourceFile &SF);
122122

123+
/// Resolve imports for a source file generated to adapt a given
124+
/// Clang module.
125+
void performImportResolutionForClangMacroBuffer(
126+
SourceFile &SF, ModuleDecl *clangModule
127+
);
128+
123129
/// Once type-checking is complete, this instruments code with calls to an
124130
/// intrinsic that record the expected values of local variables so they can
125131
/// be compared against the results from the debugger.

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/ASTScopeLookup.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "swift/AST/Stmt.h"
3131
#include "swift/AST/TypeRepr.h"
3232
#include "swift/Basic/STLExtras.h"
33+
#include "swift/ClangImporter/ClangModule.h"
3334
#include "swift/Parse/Lexer.h"
3435
#include "llvm/Support/Compiler.h"
3536

@@ -264,6 +265,20 @@ bool ASTScopeImpl::lookupLocalsOrMembers(DeclConsumer) const {
264265
return false; // many kinds of scopes have none
265266
}
266267

268+
bool AbstractFunctionDeclScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
269+
// Special case: if we're within a function inside a type context, but the
270+
// parent context is within a Clang module unit, we need to make sure to
271+
// look for members in it.
272+
auto dc = decl->getDeclContext();
273+
if (!dc->isTypeContext())
274+
return false;
275+
276+
if (!isa<ClangModuleUnit>(dc->getModuleScopeContext()))
277+
return false;
278+
279+
return consumer.lookInMembers(cast<GenericContext>(dc->getAsDecl()));
280+
}
281+
267282
bool GenericTypeOrExtensionScope::lookupLocalsOrMembers(
268283
ASTScopeImpl::DeclConsumer consumer) const {
269284
return portion->lookupMembersOf(this, consumer);

lib/AST/AvailabilityScope.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ AvailabilityScope::createForSourceFile(SourceFile *SF,
5555
case SourceFileKind::DefaultArgument: {
5656
// Look up the parent context in the enclosing file that this file's
5757
// root context should be nested under.
58-
if (auto parentScope =
59-
SF->getEnclosingSourceFile()->getAvailabilityScope()) {
58+
auto enclosingSF = SF->getEnclosingSourceFile();
59+
if (!enclosingSF)
60+
break;
61+
if (auto parentScope = enclosingSF->getAvailabilityScope()) {
6062
auto charRange = Ctx.SourceMgr.getRangeForBuffer(SF->getBufferID());
6163
range = SourceRange(charRange.getStart(), charRange.getEnd());
6264
auto originalNode = SF->getNodeInEnclosingSourceFile();

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
}
@@ -1442,6 +1444,7 @@ DiagnosticEngine::getGeneratedSourceBufferNotes(SourceLoc loc) {
14421444

14431445
case GeneratedSourceInfo::DefaultArgument:
14441446
case GeneratedSourceInfo::ReplacedFunctionBody:
1447+
case GeneratedSourceInfo::Attribute:
14451448
return childNotes;
14461449
}
14471450

0 commit comments

Comments
 (0)