Skip to content

Commit 1a4a72a

Browse files
committed
Tweaks
1 parent 6456241 commit 1a4a72a

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

include/swift/IDETool/SyntacticMacroExpansion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SyntacticMacroExpansionInstance {
4646
DiagnosticEngine Diags{SourceMgr};
4747
std::unique_ptr<ASTContext> Ctx;
4848
ModuleDecl *TheModule = nullptr;
49-
llvm::DenseMap<Identifier, MacroDecl *> MacroDecls;
49+
llvm::StringMap<MacroDecl *> MacroDecls;
5050

5151
std::mutex mtx;
5252

lib/IDETool/SyntacticMacroExpansion.cpp

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,6 @@ bool SyntacticMacroExpansionInstance::setup(
7171
return true;
7272
}
7373

74-
// Setup filesystem.
75-
auto FS = invocation.getSearchPathOptions().makeOverlayFileSystem(
76-
SourceMgr.getFileSystem());
77-
if (!FS) {
78-
llvm::consumeError(FS.takeError());
79-
error = "failed to setup overlay filesystem";
80-
return true;
81-
}
82-
SourceMgr.setFileSystem(FS.get());
83-
8474
// Setup ASTContext.
8575
Ctx.reset(ASTContext::get(
8676
invocation.getLangOptions(), invocation.getTypeCheckerOptions(),
@@ -132,9 +122,34 @@ MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl(
132122
Identifier name, const MacroExpansionSpecifier &expansion) {
133123
auto &ctx = getASTContext();
134124

125+
std::string macroID;
126+
127+
switch (expansion.macroDefinition.kind) {
128+
case MacroDefinition::Kind::External: {
129+
// '<module name>.<type name>'
130+
// It's safe to use without 'kind' because 'Expanded' always starts with a
131+
// sigil '#' which can't be valid in a module name.
132+
auto external = expansion.macroDefinition.getExternalMacro();
133+
macroID += external.moduleName.str();
134+
macroID += ".";
135+
macroID += external.macroTypeName.str();
136+
break;
137+
}
138+
case MacroDefinition::Kind::Expanded: {
139+
auto expanded = expansion.macroDefinition.getExpanded();
140+
macroID += expanded.getExpansionText();
141+
break;
142+
}
143+
case MacroDefinition::Kind::Builtin:
144+
case MacroDefinition::Kind::Invalid:
145+
case MacroDefinition::Kind::Undefined:
146+
assert(false && "invalid macro definition for syntactic expansion");
147+
macroID += name.str();
148+
}
149+
135150
// Reuse cached MacroDecl of the same name if it's already created.
136151
MacroDecl *macro;
137-
auto found = MacroDecls.find(name);
152+
auto found = MacroDecls.find(macroID);
138153
if (found != MacroDecls.end()) {
139154
macro = found->second;
140155
} else {
@@ -144,7 +159,7 @@ MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl(
144159
/*arrowLoc=*/{}, /*resultType=*/nullptr,
145160
/*definition=*/nullptr, /*parent=*/TheModule);
146161
macro->setImplicit();
147-
MacroDecls.insert({name, macro});
162+
MacroDecls.insert({macroID, macro});
148163
}
149164

150165
// Add missing role attributes to MacroDecl.
@@ -175,17 +190,12 @@ MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl(
175190
/// Create a unique name of the expansion. The result is *appended* to \p out.
176191
static void addExpansionDiscriminator(SmallString<32> &out,
177192
const SourceFile *SF, SourceLoc loc,
178-
MacroDecl *macro,
193+
Optional<SourceLoc> supplementalLoc = None,
179194
Optional<MacroRole> role = None) {
180195
SourceManager &SM = SF->getASTContext().SourceMgr;
181-
auto lineColumn = SM.getLineAndColumnInBuffer(loc);
182196

183197
StableHasher hasher = StableHasher::defaultHasher();
184198

185-
// Macro name.
186-
hasher.combine(macro->getName().getBaseIdentifier().str());
187-
hasher.combine(uint8_t{0});
188-
189199
// Module name.
190200
hasher.combine(SF->getParentModule()->getName().str());
191201
hasher.combine(uint8_t{0});
@@ -195,12 +205,20 @@ static void addExpansionDiscriminator(SmallString<32> &out,
195205
hasher.combine(llvm::sys::path::filename(SF->getFilename()));
196206
hasher.combine(uint8_t{0});
197207

198-
// Line/column
208+
// Line/column.
209+
auto lineColumn = SM.getLineAndColumnInBuffer(loc);
199210
hasher.combine(lineColumn.first);
200211
hasher.combine(lineColumn.second);
201212

213+
// Supplemental line/column.
214+
if (supplementalLoc.has_value()) {
215+
auto supLineColumn = SM.getLineAndColumnInBuffer(*supplementalLoc);
216+
hasher.combine(supLineColumn.first);
217+
hasher.combine(supLineColumn.second);
218+
}
219+
202220
// Macro role.
203-
if (role) {
221+
if (role.has_value()) {
204222
hasher.combine(*role);
205223
}
206224

@@ -218,7 +236,7 @@ expandFreestandingMacro(MacroDecl *macro,
218236
discriminator.append("macro_");
219237
addExpansionDiscriminator(discriminator,
220238
expansion->getDeclContext()->getParentSourceFile(),
221-
expansion->getPoundLoc(), macro);
239+
expansion->getPoundLoc());
222240

223241
expansion->setMacroRef(macro);
224242

@@ -243,7 +261,7 @@ expandAttachedMacro(MacroDecl *macro, CustomAttr *attr, Decl *attachedDecl) {
243261
discriminator.append("macro_");
244262
addExpansionDiscriminator(discriminator,
245263
target->getDeclContext()->getParentSourceFile(),
246-
target->getLoc(), macro, role);
264+
target->getLoc(), attr->getLocation(), role);
247265

248266
SourceFile *expandedSource = swift::evaluateAttachedMacro(
249267
macro, target, attr, passParent, role, discriminator);
@@ -267,13 +285,15 @@ expandAttachedMacro(MacroDecl *macro, CustomAttr *attr, Decl *attachedDecl) {
267285
}
268286
}
269287
if (roles.contains(MacroRole::Member)) {
270-
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Member);
288+
if (isa<IterableDeclContext>(attachedDecl))
289+
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Member);
271290
}
272291
if (roles.contains(MacroRole::Peer)) {
273292
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Peer);
274293
}
275294
if (roles.contains(MacroRole::Conformance)) {
276-
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Conformance);
295+
if (isa<NominalTypeDecl>(attachedDecl))
296+
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Conformance);
277297
}
278298
return bufferIDs;
279299
}
@@ -325,7 +345,7 @@ class MacroExpansionFinder : public ASTWalker {
325345

326346
PreWalkAction walkToDeclPre(Decl *D) override {
327347
// Visit all 'VarDecl' because 'getSourceRangeIncludingAttrs()' doesn't
328-
// include attribute its ranges.
348+
// include its attribute ranges (because attributes are part of PBD.)
329349
if (!isa<VarDecl>(D) &&
330350
!rangeContainsLocToResolve(D->getSourceRangeIncludingAttrs())) {
331351
return Action::SkipChildren();

test/SourceKit/Macros/syntactic_expansion.swift.expected

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
key.endline: 13,
2121
key.endcolumn: 1,
2222
key.text: "static func requirement() where Element : P {\n Element.requirement()\n}",
23-
key.buffer_name: "macro_185a888f5c6c1a9445a25edf7028dc75.swift"
23+
key.buffer_name: "macro_565056f2d01a68bdc1cc2ee8aa618595.swift"
2424
}
2525
],
2626
key.category: source.edit.kind.active
@@ -33,7 +33,7 @@
3333
key.endline: 13,
3434
key.endcolumn: 2,
3535
key.text: "extension Generic : P where Element: P {}",
36-
key.buffer_name: "macro_c5de351c406a933f1a952846b34e0639.swift"
36+
key.buffer_name: "macro_a05c1290cb88fd731ba683e5aa86fdb7.swift"
3737
}
3838
],
3939
key.category: source.edit.kind.active
@@ -58,7 +58,7 @@
5858
key.endline: 7,
5959
key.endcolumn: 17,
6060
key.text: "{\n get {\n _value.wrappedValue\n }\n\n set {\n _value.wrappedValue = newValue\n }\n}",
61-
key.buffer_name: "macro_695ff4226d699e531ba7bb4cc103005a.swift"
61+
key.buffer_name: "macro_f51f41c03929a32f838d53f437ec0a36.swift"
6262
}
6363
],
6464
key.category: source.edit.kind.active
@@ -71,7 +71,7 @@
7171
key.endline: 7,
7272
key.endcolumn: 12,
7373
key.text: "var _value: MyWrapperThingy<Int>",
74-
key.buffer_name: "macro_629ec828bcb8e81543f19a16c7f1ac1d.swift"
74+
key.buffer_name: "macro_4cb3f1a23406c36f4ca3076cfc889d56.swift"
7575
}
7676
],
7777
key.category: source.edit.kind.active
@@ -96,7 +96,7 @@
9696
key.endline: 7,
9797
key.endcolumn: 3,
9898
key.text: "@Wrapper",
99-
key.buffer_name: "macro_cf98ea58703480a8a577ab40e66fb8e1.swift"
99+
key.buffer_name: "macro_a4c2cd1a73caad3b1eb8f265e55461ee.swift"
100100
}
101101
],
102102
key.category: source.edit.kind.active
@@ -109,7 +109,7 @@
109109
key.endline: 10,
110110
key.endcolumn: 3,
111111
key.text: "@Wrapper",
112-
key.buffer_name: "macro_3ac1e34ba8396a85fb44db3e6f52cc26.swift"
112+
key.buffer_name: "macro_8bd5a8e09442bf0ec36f456f59a93687.swift"
113113
}
114114
],
115115
key.category: source.edit.kind.active
@@ -121,8 +121,8 @@
121121
key.column: 3,
122122
key.endline: 12,
123123
key.endcolumn: 35,
124-
key.text: "struct blah8 {\n func macro_373e16cc0bde29e604b8bfcf27292b376methodfMu_() {\n }\n func macro_373e16cc0bde29e604b8bfcf27292b376methodfMu0_() {\n }\n}\nstruct blah16 {\n func macro_373e16cc0bde29e604b8bfcf27292b376methodfMu1_() {\n }\n func macro_373e16cc0bde29e604b8bfcf27292b376methodfMu2_() {\n }\n}\nstruct blah32 {\n func macro_373e16cc0bde29e604b8bfcf27292b376methodfMu3_() {\n }\n func macro_373e16cc0bde29e604b8bfcf27292b376methodfMu4_() {\n }\n}\nstruct blah64 {\n func macro_373e16cc0bde29e604b8bfcf27292b376methodfMu5_() {\n }\n func macro_373e16cc0bde29e604b8bfcf27292b376methodfMu6_() {\n }\n}",
125-
key.buffer_name: "macro_373e16cc0bde29e604b8bfcf27292b37.swift"
124+
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}",
125+
key.buffer_name: "macro_a01f07de3e9ee8585adf794ccd6dec8c.swift"
126126
}
127127
],
128128
key.category: source.edit.kind.active

0 commit comments

Comments
 (0)