Skip to content

Commit

Permalink
Fix some crashes.
Browse files Browse the repository at this point in the history
- members_of(^int), bases_of(^int), etc.
- Tree transform that leaves a value-dependent splice in place.

Also replace 'member_of' with 'enumerators_of' in P2996 example.

Tested:
    Ran 'check-all' on MacOS.
  • Loading branch information
katzdm committed Mar 17, 2024
1 parent 704c44e commit 1cefb01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
16 changes: 11 additions & 5 deletions clang/lib/Sema/Metafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ bool get_begin_enumerator_decl_of(APValue &Result, Sema &S, EvalFn Evaluator,
case ReflectionValue::RK_type: {
Decl *D = findTypeDecl(R.getReflectedType());

if (auto enumDecl = dyn_cast<EnumDecl>(D)) {
if (auto enumDecl = dyn_cast_or_null<EnumDecl>(D)) {
if (auto itr = enumDecl->enumerator_begin();
itr != enumDecl->enumerator_end()) {
return SetAndSucceed(Result, makeReflection(*itr));
Expand Down Expand Up @@ -1030,9 +1030,9 @@ bool get_ith_base_of(APValue &Result, Sema &S, EvalFn Evaluator,
case ReflectionValue::RK_type: {
Decl *typeDecl = findTypeDecl(R.getReflectedType());

ensureInstantiated(S, typeDecl, Range);
if (auto cxxRecordDecl = dyn_cast_or_null<CXXRecordDecl>(typeDecl)) {
ensureInstantiated(S, typeDecl, Range);

if (auto cxxRecordDecl = dyn_cast<CXXRecordDecl>(typeDecl)) {
auto numBases = cxxRecordDecl->getNumBases();
if (idx >= numBases)
return SetAndSucceed(Result, Sentinel);
Expand Down Expand Up @@ -1131,6 +1131,8 @@ bool get_begin_member_decl_of(APValue &Result, Sema &S, EvalFn Evaluator,

ensureDeclared(S, QT, Range.getBegin());
Decl *typeDecl = findTypeDecl(QT);
if (!typeDecl)
return true;

DeclContext *declContext = dyn_cast<DeclContext>(typeDecl);
assert(declContext && "no DeclContext?");
Expand Down Expand Up @@ -1962,8 +1964,10 @@ bool is_accessible(APValue &Result, Sema &S, EvalFn Evaluator,

switch (R.getReflection().getKind()) {
case ReflectionValue::RK_type: {
bool Accessible = isAccessible(S, AccessDC,
findTypeDecl(R.getReflectedType()));
NamedDecl *D = findTypeDecl(R.getReflectedType());
if (!D)
return true;
bool Accessible = isAccessible(S, AccessDC, D);
return SetAndSucceed(Result, makeBool(S.Context, Accessible));
}
case ReflectionValue::RK_declaration: {
Expand All @@ -1978,6 +1982,8 @@ bool is_accessible(APValue &Result, Sema &S, EvalFn Evaluator,
case ReflectionValue::RK_base_specifier: {
// TODO(P2996): Need an edge back to derived class.
NamedDecl *ND = findTypeDecl(R.getReflectedBaseSpecifier()->getType());
if (!ND)
return true;
bool Accessible = isAccessible(S, AccessDC, ND);
return SetAndSucceed(Result, makeBool(S.Context, Accessible));
}
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -4451,6 +4451,12 @@ NestedNameSpecifierLoc TreeTransform<Derived>::TransformNestedNameSpecifierLoc(

if (ER.isInvalid())
return NestedNameSpecifierLoc();
else if (ER.get()->isValueDependent()) {
SS.MakeIndeterminateSplice(SemaRef.Context,
cast<CXXIndeterminateSpliceExpr>(ER.get()),
Q.getLocalEndLoc());
break;
}
Expr *Operand = ER.get();

// Should have a non-dependent expression now: Evaluate it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ template <typename E>
requires std::is_enum_v<E>
constexpr std::string enum_to_string(E value) {
std::string result = "<unnamed>";
[:expand(members_of(^E)):] >> [&] <auto e> {
[:expand(enumerators_of(^E)):] >> [&] <auto e> {
if (value == [:e:]) {
result = std::string(name_of(e));
}
Expand All @@ -63,7 +63,7 @@ template <typename E>
requires std::is_enum_v<E>
constexpr std::optional<E> string_to_enum(std::string_view name) {
std::optional<E> result = std::nullopt;
[:expand(members_of(^E)):] >> [&] <auto e> {
[:expand(enumerators_of(^E)):] >> [&] <auto e> {
if (name == name_of(e)) {
result = [:e:];
}
Expand Down

0 comments on commit 1cefb01

Please sign in to comment.