This is an archive of the discontinued LLVM Phabricator instance.

[clang][RecoveryExpr] Don't perform alignment check if parameter type is dependent
ClosedPublic

Authored by ArcsinX on Sep 14 2022, 12:16 PM.

Details

Summary

This patch fixes a crash which appears because of getTypeAlignInChars() call with depentent type.

Diff Detail

Event Timeline

ArcsinX created this revision.Sep 14 2022, 12:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 14 2022, 12:16 PM
ArcsinX requested review of this revision.Sep 14 2022, 12:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 14 2022, 12:16 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

This looks similar to the problem fixed in D103825

hokein added inline comments.Sep 14 2022, 1:55 PM
clang/lib/Sema/SemaChecking.cpp
5787

It looks like for the failure case the ParamTy for the parameter is a dependent array type, and it violates the "non-dependent" assumption of clang::ASTContext::getTypeInfoImpl which is called by getTypeAlignInChars in CheckArgAlignment.

so I'd suggest moving the fix to CheckArgAlignment line 5685 (adding a ParamTy->isDependentType() to the if condition).

clang/test/SemaCXX/recovery-expr-type.cpp
171

we can simplify it further:

void m(int (&)[undefined()]) {}
...


S.m(1);
ArcsinX added inline comments.Sep 15 2022, 12:12 AM
clang/lib/Sema/SemaChecking.cpp
5787

When I found this problem I fixed it like you are suggesting. But after that I replaced it with this check, because it seems there is no reason to try to check something on code with errors.

I mean that even if we are not crashing, results from CheckArgAlignment() can't be trusted if we are passing something with errors to it.

hokein added inline comments.Sep 15 2022, 2:13 AM
clang/lib/Sema/SemaChecking.cpp
5787

because it seems there is no reason to try to check something on code with errors.

I think this is case-by-case (for this case, it may be true) -- in some cases (see test7 and test8 for example in recovery-expr-type.cpp), we do want to check something even on the code with errors to emit useful secondary diagnostics. In general we want to emit a full list of diagnostics and at the same time avoid any suspicious diagnostics, however achieving both goals is hard.

Depending on how fatal the error is

  • if the error is fatal, RecoveryError is just a dependent-type wrapper, we should not call check* to emit diagnostics (we're less certain about the quality of these diagnostics), this is mostly done by leveraging on the existing template dependent mechanism;
  • if the error is minor, RecoveryError preserves a concrete type, we might want call check* to emit diagnostics as we're more confident;

The current solution makes sense to fix the crash, but I think the main reason is that CheckArgAlignment now can be invoked with a dependent-type parameter in non-template context (after we extended the "dependent" concept from depending on template parameters to depending on template parameters and errors), so we should fix CheckArgAlignment.

ArcsinX updated this revision to Diff 460358.Sep 15 2022, 3:52 AM
  • Check for dependent type inside CheckArgAlignment()
  • Simplify test
ArcsinX marked 3 inline comments as done.Sep 15 2022, 3:54 AM
ArcsinX added inline comments.
clang/lib/Sema/SemaChecking.cpp
5787

Thanks for clarification. Moved check inside CheckArgAlignment() as you suggested.

hokein accepted this revision.Sep 15 2022, 4:00 AM

Thanks!

This revision is now accepted and ready to land.Sep 15 2022, 4:00 AM
ArcsinX retitled this revision from [clang][RecoveryExpr] Don't perform alignment check if parameter type contains errors to [clang][RecoveryExpr] Don't perform alignment check if parameter type is dependent.Sep 15 2022, 5:48 AM
ArcsinX edited the summary of this revision. (Show Details)
This revision was automatically updated to reflect the committed changes.
ArcsinX marked an inline comment as done.