-3

I would like to detect the following problem with unreachable code in C++/COM by using some free tools or Visual Studio 2015.

void Foo()
{
   HRESULT hr = E_FAIL;

   // variable hr is not changed here
   // a lot of source code here so detecting this situation manually is not so easy

   if (SUCCEEDED(hr))
   {
      printf("Message."); // unreachable code
   }
}

Edited:

I have edited this question, because I have got same 'down votes', but I did not get a valuable answer. Of course in my project I set level of warning to /W4, but this setting can recognise only simply cases like this (maybe I am wrong but hence my question):

int main()
{
   return 0;

   printf("Message.");

   return 0;
}

When the code is more sophisticated the compiler does not warn me (notice when you write similar code in c# you get a proper warning):

int main()
{
   if (false)
   {
      printf("Message.");
   }

   return 0;
}

When we create even more sophisticated example we do not get the warning in both languages - C++ and C# (my original example is even more sophisticated because uses COM macro):

int main()
{
   bool someCondition = false;

   if (someCondition)
   {
      printf("Message.");
   }

   return 0;
}

Maybe I was made a mistake, because I asked about tools (I thought about some addons for Visual Studio because I think this kind of functionality is not built in this IDE), but I believe exists any reasonable approach to find this bugs. My real application has about 3 millions lines of code and my goal is to find and refactor this places. Any ideas will be appreciate.

rgb
  • 1,750
  • 1
  • 20
  • 35

1 Answers1

2

Strictly speaking, asking for tools is offtopic on StackOverflow. Luckily, you don't need any tools for this, as you have everything what is needed already.

You might want to enable compiler warning C4702: unreachable code (level W4) by either:

  • setting level of warnings to /W4 (this, however, enables bunch of other warnings and not necessarily useful ones)
  • setting level of warning C4702 to your current level of warnings (/W3 by default): /W34702
  • enabling this warning with #pragma warning(enable: 4702)

See also:

P.S. This information is very easily accessible with any internet search engine. Next time you have a problem, you might want to try search before posting the question.

Community
  • 1
  • 1
Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • Thanks for your answer, but higher level of warning does not solve my problem. I have edited my post and provide better explanation of the issue. – rgb Mar 18 '16 at 17:50
  • 1
    @rgb Then you might want to look for static analysis tools. And this is oftopic here. Note that it is hard to prove that some variable won't change it's value before entering some conditional statement, in particular in presence of multiple threads. – Ivan Aksamentov - Drop Mar 18 '16 at 17:56