In C# we can use #warning to show a warning in error window.
In Visual C++, #pragma message is something like that, but it just a outputs a message not a warning.
In C++ (Windows, Visual Studio, MSVC) is there a way to show a warning to user purposely?
I tried #warning DEBUG is defined which works in some other compilers, but not MSVC.
It gives me this error:
error C1021: invalid preprocessor command 'warning' AecProject stdafx.h 49
In gcc, #warning works fine in the way that you would expect (i.e. like #error it outputs a message at compile time, but does not cause the compilation to terminate)
It seems that #warning is not available in Visual Studio. Instead you can try experimenting with #pragma warning although this does not emit a message but allows the settings for compiler messages to be altered. The syntax for this is more complex and can be found here
I have a hacky solution that works under some conditions with a C++-14 compiler.
With C++-14 compilers that support [[deprecated("...")]], this (somewhat hacky solution) works for MSVC but also for mingw, g++, and clang:
[[deprecated("read the argument to emit_warning")]]
inline int emit_warning(char const*)
{
return 0;
}
static auto w1 = emit_warning("message1");
static auto w2 = emit_warning("message2");
int main()
{
return 0;
}
It has the advantage over #pragma message in that it actually emits a warning, which means that /WX will convert this warning to an error just like any other, and other MSVC flags can be used to turn it off, though the warning that is emitted is C4996, which is use of a deprecated function.
I played around with putting this in a macro, but the warning gets a lot more complex and hides the real issue, which is this warning message. So, while this is a bit hacky, it at least generates a proper warning.
Output with g++ (Linux):
./test1.cc:6:41: warning: ‘int emit_warning(const char*)’ is deprecated: read the argument to emit_warning [-Wdeprecated-declarations]
6 | static auto w1 = emit_warning("message1");
| ^
./test1.cc:2:12: note: declared here
2 | inline int emit_warning(char const*)
| ^~~~~~~~~~~~
./test1.cc:6:41: warning: ‘int emit_warning(const char*)’ is deprecated: read the argument to emit_warning [-Wdeprecated-declarations]
6 | static auto w1 = emit_warning("message1");
| ^
./test1.cc:2:12: note: declared here
2 | inline int emit_warning(char const*)
| ^~~~~~~~~~~~
./test1.cc:7:41: warning: ‘int emit_warning(const char*)’ is deprecated: read the argument to emit_warning [-Wdeprecated-declarations]
7 | static auto w2 = emit_warning("message2");
| ^
./test1.cc:2:12: note: declared here
2 | inline int emit_warning(char const*)
| ^~~~~~~~~~~~
./test1.cc:7:41: warning: ‘int emit_warning(const char*)’ is deprecated: read the argument to emit_warning [-Wdeprecated-declarations]
7 | static auto w2 = emit_warning("message2");
| ^
./test1.cc:2:12: note: declared here
2 | inline int emit_warning(char const*)
| ^~~~~~~~~~~~
Output with MSVC:
cl -nologo -O2 -Zi -Gy -EHsc -MD -TP -GR -W3 -w14996 -c ./test1.cc -Fo./build/test1.obj
test1.cc
./test1.cc(6): warning C4996: 'emit_warning': read the argument to emit_warning
./test1.cc(7): warning C4996: 'emit_warning': read the argument to emit_warning
Unfortunately
With MSVC, the output doesn't include the actual call, so it's a bit clumsy...I suppose you could create a separate deprecated function for each warning whose name communicates information, but that's even more hacky.
Warning 4996 is a level 3 warning by default, so you need either /W3 or /W14996 to even see the warning.
Related
I am not sure if I am the only person or this is the problem of so many other people.
What I suffer from is that for an even simple code that includes LLVM headers regardless of what ever I write in my main function, I get too many warnings when I compile my codes with extra warnings:
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/IR/Instructions.h>
int main()
{
return 0;
}
I cat get these warnings by using either gcc or clang:
g++-9 -std=c++17 main.cpp -Wall -Wextra -I/usr/lib/llvm-9/include -c -o main.o
clang++-9 -std=c++17 main.cpp -Wall -Wextra -I/usr/lib/llvm-9/include -c -o main.o
They are mainly unused parameters:
In file included from /usr/lib/llvm-9/include/llvm/IR/ConstantFolder.h:20,
from /usr/lib/llvm-9/include/llvm/IR/IRBuilder.h:24,
from main.cpp:1:
/usr/lib/llvm-9/include/llvm/IR/Constants.h: In member function ‘llvm::Value* llvm::ConstantData::handleOperandChangeImpl(llvm::Value*, llvm::Value*)’:
/usr/lib/llvm-9/include/llvm/IR/Constants.h:60:41: warning: unused parameter ‘From’ [-Wunused-parameter]
60 | Value *handleOperandChangeImpl(Value *From, Value *To) {
| ~~~~~~~^~~~
/usr/lib/llvm-9/include/llvm/IR/Constants.h:60:54: warning: unused parameter ‘To’ [-Wunused-parameter]
60 | Value *handleOperandChangeImpl(Value *From, Value *To) {
| ~~~~~~~^~
In file included from /usr/lib/llvm-9/include/llvm/IR/ConstantFolder.h:21,
from /usr/lib/llvm-9/include/llvm/IR/IRBuilder.h:24,
from main.cpp:1:
/usr/lib/llvm-9/include/llvm/IR/InstrTypes.h: In member function ‘bool llvm::CallBase::isFnAttrDisallowedByOpBundle(llvm::StringRef) const’:
/usr/lib/llvm-9/include/llvm/IR/InstrTypes.h:1913:47: warning: unused parameter ‘S’ [-Wunused-parameter]
1913 | bool isFnAttrDisallowedByOpBundle(StringRef S) const {
| ~~~~~~~~~~^
In file included from /usr/lib/llvm-9/include/llvm/IR/IRBuilder.h:33,
from main.cpp:1:
/usr/lib/llvm-9/include/llvm/IR/Instructions.h: In member function ‘llvm::BasicBlock* llvm::ReturnInst::getSuccessor(unsigned int) const’:
/usr/lib/llvm-9/include/llvm/IR/Instructions.h:2943:37: warning: unused parameter ‘idx’ [-Wunused-parameter]
2943 | BasicBlock *getSuccessor(unsigned idx) const {
| ~~~~~~~~~^~~
...
I just truncate these warnings. They are too many.
When I turn on the -Wconversion the problem becomes even worse:
In file included from /usr/lib/llvm-9/include/llvm/Support/MathExtras.h:17,
from /usr/lib/llvm-9/include/llvm/ADT/SmallVector.h:19,
from /usr/lib/llvm-9/include/llvm/ADT/STLExtras.h:20,
from /usr/lib/llvm-9/include/llvm/ADT/StringRef.h:12,
from /usr/lib/llvm-9/include/llvm/ADT/StringMap.h:16,
from /usr/lib/llvm-9/include/llvm/Support/Host.h:16,
from /usr/lib/llvm-9/include/llvm/ADT/Hashing.h:48,
from /usr/lib/llvm-9/include/llvm/ADT/ArrayRef.h:12,
from /usr/lib/llvm-9/include/llvm/IR/IRBuilder.h:18,
from main.cpp:1:
/usr/lib/llvm-9/include/llvm/Support/SwapByteOrder.h: In function ‘uint16_t llvm::sys::SwapByteOrder_16(uint16_t)’:
/usr/lib/llvm-9/include/llvm/Support/SwapByteOrder.h:36:23: warning: conversion from ‘int’ to ‘uint16_t’ {aka ‘short unsigned int’} may change value [-Wconversion]
36 | uint16_t Hi = value << 8;
| ~~~~~~^~~~
/usr/lib/llvm-9/include/llvm/Support/SwapByteOrder.h:37:23: warning: conversion from ‘int’ to ‘uint16_t’ {aka ‘short unsigned int’} may change value [-Wconversion]
37 | uint16_t Lo = value >> 8;
| ~~~~~~^~~~
In file included from /usr/lib/llvm-9/include/llvm/ADT/STLExtras.h:20,
from /usr/lib/llvm-9/include/llvm/ADT/StringRef.h:12,
from /usr/lib/llvm-9/include/llvm/ADT/StringMap.h:16,
from /usr/lib/llvm-9/include/llvm/Support/Host.h:16,
from /usr/lib/llvm-9/include/llvm/ADT/Hashing.h:48,
from /usr/lib/llvm-9/include/llvm/ADT/ArrayRef.h:12,
from /usr/lib/llvm-9/include/llvm/IR/IRBuilder.h:18,
from main.cpp:1:
/usr/lib/llvm-9/include/llvm/ADT/SmallVector.h: In constructor ‘llvm::SmallVectorBase::SmallVectorBase(void*, size_t)’:
/usr/lib/llvm-9/include/llvm/ADT/SmallVector.h:45:35: warning: conversion from ‘size_t’ {aka ‘long unsigned int’} to ‘unsigned int’ may change value [-Wconversion]
45 | : BeginX(FirstEl), Capacity(TotalCapacity) {}
| ^~~~~~~~~~~~~
...
I get these warnings by clang too:
...
In file included from main.cpp:1:
In file included from /usr/lib/llvm-9/include/llvm/IR/IRBuilder.h:18:
In file included from /usr/lib/llvm-9/include/llvm/ADT/ArrayRef.h:12:
/usr/lib/llvm-9/include/llvm/ADT/Hashing.h:190:15: warning: implicit conversion changes signedness: 'const char' to 'uint8_t' (aka 'unsigned char') [-Wsign-conversion]
uint8_t a = s[0];
~ ^~~~
/usr/lib/llvm-9/include/llvm/ADT/Hashing.h:191:15: warning: implicit conversion changes signedness: 'const char' to 'uint8_t' (aka 'unsigned char') [-Wsign-conversion]
uint8_t b = s[len >> 1];
~ ^~~~~~~~~~~
/usr/lib/llvm-9/include/llvm/ADT/Hashing.h:192:15: warning: implicit conversion changes signedness: 'const char' to 'uint8_t' (aka 'unsigned char') [-Wsign-conversion]
uint8_t c = s[len - 1];
~ ^~~~~~~~~~
...
This is just a MWE. Adding more headers, I can get other types of warnings such as
/usr/lib/llvm-9/include/llvm/ADT/Twine.h:232:16: warning: ‘<anonymous>.llvm::Twine::RHS.llvm::Twine::Child::twine’ may be used uninitialized in this function [-Wmaybe-uninitialized]
!RHS.twine->isBinary())
~~~~^~~~~
Now, I am wondering whether LLVM is written carelessly? I always turn on all warnings and make sure my application compiles with 0 warnings. When I do this while using LLVM all warnings of my code are burried under LLVM ones. I cannot easily see them. Usually the first advise from people is to turn off those warnings switches. I strictly resist that. I want to find them for my application. The alternative solution comes to my mind is to turn off these warnings only for LLVM but not for my application. Is this possible especially when using CMake?
The issue of system header files causing warnings is a common one. It is not really for me to judge whether the authors of those headers were "careless" or not, but there is a relatively simple way to disable specific warnings before including the 'offending' headers, then restore your 'full' warnings once they have been included.
For clang you can do this with various #pragma diagnostic ... lines, as shown in the following code snippet:
#if defined (__clang__)
#pragma clang diagnostic push // Saves current diagnostic settings
#pragma clang diagnostic ignored "-Wsign-conversion" // Ignore this warning
#pragma clang diagnostic ignored "-Wunused-parameter" // and this one...
// ... Add similar lines for other warnings you wish to disable
#endif
// Now include the 'offending' headers ...
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/IR/Instructions.h>
#if defined(__clang__)
#pragma clang diagnostic pop // Restores the saved settings
#endif
I don't have access to a GCC compiler but I believe very similar directives can be used: just substitute GCC for clang in the lines where that occurs. More information on such #pragma directives (for the GCC versions) can be found here.
It is dependent on your GCC/clang/OS installation. On Fedora 32 it works as expected out of the box with system gcc-10.2.1-1.fc32.x86_64 + system clang-10.0.0-2.fc32.x86_64:
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/IR/Instructions.h>
static void func(int val) {}
int main()
{
func(42);
return 0;
}
For GCC:
$ g++ -o stackoverflowllvmwarnings stackoverflowllvmwarnings.C $(llvm-config --cflags --libs) -std=c++17 -Wall -Wextra
stackoverflowllvmwarnings.C: In function ‘void func(int)’:
stackoverflowllvmwarnings.C:7:22: warning: unused parameter ‘val’ [-Wunused-parameter]
7 | static void func(int val) {}
| ~~~~^~~
$ _
For clang:
$ clang++ -o stackoverflowllvmwarnings stackoverflowllvmwarnings.C $(llvm-config --cflags --libs) -std=c++17 -Wall -Wextra
stackoverflowllvmwarnings.C:7:22: warning: unused parameter 'val' [-Wunused-parameter]
static void func(int val) {}
^
1 warning generated.
$ _
If you really want (you do not) the warnings even for system headers use Wsystem-headers (it works similar way both for gcc and clang):
$ clang++ -o stackoverflowllvmwarnings stackoverflowllvmwarnings.C $(llvm-config --cflags --libs) -std=c++17 -Wall -Wextra -Wsystem-headers
In file included from stackoverflowllvmwarnings.C:1:
In file included from /usr/include/llvm/IR/IRBuilder.h:17:
In file included from /usr/include/llvm-c/Types.h:17:
In file included from /usr/include/llvm-c/DataTypes.h:28:
In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/cmath:42:
/usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/bits/cpp_type_traits.h:110:12: warning: keyword '__is_void' will be made available as an identifier for the remainder of the translation unit [-Wkeyword-compat]
struct __is_void
^
...
I'm trying to compile NRF driver with MinGW 4.8.2. I'm getting this error:
In file included from
b:/nrfdriver/sdk/nRF51_SDK_8.1.0_b6ed55f/components/device/nrf51.h:119:0,
from b:/nrfdriver/sdk/nRF51_SDK_8.1.0_b6ed55f/components/softdevice/s130/headers/nrf_soc.h:50,
from b:/nrfdriver/pc-ble-driver-0.5.0/driver/inc_override/nrf_soc.h:21,
from b:/nrfdriver/pc-ble-driver-0.5.0/driver/inc_override/app_util_platform.h:26,
from b:/nrfdriver/sdk/nRF51_SDK_8.1.0_b6ed55f/components/drivers_nrf/uart/app_uart.h:27,
from b:\nrfdriver\pc-ble-driver-0.5.0\driver\src\app_uart_pc.c:13:
C:/MinGW/mingw64/lib/gcc/x86_64-w64-mingw32/4.8.2/include/xmmintrin.h:
In function 'void _mm_setcsr(unsigned int)':
b:/nrfdriver/sdk/nRF51_SDK_8.1.0_b6ed55f/components/toolchain/gcc/core_cm0.h:164:21:
error: expected primary-expression before 'volatile' #define __I
volatile /*!< Defines 'read only' permissions */
^ driver\CMakeFiles\s130_nrf51_ble_driver.dir\build.make:297: recipe for
target 'driver/CMakeFiles/s130_nrf51_ble_driver.dir/src/app_uart_pc.c
.obj' failed
Error comes from this line:
#define __I volatile /*!< Defines 'read only' permissions */
How can such a simple #define lead to a compiler error and any idea how I should fix that? (upgrading the compiler is not an option as this version of the driver is supposed to be built with this version of MinGW).
Note that the driver is meant to be built in 32bits and I'm trying to build it targetting 64bits, dunno if that could be the cause of the problem...
What if we also look in the xmmintrin.h header?
/* Set the control register to I. */
extern __inline void __attribute__((__gnu_inline__, __always_inline__, __artificial__))
_mm_setcsr (unsigned int __I)
{
__builtin_ia32_ldmxcsr (__I);
}
Ouch, another use of __I.
In c++ primer(5th), it mentioned:
When used with variables of built-in type, this form of initialization
has one
important property: The compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss
of information:
longdouble ld = 3.1415926536;
int a{ld}, b = {ld}; // error: narrowing conversion required
int c(ld), d = ld; // ok: but value will be truncate
I compile the code using gcc4.8.1 , it only give a warning rather than an error.
g++ -W -Wall -Wextra -pedantic -std=c++0x -o m main.cpp
main.cpp: In function ‘int main()’:
main.cpp:64:13: warning: narrowing conversion of ‘ld’ from ‘long double’ to ‘int’ inside { } [-Wnarrowing]
int a{ld}, b= {ld};
^
main.cpp:64:22: warning: narrowing conversion of ‘ld’ from ‘long double’ to ‘int’ inside { } [-Wnarrowing]
int a{ld}, b= {ld};
Is there any flags that will turn on the feature of the important property ?
A quick search for "gcc diagnostic flag" turns up documentation resources.
Inside your program, you could do this:
#ifdef __GNUC__
# pragma GCC diagnostic error "-Wnarrowing"
#endif
There is a command-line option too: -Werror=narrowing, but since you want to alter the semantic meaning of the program itself according to GCC, putting it in the source code is probably more appropriate.
Note, when it makes a difference other than simple well-formedness, such as in overload selection, GCC does diagnose the condition correctly.
The standard never calls for errors or for warnings: the standard only requires an implementation to issue a diagnostic. Whether such a diagnostic takes the form of a compiler error, or a warning, or something entirely different from them both, is outside the scope of the standard.
I'm getting some strange error from within boost::asio when simply including the asio.hpp header file, which then includes other headers till win_static_mutex.ippwithin which it generates an conversion error.
I use MinGW with GCC 4.7 with the -std=gnu++11 compiler flag (see here why gnu++11 and not c++11). The error occurs in boost 1.49, 1.50 and 1.51 (current).
The full error reads as following:
In file included from boost1.49/boost/asio/detail/win_static_mutex.hpp:71:0,
from boost1.49/boost/asio/detail/static_mutex.hpp:23,
from boost1.49/boost/asio/detail/impl/signal_set_service.ipp:24,
from boost1.49/boost/asio/detail/signal_set_service.hpp:210,
from boost/asio/signal_set_service.hpp:19,
from boost/asio/basic_signal_set.hpp:24,
from boost/asio.hpp:26,
from MyCode\Client.cpp:3:
boost1.49/boost/asio/detail/impl/win_static_mutex.ipp: In member function 'int boost::asio::detail::win_static_mutex::do_init()':
boost1.49/boost/asio/detail/impl/win_static_mutex.ipp:51:63: error: invalid conversion from 'const wchar_t*' to 'size_t {aka unsigned int}' [-fpermissive]
boost1.49/boost/asio/detail/impl/win_static_mutex.ipp:51:63: error: invalid conversion from 'unsigned int' to 'const wchar_t*' [-fpermissive]
boost1.49/boost/asio/detail/impl/win_static_mutex.ipp:53:10: warning: declaration of 'mutex' shadows a global declaration [-Wshadow]
As I said in the beginning the only thing I do in my code is including the asio.hpp header:
#include <boost/asio.hpp>
The lines 43-53 from boost's win_static_mutex.ipp file are (I've marked the line 51):
using namespace std; // For sprintf.
wchar_t mutex_name[128];
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(UNDER_CE)
swprintf_s(mutex_name, 128,
#else // BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(UNDER_CE)
swprintf(mutex_name,
#endif // BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(UNDER_CE)
L"asio-58CCDC44-6264-4842-90C2-F3C545CB8AA7-%u-%p",
static_cast<unsigned int>(::GetCurrentProcessId()), this); // line 51
HANDLE mutex = ::CreateMutexW(0, TRUE, mutex_name);
Is there now some definitions I have to define or any other compiler flags or would one need to change something directly in boost or is it rather something with MinGW that needs to be changed?
It seems to be an error in Boost. You can try to update Boost to a later release (1.51 is available) and hope it's fixed there, otherwise you should report it as a bug.
One ugly 'solution' is to set the compiler flag -fpermissive.
At the moment I see only this 'solution' and one needs to wait until boost catches up again with GCC 4.7.0...
I am hoping to enable warnings for the following C++ compilation issues and corresponding compilers:
Unused variables -- Sun Studio CC
Example: void m() { int i = 10; }
Signed to unsigned comparison - VC++ and Sun Studio CC
Example: if ((unsigned) 10 < -1);
Wrong field initialization order - VC++ and Sun Studio CC
Example: class A { int i, j; A() : j(0), i(0) {} };
All of these are caught by GCC and I would like to enable these in VC++ and Sun Studio.
bash-4.1$ g++ -Wall main.cpp
main.cpp: In function ‘void m()’:
main.cpp:1: warning: comparison between signed and unsigned integer expressions
main.cpp:1: warning: unused variable ‘i’
main.cpp: In constructor ‘A::A()’:
main.cpp:1: warning: ‘A::j’ will be initialized after
main.cpp:1: warning: ‘int A::i’
main.cpp:1: warning: when initialized here
EDIT: Outside enabling signed to unsigned comparison warnings on VC++, all other options do not seem to be possible.
In Visual Studio, Project Properties, C++, set warning level to 4 (maximum) - VC++ compiler gives all possible warnings. AFAIK, warnings 1 and 2 are reported, and field initialization order is not reported by VC++ compiler.