@@ -26,11 +26,13 @@ using namespace ide;
2626
2727std::shared_ptr<SyntacticMacroExpansionInstance>
2828SyntacticMacroExpansion::getInstance (ArrayRef<const char *> args,
29+ llvm::MemoryBuffer *inputBuf,
2930 std::string &error) {
3031 // Create and configure a new instance.
3132 auto instance = std::make_shared<SyntacticMacroExpansionInstance>();
3233
33- bool failed = instance->setup (SwiftExecutablePath, args, Plugins, error);
34+ bool failed =
35+ instance->setup (SwiftExecutablePath, args, inputBuf, Plugins, error);
3436 if (failed)
3537 return nullptr ;
3638
@@ -39,7 +41,8 @@ SyntacticMacroExpansion::getInstance(ArrayRef<const char *> args,
3941
4042bool SyntacticMacroExpansionInstance::setup (
4143 StringRef SwiftExecutablePath, ArrayRef<const char *> args,
42- std::shared_ptr<PluginRegistry> plugins, std::string &error) {
44+ llvm::MemoryBuffer *inputBuf, std::shared_ptr<PluginRegistry> plugins,
45+ std::string &error) {
4346 SmallString<256 > driverPath (SwiftExecutablePath);
4447 llvm::sys::path::remove_filename (driverPath);
4548 llvm::sys::path::append (driverPath, " swiftc" );
@@ -73,37 +76,16 @@ bool SyntacticMacroExpansionInstance::setup(
7376 pluginLoader->setRegistry (plugins.get ());
7477 Ctx->setPluginLoader (std::move (pluginLoader));
7578
76- // Create a module where SourceFiles reside .
79+ // Create the ModuleDecl and SourceFile .
7780 Identifier ID = Ctx->getIdentifier (invocation.getModuleName ());
7881 TheModule = ModuleDecl::create (ID, *Ctx);
7982
80- return false ;
81- }
82-
83- SourceFile *
84- SyntacticMacroExpansionInstance::getSourceFile (llvm::MemoryBuffer *inputBuf) {
85-
86- // If there is a SourceFile with the same name and the content, use it.
87- // Note that this finds the generated source file that was created in the
88- // previous expansion requests.
89- if (auto bufID =
90- SourceMgr.getIDForBufferIdentifier (inputBuf->getBufferIdentifier ())) {
91- if (inputBuf->getBuffer () == SourceMgr.getEntireTextForBuffer (*bufID)) {
92- SourceLoc bufLoc = SourceMgr.getLocForBufferStart (*bufID);
93- if (SourceFile *existing =
94- TheModule->getSourceFileContainingLocation (bufLoc)) {
95- return existing;
96- }
97- }
98- }
99-
100- // Otherwise, create a new SourceFile.
101- SourceFile *SF = new (getASTContext ()) SourceFile (
102- *TheModule, SourceFileKind::Main, SourceMgr.addMemBufferCopy (inputBuf));
83+ SF = new (*Ctx) SourceFile (*TheModule, SourceFileKind::Main,
84+ SourceMgr.addMemBufferCopy (inputBuf));
10385 SF->setImports ({});
10486 TheModule->addFile (*SF);
10587
106- return SF ;
88+ return false ;
10789}
10890
10991MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl (
@@ -425,8 +407,7 @@ class MacroExpansionFinder : public ASTWalker {
425407} // namespace
426408
427409void SyntacticMacroExpansionInstance::expand (
428- SourceFile *SF, const MacroExpansionSpecifier &expansion,
429- SourceEditConsumer &consumer) {
410+ const MacroExpansionSpecifier &expansion, SourceEditConsumer &consumer) {
430411
431412 // Find the expansion at 'expansion.offset'.
432413 MacroExpansionFinder expansionFinder (
@@ -475,13 +456,9 @@ void SyntacticMacroExpansionInstance::expand(
475456}
476457
477458void SyntacticMacroExpansionInstance::expandAll (
478- llvm::MemoryBuffer *inputBuf, ArrayRef<MacroExpansionSpecifier> expansions,
459+ ArrayRef<MacroExpansionSpecifier> expansions,
479460 SourceEditConsumer &consumer) {
480-
481- // Create a source file.
482- SourceFile *SF = getSourceFile (inputBuf);
483-
484461 for (const auto &expansion : expansions) {
485- expand (SF, expansion, consumer);
462+ expand (expansion, consumer);
486463 }
487464}
0 commit comments