Skip to content

Commit 309c024

Browse files
committed
[IDE] Avoid relying on after-the-fact use of addFile
We shouldn't be attempting to append SourceFiles to the module after-the-fact for syntactic macro expansion, refactor things such that the SourceFile is created alongside the ModuleDecl.
1 parent e2ba36f commit 309c024

File tree

3 files changed

+28
-52
lines changed

3 files changed

+28
-52
lines changed

include/swift/IDETool/SyntacticMacroExpansion.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,32 @@ class SyntacticMacroExpansionInstance {
4545
DiagnosticEngine Diags{SourceMgr};
4646
std::unique_ptr<ASTContext> Ctx;
4747
ModuleDecl *TheModule = nullptr;
48+
SourceFile *SF = nullptr;
4849
llvm::StringMap<MacroDecl *> MacroDecls;
4950

50-
/// Create 'SourceFile' using the buffer.
51-
swift::SourceFile *getSourceFile(llvm::MemoryBuffer *inputBuf);
52-
5351
/// Synthesize 'MacroDecl' AST object to use the expansion.
5452
swift::MacroDecl *
5553
getSynthesizedMacroDecl(swift::Identifier name,
5654
const MacroExpansionSpecifier &expansion);
5755

58-
/// Expand single 'expansion' in SF.
59-
void expand(swift::SourceFile *SF,
60-
const MacroExpansionSpecifier &expansion,
61-
SourceEditConsumer &consumer);
56+
/// Expand single 'expansion'.
57+
void expand(const MacroExpansionSpecifier &expansion,
58+
SourceEditConsumer &consumer);
6259

6360
public:
6461
SyntacticMacroExpansionInstance() {}
6562

66-
/// Setup the instance with \p args .
63+
/// Setup the instance with \p args and a given \p inputBuf.
6764
bool setup(StringRef SwiftExecutablePath, ArrayRef<const char *> args,
65+
llvm::MemoryBuffer *inputBuf,
6866
std::shared_ptr<PluginRegistry> plugins, std::string &error);
6967

7068
ASTContext &getASTContext() { return *Ctx; }
7169

72-
/// Expand all macros in \p inputBuf and send the edit results to \p consumer.
73-
/// Expansions are specified by \p expansions .
74-
void expandAll(llvm::MemoryBuffer *inputBuf,
75-
ArrayRef<MacroExpansionSpecifier> expansions,
76-
SourceEditConsumer &consumer);
70+
/// Expand all macros and send the edit results to \p consumer. Expansions are
71+
/// specified by \p expansions .
72+
void expandAll(ArrayRef<MacroExpansionSpecifier> expansions,
73+
SourceEditConsumer &consumer);
7774
};
7875

7976
/// Manager object to vend 'SyntacticMacroExpansionInstance'.
@@ -86,9 +83,11 @@ class SyntacticMacroExpansion {
8683
std::shared_ptr<PluginRegistry> Plugins)
8784
: SwiftExecutablePath(SwiftExecutablePath), Plugins(Plugins) {}
8885

89-
/// Get instance configured with the specified compiler arguments.
86+
/// Get instance configured with the specified compiler arguments and
87+
/// input buffer.
9088
std::shared_ptr<SyntacticMacroExpansionInstance>
91-
getInstance(ArrayRef<const char *> args, std::string &error);
89+
getInstance(ArrayRef<const char *> args, llvm::MemoryBuffer *inputBuf,
90+
std::string &error);
9291
};
9392

9493
} // namespace ide

lib/IDETool/SyntacticMacroExpansion.cpp

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ using namespace ide;
2626

2727
std::shared_ptr<SyntacticMacroExpansionInstance>
2828
SyntacticMacroExpansion::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

4042
bool 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

10991
MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl(
@@ -425,8 +407,7 @@ class MacroExpansionFinder : public ASTWalker {
425407
} // namespace
426408

427409
void 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

477458
void 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
}

tools/SourceKit/lib/SwiftLang/SwiftSyntacticMacroExpansion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void SwiftLangSupport::expandMacroSyntactically(
3030
CategorizedEditsReceiver receiver) {
3131

3232
std::string error;
33-
auto instance = SyntacticMacroExpansions->getInstance(args, error);
33+
auto instance = SyntacticMacroExpansions->getInstance(args, inputBuf, error);
3434
if (!instance) {
3535
return receiver(
3636
RequestResult<ArrayRef<CategorizedEdits>>::fromError(error));
@@ -85,6 +85,6 @@ void SwiftLangSupport::expandMacroSyntactically(
8585
}
8686

8787
RequestRefactoringEditConsumer consumer(receiver);
88-
instance->expandAll(inputBuf, expansions, consumer);
88+
instance->expandAll(expansions, consumer);
8989
// consumer automatically send the results on destruction.
9090
}

0 commit comments

Comments
 (0)