[Frontend] Refactor CompilerInvocation to be a pure settings object.

This mainly removes the extraneous DiagnosticEngine and SourceMgr from CompilerInvocation.
Instances of these can be found via the CompilerInstance object.

Swift SVN r6496
This commit is contained in:
Argyrios Kyrtzidis
2013-07-23 14:49:29 +00:00
parent 8d92add99b
commit 200e64d8c2
3 changed files with 41 additions and 55 deletions

View File

@@ -36,7 +36,7 @@ namespace llvm {
namespace swift { namespace swift {
class CompilerInvocation : public llvm::RefCountedBase<CompilerInvocation> { class CompilerInvocation {
std::string TargetTriple; std::string TargetTriple;
std::string ClangModuleCachePath; std::string ClangModuleCachePath;
std::vector<std::string> ImportSearchPaths; std::vector<std::string> ImportSearchPaths;
@@ -49,10 +49,6 @@ class CompilerInvocation : public llvm::RefCountedBase<CompilerInvocation> {
bool ParseOnly = false; bool ParseOnly = false;
TranslationUnit::TUKind TUKind = TranslationUnit::Main; TranslationUnit::TUKind TUKind = TranslationUnit::Main;
llvm::SourceMgr DriverDiagsSourceMgr;
DiagnosticEngine DriverDiagnostics;
std::vector<DiagnosticConsumer *> DiagnosticConsumers;
std::string ModuleName; std::string ModuleName;
CodeCompletionCallbacksFactory *CodeCompletionFactory = nullptr; CodeCompletionCallbacksFactory *CodeCompletionFactory = nullptr;
@@ -136,19 +132,6 @@ public:
return TUKind; return TUKind;
} }
DiagnosticEngine &getDriverDiags() {
return DriverDiagnostics;
}
void addDiagnosticConsumer(DiagnosticConsumer *DC) {
DriverDiagnostics.addConsumer(*DC);
DiagnosticConsumers.push_back(DC);
}
ArrayRef<DiagnosticConsumer *> getDiagnosticConsumers() const {
return DiagnosticConsumers;
}
void setModuleName(StringRef Name) { void setModuleName(StringRef Name) {
ModuleName = Name.str(); ModuleName = Name.str();
} }
@@ -167,7 +150,7 @@ public:
}; };
class CompilerInstance { class CompilerInstance {
llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation; CompilerInvocation Invocation;
llvm::SourceMgr SourceMgr; llvm::SourceMgr SourceMgr;
std::vector<unsigned> BufferIDs; std::vector<unsigned> BufferIDs;
DiagnosticEngine Diagnostics; DiagnosticEngine Diagnostics;
@@ -180,12 +163,17 @@ class CompilerInstance {
void createSILModule(); void createSILModule();
public: public:
CompilerInstance(llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation) CompilerInstance() : Diagnostics(SourceMgr), TU(nullptr) {
: Invocation(Invocation), Diagnostics(SourceMgr), TU(nullptr) {
} }
llvm::SourceMgr &getSourceMgr() { return SourceMgr; } llvm::SourceMgr &getSourceMgr() { return SourceMgr; }
DiagnosticEngine &getDiags() { return Diagnostics; }
void addDiagnosticConsumer(DiagnosticConsumer *DC) {
Diagnostics.addConsumer(*DC);
}
void setBufferIDs(const std::vector<unsigned> &IDs) { void setBufferIDs(const std::vector<unsigned> &IDs) {
BufferIDs = IDs; BufferIDs = IDs;
} }
@@ -213,7 +201,7 @@ public:
return TU; return TU;
} }
void setup(); void setup(const CompilerInvocation &Invocation);
void doIt(); void doIt();
}; };

View File

@@ -30,8 +30,7 @@
using namespace swift; using namespace swift;
swift::CompilerInvocation::CompilerInvocation() swift::CompilerInvocation::CompilerInvocation() {
: DriverDiagnostics(DriverDiagsSourceMgr) {
TargetTriple = llvm::sys::getDefaultTargetTriple(); TargetTriple = llvm::sys::getDefaultTargetTriple();
} }
@@ -47,27 +46,26 @@ void swift::CompilerInstance::createSILModule() {
TheSILModule.reset(SILModule::createEmptyModule(getASTContext())); TheSILModule.reset(SILModule::createEmptyModule(getASTContext()));
} }
void swift::CompilerInstance::setup() { void swift::CompilerInstance::setup(const CompilerInvocation &Invok) {
for (auto DC : Invocation->getDiagnosticConsumers()) Invocation = Invok;
Diagnostics.addConsumer(*DC);
Context.reset(new ASTContext(Invocation->getLangOptions(), SourceMgr, Diagnostics)); Context.reset(new ASTContext(Invocation.getLangOptions(), SourceMgr, Diagnostics));
// Give the context the list of search paths to use for modules. // Give the context the list of search paths to use for modules.
Context->ImportSearchPaths = Invocation->getImportSearchPaths(); Context->ImportSearchPaths = Invocation.getImportSearchPaths();
Context->addModuleLoader(SourceLoader::create(*Context)); Context->addModuleLoader(SourceLoader::create(*Context));
Context->addModuleLoader(SerializedModuleLoader::create(*Context)); Context->addModuleLoader(SerializedModuleLoader::create(*Context));
// If the user has specified an SDK, wire up the Clang module importer // If the user has specified an SDK, wire up the Clang module importer
// and point it at that SDK. // and point it at that SDK.
if (!Invocation->getSDKPath().empty()) { if (!Invocation.getSDKPath().empty()) {
auto ImporterCtor = Invocation->getClangImporterCtor(); auto ImporterCtor = Invocation.getClangImporterCtor();
assert(ImporterCtor && "SDK patch can't be empty without importer set!"); assert(ImporterCtor && "SDK patch can't be empty without importer set!");
auto clangImporter = auto clangImporter =
ImporterCtor(*Context, Invocation->getSDKPath(), ImporterCtor(*Context, Invocation.getSDKPath(),
Invocation->getTargetTriple(), Invocation.getTargetTriple(),
Invocation->getClangModuleCachePath(), Invocation.getClangModuleCachePath(),
Invocation->getImportSearchPaths()); Invocation.getImportSearchPaths());
if (!clangImporter) if (!clangImporter)
return; // FIXME: error reporting return; // FIXME: error reporting
@@ -75,27 +73,27 @@ void swift::CompilerInstance::setup() {
} }
// Add the runtime include path (which contains swift.swift) // Add the runtime include path (which contains swift.swift)
Context->ImportSearchPaths.push_back(Invocation->getRuntimeIncludePath()); Context->ImportSearchPaths.push_back(Invocation.getRuntimeIncludePath());
assert(Lexer::isIdentifier(Invocation->getModuleName())); assert(Lexer::isIdentifier(Invocation.getModuleName()));
if (Invocation->getTUKind() == TranslationUnit::SIL) if (Invocation.getTUKind() == TranslationUnit::SIL)
createSILModule(); createSILModule();
} }
void swift::CompilerInstance::doIt() { void swift::CompilerInstance::doIt() {
const TranslationUnit::TUKind Kind = Invocation->getTUKind(); const TranslationUnit::TUKind Kind = Invocation.getTUKind();
Component *Comp = new (Context->Allocate<Component>(1)) Component(); Component *Comp = new (Context->Allocate<Component>(1)) Component();
Identifier ID = Context->getIdentifier(Invocation->getModuleName()); Identifier ID = Context->getIdentifier(Invocation.getModuleName());
TU = new (*Context) TranslationUnit(ID, Comp, *Context, Kind); TU = new (*Context) TranslationUnit(ID, Comp, *Context, Kind);
Context->LoadedModules[ID.str()] = TU; Context->LoadedModules[ID.str()] = TU;
TU->HasBuiltinModuleAccess = Invocation->getParseStdlib(); TU->HasBuiltinModuleAccess = Invocation.getParseStdlib();
// If we're in SIL mode, don't auto import any libraries. // If we're in SIL mode, don't auto import any libraries.
// Also don't perform auto import if we are not going to do semantic // Also don't perform auto import if we are not going to do semantic
// analysis. // analysis.
if (Kind != TranslationUnit::SIL && !Invocation->getParseOnly()) if (Kind != TranslationUnit::SIL && !Invocation.getParseOnly())
performAutoImport(TU); performAutoImport(TU);
if (Kind == TranslationUnit::Library) { if (Kind == TranslationUnit::Library) {
@@ -106,12 +104,12 @@ void swift::CompilerInstance::doIt() {
assert(Done && "Parser returned early?"); assert(Done && "Parser returned early?");
(void) Done; (void) Done;
performDelayedParsing(TU, TheParser.get(), performDelayedParsing(TU, TheParser.get(),
Invocation->getCodeCompletionFactory()); Invocation.getCodeCompletionFactory());
TheParser.reset(nullptr); TheParser.reset(nullptr);
} }
// Finally, if enabled, type check the whole thing in one go. // Finally, if enabled, type check the whole thing in one go.
if (!Invocation->getParseOnly()) if (!Invocation.getParseOnly())
performTypeChecking(TU); performTypeChecking(TU);
return; return;
} }
@@ -133,11 +131,11 @@ void swift::CompilerInstance::doIt() {
parseIntoTranslationUnit(TU, BufferID, &Done, parseIntoTranslationUnit(TU, BufferID, &Done,
TheSILModule ? &SILContext : nullptr, TheSILModule ? &SILContext : nullptr,
&TheParser); &TheParser);
if (!Invocation->getParseOnly()) if (!Invocation.getParseOnly())
performTypeChecking(TU, CurTUElem); performTypeChecking(TU, CurTUElem);
CurTUElem = TU->Decls.size(); CurTUElem = TU->Decls.size();
} while (!Done); } while (!Done);
performDelayedParsing(TU, TheParser.get(), performDelayedParsing(TU, TheParser.get(),
Invocation->getCodeCompletionFactory()); Invocation.getCodeCompletionFactory());
} }

View File

@@ -70,21 +70,21 @@ int main(int argc, char **argv) {
if (PrintStats) if (PrintStats)
llvm::EnableStatistics(); llvm::EnableStatistics();
llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation( CompilerInvocation Invocation;
new CompilerInvocation());
Invocation->setMainExecutablePath( Invocation.setMainExecutablePath(
llvm::sys::fs::getMainExecutable(argv[0], llvm::sys::fs::getMainExecutable(argv[0],
reinterpret_cast<void *>(&anchorForGetMainExecutable))); reinterpret_cast<void *>(&anchorForGetMainExecutable)));
// Give the context the list of search paths to use for modules. // Give the context the list of search paths to use for modules.
Invocation->setImportSearchPaths(ImportPaths); Invocation.setImportSearchPaths(ImportPaths);
Invocation.setModuleName("main");
Invocation.setTUKind(TranslationUnit::SIL);
CompilerInstance CI;
PrintingDiagnosticConsumer PrintDiags; PrintingDiagnosticConsumer PrintDiags;
Invocation->addDiagnosticConsumer(&PrintDiags); CI.addDiagnosticConsumer(&PrintDiags);
Invocation->setModuleName("main");
Invocation->setTUKind(TranslationUnit::SIL);
CompilerInstance CI(Invocation);
// Open the input file. // Open the input file.
llvm::OwningPtr<llvm::MemoryBuffer> InputFile; llvm::OwningPtr<llvm::MemoryBuffer> InputFile;
@@ -98,7 +98,7 @@ int main(int argc, char **argv) {
// Transfer ownership of the MemoryBuffer to the SourceMgr. // Transfer ownership of the MemoryBuffer to the SourceMgr.
CI.addBufferID(CI.getSourceMgr().AddNewSourceBuffer(InputFile.take(), CI.addBufferID(CI.getSourceMgr().AddNewSourceBuffer(InputFile.take(),
llvm::SMLoc())); llvm::SMLoc()));
CI.setup(); CI.setup(Invocation);
CI.doIt(); CI.doIt();
for (auto Pass : Passes) { for (auto Pass : Passes) {