In Visual C++ you can temporarily disable a warning by using pragma:
#pragma warning(suppress: 4307)
How can I disable a warning within a macro, e.g., when I cause an "integral constant overflow" warning like this:
#define TIMES_A_MILLION(x) x * 1000000
int value = TIMES_A_MILLION(4711);
I don't want to repeat the warning at every place where the macro is used, but want the suppression to be part of the macro it self.
It is obviously not possible to do like this:
#define TIMES_A_MILLION(x) \
#pragma warning(suppress: 4307) \
x * 1000000
In your case you have to use the extension __pragma
__pragma
instead of
#pragma
Related
Is there a way to disable just a single warning line in a cpp file with visual studio?
For example, if I catch an exception and don't handle it, I get error 4101 (unreferenced local variable). Is there a way to ignore this just in that function, but otherwise report it in the compilation unit? At the moment, I put #pragma warning (disable : 4101) at the top of the file, but that obviously just turns it off for the whole unit.
#pragma warning( push )
#pragma warning( disable : 4101)
// Your function
#pragma warning( pop )
If you only want to suppress a warning in a single line of code (after preprocessing)[1], you can use the suppress warning specifier:
#pragma warning(suppress: 4101)
// here goes your single line of code where the warning occurs
For a single line of code, this works the same as writing the following:
#pragma warning(push)
#pragma warning(disable: 4101)
// here goes your code where the warning occurs
#pragma warning(pop)
[1] Others have noted in comments below that if the following statement is an #include statement that the #pragma warning(suppress: 4101) statement would not effectively suppress the warning for every line in the header file. If one were intending to do that, one would need to utilize the push/disable/pop method instead.
#pragma push/pop are often a solution for this kind of problems, but in this case why don't you just remove the unreferenced variable?
try
{
// ...
}
catch(const your_exception_type &) // type specified but no variable declared
{
// ...
}
Example:
#pragma warning(suppress:0000) // (suppress one error in the next line)
This pragma is valid for C++ starting with Visual Studio 2005.
https://msdn.microsoft.com/en-us/library/2c8f766e(v=vs.80).aspx
The pragma is NOT valid for C# through Visual Studio 2005 through Visual Studio 2015.
Error: "Expected disable or restore".
(I guess they never got around to implementing suppress ...)
https://msdn.microsoft.com/en-us/library/441722ys(v=vs.140).aspx
C# needs a different format. It would look like this (but not work):
#pragma warning suppress 0642 // (suppress one error in the next line)
Instead of suppress, you have to disable and enable:
if (condition)
#pragma warning disable 0642
; // Empty statement HERE provokes Warning: "Possible mistaken empty statement" (CS0642)
#pragma warning restore 0642
else
That is SO ugly, I think it is smarter to just re-style it:
if (condition)
{
// Do nothing (because blah blah blah).
}
else
Use #pragma warning ( push ), then #pragma warning ( disable ), then put your code, then use #pragma warning ( pop ) as described here:
#pragma warning( push )
#pragma warning( disable : WarningCode)
// code with warning
#pragma warning( pop )
as #rampion mentioned, if you are in clang gcc, the warnings are by name, not number, and you'll need to do:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
// ..your code..
#pragma clang diagnostic pop
this info comes from here
One may also use UNREFERENCED_PARAMETER defined in WinNT.H. The definition is just:
#define UNREFERENCED_PARAMETER(P) (P)
And use it like:
void OnMessage(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
}
Why would you use it, you might argue that you can just omit the variable name itself. Well, there are cases (different project configuration, Debug/Release builds) where the variable might actually be used. In another configuration that variable stands unused (and hence the warning).
Some static code analysis may still give warning for this non-nonsensical statement (wParam;). In that case, you mayuse DBG_UNREFERENCED_PARAMETER which is same as UNREFERENCED_PARAMETER in debug builds, and does P=P in release build.
#define DBG_UNREFERENCED_PARAMETER(P) (P) = (P)
Instead of putting it on top of the file (or even a header file), just wrap the code in question with #pragma warning (push), #pragma warning (disable) and a matching #pragma warning (pop), as shown here.
Although there are some other options, including #pramga warning (once).
This question comes up as one of the top 3 hits for the Google search for "how to suppress -Wunused-result in c++", so I'm adding this answer here since I figured it out and want to help the next person.
In case your warning/error is -Wunused (or one of its sub-errors) or -Wunused -Werror only, the solution is to cast to void:
For -Wunused or one of its sub-errors only1, you can just cast it to void to disable the warning. This should work for any compiler and any IDE for both C and C++.
1Note 1: see gcc documentation here, for example, for a list of these warnings: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html, then search for the phrase "All the above -Wunused options combined" and look there for the main -Wunused warning and above it for its sub-warnings. The sub-warnings that -Wunused contains include:
-Wunused-but-set-parameter
-Wunused-but-set-variable
-Wunused-function
-Wunused-label
-Wunused-local-typedefs
-Wunused-parameter
-Wno-unused-result
-Wunused-variable
-Wunused-const-variable
-Wunused-const-variable=n
-Wunused-value
-Wunused = contains all of the above -Wunused options combined
Example of casting to void to suppress this warning:
// some "unused" variable you want to keep around
int some_var = 7;
// turn off `-Wunused` compiler warning for this one variable
// by casting it to void
(void)some_var; // <===== SOLUTION! ======
For C++, this also works on functions which return a variable marked with [[nodiscard]]:
C++ attribute: nodiscard (since C++17)
If a function declared nodiscard or a function returning an enumeration or class declared nodiscard by value is called from a discarded-value expression other than a cast to void, the compiler is encouraged to issue a warning.
(Source: https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
So, the solution is to cast the function call to void, as this is actually casting the value returned by the function (which is marked with the [[nodiscard]] attribute) to void.
Example:
// Some class or struct marked with the C++ `[[nodiscard]]` attribute
class [[nodiscard]] MyNodiscardClass
{
public:
// fill in class details here
private:
// fill in class details here
};
// Some function which returns a variable previously marked with
// with the C++ `[[nodiscard]]` attribute
MyNodiscardClass MyFunc()
{
MyNodiscardClass myNodiscardClass;
return myNodiscardClass;
}
int main(int argc, char *argv[])
{
// THE COMPILER WILL COMPLAIN ABOUT THIS FUNCTION CALL
// IF YOU HAVE `-Wunused` turned on, since you are
// discarding a "nodiscard" return type by calling this
// function and not using its returned value!
MyFunc();
// This is ok, however, as casing the returned value to
// `void` suppresses this `-Wunused` warning!
(void)MyFunc(); // <===== SOLUTION! ======
}
Lastly, you can also use the C++17 [[maybe_unused]] attribute: https://en.cppreference.com/w/cpp/language/attributes/maybe_unused.
If you want to disable unreferenced local variable write in some header
template<class T>
void ignore (const T & ) {}
and use
catch(const Except & excpt) {
ignore(excpt); // No warning
// ...
}
In certain situations you must have a named parameter but you don't use it directly.
For example, I ran into it on VS2010, when 'e' is used only inside a decltype statement, the compiler complains but you must have the named varible e.
All the above non-#pragma suggestions all boil down to just adding a single statement:
bool f(int e)
{
// code not using e
return true;
e; // use without doing anything
}
ON_BLOCK_EXIT is a utility macro provided by the ScopeGuard implementation. It defines a local object for the sole reason of having its user-provided destructor executed when it falls out of scope. It is defined as:
#define CONCATENATE_DIRECT(s1, s2) s1##s2
#define CONCATENATE(s1, s2) CONCATENATE_DIRECT(s1, s2)
#define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __LINE__)
#define ON_BLOCK_EXIT ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) = MakeGuard
and can be used like this:
void foo() {
HANDLE hFile = CreateFile( ... );
ON_BLOCK_EXIT( CloseHandle, hFile );
// more...
// ... code...
// ... following
} // warning C4189
When compiled with Visual Studio 2010 the code above generates the following warning C4189: 'scopeGuard3' : local variable is initialized but not referenced.
#pragma warning( suppress : 4189 ) could be used to temporarily disable the warning. This, however, has 2 issues: 1.) It cannot be placed next to the statement that causes the warning but has to go right above the line that ends the scope. 2.) As a corollary to this it will mask out all warnings 4189 resulting from the current scope.
Using Visual Studio 2010 is there any way to disable this specific warning resulting from just those objects created with ON_BLOCK_EXIT (preferably without having to alter the call site, similar to GCC's __attribute__((unused)))?
The solution I finally went for works for Visual Studio 2005 or later:
#define ON_BLOCK_EXIT( ... ) ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) = \
MakeGuard( __VA_ARGS__ ); \
(void)ANONYMOUS_VARIABLE(scopeGuard)
The original macro did not expand to a complete expression so there was no way to append any instrumentation to instruct the compiler to not raise a warning. Using variadic macros provided that option.
Is there a generic suppress warning that i can use?
The problem is that there are times i may build using one compiler version (gcc) and then i have a partner that uses some of the common things but uses a different compiler. So the warning # are different.
The only way i could think of doing was making a macro that was defined in a file that i would pass in some generic value:
SUPPRESS_WARNING_BEGIN(NEVER_USED)
//code
SUPPRESS_WARNING_END
then the file would have something like:
#if COMPILER_A
NEVER_USED = 245
#endif
#if COMPILER_B
NEVER_USED = 332
#endif
#define SUPPRESS_WARNING_BEGIN(x) /
#if COMPILER_A
//Compiler A suppress warning x
#endif
#if COMPILER_B
//Compiler B suppress warning x
#endif
#define SUPPRESS_WARNING_END /
#if COMPILER_A
// END Compiler A suppress warning
#endif
#if COMPILER_B
// END Compiler A suppress warning
#endif
Don't know if there is an easier way? Also i know ideally we all would just use the same compiler but that is unfortunately not an option. Just trying to find the least complicated way to support something like this and am hoping there is a simpler way then mentioned above.
thanks
There's no portable way to do that. Different compilers do it in different ways (e.g. #pragma warning, #pragma GCC diagnostic, etc.).
The easiest and best thing to do is to write code that does not generate any warnings with at compiler at any warning level.
If your goal is to suppress warnings about unused variables, I recommend using a macro:
#define UNUSED(x) ((void)sizeof(x))
...
void some_function(int x, int y)
{
// No warnings will be generated if x is otherwise unused
UNUSED(x);
....
}
The sizeof operator is evaluated at compile-time, and the cast to void produces no result, so any compiler will optimize the UNUSED statement away into nothing but consider the operand to be used.
GCC also has the unused attribute`:
// No warnings will be generated if x is otherwise unused
int x __attribute__((unused));
#include < iostream >
#define MY_CHK_DEF(flag) \
#ifdef (flag) \
std::cout<<#flag<<std::endl; \
#else \
std::cout<<#flag<<" ,flag not define"<<std::endl; \
#endif
int main()
{
MY_CHK_DEF(FLAG_1);
MY_CHK_DEF(FLAG_2);
MY_CHK_DEF(FLAG_3);
...
}
complier report:
main.cpp:3:24: error: '#' is not followed by a macro parameter
any ideas?
Thanks
You can't do it. #if, #else, and #endif must be the first tokens on the logical line. Your definition is just one logical line, so it doesn't work,
You have to do it the other way round(defining the macro for each #if/#ifdef/#else condition(if you nest you have to put a definition on each branch). You probably should define it at every logical branch or it will fail to compile when you try to adjust a rarely adjusted flag. You can #define noops like this. Note to be careful not to wrap expressions with side effects into #define 'd macros that reduce to a noop when the debug flag is on, or your program may not work right.
#define N(x)
#include < iostream >
#ifdef (flag)
#define MY_CHK_DEF(flag)
std::cout<<#flag<<std::endl;
#else
#define MY_CHK_DEF(flag) \
std::cout<<#flag<<" ,flag not define"<<std::endl;
#endif
int main()
{
MY_CHK_DEF(FLAG_1);
MY_CHK_DEF(FLAG_2);
MY_CHK_DEF(FLAG_3);
...
}
C preprocessor is single-pass and #define creates a pretty dumb replacement that isn't further processed - your MY_CHK_DEF(flag) macro inserts the #if statement inline into preprocessed code that is interpreted by C compiler and not valid C.
You can either rephrase it to be one-pass, or if you can't, run through preprocessor twice, manually - once through cpp -P and the second time through normal compilation process.
You actually can do this if you use BOOST processor header lib.. it provides a BOOST_PP_IF macro allow this type of decisions.
http://www.boost.org/doc/libs/1_53_0/libs/preprocessor/doc/ref/if.html
In order to add 'todo' items into my code, I want to put a message in the compiler output.
I would like it to look like this:
c:/temp/main.cpp(104): TODO - add code to implement this
in order to make use of the Visual Studio build output functionality to navigate to the respective line by double-clicking it.
But the __LINE__ macro seems to expand to an int, which disallows writing
#pragma message( __FILE__ "("__LINE__"): ..." )
Would there be another way?
Here is one that allows you to click on the output pane:
(There are also some other nice tips there)
http://www.highprogrammer.com/alan/windev/visualstudio.html
// Statements like:
// #pragma message(Reminder "Fix this problem!")
// Which will cause messages like:
// C:\Source\Project\main.cpp(47): Reminder: Fix this problem!
// to show up during compiles. Note that you can NOT use the
// words "error" or "warning" in your reminders, since it will
// make the IDE think it should abort execution. You can double
// click on these messages and jump to the line in question.
#define Stringize( L ) #L
#define MakeString( M, L ) M(L)
#define $Line MakeString( Stringize, __LINE__ )
#define Reminder __FILE__ "(" $Line ") : Reminder: "
Once defined, use like so:
#pragma message(Reminder "Fix this problem!")
This will create output like:
C:\Source\Project\main.cpp(47): Reminder: Fix this problem!
just whipped this up now, and it sure beats my old solution of using #error :D
#define _STR(x) #x
#define STR(x) _STR(x)
#define TODO(x) __pragma(message("TODO: "_STR(x) " :: " __FILE__ "#" STR(__LINE__)))
you can modify this how ever you like/to whatever suits your needs.
An example of its usage:
//in code somewhere
TODO(Fix this);
output in the console pane:
1>TODO: Fix this :: c:\users\administrator\documents\visual studio 2008\projects\metatest\metatest\metatest.cpp#33
only downside is you can't jump to the line of this (by double clicking the message in the console pane) using __pragma (but testing with #pragma it doesn't seem to be the case anyways...)
On Visual C++ you can just do
#pragma message(__FILE__ "(" _CRT_STRINGIZE(__LINE__) ")" ": warning: [blah]")
_CRT_STRINGIZE is often already defined in some header, but if it's not, you can define it:
#define _CRT_STRINGIZE_(x) #x
#define _CRT_STRINGIZE(x) _CRT_STRINGIZE_(x)
This one allows it to be used without #pragma (Microsoft specific I think) and when you click it takes you to the line since it shows the file and line number just like a regular err/warning message does since none of the other ones seem to do this. This used to work without the __pragma but newer versions of msvc require it. Ive been using it since sometime in the 90's. I use Visual Studio 2013
#define MacroStr(x) #x
#define MacroStr2(x) MacroStr(x)
#define Message(desc) __pragma(message(__FILE__ "(" MacroStr2(__LINE__) ") :" #desc))
example :
Message("Need to add unit testing here")
output:
1> c:\source\include\mithrilsoftware.h(180) :"Need to add unit testing here"
This is an addendum to the answer for those who find it tedious to punch in #pragma directives every-time they need to put a bookmark in the code: You can save a few keystrokes by whipping up a macro to do this for you! While in general, you cannot have a #pragma directive within macros, MS C/C++ compilers 2008 and above do support a special vendor-specific extension called the __pragma which can be used with macros. See Pragma Directives and the __Pragma Keyword.
I use something akin to the following on a daily basis:
#define STR2(x) #x
#define STR1(x) STR2(x)
#define LOC __FILE__ "("STR1(__LINE__)") : Warning Msg: "
#define WARNING_BUILDER(x) __FILE__ "("STR1(__LINE__)") : Warning Msg: " __FUNCTION__ " requires " #x
#define WREVIEW WARNING_BUILDER(review)
#define WUT WARNING_BUILDER(unit-testing)
#ifdef SPECIAL_WARNINGS
#ifdef SPECIAL_WARNINGS_REVIEW
#define MARK_FOR_REVIEW() do { \
__pragma(message( WREVIEW )) \
} while (0)
#else
#define MARK_FOR_REVIEW
#endif
#ifdef SPECIAL_WARNINGS_UNIT_TEST
#define MARK_FOR_UNIT_TEST() do { \
__pragma(message( WUT )) \
} while (0)
#else
#define MARK_FOR_UNIT_TEST
#endif
#endif
// uncomment/set in build-environment to enable special warnings
//#define SPECIAL_WARNINGS
#ifdef SPECIAL_WARNINGS
// uncomment/set in build-environment if you want only code review warnings
//#define SPECIAL_WARNINGS_REVIEW
// uncomment/set in build-environment if you want only unit-test warnings
//#define SPECIAL_WARNINGS_UNIT_TEST
#endif
int main()
{
MARK_FOR_REVIEW();
MARK_FOR_UNIT_TEST();
}
You can easily extend it to suit your needs and add more warnings. The good part of having such a system is that you can selectively turn-on say, only code-review items and not have to worry about anything else by setting the appropriate macro in the build settings.
Use the # token. I've posted an example from MSDN below:
// collisions.h
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __LOC__ __FILE__ "("__STR1__(__LINE__)") : Warning Msg: "
// collisions.cpp
#pragma message(__LOC__"Need to do 3D collision testing")