Skip to content

Commit 4b0d39c

Browse files
committed
AST: Clean up and write better comments for source range assertions in addMember()
1 parent b720c04 commit 4b0d39c

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

lib/AST/DeclContext.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,18 @@ void IterableDeclContext::addMemberPreservingSourceOrder(Decl *member) {
774774
if (existingMember->isImplicit())
775775
continue;
776776

777-
if (isa<EnumCaseDecl>(existingMember) ||
778-
isa<IfConfigDecl>(existingMember))
777+
// An EnumCaseDecl contains one or more EnumElementDecls,
778+
// but the EnumElementDecls are also added as members of
779+
// the parent enum. We ignore the EnumCaseDecl since its
780+
// source range overlaps with that of the EnumElementDecls.
781+
if (isa<EnumCaseDecl>(existingMember))
782+
continue;
783+
784+
// The elements of the active clause of an IfConfigDecl
785+
// are added to the parent type. We ignore the IfConfigDecl
786+
// since its source range overlaps with the source ranges
787+
// of the active elements.
788+
if (isa<IfConfigDecl>(existingMember))
779789
continue;
780790

781791
if (!SM.isBeforeInBuffer(existingMember->getEndLoc(), start))
@@ -819,14 +829,24 @@ void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,
819829
assert(!member->NextDecl && "Already added to a container");
820830

821831
#ifndef NDEBUG
832+
// Assert that new declarations are always added in source order.
822833
auto checkSourceRange = [&](Decl *prev, Decl *next) {
834+
// SKip these checks for imported and deserialized decls.
823835
if (!member->getDeclContext()->getParentSourceFile())
824836
return;
825837

826838
auto shouldSkip = [](Decl *d) {
827-
if (isa<VarDecl>(d) || isa<EnumElementDecl>(d) || isa<IfConfigDecl>(d))
839+
// PatternBindingDecl source ranges overlap with VarDecls,
840+
// EnumCaseDecl source ranges overlap with EnumElementDecls,
841+
// and IfConfigDecl source ranges overlap with the elements
842+
// of the active clause. Skip them all here to avoid
843+
// spurious assertions.
844+
if (isa<PatternBindingDecl>(d) ||
845+
isa<EnumCaseDecl>(d) ||
846+
isa<IfConfigDecl>(d))
828847
return true;
829848

849+
// Ignore source location information of implicit declarations.
830850
if (d->isImplicit())
831851
return true;
832852

@@ -839,8 +859,10 @@ void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,
839859
SourceLoc prevEnd = prev->getEndLoc();
840860
SourceLoc nextStart = next->getStartLoc();
841861

842-
if (!prevEnd.isValid() || !nextStart.isValid())
843-
return;
862+
assert(prevEnd.isValid() &&
863+
"Only implicit decls can have invalid source location");
864+
assert(nextStart.isValid() &&
865+
"Only implicit decls can have invalid source location");
844866

845867
if (getASTContext().SourceMgr.isBeforeInBuffer(prevEnd, nextStart))
846868
return;

0 commit comments

Comments
 (0)