Skip to content

Commit 54272e5

Browse files
committed
NFC: Replace asserts with if() in SourceLocation accessors
Summary: Nowhere else in the AST classes assert on these kinds of accessors. This way, we can call the accessors and check the validity of the result instead of externally duplicating the conditions. This generality will make it possible to introspect instances for source locations: http://ec2-18-191-7-3.us-east-2.compute.amazonaws.com:10240/z/iiaWhw Reviewers: aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D56354 llvm-svn: 350573
1 parent b460f16 commit 54272e5

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

clang/include/clang/AST/DeclarationName.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,10 @@ struct DeclarationNameInfo {
729729
/// getNamedTypeInfo - Returns the source type info associated to
730730
/// the name. Assumes it is a constructor, destructor or conversion.
731731
TypeSourceInfo *getNamedTypeInfo() const {
732-
assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
733-
Name.getNameKind() == DeclarationName::CXXDestructorName ||
734-
Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
732+
if (Name.getNameKind() != DeclarationName::CXXConstructorName &&
733+
Name.getNameKind() != DeclarationName::CXXDestructorName &&
734+
Name.getNameKind() != DeclarationName::CXXConversionFunctionName)
735+
return nullptr;
735736
return LocInfo.NamedType.TInfo;
736737
}
737738

@@ -747,7 +748,8 @@ struct DeclarationNameInfo {
747748
/// getCXXOperatorNameRange - Gets the range of the operator name
748749
/// (without the operator keyword). Assumes it is a (non-literal) operator.
749750
SourceRange getCXXOperatorNameRange() const {
750-
assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
751+
if (Name.getNameKind() != DeclarationName::CXXOperatorName)
752+
return SourceRange();
751753
return SourceRange(
752754
SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
753755
SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
@@ -766,7 +768,8 @@ struct DeclarationNameInfo {
766768
/// operator name (not the operator keyword).
767769
/// Assumes it is a literal operator.
768770
SourceLocation getCXXLiteralOperatorNameLoc() const {
769-
assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
771+
if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName)
772+
return SourceLocation();
770773
return SourceLocation::
771774
getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
772775
}

clang/include/clang/AST/TemplateBase.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,22 @@ class TemplateArgumentLoc {
530530
}
531531

532532
NestedNameSpecifierLoc getTemplateQualifierLoc() const {
533-
assert(Argument.getKind() == TemplateArgument::Template ||
534-
Argument.getKind() == TemplateArgument::TemplateExpansion);
533+
if (Argument.getKind() != TemplateArgument::Template &&
534+
Argument.getKind() != TemplateArgument::TemplateExpansion)
535+
return NestedNameSpecifierLoc();
535536
return LocInfo.getTemplateQualifierLoc();
536537
}
537538

538539
SourceLocation getTemplateNameLoc() const {
539-
assert(Argument.getKind() == TemplateArgument::Template ||
540-
Argument.getKind() == TemplateArgument::TemplateExpansion);
540+
if (Argument.getKind() != TemplateArgument::Template &&
541+
Argument.getKind() != TemplateArgument::TemplateExpansion)
542+
return SourceLocation();
541543
return LocInfo.getTemplateNameLoc();
542544
}
543545

544546
SourceLocation getTemplateEllipsisLoc() const {
545-
assert(Argument.getKind() == TemplateArgument::TemplateExpansion);
547+
if (Argument.getKind() != TemplateArgument::TemplateExpansion)
548+
return SourceLocation();
546549
return LocInfo.getTemplateEllipsisLoc();
547550
}
548551
};

clang/lib/AST/NestedNameSpecifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
462462
}
463463

464464
TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
465-
assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
466-
Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
467-
"Nested-name-specifier location is not a type");
465+
if (Qualifier->getKind() != NestedNameSpecifier::TypeSpec &&
466+
Qualifier->getKind() != NestedNameSpecifier::TypeSpecWithTemplate)
467+
return TypeLoc();
468468

469469
// The "void*" that points at the TypeLoc data.
470470
unsigned Offset = getDataLength(Qualifier->getPrefix());

0 commit comments

Comments
 (0)