_Attribute_((Format(Printf, 1, 2))) for Msvc

__attribute__((format(printf, 1, 2))) for MSVC?

While GCC checks format specifiers when -Wformat is enabled, VC++ has no such checking, even for standard functions so there is no equivalent to this __attribute__ because there is no equivalent to -Wformat.

I think Microsoft's emphasis on C++ (evidenced by maintaining ISO compliance for C++ while only supporting C89) may be in part the reason why VC++ does not have format specifier checking; in C++ using <iostream> format specifiers are unnecessary.

How should I properly use __attribute__ ((format (printf, x, y))) inside a class method in C++?

You've done it. this is argument 1, so by saying format(printf, 2, 3) you're telling the compiler that you're NOT printing this, you're printing argument 2 (fmt) with additional arguments past that.

Can I enable warnings about incorrectly used specifiers for my own variadic function?

Things like _In_z_ and _Printf_format_string_ are SAL annotation macros. They are recognized by static analysis tools, but they are removed by the preprocessor before the compiler ever sees them. So they are not very useful in your situation.

Some 3rd party compilers implement vendor-specific ways to enable compile-time validation of printf-style parameters on user-defined functions (such as __attribute__(format) and __attribute__(format_arg) in GCC), however Visual C++ is not one of those compilers (see __attribute__((format(printf, 1, 2))) for MSVC?). The VC++ team chose to enable compile-time validations for only the standard printf/scanf family of C runtime functions, as documented on their blog in 2015:

C++ Team Blog: Format Specifiers Checking

By popular request, in Visual Studio 2015 RTM, we’ve implemented the checking of arguments given to printf/scanf and their variations in the C standard library. You can try the examples from this post in our online compiler.

...

Currently, the checking of format specifiers is only done for a predefined set of CRT functions and is not available for user-defined functions that would also benefit from similar checks. If there is enough interest, we will consider extending these warnings to work on such user-defined functions.

If you really want compile-time checking of user-defined variadic functions, use variadic templates instead.

How to get a pointer to a binary section in MSVC?

There is also a way to do this with out using an assembly file.

#pragma section(".init$a")
#pragma section(".init$u")
#pragma section(".init$z")

__declspec(allocate(".init$a")) int InitSectionStart = 0;
__declspec(allocate(".init$z")) int InitSectionEnd = 0;

__declspec(allocate(".init$u")) int token1 = 0xdeadbeef;
__declspec(allocate(".init$u")) int token2 = 0xdeadc0de;

The first 3 line defines the segments. These define the sections and take the place of the assembly file. Unlike the data_seg pragma, the section pragma only create the section.
The __declspec(allocate()) lines tell the compiler to put the item in that segment.

From the microsoft page:
The order here is important. Section names must be 8 characters or less. The sections with the same name before the $ are merged into one section. The order that they are merged is determined by sorting the characters after the $.

Another important point to remember are sections are 0 padded to 256 bytes. The START and END pointers will NOT be directly before and after as you would expect.

If you setup your table to be pointers to functions or other none NULL values, it should be easy to skip NULL entries before and after the table, due to the section padding

See this msdn page for more details

Implement variadic arguments checking for custom string formatting functions

So it seems that I'm indeed out of luck with Visual Studio.

As mentioned by Jonathan in his answer, it is possible to do this with both GCC and Clang. This is also explained in this answer.

However, while Visual Studio seems to output warnings for printf and a bunch of other standard functions, this is more or less hard-coded in the compiler and is not extensible to custom functions.

There is an alternative tho, which I decided not to use (I will explain why). Microsoft provides what they call SAL annotation (for source code annotation language). It is possible to annotate a function with things like _Printf_format_string_ in order to get what I was asking. This is described in this answer for example.

The drawback is, this is by default totally ignored by the compiler. These annotations are actually evaluated only if you enable the Code Analysis, either with the /analysis parameter or from the property window of your project. This analysis does a lot of checks; by default it uses the Microsoft Native Recommended Rules, but it's possible to customize what is to be checked, and even go down to only checking for string formatting.

But even to that point, the overhead in compilation time, for a relatively small project like mine, makes it not worth the pain.



Related Topics



Leave a reply



Submit