3

I'm trying to use Micosoft's SAL annotation for my project, however I get the following warning, and I don't know why.

As an example, I created a new C++ console application, and have this code:

#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:

warning C6540: The use of attribute annotations on this function will invalidate all of its existing __declspec annotations

In the file "c1xxast"

What am I doing wrong? If I remove either the _Check_return_ or the __in, the warning goes away.

I cannot find any reference to the warning C6550. However the same text can be found here: http://msdn.microsoft.com/en-us/library/dd445322.aspx, but it's not very helpful.

Justin R.
  • 23,435
  • 23
  • 108
  • 157
Kevin Doyon
  • 3,464
  • 2
  • 33
  • 38

2 Answers2

10

The problem may be because you are mixing SAL annotation types. Although made very clear on MSDN, there are two types of SAL annotation: attribute and ... er ... not.

The #defines in <sal.h> VC2005 use the non-attribute versions and start with an underscore followed by a lowercase letter. The newer VC2008 versions expand to compiler attributes and start (and end) with an underscore followed by a capital letter.

You have mixed the two types:

Attribute:

  • _In_
  • _Check_return_

Non-attribute:

  • __in
  • __checkReturn

Try changing your annotations to use a single type consistently.

This blog post explains a bit more about this.

thehouse
  • 7,957
  • 7
  • 33
  • 32
1

You must add SAL annotations to both the declaration and the definition of a method. I'm guessing SAL's upset because it can't find the definition of the method and assumes the attributes are missing.

EDIT Clarification

SAL annotions must appear on both locations for non-abstract methods. For abstract methods SAL will not look for a definition. In certain configurations it will actually ensure that the implementation of the interface has the appropriate notations.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • So that would mean SAL cannot be used with interfaces? – Kevin Doyon Mar 16 '09 at 23:40
  • @Kevin, clarified my answer. In short, SAL can be used with interfaces – JaredPar Mar 16 '09 at 23:45
  • @JaredPar: OK, thanks. It would seem _Check_return with __in parameters is one of those configurations, as making the method pure virtual doesn't remove the warning :( – Kevin Doyon Mar 16 '09 at 23:51
  • @Kevin, interesting. I'm going to read up a bit on that because I've not seen that behavior before. – JaredPar Mar 17 '09 at 00:07
  • @Kevin, have you tried adding an implementation to see if it makes the problem go away? – JaredPar Mar 17 '09 at 00:08
  • @JaredPar: I have just tried. I added the declaration of a "Base" class with the pure virtual method. I then derived another class from "Base" in the header. In the sourcefile, I implemented the Derived::Method. I now get two C6550 warnings:one for the base class declaration, and the derived class. – Kevin Doyon Mar 17 '09 at 00:22