Skip to content

Commit c74057a

Browse files
committed
[TBDGen] Factor out some visiting logic
Factor out the visiting logic for `FileUnit` and `TBDGenDescriptor` onto the `TBDGenVisitor`.
1 parent 182683a commit c74057a

File tree

2 files changed

+65
-51
lines changed

2 files changed

+65
-51
lines changed

lib/TBDGen/TBDGen.cpp

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,63 @@ void TBDGenVisitor::visit(Decl *D) {
10531053
ASTVisitor::visit(D);
10541054
}
10551055

1056+
static bool hasLinkerDirective(Decl *D) {
1057+
return !getAllMovedPlatformVersions(D).empty();
1058+
}
1059+
1060+
void TBDGenVisitor::visitFile(FileUnit *file) {
1061+
if (file == SwiftModule->getFiles()[0])
1062+
addFirstFileSymbols();
1063+
1064+
SmallVector<Decl *, 16> decls;
1065+
file->getTopLevelDecls(decls);
1066+
1067+
addMainIfNecessary(file);
1068+
1069+
for (auto d : decls) {
1070+
if (Opts.LinkerDirectivesOnly && !hasLinkerDirective(d))
1071+
continue;
1072+
visit(d);
1073+
}
1074+
}
1075+
1076+
void TBDGenVisitor::visit(const TBDGenDescriptor &desc) {
1077+
if (auto *singleFile = desc.getSingleFile()) {
1078+
assert(SwiftModule == singleFile->getParentModule() &&
1079+
"mismatched file and module");
1080+
visitFile(singleFile);
1081+
1082+
// Visit synthesized file, if it exists.
1083+
if (auto *SF = dyn_cast<SourceFile>(singleFile)) {
1084+
if (auto *synthesizedFile = SF->getSynthesizedFile())
1085+
visitFile(synthesizedFile);
1086+
}
1087+
return;
1088+
}
1089+
1090+
llvm::SmallVector<ModuleDecl*, 4> Modules;
1091+
Modules.push_back(SwiftModule);
1092+
1093+
auto &ctx = SwiftModule->getASTContext();
1094+
for (auto Name: Opts.embedSymbolsFromModules) {
1095+
if (auto *MD = ctx.getModuleByName(Name)) {
1096+
// If it is a clang module, the symbols should be collected by TAPI.
1097+
if (!MD->isNonSwiftModule()) {
1098+
Modules.push_back(MD);
1099+
continue;
1100+
}
1101+
}
1102+
// Diagnose module name that cannot be found
1103+
ctx.Diags.diagnose(SourceLoc(), diag::unknown_swift_module_name, Name);
1104+
}
1105+
// Collect symbols in each module.
1106+
llvm::for_each(Modules, [&](ModuleDecl *M) {
1107+
for (auto *file : M->getFiles()) {
1108+
visitFile(file);
1109+
}
1110+
});
1111+
}
1112+
10561113
/// The kind of version being parsed, used for diagnostics.
10571114
/// Note: Must match the order in DiagnosticsFrontend.def
10581115
enum DylibVersionKind_t: unsigned {
@@ -1100,10 +1157,6 @@ static bool isApplicationExtensionSafe(const LangOptions &LangOpts) {
11001157
llvm::sys::Process::GetEnv("LD_APPLICATION_EXTENSION_SAFE");
11011158
}
11021159

1103-
static bool hasLinkerDirective(Decl *D) {
1104-
return !getAllMovedPlatformVersions(D).empty();
1105-
}
1106-
11071160
TBDFileAndSymbols
11081161
GenerateTBDRequest::evaluate(Evaluator &evaluator,
11091162
TBDGenDescriptor desc) const {
@@ -1144,53 +1197,7 @@ GenerateTBDRequest::evaluate(Evaluator &evaluator,
11441197
auto *clang = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
11451198
TBDGenVisitor visitor(file, {target}, clang->getTargetInfo().getDataLayout(),
11461199
linkInfo, M, opts);
1147-
1148-
auto visitFile = [&](FileUnit *file) {
1149-
if (file == M->getFiles()[0]) {
1150-
visitor.addFirstFileSymbols();
1151-
}
1152-
1153-
SmallVector<Decl *, 16> decls;
1154-
file->getTopLevelDecls(decls);
1155-
1156-
visitor.addMainIfNecessary(file);
1157-
1158-
for (auto d : decls) {
1159-
if (opts.LinkerDirectivesOnly && !hasLinkerDirective(d))
1160-
continue;
1161-
visitor.visit(d);
1162-
}
1163-
};
1164-
1165-
if (auto *singleFile = desc.getSingleFile()) {
1166-
assert(M == singleFile->getParentModule() && "mismatched file and module");
1167-
visitFile(singleFile);
1168-
// Visit synthesized file, if it exists.
1169-
if (auto *SF = dyn_cast<SourceFile>(singleFile))
1170-
if (auto *synthesizedFile = SF->getSynthesizedFile())
1171-
visitFile(synthesizedFile);
1172-
} else {
1173-
llvm::SmallVector<ModuleDecl*, 4> Modules;
1174-
Modules.push_back(M);
1175-
for (auto Name: opts.embedSymbolsFromModules) {
1176-
if (auto *MD = ctx.getModuleByName(Name)) {
1177-
// If it is a clang module, the symbols should be collected by TAPI.
1178-
if (!MD->isNonSwiftModule()) {
1179-
Modules.push_back(MD);
1180-
continue;
1181-
}
1182-
}
1183-
// Diagnose module name that cannot be found
1184-
ctx.Diags.diagnose(SourceLoc(), diag::unknown_swift_module_name, Name);
1185-
}
1186-
// Collect symbols in each module.
1187-
llvm::for_each(Modules, [&](ModuleDecl *M) {
1188-
for (auto *file : M->getFiles()) {
1189-
visitFile(file);
1190-
}
1191-
});
1192-
}
1193-
1200+
visitor.visit(desc);
11941201
return std::make_pair(std::move(file), std::move(visitor.StringSymbols));
11951202
}
11961203

lib/TBDGen/TBDGenVisitor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class DataLayout;
4040

4141
namespace swift {
4242

43+
class TBDGenDescriptor;
4344
struct TBDGenOptions;
4445

4546
namespace tbdgen {
@@ -195,6 +196,12 @@ class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
195196
void visitDecl(Decl *D) {}
196197

197198
void visit(Decl *D);
199+
200+
/// Visit the symbols in a given file unit.
201+
void visitFile(FileUnit *file);
202+
203+
/// Visit the files specified by a given TBDGenDescriptor.
204+
void visit(const TBDGenDescriptor &desc);
198205
};
199206
} // end namespace tbdgen
200207
} // end namespace swift

0 commit comments

Comments
 (0)