Skip to content

Commit b8e9cf9

Browse files
committed
[SourceKit] Syntactic macro expansion disable caching
This doesn't support nested expansions for now. So there is not much to reuse. (cherry picked from commit 6f315f8)
1 parent 0feda5d commit b8e9cf9

File tree

7 files changed

+131
-156
lines changed

7 files changed

+131
-156
lines changed

include/swift/IDETool/SyntacticMacroExpansion.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ struct MacroExpansionSpecifier {
3939
/// list of compiler arguments (i.e. 'argHash'), and reused as long as the
4040
/// compiler arguments are not changed.
4141
class SyntacticMacroExpansionInstance {
42-
const Fingerprint argHash;
4342
CompilerInvocation invocation;
4443

4544
SourceManager SourceMgr;
@@ -48,8 +47,6 @@ class SyntacticMacroExpansionInstance {
4847
ModuleDecl *TheModule = nullptr;
4948
llvm::StringMap<MacroDecl *> MacroDecls;
5049

51-
std::mutex mtx;
52-
5350
/// Create 'SourceFile' using the buffer.
5451
swift::SourceFile *getSourceFile(llvm::MemoryBuffer *inputBuf);
5552

@@ -64,13 +61,12 @@ class SyntacticMacroExpansionInstance {
6461
SourceEditConsumer &consumer);
6562

6663
public:
67-
SyntacticMacroExpansionInstance(Fingerprint argHash) : argHash(argHash) {}
64+
SyntacticMacroExpansionInstance() {}
6865

6966
/// Setup the instance with \p args .
7067
bool setup(StringRef SwiftExecutablePath, ArrayRef<const char *> args,
7168
std::shared_ptr<PluginRegistry> plugins, std::string &error);
7269

73-
const Fingerprint &getArgHash() const { return argHash; }
7470
ASTContext &getASTContext() { return *Ctx; }
7571

7672
/// Expand all macros in \p inputBuf and send the edit results to \p consumer.
@@ -85,16 +81,12 @@ class SyntacticMacroExpansion {
8581
StringRef SwiftExecutablePath;
8682
std::shared_ptr<PluginRegistry> Plugins;
8783

88-
/// Cached instance.
89-
std::shared_ptr<SyntacticMacroExpansionInstance> currentInstance;
90-
9184
public:
9285
SyntacticMacroExpansion(StringRef SwiftExecutablePath,
9386
std::shared_ptr<PluginRegistry> Plugins)
9487
: SwiftExecutablePath(SwiftExecutablePath), Plugins(Plugins) {}
9588

9689
/// Get instance configured with the specified compiler arguments.
97-
/// If 'currentInstance' matches with the arguments, just return it.
9890
std::shared_ptr<SyntacticMacroExpansionInstance>
9991
getInstance(ArrayRef<const char *> args, std::string &error);
10092
};

lib/IDETool/SyntacticMacroExpansion.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,13 @@ using namespace ide;
2626
std::shared_ptr<SyntacticMacroExpansionInstance>
2727
SyntacticMacroExpansion::getInstance(ArrayRef<const char *> args,
2828
std::string &error) {
29-
// Compute the signature of the invocation.
30-
StableHasher argHasher = StableHasher::defaultHasher();
31-
for (auto arg : args)
32-
argHasher.combine(StringRef(arg));
33-
Fingerprint argHash(std::move(argHasher));
34-
35-
// Check if the current instance is usable.
36-
if (auto currentInstance = this->currentInstance) {
37-
if (currentInstance->getArgHash() == argHash) {
38-
return currentInstance;
39-
}
40-
}
41-
4229
// Create and configure a new instance.
43-
auto instance = std::make_shared<SyntacticMacroExpansionInstance>(argHash);
30+
auto instance = std::make_shared<SyntacticMacroExpansionInstance>();
4431

4532
bool failed = instance->setup(SwiftExecutablePath, args, Plugins, error);
4633
if (failed)
4734
return nullptr;
4835

49-
currentInstance = instance;
5036
return instance;
5137
}
5238

@@ -475,7 +461,6 @@ bool SyntacticMacroExpansionInstance::getExpansion(
475461
bool SyntacticMacroExpansionInstance::getExpansions(
476462
llvm::MemoryBuffer *inputBuf, ArrayRef<MacroExpansionSpecifier> expansions,
477463
SourceEditConsumer &consumer) {
478-
std::scoped_lock<std::mutex> lock(mtx);
479464

480465
// Create a source file.
481466
SourceFile *SF = getSourceFile(inputBuf);

test/SourceKit/Macros/syntactic_expansion.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ struct Generic<Element> {
5151
//##-- Prepare the macro plugin.
5252
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/plugins/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/../../Macros/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
5353

54-
// RUN: %sourcekitd-test -req=syntactic-expandmacro \
54+
// RUN: %sourcekitd-test \
55+
// RUN: -shell -- echo '### 1' \
56+
// RUN: == \
57+
// RUN: -req=syntactic-expandmacro \
5558
// RUN: -req-opts=1:1:%t/DelegatedConformance.json \
5659
// RUN: -req-opts=5:3:%t/myPropertyWrapper.json \
5760
// RUN: -req-opts=2:1:%t/wrapAllProperties.json \
@@ -61,6 +64,19 @@ struct Generic<Element> {
6164
// RUN: %t/test.swift \
6265
// RUN: -plugin-path %t/plugins -Xfrontend -dump-macro-expansions \
6366
// RUN: -module-name TestModule \
67+
// RUN: == \
68+
// RUN: -shell -- echo '### 2' \
69+
// RUN: == \
70+
// RUN: -req=syntactic-expandmacro \
71+
// RUN: -req-opts=12:3:%t/bitwidthNumberedStructs.json \
72+
// RUN: -req-opts=2:1:%t/wrapAllProperties.json \
73+
// RUN: -req-opts=5:3:%t/myPropertyWrapper.json \
74+
// RUN: -req-opts=1:1:%t/DelegatedConformance.json \
75+
// RUN: %t/test.swift \
76+
// RUN: -- \
77+
// RUN: %t/test.swift \
78+
// RUN: -plugin-path %t/plugins -Xfrontend -dump-macro-expansions \
79+
// RUN: -module-name TestModule \
6480
// RUN: | tee %t.response
6581

6682
// RUN: diff -u %s.expected %t.response
Lines changed: 107 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,108 @@
1-
{
2-
key.categorizededits: [
3-
{
4-
key.edits: [
5-
{
6-
key.line: 1,
7-
key.column: 1,
8-
key.endline: 1,
9-
key.endcolumn: 22,
10-
key.text: ""
11-
}
12-
],
13-
key.category: source.edit.kind.active
14-
},
15-
{
16-
key.edits: [
17-
{
18-
key.line: 13,
19-
key.column: 1,
20-
key.endline: 13,
21-
key.endcolumn: 1,
22-
key.text: "static func requirement() where Element : P {\n Element.requirement()\n}"
23-
}
24-
],
25-
key.category: source.edit.kind.active
26-
},
27-
{
28-
key.edits: [
29-
{
30-
key.line: 13,
31-
key.column: 2,
32-
key.endline: 13,
33-
key.endcolumn: 2,
34-
key.text: "extension Generic : P where Element: P {}"
35-
}
36-
],
37-
key.category: source.edit.kind.active
38-
},
39-
{
40-
key.edits: [
41-
{
42-
key.line: 5,
43-
key.column: 3,
44-
key.endline: 5,
45-
key.endcolumn: 21,
46-
key.text: ""
47-
}
48-
],
49-
key.category: source.edit.kind.active
50-
},
51-
{
52-
key.edits: [
53-
{
54-
key.line: 7,
55-
key.column: 17,
56-
key.endline: 7,
57-
key.endcolumn: 17,
58-
key.text: "{\n get {\n _value.wrappedValue\n }\n\n set {\n _value.wrappedValue = newValue\n }\n}"
59-
}
60-
],
61-
key.category: source.edit.kind.active
62-
},
63-
{
64-
key.edits: [
65-
{
66-
key.line: 7,
67-
key.column: 12,
68-
key.endline: 7,
69-
key.endcolumn: 12,
70-
key.text: "var _value: MyWrapperThingy<Int>"
71-
}
72-
],
73-
key.category: source.edit.kind.active
74-
},
75-
{
76-
key.edits: [
77-
{
78-
key.line: 2,
79-
key.column: 1,
80-
key.endline: 2,
81-
key.endcolumn: 19,
82-
key.text: ""
83-
}
84-
],
85-
key.category: source.edit.kind.active
86-
},
87-
{
88-
key.edits: [
89-
{
90-
key.line: 7,
91-
key.column: 3,
92-
key.endline: 7,
93-
key.endcolumn: 3,
94-
key.text: "@Wrapper"
95-
}
96-
],
97-
key.category: source.edit.kind.active
98-
},
99-
{
100-
key.edits: [
101-
{
102-
key.line: 10,
103-
key.column: 3,
104-
key.endline: 10,
105-
key.endcolumn: 3,
106-
key.text: "@Wrapper"
107-
}
108-
],
109-
key.category: source.edit.kind.active
110-
},
111-
{
112-
key.edits: [
113-
{
114-
key.line: 12,
115-
key.column: 3,
116-
key.endline: 12,
117-
key.endcolumn: 35,
118-
key.text: "struct blah8 {\n func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu_() {\n }\n func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu0_() {\n }\n}\nstruct blah16 {\n func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu1_() {\n }\n func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu2_() {\n }\n}\nstruct blah32 {\n func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu3_() {\n }\n func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu4_() {\n }\n}\nstruct blah64 {\n func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu5_() {\n }\n func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu6_() {\n }\n}"
119-
}
120-
],
121-
key.category: source.edit.kind.active
122-
}
123-
]
1+
### 1
2+
source.edit.kind.active:
3+
1:1-1:22 ""
4+
source.edit.kind.active:
5+
13:1-13:1 "static func requirement() where Element : P {
6+
Element.requirement()
7+
}"
8+
source.edit.kind.active:
9+
13:2-13:2 "extension Generic : P where Element: P {}"
10+
source.edit.kind.active:
11+
5:3-5:21 ""
12+
source.edit.kind.active:
13+
7:17-7:17 "{
14+
get {
15+
_value.wrappedValue
16+
}
17+
18+
set {
19+
_value.wrappedValue = newValue
20+
}
21+
}"
22+
source.edit.kind.active:
23+
7:12-7:12 "var _value: MyWrapperThingy<Int>"
24+
source.edit.kind.active:
25+
2:1-2:19 ""
26+
source.edit.kind.active:
27+
7:3-7:3 "@Wrapper"
28+
source.edit.kind.active:
29+
10:3-10:3 "@Wrapper"
30+
source.edit.kind.active:
31+
12:3-12:35 "struct blah8 {
32+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu_() {
33+
}
34+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu0_() {
35+
}
12436
}
37+
struct blah16 {
38+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu1_() {
39+
}
40+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu2_() {
41+
}
42+
}
43+
struct blah32 {
44+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu3_() {
45+
}
46+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu4_() {
47+
}
48+
}
49+
struct blah64 {
50+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu5_() {
51+
}
52+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu6_() {
53+
}
54+
}"
55+
### 2
56+
source.edit.kind.active:
57+
12:3-12:35 "struct blah8 {
58+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu_() {
59+
}
60+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu0_() {
61+
}
62+
}
63+
struct blah16 {
64+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu1_() {
65+
}
66+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu2_() {
67+
}
68+
}
69+
struct blah32 {
70+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu3_() {
71+
}
72+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu4_() {
73+
}
74+
}
75+
struct blah64 {
76+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu5_() {
77+
}
78+
func macro_a01f07de3e9ee8585adf794ccd6dec8c6methodfMu6_() {
79+
}
80+
}"
81+
source.edit.kind.active:
82+
2:1-2:19 ""
83+
source.edit.kind.active:
84+
7:3-7:3 "@Wrapper"
85+
source.edit.kind.active:
86+
10:3-10:3 "@Wrapper"
87+
source.edit.kind.active:
88+
5:3-5:21 ""
89+
source.edit.kind.active:
90+
7:17-7:17 "{
91+
get {
92+
_value.wrappedValue
93+
}
94+
95+
set {
96+
_value.wrappedValue = newValue
97+
}
98+
}"
99+
source.edit.kind.active:
100+
7:12-7:12 "var _value: MyWrapperThingy<Int>"
101+
source.edit.kind.active:
102+
1:1-1:22 ""
103+
source.edit.kind.active:
104+
13:1-13:1 "static func requirement() where Element : P {
105+
Element.requirement()
106+
}"
107+
source.edit.kind.active:
108+
13:2-13:2 "extension Generic : P where Element: P {}"

tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
156156
.Case("syntactic-expandmacro", SourceKitRequest::SyntacticMacroExpansion)
157157
#define SEMANTIC_REFACTORING(KIND, NAME, ID) .Case("refactoring." #ID, SourceKitRequest::KIND)
158158
#include "swift/Refactoring/RefactoringKinds.def"
159-
.Default(SourceKitRequest::None);
159+
.Default(SourceKitRequest::None);
160160

161161
if (Request == SourceKitRequest::None) {
162162
llvm::errs() << "error: invalid request '" << InputArg->getValue()

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,6 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
14121412
case SourceKitRequest::ConformingMethodList:
14131413
case SourceKitRequest::DependencyUpdated:
14141414
case SourceKitRequest::Diagnostics:
1415-
case SourceKitRequest::SyntacticMacroExpansion:
14161415
printRawResponse(Resp);
14171416
break;
14181417
case SourceKitRequest::Compile:
@@ -1568,6 +1567,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
15681567
#define SEMANTIC_REFACTORING(KIND, NAME, ID) case SourceKitRequest::KIND:
15691568
#include "swift/Refactoring/RefactoringKinds.def"
15701569
case SourceKitRequest::SyntacticRename:
1570+
case SourceKitRequest::SyntacticMacroExpansion:
15711571
printSyntacticRenameEdits(Info, llvm::outs());
15721572
break;
15731573
case SourceKitRequest::FindRenameRanges:

tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,11 +1822,9 @@ handleRequestDiagnostics(const RequestDict &Req,
18221822
/// key.macro_roles: [<macro role UID>...]
18231823
/// }
18241824
///
1825-
/// Sends the results as a 'CategorizedEdits'. Each edit object has
1826-
/// 'key.buffer_name' that can be used for recursive expansion. If the
1827-
/// client finds nested macro expansion in the expanded source, it can send
1828-
/// another request using the buffer name and the source text in the subsequent
1829-
/// request.
1825+
/// Sends the results as a 'CategorizedEdits'.
1826+
/// Note that, unlike refactoring, each edit doesn't have 'key.buffer_name'.
1827+
/// FIXME: Support nested expansion.
18301828
static void handleRequestExpandMacroSyntactically(
18311829
const RequestDict &req, SourceKitCancellationToken cancellationToken,
18321830
ResponseReceiver rec) {

0 commit comments

Comments
 (0)