Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cling] Ignore -Wunused-result in wrapped code #12654

Merged

Conversation

jalopezg-git
Copy link
Collaborator

@jalopezg-git jalopezg-git commented Apr 13, 2023

Make FilteringDiagConsumer also ignore -Wunused-result. Whether or not such diagnostic is filtered depends on CompilationOptions::IgnorePromptDiags.

In particular, IgnorePromptDiags should only be enabled for code parsed via Interpreter::EvaluateInternal(). Thus, as of this commit IgnorePromptDiags defaults to 0 in makeDefaultCompilationOpts()

The observable effect of this change is ignoring -Wunused-result for wrapped code, e.g.

[[nodiscard]] int f() { return 0; }

// This yields `warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]`
void g() { f(); }

f(); // but this should not

Alternatively, as discussed with @Axel-Naumann, we could insert #pragma clang diagnositc ... directives in Interpreter::WrapInput(), but I see that as much less elegant.

Checklist:

  • tested changes locally
  • the patch passes cling tests

This PR fixes #11562.

Make `FilteringDiagConsumer` also ignore -Wunused-result. Whether or not
such diagnostic is filtered depends on `CompilationOptions::IgnorePromptDiags`.

In particular, `IgnorePromptDiags` should _only_ be enabled for code parsed
via `Interpreter::EvaluateInternal()`.  Thus, as of this commit `IgnorePromptDiags`
defaults to 0 in `makeDefaultCompilationOpts()`

The observable effect of this change is ignoring `-Wunused-result` for
wrapped code, e.g.
```c++
[[nodiscard]] int f() { return 0; }

// This yields `warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]`
void g() { f(); }

f(); // but this should not
```
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/soversion, ROOT-performance-centos8-multicore/cxx17, ROOT-ubuntu18.04/nortcxxmod, ROOT-ubuntu2004/python3, mac12/noimt, mac11/cxx14, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-debian10-i386/soversion.
Running on pcepsft10.dyndns.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu2004/python3.
Running on root-ubuntu-2004-1.cern.ch:/home/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu18.04/nortcxxmod.
Running on sft-ubuntu-1804-2.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-performance-centos8-multicore/cxx17.
Running on olbdw-01.cern.ch:/data/sftnight/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

Build failed on mac12/noimt.
Running on macphsft17.dyndns.cern.ch:/Users/sftnight/build/jenkins/workspace/root-pullrequests-build
See console output.

Failing tests:

@jalopezg-git
Copy link
Collaborator Author

jalopezg-git commented Apr 13, 2023

Both tests fail due to the change in the default value for IgnorePromptDiags, but see comments below.

Taking a look at the test, to return something from the entry point of a macro seems legal even if the function is void-returning. I guess that's why https://github.com/root-project/root/blob/master/interpreter/cling/lib/Interpreter/IncrementalParser.cpp#L173 is there.
I would vote for either dropping that legacy behavior or always filtering the diagnostic. Any personal preferences, @Axel-Naumann @hahnjo @vgvassilev ?

This seems a legitimate error that was being filtered (see here); I think the test should be fixed.

@phsft-bot
Copy link
Collaborator

Build failed on windows10/cxx14.
Running on null:C:\build\workspace\root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

Build failed on mac11/cxx14.
Running on macphsft20.dyndns.cern.ch:/Users/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@github-actions
Copy link

Test Results

         5 files           5 suites   1d 3h 29m 2s ⏱️
  2 420 tests   2 413 ✔️ 0 💤   7
11 942 runs  11 912 ✔️ 0 💤 30

For more details on these failures, see this check.

Results for commit 4045776.

@pcanal
Copy link
Member

pcanal commented Apr 15, 2023

If I understand correctly, with this patch neither:

// This yields `warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]`
void g() { f(); }

f(); // but this should not

will issue the warning ... which seems wrong.

The issue being addressed is:

root [1] v.size()
ROOT_prompt_1:1:1: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
v.size()
^~~~~~
(unsigned long) 0

where we are in a use case where the warning is literally speaking incorrect since we do use the value and actually print it.

i.e. Semantically, it seems that:

root [1] v.size();

should/could issue the warning while

root [1] v.size()

should definitively not issue the warning.

So a genuine question is "is the fix here 'too broad' ? " and/or "is the "better" fix so expensive that it is better overall to suppress the warning globally?"

@pcanal
Copy link
Member

pcanal commented Apr 15, 2023

Both tests fail due to the change in the default value for IgnorePromptDiags

I agree that both tests should be fixed to not have functions that returns a value when declared to return void.
However

{
   return 0;
}

should continue to work (i.e. return in unnamed macros)

@jalopezg-git
Copy link
Collaborator Author

If I understand correctly, with this patch neither:

// This yields `warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]`
void g() { f(); }

f(); // but this should not

Actually, the first line does issue a warning (which is expected), while it is suppressed in the second case.

The issue being addressed is:

root [1] v.size()
ROOT_prompt_1:1:1: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
v.size()
^~~~~~
(unsigned long) 0

I have pasted below the current state of affairs after applying the patch in this PR (i.e., -Wunused-result, and others should be filtered only for wrapped code).

root [0] std::vector<int> v;
root [1] v.size()
(unsigned long) 0
root [2] // `return` was intentionally ommitted in f() below
root [3] size_t f() { v.size(); }
ROOT_prompt_3:1:14: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
size_t f() { v.size(); }
             ^~~~~~
ROOT_prompt_3:1:24: warning: non-void function does not return a value [-Wreturn-type]
size_t f() { v.size(); }
                       ^

So a genuine question is "is the fix here 'too broad' ? " and/or "is the "better" fix so expensive that it is better overall to suppress the warning globally?"

It's a bit too broad, as it suppresses the warning in wrapped code even if the expression is not part of value capture/printing.

Given that the diagnostic is emitted in SemaExpr when the expression is parsed, the ideal solution would be to buffer diagnostics and then selectively drop this one depending on the source location. But that is probably not worth the additional complexity. 🙂

@pcanal
Copy link
Member

pcanal commented Apr 17, 2023

ok fair enough. This looks good then. Did you also test going through the TMethodCall interfaces?

@jalopezg-git
Copy link
Collaborator Author

However

{
   return 0;
}

should continue to work (i.e. return in unnamed macros)

And I confirm that it does, as code in unnamed macros is wrapped as needed.

@jalopezg-git
Copy link
Collaborator Author

jalopezg-git commented Apr 17, 2023

ok fair enough. This looks good then. Did you also test going through the TMethodCall interfaces?

I think TMethodCall uses the TClingCallFunc interface, and if I am not mistaken, it was addressed by this PR: #9244.

@vgvassilev
Copy link
Member

@wlav, fyi.

@vgvassilev
Copy link
Member

Does having nodiscard avoid stack corruption if we still forget to return a result?

@jalopezg-git
Copy link
Collaborator Author

Does having nodiscard avoid stack corruption if we still forget to return a result?

No, I don't think it has any effect on that.

@jalopezg-git
Copy link
Collaborator Author

jalopezg-git commented Apr 18, 2023

And I confirm that it does, as code in unnamed macros is wrapped as needed.

Unless @Axel-Naumann, @vgvassilev or @hahnjo suggest otherwise, the only thing missing is to fix the two failing tests reported above.

EDIT: sibling roottest PR is root-project/roottest#951.

jalopezg-git added a commit to jalopezg-git/roottest that referenced this pull request Apr 19, 2023
As of PR root-project/root#12654, returning a
non-`void` expression from `void`-returning function is only valid in
an unnamed macro (in which case the diagnostic is filtered).
@jalopezg-git
Copy link
Collaborator Author

@phsft-bot build

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/soversion, ROOT-performance-centos8-multicore/cxx17, ROOT-ubuntu18.04/nortcxxmod, ROOT-ubuntu2004/python3, mac12/noimt, mac11/cxx14, windows10/cxx14
How to customize builds

jalopezg-git added a commit to root-project/roottest that referenced this pull request Apr 20, 2023
As of PR root-project/root#12654, returning a
non-`void` expression from `void`-returning function is only valid in
an unnamed macro (in which case the diagnostic is filtered).
Copy link
Member

@hahnjo hahnjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG

@jalopezg-git jalopezg-git merged commit ade784d into root-project:master Apr 20, 2023
11 of 14 checks passed
@jalopezg-git jalopezg-git deleted the cling-diag-Wunused-result branch April 20, 2023 13:08
jalopezg-git added a commit to jalopezg-git/roottest that referenced this pull request Apr 24, 2023
As of PR root-project/root#12654, returning a
non-`void` expression from `void`-returning function is only valid in
an unnamed macro (in which case the diagnostic is filtered).

Cherry-picked from a905ea5.
jalopezg-git added a commit to root-project/roottest that referenced this pull request Apr 24, 2023
As of PR root-project/root#12654, returning a
non-`void` expression from `void`-returning function is only valid in
an unnamed macro (in which case the diagnostic is filtered).

Cherry-picked from a905ea5.
jalopezg-git added a commit to jalopezg-git/root that referenced this pull request May 19, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
root-project#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
jalopezg-git added a commit to jalopezg-git/root that referenced this pull request May 19, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
root-project#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
jalopezg-git added a commit to jalopezg-git/root that referenced this pull request May 19, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
root-project#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
jalopezg-git added a commit to jalopezg-git/root that referenced this pull request May 19, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
root-project#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
jalopezg-git added a commit that referenced this pull request May 19, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
jalopezg-git added a commit that referenced this pull request May 19, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
FonsRademakers pushed a commit to root-project/cling that referenced this pull request May 19, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
root-project/root#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
enirolf pushed a commit to enirolf/root that referenced this pull request May 26, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
root-project#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
maksgraczyk pushed a commit to maksgraczyk/root that referenced this pull request Jun 28, 2023
This diagnostic should always be promoted to error, regardless of the ignoring
state in `FilteringDiagConsumer`.
This fixes the SourceCall/ErrorMacro.C test.

The failure became visible after merging
root-project#12654, given that `IgnorePromptDiags`
now defaults to 0 in `makeDefaultCompilationOptions()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interpreter warns when calling [[nodiscard]] functions
5 participants