This is an archive of the discontinued LLVM Phabricator instance.

[clang] add diagnose when member function contains invalid default argument
ClosedPublic

Authored by HerrCai0907 on Apr 14 2023, 1:24 PM.

Details

Summary

Fixed: https://github.com/llvm/llvm-project/issues/62122
This change pointer to add diagnose message for this code.

struct S {
    static int F(int n = 0 ? 0) {
        return 0;
    }
};

For default parameter, we should set it as unparsed even if meeting
syntax error because it should be issued in real parser time instead of
set is as invalid directly without diagnose.

Diff Detail

Event Timeline

HerrCai0907 created this revision.Apr 14 2023, 1:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2023, 1:24 PM
HerrCai0907 requested review of this revision.Apr 14 2023, 1:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2023, 1:24 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
HerrCai0907 added a subscriber: Restricted Project.Apr 14 2023, 1:54 PM
shafik added a subscriber: shafik.Apr 14 2023, 1:56 PM

Thank you for the fix!

While this will fix the crash I am a little concerned that I don't a diagnostic for each place we call ActOnParamDefaultArgumentError(...), there is only once place where we diagnose this the rest don't. So I wonder if this was purposeful or not.

We are not actually diagnosing the main problem which is a malformed conditional expression. I would hope that we would error out on that. That feels like where we should be applying the fix to me.

If you have malformed conditional expressions in other situation we do diagnose that. I was trying to debug why we don't catch this case but the code around parsing conditional expressions is involved.

inclyc added a subscriber: inclyc.Apr 14 2023, 7:57 PM
shafik added a reviewer: Restricted Project.Apr 14 2023, 9:04 PM

While this will fix the crash I am a little concerned that I don't a diagnostic for each place we call ActOnParamDefaultArgumentError(...), there is only once place where we diagnose this the rest don't. So I wonder if this was purposeful or not.

No, before ActOnParamDefaultArgumentError we either call Diag or use Actions.CorrectDelayedTyposInExpr to diagnoise except here. And the coverage report shows that this is not covered by any test code[1]. So I guess it miss something.

We are not actually diagnosing the main problem which is a malformed conditional expression.

I also want to diagnose main problem. Should I emit diagnose in ConsumeAndStoreInitializer directly (control by a bool argument e.g. IsDiag)?

[1] https://lab.llvm.org/coverage/coverage-reports/coverage/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Parse/ParseDecl.cpp.html#L7388

rsmith added a subscriber: rsmith.Apr 16 2023, 3:47 PM
rsmith added inline comments.
clang/lib/Parse/ParseDecl.cpp
7383–7384

I think we should just remove this reset call. That way, the tokens we've accumulated will get parsed when we come to later process the default argument, and we can produce the proper error at that time, such as diagnosing a missing :. That's what we do for default member initializers.

HerrCai0907 marked an inline comment as done.Apr 17 2023, 4:18 PM
HerrCai0907 added inline comments.
clang/lib/Parse/ParseDecl.cpp
7383–7384

Only remove reset does not work. I think it doesn't need to issue diagnose here. We can add it as a unparsedDefaultArgument and issue it when parsing.

HerrCai0907 edited the summary of this revision. (Show Details)Apr 17 2023, 4:23 PM
rsmith accepted this revision.Apr 25 2023, 9:33 AM

Are there any calls to Sema::ActOnParamDefaultArgumentError left? If not, can you also remove the function?

Otherwise this LGTM, thanks!

This revision is now accepted and ready to land.Apr 25 2023, 9:33 AM

Are there any calls to Sema::ActOnParamDefaultArgumentError left?

Yes, it has.

This revision was landed with ongoing or failed builds.Apr 25 2023, 2:08 PM
This revision was automatically updated to reflect the committed changes.