Skip to content

Commit 2e96587

Browse files
committed
Use the mangling prefix @__swiftmacro_ for macro expansion buffers
1 parent 4ae434e commit 2e96587

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/Basic/SourceManager.h"
3030
#include "swift/Basic/StringExtras.h"
3131
#include "swift/Demangling/Demangler.h"
32+
#include "swift/Demangling/ManglingMacros.h"
3233
#include "swift/Parse/Lexer.h"
3334
#include "swift/Subsystems.h"
3435

@@ -310,6 +311,20 @@ ExternalMacroDefinitionRequest::evaluate(
310311
return ExternalMacroDefinition{nullptr};
311312
}
312313

314+
/// Adjust the given mangled name for a macro expansion to produce a valid
315+
/// buffer name.
316+
static std::string adjustMacroExpansionBufferName(StringRef name) {
317+
std::string result;
318+
if (name.startswith(MANGLING_PREFIX_STR)) {
319+
result += MACRO_EXPANSION_BUFFER_MANGLING_PREFIX;
320+
name = name.drop_front(StringRef(MANGLING_PREFIX_STR).size());
321+
}
322+
323+
result += name;
324+
result += ".swift";
325+
return result;
326+
}
327+
313328
bool ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
314329
Decl *decl) const {
315330
auto *parentDecl = decl->getDeclContext()->getAsDecl();
@@ -468,12 +483,12 @@ Expr *swift::expandMacroExpr(
468483

469484
// Figure out a reasonable name for the macro expansion buffer.
470485
std::string bufferName;
471-
{
486+
if (auto expansionExpr = dyn_cast<MacroExpansionExpr>(expr)) {
472487
Mangle::ASTMangler mangler;
473-
if (auto expansionExpr = dyn_cast<MacroExpansionExpr>(expr))
474-
bufferName = mangler.mangleMacroExpansion(expansionExpr);
475-
else
476-
bufferName = "<macro expansion>";
488+
bufferName = adjustMacroExpansionBufferName(
489+
mangler.mangleMacroExpansion(expansionExpr));
490+
} else {
491+
bufferName = "macro-expansion";
477492
}
478493

479494
// Dump macro expansions to standard output, if requested.

lib/Sema/TypeCheckMacros.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include "swift/AST/ConcreteDeclRef.h"
2121
#include "swift/AST/Type.h"
2222

23+
/// Prefix used for the names of macro expansion buffers, to replace the
24+
/// leading "$s" used for Swift manglings.
25+
#define MACRO_EXPANSION_BUFFER_MANGLING_PREFIX "@__swiftmacro_"
26+
2327
namespace swift {
2428

2529
class CustomAttr;

test/Macros/macro_expand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
9494
// expected-note@-1{{in expansion of macro 'addBlocker' here}}
9595
// expected-note@-2{{use '-'}}{{22-23=-}}
9696

97-
// CHECK-DIAGS: macro_expand.swift:[[@LINE-4]]:7-[[@LINE-4]]:27:1:4: error: binary operator '-' cannot be applied to two 'OnlyAdds' operands [] []
98-
// CHECK-DIAGS: CONTENTS OF FILE{{.*}}addBlocker
97+
// CHECK-DIAGS: @__swiftmacro_9MacroUser14testAddBlocker1a1b1c2oaySi_S2iAA8OnlyAddsVtF03addE0fMf1_.swift:1:4: error: binary operator '-' cannot be applied to two 'OnlyAdds' operands [] []
98+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser14testAddBlocker1a1b1c2oaySi_S2iAA8OnlyAddsVtF03addE0fMf1_.swift:
9999
// CHECK-DIAGS-NEXT: Original source range: {{.*}}macro_expand.swift:[[@LINE-6]]:7 - {{.*}}macro_expand.swift:[[@LINE-6]]:27
100100
// CHECK-DIAGS-NEXT: oa - oa
101101
// CHECK-DIAGS-NEXT: END CONTENTS OF FILE

0 commit comments

Comments
 (0)