Skip to content

Commit db56ddc

Browse files
authored
Merge pull request #11309 from ahoppen/sil-escape-keywords
Escape all keywords when printing DeclBaseNames
2 parents 61746b3 + db02b1b commit db56ddc

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"
@@ -244,11 +245,21 @@ static void printValueDecl(ValueDecl *Decl, raw_ostream &OS) {
244245

245246
if (Decl->isOperator()) {
246247
OS << '"' << Decl->getBaseName() << '"';
247-
} else if (Decl->getBaseName() == "subscript" ||
248-
Decl->getBaseName() == "deinit") {
249-
OS << '`' << Decl->getBaseName() << '`';
250248
} else {
251-
OS << Decl->getBaseName();
249+
bool shouldEscape = !Decl->getBaseName().isSpecial() &&
250+
llvm::StringSwitch<bool>(Decl->getBaseName().userFacingName())
251+
// FIXME: Represent "init" by a special name and remove this case
252+
.Case("init", false)
253+
#define KEYWORD(kw) \
254+
.Case(#kw, true)
255+
#include "swift/Syntax/TokenKinds.def"
256+
.Default(false);
257+
258+
if (shouldEscape) {
259+
OS << '`' << Decl->getBaseName().userFacingName() << '`';
260+
} else {
261+
OS << Decl->getBaseName().userFacingName();
262+
}
252263
}
253264
}
254265

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)