2017-03-23 49 views

回答

1

最近版本的Clang实现了__has_warning特征检查宏。由于铛只有一个池警告标志的模拟GCC(而不是相反),这是合理使用的代码对GCC功能检查反省:

#if __GNUC__ && defined(__has_warning) 
# if __has_warning("-Wwhatever") 
#  define SUPPRESSING 
#  pragma GCC diagnostic push 
#  pragma GCC diagnostic ignored "-Wwhatever" 
# endif 
#endif 

// Code that trips warning 

#ifdef SUPPRESSING 
# undef SUPPRESSING 
# pragma GCC diagnostic pop 
#endif 

这是一个有点麻烦copypasta的。它可以通过一个包含文件来解决,就像这样:

#define SUPPRESS_WARNING "-Wwhatever" 
#include "suppress_warning.h" 

// Code that trips warning 

#include "unsuppress_warning.h" 

suppress_warning.h是有点棘手,因为__has_warning#pragma不接受宏作为参数。所以,从GithubWandbox demo得到它。

+0

@NathanOliver'defined(__has_warning)'将在此类编译器上评估为“false”。 – Potatoswatter

+0

刚才看到了。评论撤回 – NathanOliver

+0

@NathanOliver这没关系:)我只是留下我的反应,作为护身符反对再次提出的担忧。 – Potatoswatter