Skip to content

Commit db02b1b

Browse files
committed
Escape all keywords when printing SIL dotted paths
1 parent 8fa2b96 commit db02b1b

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/SIL/SILPrinter.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "llvm/ADT/SmallString.h"
4343
#include "llvm/ADT/StringExtras.h"
4444
#include "llvm/ADT/StringRef.h"
45+
#include "llvm/ADT/StringSwitch.h"
4546
#include "llvm/Support/CommandLine.h"
4647
#include "llvm/Support/FormattedStream.h"
4748
#include "llvm/Support/FileSystem.h"
@@ -239,11 +240,21 @@ static void printValueDecl(ValueDecl *Decl, raw_ostream &OS) {
239240

240241
if (Decl->isOperator()) {
241242
OS << '"' << Decl->getBaseName() << '"';
242-
} else if (Decl->getBaseName() == "subscript" ||
243-
Decl->getBaseName() == "deinit") {
244-
OS << '`' << Decl->getBaseName() << '`';
245243
} else {
246-
OS << Decl->getBaseName();
244+
bool shouldEscape = !Decl->getBaseName().isSpecial() &&
245+
llvm::StringSwitch<bool>(Decl->getBaseName().userFacingName())
246+
// FIXME: Represent "init" by a special name and remove this case
247+
.Case("init", false)
248+
#define KEYWORD(kw) \
249+
.Case(#kw, true)
250+
#include "swift/Syntax/TokenKinds.def"
251+
.Default(false);
252+
253+
if (shouldEscape) {
254+
OS << '`' << Decl->getBaseName().userFacingName() << '`';
255+
} else {
256+
OS << Decl->getBaseName().userFacingName();
257+
}
247258
}
248259
}
249260

test/SILGen/decls.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,9 @@ func unboundMethodReferences() {
189189
_ = type(of: Derived.method1)
190190
_ = type(of: Derived.method2)
191191
}
192+
193+
// CHECK-LABEL: sil_vtable EscapeKeywordsInDottedPaths
194+
class EscapeKeywordsInDottedPaths {
195+
// CHECK: #EscapeKeywordsInDottedPaths.`switch`!getter.1
196+
var `switch`: String = ""
197+
}

0 commit comments

Comments
 (0)