-w seçeneği
Açıklaması
şöyle.
Inhibit all warning messages.
-Wall
Sanılanın aksine tüm warning'leri
etkinleştirmez! Açıklaması
şöyle.
Note that some warning flags are not implied by -Wall. Some of them warn about constructions that users generally do not consider questionable, but which occasionally you might wish to check for; others warn about constructions that are necessary or hard to avoid in some cases, and there is no simple way to modify the code to suppress the warning. Some of them are enabled by -Wextra but many of them must be enabled individually.
Şöyle
yaparız. Aşağıdaki örnekte -msse3 ile sanırım SSE instruction set etkinleştiriliyor.
$ g++ -Wall -msse3 -O3 test.cpp && ./a.out
-Warray-bounds
-Wall ile
-Warray-bounds seçeneği de etkinleşir.
Örnek
Şöyle
yaparız. Bu hatanın üretilmesine sebep olur.
int arr[10];
return arr[10];
Örnek
Elimizde şöyle bir kod
olsun.
b[9] hatalı kod.
#include <stdio.h>
int main(void)
{
char b[] = {'N', 'i', 'c', 'e', ' ', 'y', 'o', 'u', '!'};
printf("b[9] = %d\n", b[9]);
return 0;
}
Şöyle
yaparız
% gcc -O2 -W -Wall -pedantic -c foo.c
foo.c: In function ‘main’:
foo.c:6:5: warning: ‘b[9]’ is used uninitialized in this function [-Wuninitialized]
printf("b[9] = %d\n", b[9]);
-Wextra
-Wall ile etkinleşmeyen diğer uyarıları etkinleştirir. Şöyle
yaparız.
-Werror
Warning'lere error muamelesi yapar. Böylece en ufak warning bile kodun derlenmemesine sebep olur.
Örnek
Şöyle
yaparız.
$ g++ -Werror -Wall
Örnek
Extra warning'leri de kullanmak istersek şöyle
yaparız.
g++ -Wall -Wextra -Werror ...
-Weffc++
Effective C++ uyarınıların etkinleştirir. Şöyle
yaparız.
g++ -std=c++14 -Weffc++ main.cpp
-Wmissing-field-initializers
struct yapısındaki tüm alanlar ilklendirilmezse uyarı verir.
Örnek
Elimizde şöyle bir kod
olsun.
struct VariablePointers {
VariablePtr active;
VariablePtr wasactive;
VariablePtr filename;
};
Eğer şöyle kullanırsak uyarı
verir.
VariablePointers{activePtr, filename}
-Wpadded
Açıklaması
şöyle.
gcc has the -Wpadded warning that warns when padding is added to a structure:
Örnek
Elimizde şöyle bir kod
olsun.
struct X
{
int a;
int c;
double b;
};
Çıktı olarak şunu
alırız.
<source>:4:12: warning: padding struct to align 'X::b' [-Wpadded]
4 | double b;
| ^
<source>:1:8: warning: padding struct size to alignment boundary [-Wpadded]
1 | struct X
| ^
-Wshadow
Açıklaması
şöyle.
In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. At the level of identifiers (names, rather than variables), this is known as name masking. This outer variable is said to be shadowed by the inner variable, while the inner identifier is said to mask the outer identifier.
Şöyle
kullanılır.
g++ -Wshadow --std=c++1z -o main main.cpp
Örnek
Elimizde şöyle bir kod
olsun. character değişkeni global değişkeni gölgeler.
#include <stdio.h>
char character = 'c';
int main (void)
{
char character = 'b';
printf("The current value of head is %c", character);
}
Örnek
Sınıf içindeki bir alan (member variable) ile sınıfın metoduna girilen parametre aynı isme sahipse yani birbirlerini gölgeliyorlarsa uyarı verir.
Elimizde şöyle bir kod
olsun. a alanı ve a parametresi birbirlerini gölgeler.
struct A{
A(int a, int b);
int a, b;
};
A::A(int a, int b)
: a(a)
, b(b)
{}
-Wuninitialized
Bir değişken ilklendirilmeden kullanılırsa verilebilir. Uyarı verilmesi garanti değildir. Optimizasyon seviyesine göre uyarı es geçilebilir. Açıklaması şöyle
Warn if an automatic variable is used without first being initialized or if a variable may be clobbered by a setjmp call. [...]
Because these warnings depend on optimization, the exact variables or elements for which there are warnings depends on the precise optimization options and version of GCC used.
Örnek
Elimizde şöyle bir kod olsun.
#include <stdio.h>
int main()
{
int i,len=12;
/* printf("%d\n",i); */
while(i!=len-1)
{
i++;
len--;
}
return 0;
}
Şöyle
yaparız. -Wall ile -Wuninitialized seçeneği de etkinleşir.
gcc -Wall -Wextra -pedantic
warning: 'i' is used uninitialized in this function [-Wuninitialized]
-Wunused-variable
Açıklaması
şöyle.
Warn whenever a local or static variable is unused aside from its declaration.
Elimizde şöyle bir kod
olsun.
int check(){
int x = 5;
++x; /* line 1.*/
return 0;
}
Çıktı olarak şunu
alırız.
warning: unused variable ‘x’ [-Wunused-variable]
-Wwrite-strings
When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer produces a warning. These warnings help you find at compile time code that can try to write into a string constant, but only if you have been very careful about using const in declarations and prototypes. Otherwise, it is just a nuisance. This is why we did not make -Wall request these warnings.
Şu tarz hataların yakalanmasını
sağlar.
void f(char *s) {
s[0] = '0';
}
int main() {
char *s = "immutable";
f("literal"); // oops
f(s); // oops
return 0;
}