简体   繁体   English

如何检测 C++/COM 中无法访问的代码

[英]How to detect unreachable code in C++/COM

I would like to detect the following problem with unreachable code in C++/COM by using some free tools or Visual Studio 2015.我想通过使用一些免费工具或 Visual Studio 2015 来检测 C++/COM 中无法访问的代码的以下问题。

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):当然,在我的项目中,我将警告级别设置为/W4 ,但此设置只能识别这样的简单情况(也许我错了,但因此是我的问题):

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):当代码更复杂时,编译器不会警告我(请注意,当您在 c# 中编写类似的代码时,您会收到正确的警告):

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):当我们创建更复杂的示例时,我们不会在两种语言 - C++ 和 C# 中收到警告(我的原始示例更加复杂,因为使用了 COM 宏):

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.也许我犯了一个错误,因为我询问了工具(我考虑了 Visual Studio 的一些插件,因为我认为这种功能不是在这个 IDE 中构建的),但我相信存在任何合理的方法来找到这些错误。 My real application has about 3 millions lines of code and my goal is to find and refactor this places.我的实际应用程序有大约 300 万行代码,我的目标是找到并重构这些地方。 Any ideas will be appreciate.任何想法将不胜感激。

Strictly speaking, asking for tools is offtopic on StackOverflow .严格来说,在 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:您可能希望通过以下任一方式启用编译器警告C4702:无法访问的代码(级别 W4):

  • setting level of warnings to /W4 (this, however, enables bunch of other warnings and not necessarily useful ones)将警告级别设置为/W4 (但是,这会启用一堆其他警告,但不一定有用)
  • setting level of warning C4702 to your current level of warnings ( /W3 by default): /W34702将警告 C4702 的级别设置为您当前的警告级别(默认为/W3 ): /W34702
  • enabling this warning with #pragma warning(enable: 4702)使用#pragma warning(enable: 4702)启用此警告#pragma warning(enable: 4702)

See also:也可以看看:

PS This information is very easily accessible with any internet search engine. PS 此信息可通过任何互联网搜索引擎轻松访问。 Next time you have a problem, you might want to try search before posting the question.下次遇到问题时,您可能想在发布问题之前尝试搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM