@@ -71,16 +71,6 @@ bool SyntacticMacroExpansionInstance::setup(
71
71
return true ;
72
72
}
73
73
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
-
84
74
// Setup ASTContext.
85
75
Ctx.reset (ASTContext::get (
86
76
invocation.getLangOptions (), invocation.getTypeCheckerOptions (),
@@ -132,9 +122,34 @@ MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl(
132
122
Identifier name, const MacroExpansionSpecifier &expansion) {
133
123
auto &ctx = getASTContext ();
134
124
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
+
135
150
// Reuse cached MacroDecl of the same name if it's already created.
136
151
MacroDecl *macro;
137
- auto found = MacroDecls.find (name );
152
+ auto found = MacroDecls.find (macroID );
138
153
if (found != MacroDecls.end ()) {
139
154
macro = found->second ;
140
155
} else {
@@ -144,7 +159,7 @@ MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl(
144
159
/* arrowLoc=*/ {}, /* resultType=*/ nullptr ,
145
160
/* definition=*/ nullptr , /* parent=*/ TheModule);
146
161
macro->setImplicit ();
147
- MacroDecls.insert ({name , macro});
162
+ MacroDecls.insert ({macroID , macro});
148
163
}
149
164
150
165
// Add missing role attributes to MacroDecl.
@@ -175,17 +190,12 @@ MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl(
175
190
// / Create a unique name of the expansion. The result is *appended* to \p out.
176
191
static void addExpansionDiscriminator (SmallString<32 > &out,
177
192
const SourceFile *SF, SourceLoc loc,
178
- MacroDecl *macro ,
193
+ Optional<SourceLoc> supplementalLoc = None ,
179
194
Optional<MacroRole> role = None) {
180
195
SourceManager &SM = SF->getASTContext ().SourceMgr ;
181
- auto lineColumn = SM.getLineAndColumnInBuffer (loc);
182
196
183
197
StableHasher hasher = StableHasher::defaultHasher ();
184
198
185
- // Macro name.
186
- hasher.combine (macro->getName ().getBaseIdentifier ().str ());
187
- hasher.combine (uint8_t {0 });
188
-
189
199
// Module name.
190
200
hasher.combine (SF->getParentModule ()->getName ().str ());
191
201
hasher.combine (uint8_t {0 });
@@ -195,12 +205,20 @@ static void addExpansionDiscriminator(SmallString<32> &out,
195
205
hasher.combine (llvm::sys::path::filename (SF->getFilename ()));
196
206
hasher.combine (uint8_t {0 });
197
207
198
- // Line/column
208
+ // Line/column.
209
+ auto lineColumn = SM.getLineAndColumnInBuffer (loc);
199
210
hasher.combine (lineColumn.first );
200
211
hasher.combine (lineColumn.second );
201
212
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
+
202
220
// Macro role.
203
- if (role) {
221
+ if (role. has_value () ) {
204
222
hasher.combine (*role);
205
223
}
206
224
@@ -218,7 +236,7 @@ expandFreestandingMacro(MacroDecl *macro,
218
236
discriminator.append (" macro_" );
219
237
addExpansionDiscriminator (discriminator,
220
238
expansion->getDeclContext ()->getParentSourceFile (),
221
- expansion->getPoundLoc (), macro );
239
+ expansion->getPoundLoc ());
222
240
223
241
expansion->setMacroRef (macro);
224
242
@@ -243,7 +261,7 @@ expandAttachedMacro(MacroDecl *macro, CustomAttr *attr, Decl *attachedDecl) {
243
261
discriminator.append (" macro_" );
244
262
addExpansionDiscriminator (discriminator,
245
263
target->getDeclContext ()->getParentSourceFile (),
246
- target->getLoc (), macro , role);
264
+ target->getLoc (), attr-> getLocation () , role);
247
265
248
266
SourceFile *expandedSource = swift::evaluateAttachedMacro (
249
267
macro, target, attr, passParent, role, discriminator);
@@ -267,13 +285,15 @@ expandAttachedMacro(MacroDecl *macro, CustomAttr *attr, Decl *attachedDecl) {
267
285
}
268
286
}
269
287
if (roles.contains (MacroRole::Member)) {
270
- evaluate (attachedDecl, /* passParent=*/ false , MacroRole::Member);
288
+ if (isa<IterableDeclContext>(attachedDecl))
289
+ evaluate (attachedDecl, /* passParent=*/ false , MacroRole::Member);
271
290
}
272
291
if (roles.contains (MacroRole::Peer)) {
273
292
evaluate (attachedDecl, /* passParent=*/ false , MacroRole::Peer);
274
293
}
275
294
if (roles.contains (MacroRole::Conformance)) {
276
- evaluate (attachedDecl, /* passParent=*/ false , MacroRole::Conformance);
295
+ if (isa<NominalTypeDecl>(attachedDecl))
296
+ evaluate (attachedDecl, /* passParent=*/ false , MacroRole::Conformance);
277
297
}
278
298
return bufferIDs;
279
299
}
@@ -325,7 +345,7 @@ class MacroExpansionFinder : public ASTWalker {
325
345
326
346
PreWalkAction walkToDeclPre (Decl *D) override {
327
347
// Visit all 'VarDecl' because 'getSourceRangeIncludingAttrs()' doesn't
328
- // include attribute its ranges.
348
+ // include its attribute ranges (because attributes are part of PBD.)
329
349
if (!isa<VarDecl>(D) &&
330
350
!rangeContainsLocToResolve (D->getSourceRangeIncludingAttrs ())) {
331
351
return Action::SkipChildren ();
0 commit comments