gcc-warning

Why does -Wcast-align not warn about cast from char* to int* on x86?

拟墨画扇 提交于 2019-12-01 15:11:02
问题 I understand that gcc has an option -Wcast-align which warns whenever a pointer is cast such that the required alignment of the target is increased. Here's my program: char data[10]; int ptr = *((int *)data); On my machine, the alignment requirement of data is 1 whereas it's 8 for ptr. Why don't I get a warning? Could it be because I'm compiling it for x86? 回答1: The warning will never be emitted when compiling for Linux i386 or x86-64, when using the standard ABIs for these systems. Let me

How can I tell gcc to warn (or fail) on switch/case statements without a break?

最后都变了- 提交于 2019-12-01 15:00:39
问题 I have a complicated switch statement, and I forgot to put a break at the end of one of the case s. This is quite legal, and as a result I had a fall-through to the next case . Is there any way to have gcc warn (or even better, fail) if I neglect to put a break statement? I realize that there are many valid use cases (and I use them often in my code), as exemplified in this question, so obviously such a warning (or failure) would need a simple waiver so that I could easily say, "I do want to

Is there a GCC warning that detects bit shift operations on signed types?

只谈情不闲聊 提交于 2019-12-01 02:48:50
If I read the C++ ISO specification (sections 5.8.2 and 5.8.3) right, the right-shift of negative signed types is implementation specific and the left-shift undefined behaviour. Therefore I would like to find shift operations on signed types in our legacy source code which we compile with g++ 4.8.2. Unfortunately, I couldn't find such an option in the manual . I can for example compile this code with "g++ -Wall -Wextra -pedantic" without a warning: int si = -1; int left = si << 1; // -2 (multiplication by 2, sign is preserved) int right = si >> 1; // -1 (no change, only 1s) Can anyone tell me

Assignment <pointer to array of constants> = <pointer to array>: incompatible pointers

孤者浪人 提交于 2019-11-30 23:54:31
问题 When I compile something like this double da[ 3 ] = { 2., 3., 4. }; double (* pda)[ 3 ] = &da; double const (* cpda)[ 3 ] = pda; // gcc: warning; MSVC: ok gcc warns me warning: initialization from incompatible pointer type [enabled by default] Question: What's the problem with this assignment? Yes, technically, these are different types, but I don't see any danger here, double const (*)[ 3 ] looks even safer for me than double (*)[ 3 ] . I did some tests and results confuse me even more: 1)

Is there a GCC warning that detects bit shift operations on signed types?

走远了吗. 提交于 2019-11-30 22:32:19
问题 If I read the C++ ISO specification (sections 5.8.2 and 5.8.3) right, the right-shift of negative signed types is implementation specific and the left-shift undefined behaviour. Therefore I would like to find shift operations on signed types in our legacy source code which we compile with g++ 4.8.2. Unfortunately, I couldn't find such an option in the manual. I can for example compile this code with "g++ -Wall -Wextra -pedantic" without a warning: int si = -1; int left = si << 1; // -2

Why doesn't GCC produce a warning when assigning a signed literal to an unsigned type?

穿精又带淫゛_ 提交于 2019-11-30 17:31:00
Several questions on this website reveal pitfalls when mixing signed and unsigned types and most compilers seem to do a good job about generating warnings of this type. However, GCC doesn't seem to care when assigning a signed constant to an unsigned type! Consider the following program: /* foo.c */ #include <stdio.h> int main(void) { unsigned int x=20, y=-30; if (x > y) { printf("%d > %d\n", x, y); } else { printf("%d <= %d\n", x, y); } return 0; } Compilation with GCC 4.2.1 as below produces no output on the console: gcc -Werror -Wall -Wextra -pedantic foo.c -o foo The resulting executable

Tell gcc that a function call will not return

坚强是说给别人听的谎言 提交于 2019-11-30 08:05:26
I am using C99 under GCC . I have a function declared static inline in a header that I cannot modify. The function never returns but is not marked __attribute__((noreturn)) . How can I call the function in a way that tells the compiler it will not return? I am calling it from my own noreturn function, and partly want to suppress the "noreturn function returns" warning but also want to help the optimizer etc. I have tried including a declaration with the attribute but get a warning about the repeated declaration. I have tried creating a function pointer and applying the attribute to that, but

How to eliminate external lib/third party warnings in GCC [duplicate]

本小妞迷上赌 提交于 2019-11-30 03:03:39
This question already has an answer here: How to suppress GCC warnings from library headers? 9 answers In the software project I'm working on, we use certain 3rd party libraries which, sadly, produce annoying gcc warnings. We are striving to clean all code of warnings, and want to enable the treat-warnings-as-errors (-Werror) flag in GCC. Is there a way to make these 3rd party generated warnings, which we cannot fix, to disappear? Dummy00001 I presume you are talking about the warnings coming from the 3rd party library headers. The GCC specific solution would be to create another wrapper

Getting the warning “cast to pointer from integer of different size” from the following code

雨燕双飞 提交于 2019-11-30 00:49:20
The code is: Push(size, (POINTER)(GetCar(i) == term_Null()? 0 : 1)); Here is the C code push returns ABC which is typedef POINTER *ABC typedef void * POINTER ABC size; Push(ABC,POINTER); XYZ GetCar(int); typedef struct xyz *XYZ; XYZ term_Null(); long int i; What is the reason for the particular warning? You can use intptr_t to ensure the integer has the same width as pointer. This way, you don't need to discover stuff about your specific platform, and it will work on another platform too (unlike the unsigned long solution). #include <stdint.h> Push(size, (POINTER)(intptr_t)(GetCar(i) == term

Is there any way to get readable gcc error and warning output at the command line?

﹥>﹥吖頭↗ 提交于 2019-11-29 23:02:41
For some long errors, the gcc output is dense and has lots of line-wrapping etc. Especially when errors are subtle, it can take me 10-30 seconds of squinting to parse it with my eyes. I've taken to pasting this in an open code-editor window to get some basic syntax highlighting and enable reformatting with regex's. Has anyone invented a more automated method? I use this script, called colorize : #!/bin/bash while read x ; do echo $x ; done \ | sed -e "s/.*error:.*/\x1b[1;36m&\x1b[0m/" \ -e "s/.*warning:.*/\x1b[1;36m&\x1b[0m/" \ -e "s/^\(.*\)\(required from\)/\x1b[1;36m\1\x1b[0mnote: \2/" \ -e