Skip to content

Commit

Permalink
Check member access expressions more rigorously.
Browse files Browse the repository at this point in the history
Closes issue #21.
  • Loading branch information
katzdm committed Apr 3, 2024
1 parent 20df57e commit de7203a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
4 changes: 4 additions & 0 deletions clang/lib/CodeGen/CGExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
void VisitPackIndexingExpr(PackIndexingExpr *E) {
Visit(E->getSelectedExpr());
}

void VisitCXXExprSpliceExpr(CXXExprSpliceExpr *E) {
Visit(E->getOperand());
}
};
} // end anonymous namespace.

Expand Down
41 changes: 31 additions & 10 deletions clang/lib/Sema/SemaExprMember.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,15 @@ static bool IsInFnTryBlockHandler(const Scope *S) {
return false;
}

static bool isRecordType(QualType T) {
return T->isRecordType();
}
static bool isPointerToRecordType(QualType T) {
if (const PointerType *PT = T->getAs<PointerType>())
return PT->getPointeeType()->isRecordType();
return false;
}

ExprResult
Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
SourceLocation OpLoc, bool IsArrow,
Expand Down Expand Up @@ -1231,9 +1240,30 @@ Sema::BuildMemberReferenceExpr(Scope *S, Expr *Base, SourceLocation OpLoc,
// Disable access control for the duration of the splice expression
AccessControlScopeGuard guard {*this, true};

bool IsRHSDependent = (RHS->isValueDependent() || RHS->isTypeDependent());
bool IsArrow = (OpKind == tok::arrow);
if (RHS->isValueDependent() || RHS->isTypeDependent())
if (Base) {
const PointerType *PT = Base->getType()->getAs<PointerType>();
bool IsPtr = (PT != nullptr);
bool IsRecord = isRecordType(PT ? PT->getPointeeType() : Base->getType());
if (IsRecord && IsArrow != IsPtr) {
std::string Suggestion = IsPtr ? "." : "->";
Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
<< Base->getType() << int(IsArrow) << Base->getSourceRange()
<< FixItHint::CreateReplacement(OpLoc, Suggestion);
return ExprError();
} else if (!IsRecord && !IsRHSDependent) {
Base->getType()->dump();

This comment has been minimized.

Copy link
@tambry

tambry Apr 8, 2024

Debug leftover?

This comment has been minimized.

Copy link
@katzdm

katzdm Apr 8, 2024

Author Collaborator

Thanks for calling this out; removed.

Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
<< Base->getType() << Base->getSourceRange()
<< RHS->getSourceRange();
return ExprError();
}
}

if (IsRHSDependent) {
return BuildDependentMemberSpliceExpr(Base, OpLoc, IsArrow, RHS);
}

CXXScopeSpec SS;
ValueDecl *VD = nullptr;
Expand Down Expand Up @@ -1321,15 +1351,6 @@ static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
return true;
}

static bool isRecordType(QualType T) {
return T->isRecordType();
}
static bool isPointerToRecordType(QualType T) {
if (const PointerType *PT = T->getAs<PointerType>())
return PT->getPointeeType()->isRecordType();
return false;
}

/// Perform conversions on the LHS of a member access expression.
ExprResult
Sema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Sema/SemaReflect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,15 +642,15 @@ ExprResult Sema::BuildReflectionSpliceExpr(SourceLocation LSplice,
}
case ReflectionValue::RK_const_value: {
Operand = RV.getAsConstValueExpr();
Operand = CXXExprSpliceExpr::Create(Context, VK_PRValue, LSplice, Operand,
RSplice);
if (!isConstantEvaluatedContext()) {
if (!isConstantEvaluatedContext() && !isa<ConstantExpr>(Operand)) {
Operand = ConstantExpr::Create(Context, Operand,
ConstantResultStorageKind::APValue,
true);
ExprEvalContexts.back().ImmediateInvocationCandidates.emplace_back(
cast<ConstantExpr>(Operand), 0);
}
Operand = CXXExprSpliceExpr::Create(Context, VK_PRValue, LSplice, Operand,
RSplice);
break;
}
case ReflectionValue::RK_template: {
Expand Down

0 comments on commit de7203a

Please sign in to comment.