Skip to content

Commit 8f390ea

Browse files
authored
Merge pull request #62363 from hamishknight/the-doctor-doctor-fun
2 parents 2ebddf5 + 9348688 commit 8f390ea

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace swift {
5151
enum class StaticSpellingKind : uint8_t;
5252
enum class DescriptiveDeclKind : uint8_t;
5353
enum DeclAttrKind : unsigned;
54+
enum class StmtKind;
5455

5556
/// Enumeration describing all of possible diagnostics.
5657
///
@@ -116,6 +117,7 @@ namespace swift {
116117
ReferenceOwnership,
117118
StaticSpellingKind,
118119
DescriptiveDeclKind,
120+
DescriptiveStmtKind,
119121
DeclAttribute,
120122
VersionTuple,
121123
LayoutConstraint,
@@ -149,6 +151,7 @@ namespace swift {
149151
ReferenceOwnership ReferenceOwnershipVal;
150152
StaticSpellingKind StaticSpellingKindVal;
151153
DescriptiveDeclKind DescriptiveDeclKindVal;
154+
StmtKind DescriptiveStmtKindVal;
152155
const DeclAttribute *DeclAttributeVal;
153156
llvm::VersionTuple VersionVal;
154157
LayoutConstraint LayoutConstraintVal;
@@ -235,6 +238,10 @@ namespace swift {
235238
: Kind(DiagnosticArgumentKind::DescriptiveDeclKind),
236239
DescriptiveDeclKindVal(DDK) {}
237240

241+
DiagnosticArgument(StmtKind SK)
242+
: Kind(DiagnosticArgumentKind::DescriptiveStmtKind),
243+
DescriptiveStmtKindVal(SK) {}
244+
238245
DiagnosticArgument(const DeclAttribute *attr)
239246
: Kind(DiagnosticArgumentKind::DeclAttribute),
240247
DeclAttributeVal(attr) {}
@@ -341,6 +348,11 @@ namespace swift {
341348
return DescriptiveDeclKindVal;
342349
}
343350

351+
StmtKind getAsDescriptiveStmtKind() const {
352+
assert(Kind == DiagnosticArgumentKind::DescriptiveStmtKind);
353+
return DescriptiveStmtKindVal;
354+
}
355+
344356
const DeclAttribute *getAsDeclAttribute() const {
345357
assert(Kind == DiagnosticArgumentKind::DeclAttribute);
346358
return DeclAttributeVal;

include/swift/AST/Stmt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ class alignas(8) Stmt : public ASTAllocated<Stmt> {
121121
/// to the user of the compiler in any way.
122122
static StringRef getKindName(StmtKind kind);
123123

124+
/// Retrieve the descriptive kind name for a given statement. This is suitable
125+
/// for use in diagnostics.
126+
static StringRef getDescriptiveKindName(StmtKind K);
127+
124128
/// Return the location of the start of the statement.
125129
SourceLoc getStartLoc() const;
126130

lib/AST/DiagnosticEngine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/Pattern.h"
2626
#include "swift/AST/PrintOptions.h"
2727
#include "swift/AST/SourceFile.h"
28+
#include "swift/AST/Stmt.h"
2829
#include "swift/AST/TypeRepr.h"
2930
#include "swift/Basic/SourceManager.h"
3031
#include "swift/Config.h"
@@ -804,6 +805,11 @@ static void formatDiagnosticArgument(StringRef Modifier,
804805
Out << Decl::getDescriptiveKindName(Arg.getAsDescriptiveDeclKind());
805806
break;
806807

808+
case DiagnosticArgumentKind::DescriptiveStmtKind:
809+
assert(Modifier.empty() && "Improper modifier for StmtKind argument");
810+
Out << Stmt::getDescriptiveKindName(Arg.getAsDescriptiveStmtKind());
811+
break;
812+
807813
case DiagnosticArgumentKind::DeclAttribute:
808814
assert(Modifier.empty() &&
809815
"Improper modifier for DeclAttribute argument");

lib/AST/Stmt.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,50 @@ StringRef Stmt::getKindName(StmtKind K) {
4343
llvm_unreachable("bad StmtKind");
4444
}
4545

46+
StringRef Stmt::getDescriptiveKindName(StmtKind K) {
47+
switch (K) {
48+
case StmtKind::Brace:
49+
return "brace";
50+
case StmtKind::Return:
51+
return "return";
52+
case StmtKind::Yield:
53+
return "yield";
54+
case StmtKind::Defer:
55+
return "defer";
56+
case StmtKind::If:
57+
return "if";
58+
case StmtKind::Guard:
59+
return "guard";
60+
case StmtKind::While:
61+
return "while";
62+
case StmtKind::Do:
63+
return "do";
64+
case StmtKind::DoCatch:
65+
return "do-catch";
66+
case StmtKind::RepeatWhile:
67+
return "repeat-while";
68+
case StmtKind::ForEach:
69+
return "for-in";
70+
case StmtKind::Switch:
71+
return "switch";
72+
case StmtKind::Case:
73+
return "case";
74+
case StmtKind::Break:
75+
return "break";
76+
case StmtKind::Continue:
77+
return "continue";
78+
case StmtKind::Fallthrough:
79+
return "fallthrough";
80+
case StmtKind::Fail:
81+
return "return";
82+
case StmtKind::Throw:
83+
return "throw";
84+
case StmtKind::PoundAssert:
85+
return "#assert";
86+
}
87+
llvm_unreachable("Unhandled case in switch!");
88+
}
89+
4690
// Helper functions to check statically whether a method has been
4791
// overridden from its implementation in Stmt. The sort of thing you
4892
// need when you're avoiding v-tables.

0 commit comments

Comments
 (0)