@@ -26,11 +26,13 @@ using namespace ide;
26
26
27
27
std::shared_ptr<SyntacticMacroExpansionInstance>
28
28
SyntacticMacroExpansion::getInstance (ArrayRef<const char *> args,
29
+ llvm::MemoryBuffer *inputBuf,
29
30
std::string &error) {
30
31
// Create and configure a new instance.
31
32
auto instance = std::make_shared<SyntacticMacroExpansionInstance>();
32
33
33
- bool failed = instance->setup (SwiftExecutablePath, args, Plugins, error);
34
+ bool failed =
35
+ instance->setup (SwiftExecutablePath, args, inputBuf, Plugins, error);
34
36
if (failed)
35
37
return nullptr ;
36
38
@@ -39,7 +41,8 @@ SyntacticMacroExpansion::getInstance(ArrayRef<const char *> args,
39
41
40
42
bool SyntacticMacroExpansionInstance::setup (
41
43
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) {
43
46
SmallString<256 > driverPath (SwiftExecutablePath);
44
47
llvm::sys::path::remove_filename (driverPath);
45
48
llvm::sys::path::append (driverPath, " swiftc" );
@@ -73,37 +76,16 @@ bool SyntacticMacroExpansionInstance::setup(
73
76
pluginLoader->setRegistry (plugins.get ());
74
77
Ctx->setPluginLoader (std::move (pluginLoader));
75
78
76
- // Create a module where SourceFiles reside .
79
+ // Create the ModuleDecl and SourceFile .
77
80
Identifier ID = Ctx->getIdentifier (invocation.getModuleName ());
78
81
TheModule = ModuleDecl::create (ID, *Ctx);
79
82
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));
103
85
SF->setImports ({});
104
86
TheModule->addFile (*SF);
105
87
106
- return SF ;
88
+ return false ;
107
89
}
108
90
109
91
MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl (
@@ -425,8 +407,7 @@ class MacroExpansionFinder : public ASTWalker {
425
407
} // namespace
426
408
427
409
void SyntacticMacroExpansionInstance::expand (
428
- SourceFile *SF, const MacroExpansionSpecifier &expansion,
429
- SourceEditConsumer &consumer) {
410
+ const MacroExpansionSpecifier &expansion, SourceEditConsumer &consumer) {
430
411
431
412
// Find the expansion at 'expansion.offset'.
432
413
MacroExpansionFinder expansionFinder (
@@ -475,13 +456,9 @@ void SyntacticMacroExpansionInstance::expand(
475
456
}
476
457
477
458
void SyntacticMacroExpansionInstance::expandAll (
478
- llvm::MemoryBuffer *inputBuf, ArrayRef<MacroExpansionSpecifier> expansions,
459
+ ArrayRef<MacroExpansionSpecifier> expansions,
479
460
SourceEditConsumer &consumer) {
480
-
481
- // Create a source file.
482
- SourceFile *SF = getSourceFile (inputBuf);
483
-
484
461
for (const auto &expansion : expansions) {
485
- expand (SF, expansion, consumer);
462
+ expand (expansion, consumer);
486
463
}
487
464
}
0 commit comments