gcc-warning

Wunused-but-set-variable warning treatment

时光毁灭记忆、已成空白 提交于 2019-12-05 06:11:40
I have the following code, and while compiling it with gcc-4.6 I get warning: warning: variable ‘status’ set but not used [-Wunused-but-set-variable] #if defined (_DEBUG_) #define ASSERT assert #else /* _DEBUG_ */ #define ASSERT( __exp__ ) #endif static inline void cl_plock(cl_plock_t * const p_lock) { status_t status; ASSERT(p_lock); ASSERT(p_lock->state == INITIALIZED); status = pthread_rwlock_unlock(&p_lock->lock); ASSERT(status == 0); } When _DEBUG_ flag isn't set I get the warning. Any ideas how can I workaround this warning? You can change your ASSERT macro to: #if defined (_DEBUG_)

Why doesn't gcc -Wformat warn about printf %d on an unsigned int?

孤者浪人 提交于 2019-12-05 02:27:31
The following program has undefined behavior: #include <stdio.h> int main(void) { unsigned int x = -100; // This is fine, becomes UINT_MAX - 100 printf("%d\n", x); // This is undefined behavior. return 0; } C99 7.19.6.1p8 states %d expects an int argument. C99 7.19.6.1p9 states "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined ." However, gcc -Wformat (which is included with -Wall ) will not complain about the above program, why? Is this a bug, or a deliberate omission? From the gcc manpage: -Wformat Check calls to "printf" and

How to do an explicit fall-through in C

一曲冷凌霜 提交于 2019-12-05 00:00:54
The newer versions of gcc offer the Wimplicit-fallthrough , which is great to have for most switch statements. However, I have one switch statement where I want to allow fall throughs from all case-statements. Is there a way to do an explicit fall through? I'd prefer to avoid having to compile with Wno-implicit-fallthrough for this file. EDIT: I'm looking for a way to make the fall through explicit (if it's possible), not to turn off the warning via a compiler switch or pragma. Use __attribute__ ((fallthrough)) switch (condition) { case 1: __attribute__ ((fallthrough)); case 2: __attribute__ (

How to use C++20's likely/unlikely attribute in if-else statement

余生颓废 提交于 2019-12-04 17:40:27
问题 This question is about C++20's [[likely]] / [[unlikely]] feature, not compiler-defined macros. This documents (cppreference) only gave an example on applying them to a switch-case statement. This switch-case example compiles perfectly with my compiler (g++-7.2) so I assume the compiler has implemented this feature, though it's not yet officially introduced in current C++ standards. But when I use them like this: if (condition) [[likely]] { ... } else { ... } , I got a warning: "warning:

Don't understand “assuming signed overflow” warning

守給你的承諾、 提交于 2019-12-04 10:00:33
问题 I am getting: warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false [-Wstrict-overflow] on this line: if ( this->m_PositionIndex[in] < this->m_EndIndex[in] ) m_PositionIndex and m_EndIndex of type itk::Index (http://www.itk.org/Doxygen/html/classitk_1_1Index.html), and their operator[] returns a signed long . (it is line 37 here: https://github.com/Kitware/ITK/blob/master/Modules/Core/Common/include/itkImageRegionConstIteratorWithIndex.hxx for context

Crashing threads with *(int*)NULL = 1; problematic?

一世执手 提交于 2019-12-04 07:32:01
I found this in a multi-threaded c application. The authors commented that it's used to make a thread crash in a custom assert function. GCC is fine with it, but clang issues the following warning: note: consider using __builtin_trap() or qualifying pointer with 'volatile' and also issues one of those, for each usage of the assert function: warning: indirection of non-volatile null pointer will be deleted, not trap What is going on here? Is __builtin_trap specific to clang? Should I use it? Writing to NULL address is not guaranteed to crash your program reliably, so GCC introduced __builtin

Mysterious type conversion warning when using sys/socket.h macro

半城伤御伤魂 提交于 2019-12-04 04:26:00
问题 I'm trying to resolve conversion warnings in our C code base on Solaris 11 64-bit using GCC 9.1 (iso9899:1999) and GNU make 4.2, and I came across this one: warning: unsigned conversion from ‘int’ to ‘long unsigned int’ changes value from ‘-8’ to ‘18446744073709551608’ [-Wsign-conversion] 187 | char ccmsg[CMSG_SPACE(sizeof(int))]; | ^~~~~~~~~~ I know that CMSG_SPACE is defined in sys/socket.h as: /* Amount of space + padding needed for a message of length l */ #define CMSG_SPACE(l) \ (

What is the explanation for “warning: assuming that the loop is not infinite”

試著忘記壹切 提交于 2019-12-04 01:36:09
I had just taken the decision to change as many variables from unsigned to int and upon recompiling the code in question, was greeted by this warning message: freespace_state.c:203: warning: assuming that the loop is not infinite The line in question: for (x = startx; x <= endx; ++x, ++xptr) This loop is 60 lines of code (inc white space/brackets etc), and has a goto within it, and at least one occurrence of continue . In this case, I think I am appreciative that GCC is assuming this loop is not infinite, because, it should never loop indefinitely. What is GCC trying to tell me here? The

Have compiler check the number of array initializers

隐身守侯 提交于 2019-12-04 00:49:55
Initializing an array (in C++, but any solution which works for C will likely work here as well) with less initializers than it has elements is perfectly legal: int array[10] = { 1, 2, 3 }; However, this can be a source of obscure bugs. Is there a way to have the compiler (gcc) check the number of initializers for one specific array, and emit a warning or even an error if declared and actual size don't match? I know I can use int array[] = { 1, 2, 3 }; and could then use static assertions involving sizeof(array) to verify my expectation there. But I'm using array in other translation units, so

How to define extern variable along with declaration?

橙三吉。 提交于 2019-12-03 23:57:05
Wiki says: The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable . That means, an extern declaration that initializes the variable serves as a definition for that variable . So, /* Just for testing purpose only */ #include <stdio.h> extern int y = 0; int main(){ printf("%d\n", y); return 0; } should be valid ( compiled in C++11 ). But when