C++: S5536 and function pointers

Hello,

It seems that SonarQube is lost with function pointers.
When a function F is called only through a pointer, it is deemed as unused, as all the functions it calls (if these functions are actually called only by F).
Do you confirm ?

Note that for the case I’m working on, in spite of the use of function pointer, the pointer’s value is set statically.

Hi,

Remind me of your context for this? I.e. are you on SonarQube Cloud? SonarQube for IDE (flavor and version)? SonarQube self-managed (flavor and version)?

 
Thx,
Ann

  • SonarCube Cloud
  • SonarScanner CLI 8.0.1.6346
  • Wrapper C++ 6.77
  • SonarQube for IDE 9.6.0.16157

I’ll add that we get MANY S5536 issues these last days.
Did the Soanr developers changed anything recently whic could explain the changed behavior ?

Hi,

Nothing related to this rule is jumping out at me in Jira. It looks like we last worked on it direclty in August. But that doesn’t mean other changes don’t impact it. I’ve flagged this for the language experts.

 
Ann

OK, thank you.

Hi,

Actually, I should have asked you for your analysis log. Can you post that, redacted as necessary?

 
Thx,
Ann

No, this is not how the analyzer is supposed to work. As soon as any reference is made to the function, for instance to take its address, it should be considered as called.

As requested by @ganncamp, the analysis log might help, along with some description of where the function is defined and where its address is taken.

Additionally, it could help if you can also provide a reproducer for the file where the address of the function is taken.

Hi again,

About the S5536 issues…
I just ran a Merge Request (to th emain branch, develop). I’ve got 11 issues. All of type cpp:S5536.
On a file I didn’t even touch…
In the issues card, they’re tagged as found during the analysis, but the last modifications on the concerned lines are several months/years old…

I join you the file in a private message.

Hi @JolyLoic ,

OK, I note your answer.
Nevertheless, as we use Qt, I suspect it is not so easy for Sonar to find the calls…

Is this function called through the signal/slot mechanism?
Are the moc_* files analyzed?

They’re called in an indirect way through MOC files. But we don’t analyse these ones: too many Sonar errors in these generated codes…

The question suprised me. Is there a recommendation to include the moc files in the sonar analysis?

Hi @jsinge,

Not a recommendation, because as @Oodini said, they are full of generated code that would raise many issues. For now, I’m just trying to understand the situation, and especially if the function erroneously identified as never used are actually referenced from one of the analyzed files.

Recommendations will come later, when I have a better understanding.

1 Like

Hi,

If a function is called only through an on_*() function, managed by AutoMOC, this function and all the call graph from this function may be marked as unused.
The problem is when you get a such issue, it may be long to crawl to its Qt source. So these issues are tedious to manage. It should be great to be able to mark a function as a comment to say: “All the the functions called from this function (in a direct or indirect way) should be considered as called, even if this function is deemed as uncalled.”

Maybe a little code example would also help with clarification?

Don’t forget c++ usually offers more than one way to do a thing :wink:

1 Like

I’m not sure about “functions called from this function”, because AFAICT this rule should detect them as used in the first place.

About marking a function with a comment, it is possible. If you mark a function with [[maybe_unused]], it will not be reported by this rule. So I hope this is going to solve your issue.

If it does not, as already requested, I will need more information to actually be able to investigate. Most probably a reproducer for the file where this function is defined, plus the code that triggers its call from the on_* function.

Sonar cannot detect them, because there isn’t any static call. It is injected dynamically by AutoMOC, during the runtime.

I’ll try, but I don’t think it will have a recursive effect. I don’t wish to tag all these functions as [[maybe_unused]]. If the “root” function is removed, I want to be warned that these indirect functions are not called anymore.

There is no static code calling the on_* function. Qt uses a kind of table of pointers to functions.

Hi @Oodini,

I’m shooting a little bit in the dark currently, which makes it hard to give good answers.

As far as I understand, you have:

  • A root function, only called from files that are not analyzed (moc_*), but defined in a file that is analyzed
  • Several functions, called from this root function only, also defined in an analyzed file.

Am I right?
If not, could you provide a reproducer, or a sample project that would exhibit the problematic behavior?

Hello @JolyLoic

You are !

In that case, the expected behavior is the following:

  • The functions called from root() will be detected as used, no matter if root() is used or not, as long as root() is analyzed.
  • root() will be detected as unused, but this can be avoided if it is marked [[maybe_unused]].

As far as I understand, this behavior would be acceptable to you, but this is not what you observe: Other functions called from root are also detected as unused, which makes the use of [[maybe_unused]] unpractical.

It looks like there is an issue when analyzing the definition of root(), which prevents us from seeing the functions it calls. Can we have a reproducer of the file where root() is defined?