简体   繁体   English

奇怪的 SAL 注释警告

[英]Strange SAL annotation warning

I'm trying to use Micosoft's SAL annotation for my project, however I get the following warning, and I don't know why.我正在尝试为我的项目使用 Micosoft 的 SAL 注释,但是我收到以下警告,我不知道为什么。

As an example, I created a new C++ console application, and have this code:例如,我创建了一个新的 C++ 控制台应用程序,并具有以下代码:

#include <sal.h>

class Whatever
{
public:
    _Check_return_ int Method(__in int number) ;
};

int main()
{
    return 0;
}

When I compile using Visual Studio 2008, I get the following warning:当我使用 Visual Studio 2008 编译时,我收到以下警告:

warning C6540: The use of attribute annotations on this function will invalidate all of its existing __declspec annotations警告 C6540:在此 function 上使用属性注释将使其所有现有的 __declspec 注释无效

In the file "c1xxast"在文件“c1xxast”中

What am I doing wrong?我究竟做错了什么? If I remove either the _Check_return_ or the __in , the warning goes away.如果我删除_Check_return___in ,警告就会消失。

I cannot find any reference to the warning C6550.我找不到对警告 C6550 的任何引用。 However the same text can be found here: http://msdn.microsoft.com/en-us/library/dd445322.aspx , but it's not very helpful.但是,可以在此处找到相同的文本: http://msdn.microsoft.com/en-us/library/dd445322.aspx ,但这不是很有帮助。

The problem may be because you are mixing SAL annotation types.问题可能是因为您混合了 SAL 注释类型。 Although made very clear on MSDN, there are two types of SAL annotation: attribute and... er... not.虽然在 MSDN 上说得很清楚,但有两种类型的 SAL 注释:属性和......呃......不是。

The #define s in <sal.h> VC2005 use the non-attribute versions and start with an underscore followed by a lowercase letter. <sal.h> VC2005 中的#define使用非属性版本,并以下划线开头,后跟小写字母。 The newer VC2008 versions expand to compiler attributes and start (and end) with an underscore followed by a capital letter.较新的 VC2008 版本扩展到编译器属性,并以下划线后跟大写字母开头(和结尾)。

You have mixed the two types:您混合了两种类型:

Attribute:属性:

  • _In_ _在_
  • _Check_return_ _Check_return_

Non-attribute:非属性:

  • __in __在
  • __checkReturn __checkReturn

Try changing your annotations to use a single type consistently.尝试更改注释以始终使用单一类型。

This blog post explains a bit more about this.这篇博客文章对此进行了更多解释。

You must add SAL annotations to both the declaration and the definition of a method.您必须将 SAL 注释添加到方法的声明和定义中。 I'm guessing SAL's upset because it can't find the definition of the method and assumes the attributes are missing.我猜 SAL 很不高兴,因为它找不到方法的定义并假设缺少属性。

EDIT Clarification编辑澄清

SAL annotions must appear on both locations for non-abstract methods.对于非抽象方法,SAL 注释必须出现在两个位置。 For abstract methods SAL will not look for a definition.对于抽象方法,SAL 不会寻找定义。 In certain configurations it will actually ensure that the implementation of the interface has the appropriate notations.在某些配置中,它实际上将确保接口的实现具有适当的符号。

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

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