Skip to content

[AST] Add descriptions for statement kinds #62363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace swift {
enum class StaticSpellingKind : uint8_t;
enum class DescriptiveDeclKind : uint8_t;
enum DeclAttrKind : unsigned;
enum class StmtKind;

/// Enumeration describing all of possible diagnostics.
///
Expand Down Expand Up @@ -116,6 +117,7 @@ namespace swift {
ReferenceOwnership,
StaticSpellingKind,
DescriptiveDeclKind,
DescriptiveStmtKind,
DeclAttribute,
VersionTuple,
LayoutConstraint,
Expand Down Expand Up @@ -149,6 +151,7 @@ namespace swift {
ReferenceOwnership ReferenceOwnershipVal;
StaticSpellingKind StaticSpellingKindVal;
DescriptiveDeclKind DescriptiveDeclKindVal;
StmtKind DescriptiveStmtKindVal;
const DeclAttribute *DeclAttributeVal;
llvm::VersionTuple VersionVal;
LayoutConstraint LayoutConstraintVal;
Expand Down Expand Up @@ -235,6 +238,10 @@ namespace swift {
: Kind(DiagnosticArgumentKind::DescriptiveDeclKind),
DescriptiveDeclKindVal(DDK) {}

DiagnosticArgument(StmtKind SK)
: Kind(DiagnosticArgumentKind::DescriptiveStmtKind),
DescriptiveStmtKindVal(SK) {}

DiagnosticArgument(const DeclAttribute *attr)
: Kind(DiagnosticArgumentKind::DeclAttribute),
DeclAttributeVal(attr) {}
Expand Down Expand Up @@ -341,6 +348,11 @@ namespace swift {
return DescriptiveDeclKindVal;
}

StmtKind getAsDescriptiveStmtKind() const {
assert(Kind == DiagnosticArgumentKind::DescriptiveStmtKind);
return DescriptiveStmtKindVal;
}

const DeclAttribute *getAsDeclAttribute() const {
assert(Kind == DiagnosticArgumentKind::DeclAttribute);
return DeclAttributeVal;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ class alignas(8) Stmt : public ASTAllocated<Stmt> {
/// to the user of the compiler in any way.
static StringRef getKindName(StmtKind kind);

/// Retrieve the descriptive kind name for a given statement. This is suitable
/// for use in diagnostics.
static StringRef getDescriptiveKindName(StmtKind K);

/// Return the location of the start of the statement.
SourceLoc getStartLoc() const;

Expand Down
6 changes: 6 additions & 0 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "swift/AST/Pattern.h"
#include "swift/AST/PrintOptions.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Config.h"
Expand Down Expand Up @@ -804,6 +805,11 @@ static void formatDiagnosticArgument(StringRef Modifier,
Out << Decl::getDescriptiveKindName(Arg.getAsDescriptiveDeclKind());
break;

case DiagnosticArgumentKind::DescriptiveStmtKind:
assert(Modifier.empty() && "Improper modifier for StmtKind argument");
Out << Stmt::getDescriptiveKindName(Arg.getAsDescriptiveStmtKind());
break;

case DiagnosticArgumentKind::DeclAttribute:
assert(Modifier.empty() &&
"Improper modifier for DeclAttribute argument");
Expand Down
44 changes: 44 additions & 0 deletions lib/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,50 @@ StringRef Stmt::getKindName(StmtKind K) {
llvm_unreachable("bad StmtKind");
}

StringRef Stmt::getDescriptiveKindName(StmtKind K) {
switch (K) {
case StmtKind::Brace:
return "brace";
case StmtKind::Return:
return "return";
case StmtKind::Yield:
return "yield";
case StmtKind::Defer:
return "defer";
case StmtKind::If:
return "if";
case StmtKind::Guard:
return "guard";
case StmtKind::While:
return "while";
case StmtKind::Do:
return "do";
case StmtKind::DoCatch:
return "do-catch";
case StmtKind::RepeatWhile:
return "repeat-while";
case StmtKind::ForEach:
return "for-in";
case StmtKind::Switch:
return "switch";
case StmtKind::Case:
return "case";
case StmtKind::Break:
return "break";
case StmtKind::Continue:
return "continue";
case StmtKind::Fallthrough:
return "fallthrough";
case StmtKind::Fail:
return "return";
case StmtKind::Throw:
return "throw";
case StmtKind::PoundAssert:
return "#assert";
}
llvm_unreachable("Unhandled case in switch!");
}

// Helper functions to check statically whether a method has been
// overridden from its implementation in Stmt. The sort of thing you
// need when you're avoiding v-tables.
Expand Down