Skip to content

Commit 773eec1

Browse files
authored
Merge pull request #33683 from nkcsgexi/clang-importer-options-refactor
ClangImporter: refactor ClangImporterOptions to be ASTContext-owned. NFC
2 parents 2330e61 + c403b14 commit 773eec1

File tree

19 files changed

+184
-215
lines changed

19 files changed

+184
-215
lines changed

include/swift/AST/ASTContext.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ class ASTContext final {
214214
void operator=(const ASTContext&) = delete;
215215

216216
ASTContext(LangOptions &langOpts, TypeCheckerOptions &typeckOpts,
217-
SearchPathOptions &SearchPathOpts, SourceManager &SourceMgr,
217+
SearchPathOptions &SearchPathOpts,
218+
ClangImporterOptions &ClangImporterOpts,
219+
SourceManager &SourceMgr,
218220
DiagnosticEngine &Diags);
219221

220222
public:
@@ -228,6 +230,7 @@ class ASTContext final {
228230

229231
static ASTContext *get(LangOptions &langOpts, TypeCheckerOptions &typeckOpts,
230232
SearchPathOptions &SearchPathOpts,
233+
ClangImporterOptions &ClangImporterOpts,
231234
SourceManager &SourceMgr, DiagnosticEngine &Diags);
232235
~ASTContext();
233236

@@ -246,6 +249,9 @@ class ASTContext final {
246249
/// The search path options used by this AST context.
247250
SearchPathOptions &SearchPathOpts;
248251

252+
/// The clang importer options used by this AST context.
253+
ClangImporterOptions &ClangImporterOpts;
254+
249255
/// The source manager object.
250256
SourceManager &SourceMgr;
251257

include/swift/Basic/LangOptions.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,115 @@ namespace swift {
577577
/// parameters of closures.
578578
bool EnableOneWayClosureParameters = false;
579579
};
580+
581+
/// Options for controlling the behavior of the Clang importer.
582+
class ClangImporterOptions final {
583+
public:
584+
/// The module cache path which the Clang importer should use.
585+
std::string ModuleCachePath;
586+
587+
/// Extra arguments which should be passed to the Clang importer.
588+
std::vector<std::string> ExtraArgs;
589+
590+
/// A directory for overriding Clang's resource directory.
591+
std::string OverrideResourceDir;
592+
593+
/// The target CPU to compile for.
594+
///
595+
/// Equivalent to Clang's -mcpu=.
596+
std::string TargetCPU;
597+
598+
/// The path to which we should store indexing data, if any.
599+
std::string IndexStorePath;
600+
601+
/// The bridging header or PCH that will be imported.
602+
std::string BridgingHeader;
603+
604+
/// When automatically generating a precompiled header from the bridging
605+
/// header, place it in this directory.
606+
std::string PrecompiledHeaderOutputDir;
607+
608+
/// The optimizaton setting. This doesn't typically matter for
609+
/// import, but it can affect Clang's IR generation of static functions.
610+
std::string Optimization;
611+
612+
/// Disable validating the persistent PCH.
613+
bool PCHDisableValidation = false;
614+
615+
/// \see Mode
616+
enum class Modes : uint8_t {
617+
/// Set up Clang for importing modules into Swift and generating IR from
618+
/// Swift code.
619+
Normal,
620+
/// Set up Clang for backend compilation only.
621+
EmbedBitcode,
622+
/// Set up Clang to emit a precompiled module from a C/Objective-C module
623+
/// map or dump debugging info about a precompiled module.
624+
PrecompiledModule
625+
};
626+
627+
/// Controls how Clang is initially set up.
628+
Modes Mode = Modes::Normal;
629+
630+
/// When set, preserves more information during import.
631+
///
632+
/// Also \em disables some information that is only needed for object file
633+
/// generation.
634+
bool DetailedPreprocessingRecord = false;
635+
636+
/// If true, Clang diagnostics will be dumped to stderr using Clang's
637+
/// diagnostic printer as well as being passed to Swift's diagnostic engine.
638+
bool DumpClangDiagnostics = false;
639+
640+
/// If true, forward declarations will be imported using unavailable types
641+
/// instead of dropped altogether when possible.
642+
bool ImportForwardDeclarations = false;
643+
644+
/// Whether to use the import as member inference system
645+
///
646+
/// When importing a global, try to infer whether we can import it as a
647+
/// member of some type instead. This includes inits, computed properties,
648+
/// and methods.
649+
bool InferImportAsMember = false;
650+
651+
/// If true ignore the swift bridged attribute.
652+
bool DisableSwiftBridgeAttr = false;
653+
654+
/// When set, don't look for or load overlays.
655+
bool DisableOverlayModules = false;
656+
657+
/// When set, don't enforce warnings with -Werror.
658+
bool DebuggerSupport = false;
659+
660+
/// When set, ClangImporter is disabled, and all requests go to the
661+
/// DWARFImporter delegate.
662+
bool DisableSourceImport = false;
663+
664+
/// When set, use ExtraArgs alone to configure clang instance because ExtraArgs
665+
/// contains the full option set.
666+
bool ExtraArgsOnly = false;
667+
668+
/// Return a hash code of any components from these options that should
669+
/// contribute to a Swift Bridging PCH hash.
670+
llvm::hash_code getPCHHashComponents() const {
671+
using llvm::hash_combine;
672+
using llvm::hash_combine_range;
673+
674+
return hash_combine(ModuleCachePath,
675+
hash_combine_range(ExtraArgs.begin(), ExtraArgs.end()),
676+
OverrideResourceDir,
677+
TargetCPU,
678+
BridgingHeader,
679+
PrecompiledHeaderOutputDir,
680+
static_cast<uint8_t>(Mode),
681+
DetailedPreprocessingRecord,
682+
ImportForwardDeclarations,
683+
InferImportAsMember,
684+
DisableSwiftBridgeAttr,
685+
DisableOverlayModules);
686+
}
687+
};
688+
580689
} // end namespace swift
581690

582691
#endif // SWIFT_BASIC_LANGOPTIONS_H

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ClangImporter final : public ClangModuleLoader {
115115
private:
116116
Implementation &Impl;
117117

118-
ClangImporter(ASTContext &ctx, const ClangImporterOptions &clangImporterOpts,
118+
ClangImporter(ASTContext &ctx,
119119
DependencyTracker *tracker,
120120
DWARFImporterDelegate *dwarfImporterDelegate);
121121

@@ -137,8 +137,6 @@ class ClangImporter final : public ClangModuleLoader {
137137
/// \param ctx The ASTContext into which the module will be imported.
138138
/// The ASTContext's SearchPathOptions will be used for the Clang importer.
139139
///
140-
/// \param importerOpts The options to use for the Clang importer.
141-
///
142140
/// \param swiftPCHHash A hash of Swift's various options in a compiler
143141
/// invocation, used to create a unique Bridging PCH if requested.
144142
///
@@ -150,12 +148,12 @@ class ClangImporter final : public ClangModuleLoader {
150148
/// \returns a new Clang module importer, or null (with a diagnostic) if
151149
/// an error occurred.
152150
static std::unique_ptr<ClangImporter>
153-
create(ASTContext &ctx, const ClangImporterOptions &importerOpts,
151+
create(ASTContext &ctx,
154152
std::string swiftPCHHash = "", DependencyTracker *tracker = nullptr,
155153
DWARFImporterDelegate *dwarfImporterDelegate = nullptr);
156154

157155
static std::vector<std::string>
158-
getClangArguments(ASTContext &ctx, const ClangImporterOptions &importerOpts);
156+
getClangArguments(ASTContext &ctx);
159157

160158
static std::unique_ptr<clang::CompilerInvocation>
161159
createClangInvocation(ClangImporter *importer,

include/swift/ClangImporter/ClangImporterOptions.h

Lines changed: 0 additions & 133 deletions
This file was deleted.

include/swift/Frontend/Frontend.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "swift/Basic/LangOptions.h"
3131
#include "swift/Basic/SourceManager.h"
3232
#include "swift/ClangImporter/ClangImporter.h"
33-
#include "swift/ClangImporter/ClangImporterOptions.h"
3433
#include "swift/Frontend/DiagnosticVerifier.h"
3534
#include "swift/Frontend/FrontendOptions.h"
3635
#include "swift/Frontend/ModuleInterfaceSupport.h"

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ void ASTContext::operator delete(void *Data) throw() {
548548
ASTContext *ASTContext::get(LangOptions &langOpts,
549549
TypeCheckerOptions &typeckOpts,
550550
SearchPathOptions &SearchPathOpts,
551+
ClangImporterOptions &ClangImporterOpts,
551552
SourceManager &SourceMgr,
552553
DiagnosticEngine &Diags) {
553554
// If more than two data structures are concatentated, then the aggregate
@@ -561,15 +562,19 @@ ASTContext *ASTContext::get(LangOptions &langOpts,
561562
llvm::alignAddr(impl, llvm::Align(alignof(Implementation))));
562563
new (impl) Implementation();
563564
return new (mem)
564-
ASTContext(langOpts, typeckOpts, SearchPathOpts, SourceMgr, Diags);
565+
ASTContext(langOpts, typeckOpts, SearchPathOpts, ClangImporterOpts,
566+
SourceMgr, Diags);
565567
}
566568

567569
ASTContext::ASTContext(LangOptions &langOpts, TypeCheckerOptions &typeckOpts,
568570
SearchPathOptions &SearchPathOpts,
571+
ClangImporterOptions &ClangImporterOpts,
569572
SourceManager &SourceMgr, DiagnosticEngine &Diags)
570573
: LangOpts(langOpts),
571574
TypeCheckerOpts(typeckOpts),
572-
SearchPathOpts(SearchPathOpts), SourceMgr(SourceMgr), Diags(Diags),
575+
SearchPathOpts(SearchPathOpts),
576+
ClangImporterOpts(ClangImporterOpts),
577+
SourceMgr(SourceMgr), Diags(Diags),
573578
evaluator(Diags, langOpts),
574579
TheBuiltinModule(createBuiltinModule(*this)),
575580
StdlibModuleName(getIdentifier(STDLIB_NAME)),

0 commit comments

Comments
 (0)