1

I want a tool to parse C / C++ (others welcome, particularly PHP, but C / C++ == priority) and find calls to typed functions which do not check the return value.

I don't see a g++ option (does MS have such an option).

Maybe something like Splint? Or is there an IDE which will let me check this? GUI and CLI solutions are both welcome.

Preferably Linux, but Windows is acceptable.

Must be gratis.

Any ideas?

  • 1
    What do you mean by check the return value? Use it in a predicate? What's the use case for this? – Ira Baxter Sep 22 '15 at 10:04
  • 1
    Then you insist that the result is used in a predicate. Why is Steve's answer the "right one" in that case? – Ira Baxter Sep 22 '15 at 10:08
  • 1
    How can an answer that does not meet your requirement of "checks the the return value" be an answer? Yes, I would have left it marked as unanswered. Yes, Steve's response is probably useful, just not complete with respect to your requirements, so I would not delete it or downvote it. – Ira Baxter Sep 22 '15 at 19:51
  • 1
    Perhaps you don't understand the word "predicate"? Your request appears to be for tool that verifies for a value-returning function foo, that every use of foo is equivalent to the following code: [[x=foo(...); ... if (...x...) ... ]] where ... means "arbitrary code that that doesn't affect x". Steve's answer at best handles detecting that every use of foo is equivalent to [[x=foo(...);]]; there's no test of x required. Those look pretty different to me. – Ira Baxter Sep 23 '15 at 8:46
  • 1
    All that will do is discourage the person with the right answer from providing it; after all, you've already chosen the wrong one. (This isn't intended to be "against" Steve, just against marking the wrong answer.) – Ira Baxter Sep 24 '15 at 1:52
2

In gcc you have the following options:

-Wunused-result Warns if returned value is unused -Wunused warn about a number of unused cases and -Wall (my favorite) turns on a bunch of warnings including -Wunused-result

A full list of gcc warning options can be found here.

You can also use function atributes to set warn_unused_result so as to generate a warning if a specific functions return value is not used.

And of course gcc is free.

  • I don't know how I missed that. Also for g++ ? Only -Wunused-resul or do I need to add __attribute__((warn_unused_result to ecey function declaration? – Mawg Sep 4 '15 at 17:43
  • 1
    I would just give it a try - most gcc flags also apply to g++ – Steve Barnes Sep 4 '15 at 21:23
  • I am posting an answer, because I can't format code in a comment. Obviously, though, I am awarding you the answer and an upvote and my thanks. – Mawg Sep 9 '15 at 6:41
  • "Unused" certainly implies "not checked" but it seems like a only a small subset. – Ira Baxter Sep 22 '15 at 10:05
1

Strangely, neither -Wunused-result nor -Wall worked in g++ v4.6.3. However, the function attribute which Steve suggested did.

int GenerateRandomNumber() __attribute__((warn_unused_result));

int GenerateRandomNumber()
{
   return 4;
}

int main()
{
    GenerateRandomNumber();

    return 0;
}

results in

**** Build of configuration Debug for project unused_return_value ****

make all Building file: ../src/unused_return_value.cpp Invoking: GCC C++ Compiler g++ -O0 -g3 -pedantic -Wall -c -fmessage-length=0 -Wunused-result -MMD -MP -MF"src/unused_return_value.d" -MT"src/unused_return_value.d" -o "src/unused_return_value.o" "../src/unused_return_value.cpp" ../src/unused_return_value.cpp: In function ‘int main()’: ../src/unused_return_value.cpp:10:24: warning: ignoring return value of ‘int GenerateRandomNumber()’, declared with attribute warn_unused_result [-Wunused-result] Finished building: ../src/unused_return_value.cpp Building target: unused_return_value Invoking: GCC C++ Linker g++ -o "unused_return_value" ./src/unused_return_value.o Finished building target: unused_return_value

**** Build Finished ****

I might wrap the attribute in a #define.

I am also pushing strongly to use the "treat warnings as errors" option. I firmly believe that anyone who can craft a compiler knows more about the language than me (I struggled with the Dragon Book), so can never understand when people shrug ans say "it's only a warning".

I will also be pushing to use a Linter. And for code to be compiler and linter warning free before code review (results of both to be submitted with the code) and before release to version control.

Thanks, Steve!

  • 1
    Mawg - I would personally say blocking people from putting things under version control because they still have warnings/lint failures is not the best way to go as it can result in people not using version control - keep them on a branch until the quality criteria are met then allow onto the trunk might be a better way to go. – Steve Barnes Sep 25 '15 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.