Article View: pl.comp.lang.c
Article #309891Re: W której wersji standardu C lub C++ pracujecie?
From: PawelS pawel(at)wbcd(dot)pl
Date: Wed, 14 Aug 2019 17:24
Date: Wed, 14 Aug 2019 17:24
105 lines
5272 bytes
5272 bytes
Maciek Godek pisze:
> W dniu wtorek, 13 sierpnia 2019 18:08:20 UTC+2 użytkownik PawelS pawel(at)wbcd(dot)pl napisał:
>> Maciek Godek pisze:
>>> W dniu niedziela, 11 sierpnia 2019 20:12:34 UTC+2 użytkownik PawelS pawel(at)wbcd(dot)pl napisał:
>>>> Maciek Godek pisze:
>>>>> W dniu wtorek, 6 sierpnia 2019 14:02:33 UTC+2 użytkownik Queequeg napisał:
>>>>> To ważne, bo ani -Wall ani nawet -Wextra w GCC nie wyłapie czegoś takiego:
>>>>>
>>>>> uint32_t f() {
>>>>> return JAKAŚ_DUŻA_WARTOŚĆ;
>>>>> }
>>>>>
>>>>> uint16_t x = f();
>>>>>
>>>>> (co uważam że jest skandaliczne i byłem w szoku jak to odkryłem. Zresztą nadal jestem w szoku)
>>>> A to ciekawe, bo bez żadnych opcji gcc zgłasza ostrzeżenie:
>>>>
>>>> gcc -v
>>>> ------
>>>> gcc version 4.1.2 20080704 (Red Hat 4.1.2-44)
>>>>
>>>> test.c
>>>> ------
>>>> #include <stdint.h>
>>>> uint32_t f() {
>>>> return 0xffffffffffff;
>>>> }
>>>> int main() {
>>>> uint16_t x = f();
>>>> return 0;
>>>> }
>>>>
>>>> gcc -o test test.c
>>>> ------------------
>>>> test.c: In function 'f':
>>>> test.c:3: warning: integer constant is too large for 'long' type
>>>> test.c:3: warning: large integer implicitly truncated to unsigned type
>>> W tym przykładzie zgłasza błąd konwersji stałej. Zamiast tego spróbuj np.
>>>
>>> #include <stdint.h>
>>> uint32_t f(uint32_t x) {
>>> return x*x;
>>> }
>>> int main(int argc, char *argv[]) {
>>> uint16_t x = f(argc);
>>> return 0;
>>> }
>>>
>>> U mnie na telefonie (gcc 6) jest milczenie, choć typ zmiennej ewidentnie się nie zgadza z typem funkcji.
>>> (Dopiero -Wconversion pomaga)
>> gcc -o test test.c
>> ------------------
>>
>> gcc -o test test.c -Wconversion
>> -------------------------------
>> test.c: In function ‘main’:
>> test.c:6: warning: passing argument 1 of ‘f’ as unsigned due to prototype
>>
>>> choć typ zmiennej ewidentnie się nie zgadza z typem funkcji.
>> /usr/include/stdint.h
>> typedef unsigned int uint32_t;
>
> Nie o ten fragment mi chodzi, tylko o przypisanie wartości funkcji do zmiennej.
>
> Może wyraźniej to będzie widoczne tutaj:
>
> #include <stdint.h>
> uint32_t f(int32_t x) {
> return x*x;
> }
>
> int main(int32_t argc, char *argv[]) {
> (void) argv;
> uint16_t x = f(argc);
> return x;
> }
>
> Jak kompilujesz z -Wall -Wextra to cisza. Jak dodasz -Wconversion, to masz
>
> x.c: In function ‘f’:
> x.c:3:13: warning: conversion to ‘uint32_t {aka unsigned int}’ from ‘int32_t {aka int}’ may change the sign of the result [-Wsign-conversion]
> return x*x;
> ~^~
> x.c: In function ‘main’:
> x.c:8:18: warning: conversion to ‘uint16_t {aka short unsigned int}’ from ‘uint32_t {aka unsigned int}’ may alter its value [-Wconversion]
> uint16_t x = f(argc);
>
> Chodzi mi o tego drugiego warninga. Brak takich ostrzeżeń powoduje, że bardzo poważne błędy mogą bardzo łatwo umknąć.
Prawda. Nie wiem czy do tego zmierzasz, prawdopodobnie tak, że -Wall
to wcale nie włącza wszystkich dostępnych dla gcc opcji -W***
-Wall
All of the above -W options combined. This enables all the warnings about
constructions that some users consider questionable, and that are easy to
avoid (or modify to prevent the warning), even in conjunction with macros.
This also enables some language-specific warnings described in C++ Dialect
Options and Objective-C and Objective-C++ Dialect Options.
Zgodnie z man gcc -Wall włącza wszystkie opcje -W*** powyżej -Wall
znajdującego się w man gcc (All of the above -W options combined.).
Być może to celowa niekonsekwencja, aby dla odmiany razem z -Wall
nie dodawano dużej ilości opcji typu: -Wno**** (np: -Wno-long-long).
Stąd wniosek, że trzeba wiedzieć jakie dodatkowe opcje ostrzeżeń
trzeba jeszcze podawać dla gcc przy kompilacji, aby kompilator
zgłaszał takie istotne błędy, zwłaszcza przy konwersji.
Technical Details
Message-ID:
<5d5443dc$0$17361$65785112@news.neostrada.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!wsisiz.edu.pl!goblin2!goblin1!goblin.stu.neva.ru!newsfeed.neostrada.pl!unt-exc-01.news.neostrada.pl!unt-spo-b-01.news.neostrada.pl!news.neostrada.pl.POSTED!not-for-mailReferences:
<a1fbe21b-aed9-4b32-9691-a824c4e31a21@trust.no1> <3f1215b5-3c16-4248-a74f-1b57d21d4c98@googlegroups.com> <5d505a6e$0$17364$65785112@news.neostrada.pl> <d897b352-477c-42c0-ae44-f2df4bda7361@googlegroups.com> <5d52e072$0$521$65785112@news.neostrada.pl> <dc11d608-88c5-4511-bd8d-9e1922cd5e30@googlegroups.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!wsisiz.edu.pl!goblin2!goblin1!goblin.stu.neva.ru!newsfeed.neostrada.pl!unt-exc-01.news.neostrada.pl!unt-spo-b-01.news.neostrada.pl!news.neostrada.pl.POSTED!not-for-mail Date: Wed, 14 Aug 2019 19:24:43 +0200 From: "PawelS pawel(at)wbcd(dot)pl" <fake@email.org> User-Agent: Thunderbird 2.0.0.18 (X11/20081120) MIME-Version: 1.0 Newsgroups: pl.comp.lang.c Subject: Re: W której wersji standardu C lub C++ pracujecie? References: <a1fbe21b-aed9-4b32-9691-a824c4e31a21@trust.no1> <3f1215b5-3c16-4248-a74f-1b57d21d4c98@googlegroups.com> <5d505a6e$0$17364$65785112@news.neostrada.pl> <d897b352-477c-42c0-ae44-f2df4bda7361@googlegroups.com> <5d52e072$0$521$65785112@news.neostrada.pl> <dc11d608-88c5-4511-bd8d-9e1922cd5e30@googlegroups.com> In-Reply-To: <dc11d608-88c5-4511-bd8d-9e1922cd5e30@googlegroups.com> Lines: 105 Message-ID: <5d5443dc$0$17361$65785112@news.neostrada.pl> Organization: Telekomunikacja Polska NNTP-Posting-Host: 89.231.96.70 X-Trace: 1565803484 unt-rea-a-01.news.neostrada.pl 17361 89.231.96.70:34294 X-Complaints-To: abuse@news.neostrada.pl Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit