2

When browsing the source of the open source .NET Framework 4.7 I stumbled across the C++ header sal.h and found a line of code saying [source_annotation_attribute( SA( Method ) )] which seems to be similar to attributes and the AttributesUsage class in C#.

Now I know that generally, there are no user defined attributes in C++ as there are in C#, and my first guess was that [source_annotation_attribute( SA( Method ) )] is just a macro, but it is neither defined in sal.h nor in any other headers, since sal.h does not #include any.

My next guess is that [source_annotation_attribute] is actually built in the MSVC, just like for e.g. [[noreturn]] attribute.

I would be glad if somebody could shed some light on what it actually is and if I can declare my own attributes similar to that, if it is not built into the compiler. If you want to see for your self, the particular file is \Source\externalapis\legacy\vctools\vc12\inc\vc\sal.h and the attribute occurs (among others) in line 1934.

Here is an example on the usage in sal.h:

[source_annotation_attribute( SA( Method ) )]
struct __M_
{
#ifdef __cplusplus // [
    __M_();
#endif // ]
   int __d_;
};
typedef struct __M_ __M_;

Many thanks in advance.

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • @VTT I added an example on the usage, and I am not sure if that is C++/Cli. It could of course be, but it seems like C++/Cli attributes seem to be [used differently](https://msdn.microsoft.com/en-us/library/yd21828z.aspx) from the ones in `sal.h`. – Thomas Flinkow Jul 10 '17 at 20:00
  • 1
    You should check the block in which this text is present. It has `_MSC_EXTENSIONS` and `_PFT_VER` so it is clearly not a regular c++, maybe it is intended to be used during some special compiler pass. – user7860670 Jul 10 '17 at 20:03
  • @VTT thank you, I believe this is the answer. So I was more right with my second guess, that it is built in to the compiler, too bad. Thank you for your effort. – Thomas Flinkow Jul 10 '17 at 20:07

1 Answers1

1

To conclude what @VTT has already said, it looks like the source_annotation_attribute is a compiler inbuilt construct, which is shipped as part of a Microsoft extension to C++ (even if it is not mentioned there because it is an implementation detail, meant for internal use only) that is valid only when compiled with the compiler switch /Ze

What adds to this is the fact that Microsofts SAL is built in deeply in Visual Studio i.e.

Build -> Run Code Analysis on Solution and since Visual Studio (obviously) uses their MSVC compiler, it is not too implausible that Microsoft would not build any internal constructs like this in their compilers.

Dr. Dre
  • 26
  • 2