简体   繁体   English

gsl :: suppress整个include语句

[英]gsl::suppress whole include statements

I am integrating Guideline Support Library Checkers into a project of mine. 我正在将Guideline Support Library Checkers整合到我的项目中。

Microsoft.CppCoreCheck
Microsoft.Gsl

When I run it I get a bunch of errors from included libraries like standard libraries, glm, boost, etc. 当我运行它时,我从包含的库中获得了一堆错误,如标准库,glm,boost等。

One concrete example is SDL.h where I get warnings in sdl_stdinc.h . 一个具体的例子是SDL.h ,我在sdl_stdinc.h收到警告。 I made sure that I include SDL only via one header under my control: 我确保只通过我控制下的一个标头包含SDL:

ExtSDL.hpp ExtSDL.hpp

#pragma once
#pragma warning(disable: 4710)
#pragma warning(push, 0)
#include <SDL.h>
#pragma warning(pop)

I can not find information on how to exclude this library from the static code analysis. 我找不到有关如何从静态代码分析中排除此库的信息。

The most practical approach I've found so far is to build up #defines like 到目前为止,我发现的最实用的方法是建立#defines类的

#define SDL_WARNINGS 4710 26135

and then #include other people's dirty code thusly 然后#include其他人的脏代码

#pragma warning(push)
#pragma warning(disable: SDL_WARNINGS)
#include <SDL.h>
#pragma warning(pop)

That will silence warnings for gsl checkers, based on their associated warning codes eg C26135 above. 这将基于其相关警告代码(例如上面的C26135)使gsl检查器的警告静音。 It silences the compiler exactly where you want it to keep quiet. 它使编译器完全沉默在你希望它保持安静的位置。 Note the warning disablement is local to the push/pop scope. 请注意,警告禁用是推/弹范围的本地

This sort of approach allows one to compile /Wall /WX even if you turn additional checking on, including gsl. 即使你打开额外的检查,这种方法也允许编译/ Wall / WX,包括gsl。 Critically it works even when you have dependencies on other people's headers which aren't warning clean. 重要的是,即使您对其他人的标题有依赖性而且没有警告干净,它仍然有效。 Sadly this includes every C and C++ standard library implementation I've seen plus Boost, LLVM, the Windows SDK etc. etc. ie basically everything. 遗憾的是,这包括我见过的每个C和C ++标准库实现,加上Boost,LLVM,Windows SDK等等,即基本上所有内容。 Additionally it protects you from evil headers which alter warning pragmas (some standard library implementations used to do this and may still...) This approach allows you to lift your own code to a higher level of cleanliness and quality than the dross you are dependent upon. 此外,它可以保护您免受恶意标题的影响,这些标题会改变警告编译指示(一些标准库实现用于执行此操作并且可能仍然......)此方法允许您将自己的代码提升到比您依赖的渣滓更高的清洁度和质量水平根据。

One of the good things about the Microsoft C++ Core Check stuff is the way they've tied it into the usual mechanisms used for warnings, so this approach works uniformly for regular warnings and checkers in extra rulesets. 关于Microsoft C ++核心检查的一个好处是它们将它与用于警告的常用机制联系起来,因此这种方法可以统一用于常规警告和额外规则集中的检查程序。 Thank goodness they did something like this: some of the gsl checkers are rather questionable and incompatible with many extant coding styles ie turn gsl on for code which #includes big standard vendor library and you rapidly need to construct a long list of warning codes to disable before you can dial the noise down so you can focus on your own code. 谢天谢地,他们做了类似这样的事情:一些gsl检查器相当可疑,并且与许多现存的编码风格不兼容,即转换gsl on代码#includes大标准供应商库,你很快就需要构建一长串警告代码来禁用在您将噪音降低之前,您可以专注于自己的代码。 Of course you can globally #pragma warning(disable: GSL_CHECKERS_YOU_DONT_LIKE) for your own code, so you can focus on the aspects of it you find useful. 当然,您可以为您自己的代码全局#pragma warning(disable: GSL_CHECKERS_YOU_DONT_LIKE) ,这样您就可以专注于您认为有用的方面。

Alternatively you can pick the rules to apply by selecting rulesets to use and/or making a custom one. 或者,您可以通过选择要使用的规则集和/或制作自定义规则来选择要应用的规则。 That's presumably going to minimise your build times which aren't as quick with Code Analysis enabled. 这可能会最大程度地缩短您的构建时间,而这些构建时间并不像启用代码分析那么快。

It would be nice to have a more direct answer to your question which essentially made the dirty headers build quickly because you could disable checkers for "other people's stuff". 对你的问题有一个更直接的答案会很好,这实际上使得脏标题快速构建,因为你可以禁用“其他人的东西”的检查器。 It's an obvious feature request but I'm not aware of it being supported. 这是一个明显的功能请求,但我不知道它被支持。 Presumably it would be pretty trivial to implement eg only run the checkers on source code found in a specified set of directories, so if a #include steps outside that zone the checkers are automatically disabled. 据推测,实现它是非常简单的,例如只对在指定的一组目录中找到的源代码运行检查器,因此如果#include步骤超出该区域,则会自动禁用检查器。 Is anyone at Microsoft reading this? 微软有人在读这篇文章吗?

There are multiple ways to suppress CppCoreCheck warnings: 有多种方法可以抑制CppCoreCheck警告:

  • you can suppress CppCoreChecks either using [[gsl::suppress( chapter )]] attribute, where chapter comes from C++ Core Guidelines , for example, con.4 . 您可以使用[[gsl :: suppress( chapter )]]属性来抑制CppCoreChecks,其中章节来自C ++核心指南 ,例如con.4 Please also look at MS docs for information. 另请参阅MS文档以获取信息。
  • you can use #pragma warning to suppress warnings individually or in bulk, as mentioend above. 您可以使用#pragma警告单独或批量抑制警告,如上面的mentioend。
  • you can suppress all warnings for "not your code" using CAExcludePath . 您可以使用CAExcludePath禁止“不是您的代码”的所有警告。

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

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