Skip to content

Commit 1a4d6b6

Browse files
committed
[Macros] Ensure that we escape macro declared names in printing.
Fixes an issue where we couldn't round-trip macro declared names
1 parent af6fe8b commit 1a4d6b6

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ void printWithCompatibilityFeatureChecks(ASTPrinter &printer,
406406
Decl *decl,
407407
llvm::function_ref<void()> printBody);
408408

409+
/// Determine whether we need to escape the given keyword within the
410+
/// given context, by wrapping it in backticks.
411+
bool escapeKeywordInContext(StringRef keyword, PrintNameContext context);
412+
409413
} // namespace swift
410414

411415
#endif // LLVM_SWIFT_AST_ASTPRINTER_H

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,10 @@ ASTPrinter &operator<<(ASTPrinter &printer, tok keyword) {
489489
}
490490

491491
/// Determine whether to escape the given keyword in the given context.
492-
static bool escapeKeywordInContext(StringRef keyword, PrintNameContext context){
493-
492+
bool swift::escapeKeywordInContext(
493+
StringRef keyword,
494+
PrintNameContext context
495+
) {
494496
bool isKeyword = llvm::StringSwitch<bool>(keyword)
495497
#define KEYWORD(KW) \
496498
.Case(#KW, true)

lib/AST/Attr.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,16 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13651365
[&](MacroIntroducedDeclName name) {
13661366
Printer << getMacroIntroducedDeclNameString(name.getKind());
13671367
if (macroIntroducedNameRequiresArgument(name.getKind())) {
1368-
Printer << "(" << name.getIdentifier() << ")";
1368+
StringRef nameText = name.getIdentifier().str();
1369+
bool shouldEscape = escapeKeywordInContext(
1370+
nameText, PrintNameContext::Normal) || nameText == "$";
1371+
Printer << "(";
1372+
if (shouldEscape)
1373+
Printer << "`";
1374+
Printer << nameText;
1375+
if (shouldEscape)
1376+
Printer << "`";
1377+
Printer << ")";
13691378
}
13701379
},
13711380
[&] {

test/ModuleInterface/macros.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@
2121
// CHECK-NEXT: #endif
2222
@attached(accessor) public macro myWrapper() = #externalMacro(module: "SomeModule", type: "Wrapper")
2323

24+
// CHECK: #if compiler(>=5.3) && $Macros && $AttachedMacros
25+
// CHECK: @attached(member, names: named(`init`), prefixed(`$`)) public macro MemberwiseInit() -> () = #externalMacro(module: "SomeModule", type: "MemberwiseInitMacro")
26+
// CHECK-NEXT: #endif
27+
@attached(member, names: named(`init`), prefixed(`$`)) public macro MemberwiseInit() -> () = #externalMacro(module: "SomeModule", type: "MemberwiseInitMacro")
28+
2429
// CHECK-NOT: internalStringify
2530
@freestanding(expression) macro internalStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")

0 commit comments

Comments
 (0)