GCC의 모든 경고를 (문자 그대로) 켜는 방법은 무엇입니까?
말 그대로 GCC에있는 모든 경고 를 활성화하고 싶습니다 . (당신은 그것이 쉬울 것이라고 생각할 것입니다 ...)
당신
-Wall
은 트릭을 할지도 모른다고 생각 하지만, 아닙니다! 여전히 필요합니다-Wextra
.당신
-Wextra
은 트릭을 할지도 모른다고 생각 하지만, 아닙니다! 여기에 나열된 모든 경고 (예 :)-Wshadow
가이 기능으로 활성화 된 것은 아닙니다 . 그리고 나는이 목록이 포괄적인지 전혀 모른다.
GCC에 모든 경고 를 사용하도록 설정하려면 어떻게해야 합니까?
당신은 할 수 없습니다.
GCC 4.4.0 매뉴얼은 해당 버전에 대해서만 포괄적이지만 4.4.0에 대한 모든 가능한 경고를 나열합니다. 예를 들어 일부 언어 별 옵션은 C ++ 옵션 또는 Obj-C 옵션 페이지에 있습니다. 옵션 요약을 살펴 보는 것이 좋습니다.
켜기 모든 것을 포함 할 것입니다 -Wdouble-promotion
32 비트 단 정밀도 부동 소수점 유닛을 구현하는 CPU에만 관련되는 float
하드웨어, 그러나 에뮬레이션 double
소프트웨어입니다. double
소프트웨어 에뮬레이션을 사용할 때 와 같이 계산을 수행하면 속도가 느려집니다. 이는 일부 임베디드 CPU와 관련이 있지만 64 비트 부동 소수점을 지원하는 하드웨어를 갖춘 최신 데스크탑 CPU와는 전혀 관련이 없습니다.
일반적으로 유용하지 않은 또 다른 경고는이며 -Wtraditional
, 이는 전통적인 C (예 : "string " "concatenation"
ISO C 함수 정의 )에서 다른 의미를 갖거나 작동하지 않는 완벽하게 구성된 코드에 대해 경고 합니다! 30 년 된 컴파일러와의 호환성에 정말로 관심이 있습니까? 글쓰기에 대한 경고를 정말로 원하십니까 int inc(int i) { return i+1; }
?
나는 -Weffc++
너무 시끄러워서 쓸모가 없다고 생각 합니다. 구식 C ++ 의 오래된 첫 번째 버전을 기반으로하며 완벽하게 유효한 C ++ 인 구조체에 대해 경고합니다 (이 책의 이후 버전에서 지침이 변경됨). std::string
생성자에서 멤버를 초기화하지 않았다고 경고했다 . 그것은 내가 원하는 것을 정확하게하는 기본 생성자를 가지고 있습니다. 왜 m_str()
그것을 호출하기 위해 작성해야 합니까? -Weffc++
도움이 될 것입니다 경고는 컴파일러가 정확하게 (위음성을 제공) 감지하기 너무 어려운, 그러한 명시 적으로 모든 멤버를 초기화하는 등의 유용하지 않은 사람은 단지 잘못된 반응을 제공, 많은 소음이 너무 생산하고 있습니다.
Luc Danton 은 C ++ 코드에는 거의 이해가되지 않는 쓸모없는 경고 의 훌륭한 예 를 제공했습니다 -Waggregate-return
.
즉, 당신은 정말로 모든 경고를 원하지는 않습니다 .
매뉴얼을 살펴보고, 그들에 대해 읽고, 어떤 것을 가능하게 할 것인지 결정하고 시도하십시오. 어쨌든 컴파일러 매뉴얼을 읽는 것은 Good Thing TM 이며, 지름길을 이해하고 알지 못하는 경고를 활성화하는 것은 특히 좋은 생각이 아닙니다. 특히 RTFM을 피해야하는 경우에는 그렇습니다.
방금 모든 것을 켜는 사람 은 아마 우두머리이거나 뾰족한 머리의 상사가 "경고 없음"이라고 말했기 때문에 그렇게하고있을 것입니다.
편집 : WONTFIX로 닫히는 모든 경고를 활성화하려면 -Wall-all 도 참조하십시오 .
편집 2 : 컴파일러 버전에 따라 다른 경고를 사용해야하는 makefile에 대한 DevSolar의 불만에 대한 응답으로 -Wall -Wextra
적합하지 않은 경우 컴파일러 별 및 버전 별 CFLAGS를 사용하는 것이 어렵지 않습니다.
compiler_name := $(notdir $(CC))
ifeq ($(compiler_name),gcc)
compiler_version := $(basename $(shell $(CC) -dumpversion))
endif
ifeq ($(compile_name),clang)
compiler_version := $(shell $(CC) --version | awk 'NR==1{print $$3}')
endif
# ...
wflags.gcc.base := -Wall -Wextra
wflags.gcc.4.7 := -Wzero-as-null-pointer-constant
wflags.gcc.4.8 := $(wflags.gcc.4.7)
wflags.clang.base := -Wall -Wextra
wflags.clang.3.2 := -Weverything
CFLAGS += $(wflags.$(compiler_name).base) $(wflags.$(compiler_name).$(compiler_version))
문자 그대로 모든 경고를 활성화하는 것이 유리하지 않을 수도 있다는 이전 답변에 동의하지만 GCC는이를 달성하기 위해 합리적으로 편리한 방법을 제공합니다. 명령
gcc -Q --help=warning
지원되는 모든 경고 옵션 목록이 활성화되어 있는지 여부에 대한 정보를 제공합니다. 그런데이 캔이 옵션은 예를 들어으로 활성화 (안)되는 찾을 수 -Wall
및-Wextra
gcc -Wall -Wextra -Q --help=warning
모든 경고를 활성화하려면 일부 정규식을 사용하여 명령 줄 매개 변수를 추출 할 수 있습니다
gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n'
내 현재 GCC의 경우 다음을 제공합니다.
-Wabi -Wabi-tag -Waddress -Waggregate-return -Waggressive-loop-optimizations -Waliasing -Walign-commons -Wampersand -Warray-bounds -Warray-temporaries -Wassign-intercept -Wattributes -Wbad-function-cast -Wbool-compare -Wbuiltin-macro-redefined -Wc++-compat -Wc++0x-compat -Wc++14-compat -Wc-binding-type -Wc90-c99-compat -Wc99-c11-compat -Wcast-align -Wcast-qual -Wchar-subscripts -Wcharacter-truncation -Wchkp -Wclobbered -Wcomment -Wcompare-reals -Wconditionally-supported -Wconversion -Wconversion-extra -Wconversion-null -Wcoverage-mismatch -Wcpp -Wctor-dtor-privacy -Wdate-time -Wdeclaration-after-statement -Wdelete-incomplete -Wdelete-non-virtual-dtor -Wdeprecated -Wdeprecated-declarations -Wdesignated-init -Wdisabled-optimization -Wdiscarded-array-qualifiers -Wdiscarded-qualifiers -Wdiv-by-zero -Wdouble-promotion -Weffc++ -Wempty-body -Wendif-labels -Wenum-compare -Wextra -Wfloat-equal -Wformat-contains-nul -Wformat-extra-args -Wformat-nonliteral -Wformat-security -Wformat-signedness -Wformat-y2k -Wformat-zero-length -Wfree-nonheap-object -Wfunction-elimination -Wignored-qualifiers -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wimplicit-interface -Wimplicit-procedure -Wincompatible-pointer-types -Winherited-variadic-ctor -Winit-self -Winline -Wint-conversion -Wint-to-pointer-cast -Wintrinsic-shadow -Wintrinsics-std -Winvalid-memory-model -Winvalid-offsetof -Winvalid-pch -Wjump-misses-init -Wline-truncation -Wliteral-suffix -Wlogical-not-parentheses -Wlogical-op -Wlong-long -Wmain -Wmaybe-uninitialized -Wmemset-transposed-args -Wmissing-braces -Wmissing-declarations -Wmissing-field-initializers -Wmissing-include-dirs -Wmissing-parameter-type -Wmissing-prototypes -Wmultichar -Wnarrowing -Wnested-externs -Wnoexcept -Wnon-template-friend -Wnon-virtual-dtor -Wnonnull -Wodr -Wold-style-cast -Wold-style-declaration -Wold-style-definition -Wopenmp-simd -Woverflow -Woverlength-strings -Woverloaded-virtual -Woverride-init -Wpacked -Wpacked-bitfield-compat -Wpadded -Wparentheses -Wpedantic -Wpmf-conversions -Wpointer-arith -Wpointer-sign -Wpointer-to-int-cast -Wpragmas -Wproperty-assign-default -Wprotocol -Wreal-q-constant -Wrealloc-lhs -Wrealloc-lhs-all -Wredundant-decls -Wreorder -Wreturn-local-addr -Wreturn-type -Wselector -Wsequence-point -Wshadow -Wshadow-ivar -Wshift-count-negative -Wshift-count-overflow -Wsign-compare -Wsign-promo -Wsized-deallocation -Wsizeof-array-argument -Wsizeof-pointer-memaccess -Wstack-protector -Wstrict-null-sentinel -Wstrict-prototypes -Wstrict-selector-match -Wsuggest-attribute=const -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wsuggest-final-methods -Wsuggest-final-types -Wsuggest-override -Wsurprising -Wswitch -Wswitch-bool -Wswitch-default -Wswitch-enum -Wsync-nand -Wsynth -Wsystem-headers -Wtabs -Wtarget-lifetime -Wtraditional -Wtraditional-conversion -Wtrampolines -Wtrigraphs -Wtype-limits -Wundeclared-selector -Wundef -Wunderflow -Wuninitialized -Wunknown-pragmas -Wunsafe-loop-optimizations -Wunsuffixed-float-constants -Wunused -Wunused-but-set-parameter -Wunused-but-set-variable -Wunused-dummy-argument -Wunused-function -Wunused-label -Wunused-local-typedefs -Wunused-macros -Wunused-parameter -Wunused-result -Wunused-value -Wunused-variable -Wuse-without-only -Wuseless-cast -Wvarargs -Wvariadic-macros -Wvector-operation-performance -Wvirtual-move-assign -Wvla -Wvolatile-register-var -Wwrite-strings -Wzero-as-null-pointer-constant -Wzerotrip -frequire-return-statement
This can now be used to call the GCC, i.e.
gcc $(gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n')
Note however that this results in warnings due to some warning options only being available for certain languages (e.g. C++
). These could be avoided by using some more regex to only include the options allowed for the current language or by adding an appropriate -Wno-whatever
at the end of the call.
It's simply impossible to program with all warnings enabled (unless you are going to ignore them, but then, why bother?). For example, let's assume you use following set of flags: -Wstrict-prototypes -Wtraditional
.
Even with two warnings enabled, the following program would complain.
/tmp $ cat main.c
int main(int argc, char **argv) {
return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c
main.c: In function ‘main’:
main.c:1:5: warning: traditional C rejects ISO C style function definitions [-Wtraditional]
int main(int argc, char **argv) {
^
You may think "well, I'm going to use old style prototypes then". Nope, this won't work.
/tmp $ cat main.c
int main(argc, argv)
int argc;
char **argv;
{
return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c
main.c:1:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
int main(argc, argv)
^
And no, not specifying any prototype is also wrong, as the compiler will also complain.
/tmp $ cat main.c
int main() {
return 0;
}
/tmp $ gcc -Wstrict-prototypes -Wtraditional main.c
main.c:1:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
int main() {
^
If you define any functions inside your program, you cannot use all flags, because the compiler will complain about any imaginable function definition.
For C++, this is possible (the -Wtraditional
flag doesn't exist), and very simple programs can be compiled. To enable all warnings, use following list of warnings (probably some warnings are duplicated, because I didn't bother to filter warnings enabled by -Wall
).
-Wabi -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Weffc++ -Wstrict-null-sentinel -Wno-non-template-friend -Wold-style-cast -Woverloaded-virtual -Wno-pmf-conversions -Wsign-promo -Wextra -Wall -Waddress -Waggregate-return -Warray-bounds -Wno-attributes -Wno-builtin-macro-redefined -Wc++0x-compat -Wcast-align -Wcast-qual -Wchar-subscripts -Wclobbered -Wcomment -Wconversion -Wcoverage-mismatch -Wno-deprecated -Wno-deprecated-declarations -Wdisabled-optimization -Wno-div-by-zero -Wempty-body -Wenum-compare -Wno-endif-labels -Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 -Wno-format-contains-nul -Wno-format-extra-args -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wignored-qualifiers -Winit-self -Winline -Wno-int-to-pointer-cast -Wno-invalid-offsetof -Winvalid-pch -Wunsafe-loop-optimizations -Wlogical-op -Wlong-long -Wmain -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wno-mudflap -Wno-multichar -Wnonnull -Wno-overflow -Woverlength-strings -Wpacked -Wpacked-bitfield-compat -Wpadded -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wsign-conversion -Wstack-protector -Wstrict-aliasing=1 -Wstrict-overflow=5 -Wswitch -Wswitch-default -Wswitch-enum -Wsync-nand -Wsystem-headers -Wtrigraphs -Wtype-limits -Wundef -Wuninitialized -Wunknown-pragmas -Wno-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvla -Wvolatile-register-var -Wwrite-strings
Someone has created a set of tools for determining the complete set of warnings for a given GCC or Clang version.
For GCC, copying from the full list of warnings provided by this tool for your compiler version appears to be the only way to ensure that all warnings are turned on, since (unlike Clang) GCC does not provide -Weverything
.
The tool appears to parse the actual c.opt
file in the GCC source code, so its results should be definitive.
The repository also contains text files with the warning lists generated for most GCC and Clang versions (currently Clang 3.2 through 3.7 and GCC 3.4 through 5.3).
https://github.com/barro/compiler-warnings
Gcc 4.3+ now has -Q --help=warnings, you can even specify --help=warnings,C to just print out the C related warnings.
I just wrote a m4 module to take advantage of this (also supports clang's -Weverything), see wget_manywarnings.m4
How to use it is pretty simple, basically the module turns every warn flag on. And you remove warnings as needed - some are really very verbose. Example: configure.ac
If you don't use autotools, you'll find the code to turn on all disabled warnings in the m4 module, which basically is the gcc call piped through awk:
flags="-Wall -Wextra -Wformat=2 "$(gcc -Wall -Wextra -Wformat=2 -Q --help=warning,C|awk '{ if (($2 == "[disabled]" || $2 == "") && $1!~/=/ && $1~/^-W/&& $1!="-Wall") print $1 }'
From this page:
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.
I guess the question is which ones? Perhaps you could grep that page for all lines beginning with -W, and get a complete list of warning flags. Then compare those with the lists under -Wall
and -Wextra
. There is also -Wpedantic
, though you are obviously wanting to be even more pedantic still =)
And I still have no idea if this list is comprehensive.
It probably is, but the only list that is 100% comprehensive is the actual source for the compiler. However, GCC is big! And I don't know if all command-line parameters are collected in one place or spread out over several source files. Also note that some warnings are for the pre-processor, some for the actual compiler and some for the linker (which is a completely separate program, and found in the binutils package) so they most likely are spread out.
참고URL : https://stackoverflow.com/questions/11714827/how-to-turn-on-literally-all-of-gccs-warnings
'IT story' 카테고리의 다른 글
사용자가 오류 메시지를 읽도록하는 방법은 무엇입니까? (0) | 2020.05.22 |
---|---|
CSS 16 진 RGBA? (0) | 2020.05.22 |
read ()와 recv () 그리고 send ()와 write ()의 차이점은 무엇입니까? (0) | 2020.05.22 |
캐치되지 않는 구문 에러 : 예기치 않은 토큰 : (0) | 2020.05.22 |
#! / usr / bin / env bash가 #! / bin / bash보다 우수한 이유는 무엇입니까? (0) | 2020.05.22 |