Skip to content

Commit 9348688

Browse files
committed
[AST] Add descriptions for statement kinds
And plumb through the logic such that the DiagnosticEngine can handle StmtKind. We could introduce a separate DescriptiveStmtKind a la DescriptiveDeclKind, but it would be a 1:1 map, so I'm not convinced it's currently worth doing.
1 parent 8e857d5 commit 9348688

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
@@ -120,6 +120,10 @@ class alignas(8) Stmt : public ASTAllocated<Stmt> {
120120
/// to the user of the compiler in any way.
121121
static StringRef getKindName(StmtKind kind);
122122

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

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
@@ -42,6 +42,50 @@ StringRef Stmt::getKindName(StmtKind K) {
4242
llvm_unreachable("bad StmtKind");
4343
}
4444

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

0 commit comments

Comments
 (0)