Skip to content

[Parse/AST] Remove PoundDiagnosticDecl #79857

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 2 commits into from
Mar 10, 2025
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
2 changes: 0 additions & 2 deletions SwiftCompilerSources/Sources/AST/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ final public class TopLevelCodeDecl: Decl {}

final public class ImportDecl: Decl {}

final public class PoundDiagnosticDecl: Decl {}

final public class PrecedenceGroupDecl: Decl {}

final public class MissingDecl: Decl {}
Expand Down
1 change: 0 additions & 1 deletion SwiftCompilerSources/Sources/AST/Registration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public func registerAST() {
registerDecl(ExtensionDecl.self)
registerDecl(TopLevelCodeDecl.self)
registerDecl(ImportDecl.self)
registerDecl(PoundDiagnosticDecl.self)
registerDecl(PrecedenceGroupDecl.self)
registerDecl(MissingDecl.self)
registerDecl(MissingMemberDecl.self)
Expand Down
55 changes: 0 additions & 55 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ enum class DescriptiveDeclKind : uint8_t {
Extension,
EnumCase,
TopLevelCode,
PoundDiagnostic,
PatternBinding,
Var,
Param,
Expand Down Expand Up @@ -839,14 +838,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
HasLazyConformances : 1
);

SWIFT_INLINE_BITFIELD(PoundDiagnosticDecl, Decl, 1+1,
/// `true` if the diagnostic is an error, `false` if it's a warning.
IsError : 1,

/// Whether this diagnostic has already been emitted.
HasBeenEmitted : 1
);

SWIFT_INLINE_BITFIELD(MissingMemberDecl, Decl, 1+2,
NumberOfFieldOffsetVectorEntries : 1,
NumberOfVTableEntries : 2
Expand Down Expand Up @@ -2799,52 +2790,6 @@ class SerializedTopLevelCodeDeclContext : public DeclContext {
}
};

class StringLiteralExpr;

class PoundDiagnosticDecl : public Decl {
SourceLoc StartLoc;
SourceLoc EndLoc;
StringLiteralExpr *Message;
SourceLoc getLocFromSource() const { return StartLoc; }
friend class Decl;
public:
PoundDiagnosticDecl(DeclContext *Parent, bool IsError, SourceLoc StartLoc,
SourceLoc EndLoc, StringLiteralExpr *Message)
: Decl(DeclKind::PoundDiagnostic, Parent), StartLoc(StartLoc),
EndLoc(EndLoc), Message(Message) {
Bits.PoundDiagnosticDecl.IsError = IsError;
Bits.PoundDiagnosticDecl.HasBeenEmitted = false;
}

DiagnosticKind getKind() const {
return isError() ? DiagnosticKind::Error : DiagnosticKind::Warning;
}

StringLiteralExpr *getMessage() const { return Message; }

bool isError() const {
return Bits.PoundDiagnosticDecl.IsError;
}

bool hasBeenEmitted() const {
return Bits.PoundDiagnosticDecl.HasBeenEmitted;
}

void markEmitted() {
Bits.PoundDiagnosticDecl.HasBeenEmitted = true;
}

SourceLoc getEndLoc() const { return EndLoc; };

SourceRange getSourceRange() const {
return SourceRange(StartLoc, EndLoc);
}

static bool classof(const Decl *D) {
return D->getKind() == DeclKind::PoundDiagnostic;
}
};

class OpaqueTypeDecl;

/// ValueDecl - All named decls that are values in the language. These can
Expand Down
1 change: 0 additions & 1 deletion include/swift/AST/DeclExportabilityVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ class DeclExportabilityVisitor
bool visit##KIND##Decl(const KIND##Decl *D) { return true; }
UNINTERESTING(TopLevelCode);
UNINTERESTING(Import);
UNINTERESTING(PoundDiagnostic);
UNINTERESTING(PrecedenceGroup);
UNINTERESTING(EnumCase);
UNINTERESTING(Operator);
Expand Down
1 change: 0 additions & 1 deletion include/swift/AST/DeclNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ ABSTRACT_DECL(Value, Decl)
ITERABLE_GENERIC_DECL(Extension, Decl)
CONTEXT_DECL(TopLevelCode, Decl)
DECL(Import, Decl)
DECL(PoundDiagnostic, Decl)
DECL(PrecedenceGroup, Decl)
DECL(Missing, Decl)
DECL(MissingMember, Decl)
Expand Down
7 changes: 7 additions & 0 deletions include/swift/AST/DiagnosticsCommon.def
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,12 @@ ERROR(bridged_error, none, "%0", (StringRef))
ERROR(bridged_fatal_error, Fatal, "%0", (StringRef))
REMARK(bridged_remark, none, "%0", (StringRef))

//------------------------------------------------------------------------------
// MARK: #warning and #error
//------------------------------------------------------------------------------

WARNING(pound_warning, none, "%0", (StringRef))
ERROR(pound_error, none, "%0", (StringRef))

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
3 changes: 0 additions & 3 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1708,9 +1708,6 @@ ERROR(invalid_autoclosure_pointer_conversion,none,
ERROR(missing_initializer_def,PointsToFirstBadToken,
"initializer requires a body", ())

WARNING(pound_warning, none, "%0", (StringRef))
ERROR(pound_error, none, "%0", (StringRef))

// Attributes

ERROR(operator_not_func,none,
Expand Down
38 changes: 9 additions & 29 deletions include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ class CaseStmt final

/// Switch statement.
class SwitchStmt final : public LabeledStmt,
private llvm::TrailingObjects<SwitchStmt, ASTNode> {
private llvm::TrailingObjects<SwitchStmt, CaseStmt *> {
friend TrailingObjects;

SourceLoc SwitchLoc, LBraceLoc, RBraceLoc;
Expand All @@ -1431,16 +1431,13 @@ class SwitchStmt final : public LabeledStmt,
public:
/// Allocate a new SwitchStmt in the given ASTContext.
static SwitchStmt *create(LabeledStmtInfo LabelInfo, SourceLoc SwitchLoc,
Expr *SubjectExpr,
SourceLoc LBraceLoc,
ArrayRef<ASTNode> Cases,
SourceLoc RBraceLoc,
SourceLoc EndLoc,
ASTContext &C);
Expr *SubjectExpr, SourceLoc LBraceLoc,
ArrayRef<CaseStmt *> Cases, SourceLoc RBraceLoc,
SourceLoc EndLoc, ASTContext &C);

static SwitchStmt *createImplicit(LabeledStmtInfo LabelInfo,
Expr *SubjectExpr, ArrayRef<ASTNode> Cases,
ASTContext &C) {
Expr *SubjectExpr,
ArrayRef<CaseStmt *> Cases, ASTContext &C) {
return SwitchStmt::create(LabelInfo, /*SwitchLoc=*/SourceLoc(), SubjectExpr,
/*LBraceLoc=*/SourceLoc(), Cases,
/*RBraceLoc=*/SourceLoc(), /*EndLoc=*/SourceLoc(),
Expand All @@ -1463,27 +1460,10 @@ class SwitchStmt final : public LabeledStmt,
Expr *getSubjectExpr() const { return SubjectExpr; }
void setSubjectExpr(Expr *e) { SubjectExpr = e; }

ArrayRef<ASTNode> getRawCases() const {
return {getTrailingObjects<ASTNode>(), static_cast<size_t>(Bits.SwitchStmt.CaseCount)};
}

private:
struct AsCaseStmtWithSkippingNonCaseStmts {
AsCaseStmtWithSkippingNonCaseStmts() {}
std::optional<CaseStmt *> operator()(const ASTNode &N) const {
if (auto *CS = llvm::dyn_cast_or_null<CaseStmt>(N.dyn_cast<Stmt*>()))
return CS;
return std::nullopt;
}
};

public:
using AsCaseStmtRange = OptionalTransformRange<ArrayRef<ASTNode>,
AsCaseStmtWithSkippingNonCaseStmts>;

/// Get the list of case clauses.
AsCaseStmtRange getCases() const {
return AsCaseStmtRange(getRawCases(), AsCaseStmtWithSkippingNonCaseStmts());
ArrayRef<CaseStmt *> getCases() const {
return {getTrailingObjects<CaseStmt *>(),
static_cast<size_t>(Bits.SwitchStmt.CaseCount)};
}

/// Retrieve the complete set of branches for this switch statement.
Expand Down
5 changes: 0 additions & 5 deletions include/swift/AST/TypeMemberVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
BAD_MEMBER(PrecedenceGroup)
BAD_MEMBER(Macro)

// These decls are disregarded.
RetTy visitPoundDiagnosticDecl(PoundDiagnosticDecl *D) {
return RetTy();
}

RetTy visitMacroExpansionDecl(MacroExpansionDecl *D) {
// Expansion already visited as auxiliary decls.
return RetTy();
Expand Down
5 changes: 3 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ class Parser {
DeclAttributes &attributes, bool ifConfigsAreDeclAttrs);

/// Parse a #error or #warning diagnostic.
ParserResult<PoundDiagnosticDecl> parseDeclPoundDiagnostic();
ParserStatus parseDeclPoundDiagnostic();

/// Parse a #line/#sourceLocation directive.
/// 'isLine = true' indicates parsing #line instead of #sourcelocation
Expand Down Expand Up @@ -1969,7 +1969,8 @@ class Parser {
ParserResult<CaseStmt> parseStmtCatch();
ParserResult<Stmt> parseStmtForEach(LabeledStmtInfo LabelInfo);
ParserResult<Stmt> parseStmtSwitch(LabeledStmtInfo LabelInfo);
ParserStatus parseStmtCases(SmallVectorImpl<ASTNode> &cases, bool IsActive);
ParserStatus parseStmtCases(SmallVectorImpl<CaseStmt *> &cases,
bool IsActive);
ParserResult<CaseStmt> parseStmtCase(bool IsActive);
ParserResult<Stmt> parseStmtPoundAssert();

Expand Down
4 changes: 0 additions & 4 deletions include/swift/SIL/SILWitnessVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
// We only care about the contained VarDecls.
}

void visitPoundDiagnosticDecl(PoundDiagnosticDecl *pdd) {
// We don't care about diagnostics at this stage.
}

private:
void addAutoDiffDerivativeMethodsIfRequired(AbstractFunctionDecl *AFD,
SILDeclRef::Kind kind) {
Expand Down
2 changes: 0 additions & 2 deletions lib/APIDigester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1935,8 +1935,6 @@ SwiftDeclCollector::addMembersToRoot(SDKNode *Root, IterableDeclContext *Context
// All containing variables should have been handled.
} else if (isa<EnumCaseDecl>(Member)) {
// All containing variables should have been handled.
} else if (isa<PoundDiagnosticDecl>(Member)) {
// All containing members should have been handled.
} else if (isa<DestructorDecl>(Member)) {
// deinit has no impact.
} else if (isa<MissingMemberDecl>(Member)) {
Expand Down
16 changes: 3 additions & 13 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2691,13 +2691,6 @@ namespace {
printFoot();
}

void visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD, Label label) {
printCommon(PDD, "pound_diagnostic_decl", label);
printField(PDD->isError() ? "error" : "warning", Label::always("kind"));
printRec(PDD->getMessage(), Label::optional("message"));
printFoot();
}

void visitPrecedenceGroupDecl(PrecedenceGroupDecl *PGD, Label label) {
printCommon(PGD, "precedence_group_decl", label);
printName(PGD->getName(), Label::optional("name"));
Expand Down Expand Up @@ -3130,12 +3123,9 @@ class PrintStmt : public StmtVisitor<PrintStmt, void, Label>,
void visitSwitchStmt(SwitchStmt *S, Label label) {
printCommon(S, "switch_stmt", label);
printRec(S->getSubjectExpr(), Label::optional("subject_expr"));
printList(S->getRawCases(), [&](ASTNode N, Label label) {
if (N.is<Stmt*>())
printRec(N.get<Stmt*>(), label);
else
printRec(N.get<Decl*>(), label);
}, Label::optional("cases"));
printList(
S->getCases(), [&](CaseStmt *CS, Label label) { printRec(CS, label); },
Label::optional("cases"));
printFoot();
}
void visitCaseStmt(CaseStmt *S, Label label) {
Expand Down
1 change: 0 additions & 1 deletion lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5289,7 +5289,6 @@ ASTMangler::BaseEntitySignature::BaseEntitySignature(const Decl *decl)
case DeclKind::Extension:
case DeclKind::TopLevelCode:
case DeclKind::Import:
case DeclKind::PoundDiagnostic:
case DeclKind::PrecedenceGroup:
case DeclKind::Missing:
case DeclKind::MissingMember:
Expand Down
16 changes: 2 additions & 14 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3522,17 +3522,6 @@ void PrintAST::visitTopLevelCodeDecl(TopLevelCodeDecl *decl) {
printASTNodes(decl->getBody()->getElements(), /*NeedIndent=*/false);
}

void PrintAST::visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
/// TODO: Should we even print #error/#warning?
if (PDD->isError()) {
Printer << tok::pound_error;
} else {
Printer << tok::pound_warning;
}

Printer << "(\"" << PDD->getMessage()->getValue() << "\")";
}

void PrintAST::visitOpaqueTypeDecl(OpaqueTypeDecl *decl) {
// TODO: If we introduce explicit opaque type decls, print them.
assert(decl->getName().empty());
Expand Down Expand Up @@ -5668,9 +5657,8 @@ void PrintAST::visitSwitchStmt(SwitchStmt *stmt) {
visit(stmt->getSubjectExpr());
Printer << " {";
Printer.printNewline();
for (auto N : stmt->getRawCases()) {
if (N.is<Stmt*>())
visit(cast<CaseStmt>(N.get<Stmt*>()));
for (auto *CS : stmt->getCases()) {
visit(CS);
Printer.printNewline();
}
indent();
Expand Down
1 change: 0 additions & 1 deletion lib/AST/ASTScopeCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ class NodeAdder
VISIT_AND_IGNORE(AssociatedTypeDecl)
VISIT_AND_IGNORE(ModuleDecl)
VISIT_AND_IGNORE(ParamDecl)
VISIT_AND_IGNORE(PoundDiagnosticDecl)
VISIT_AND_IGNORE(MissingDecl)
VISIT_AND_IGNORE(MissingMemberDecl)

Expand Down
24 changes: 6 additions & 18 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
return true;
}

bool visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
// By default, ignore #error/#warning.
return false;
}

bool visitOperatorDecl(OperatorDecl *OD) {
return false;
}
Expand Down Expand Up @@ -2068,19 +2063,12 @@ Stmt *Traversal::visitSwitchStmt(SwitchStmt *S) {
else
return nullptr;

for (auto N : S->getRawCases()) {
if (Stmt *aCase = N.dyn_cast<Stmt*>()) {
assert(isa<CaseStmt>(aCase));
if (Stmt *aStmt = doIt(aCase)) {
assert(aCase == aStmt && "switch case remap not supported");
(void)aStmt;
} else
return nullptr;
} else {
assert(isa<PoundDiagnosticDecl>(N.get<Decl*>()));
if (doIt(N.get<Decl*>()))
return nullptr;
}
for (auto *aCase : S->getCases()) {
if (Stmt *aStmt = doIt(aCase)) {
assert(aCase == aStmt && "switch case remap not supported");
(void)aStmt;
} else
return nullptr;
}

return S;
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/Bridging/StmtBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ BridgedSwitchStmt BridgedSwitchStmt_createParsed(
BridgedSourceLoc cLBraceLoc, BridgedArrayRef cCases,
BridgedSourceLoc cRBraceLoc) {
auto &context = cContext.unbridged();
auto cases =
context.AllocateTransform<ASTNode>(cCases.unbridged<BridgedASTNode>(),
[](auto &e) { return e.unbridged(); });
SmallVector<CaseStmt *, 16> cases;
for (auto cCase : cCases.unbridged<BridgedCaseStmt>())
cases.push_back(cCase.unbridged());
return SwitchStmt::create(cLabelInfo.unbridged(), cSwitchLoc.unbridged(),
cSubjectExpr.unbridged(), cLBraceLoc.unbridged(),
cases, cRBraceLoc.unbridged(),
Expand Down
Loading