gcc treat all warnings as errors EXCEPT X - gcc

For reasons best left unmentioned, I want to treat all warnings as erros, except for a single warning (deprecated) which I would like to tread as a warning.
Is there any more convenient way to do this than listing out all the warnings I want to treat as errors by hand?

You can do -Werror -Wno-error=deprecated.

Related

Turning off error message caret in GCC 4.8

GCC 4.8 seems to have added Clang-like error message display, e.g. like this:
player.c:725:9: warning: variable ‘delta’ set but not used [-Wunused-but-set-variable]
int delta, rdelta;
^
I guess some people find this useful, but I find it superfluous, and it makes every error message use three lines of screen-space instead of one, where more error messages could be displayed instead.
Is there any way to turn it off? I've been reading through the GCC documentation, but haven't found it yet, at least. In particular, is there a way to turn it off "by default", so that I don't have to specify some -fno-error-caret or similar option to everything I compile?
It seems I still had the GCC 4.7 documentation installed, so that's why I didn't find the -fno-diagnostics-show-caret option to turn it off.
However, that still doesn't answer the question of how to turn it off by default, so I'll keep this question unanswered for quite a while in case anyone has information on that.

gcc compiler not stop on first error

How does one get gcc to not stop compiling after the first error. Is there a compiler flag that will do this?
Basically I'm wanting to remove a class, but i'm not sure how much of an impact that will have, so i'm wanting to determine how many classes would have provblems if i, say, remove the class from the makefile.
Is there a better way to determine this impact?
There's a GCC compiler option -Wfatal-errors to stop after the first error:
-Wfatal-errors
This option causes the compiler to abort compilation on the first error occurred rather than trying to keep going and printing further
error messages
You can also use -Werror if you want to treat warnings as errors so that you'll catch any warning that might be generated when you remove your class.
Is there a better way to determine this impact?
Use the refactoring support, built-in in many IDEs. For example, with NetBeans, you can choose to rename a class and preview all affected places.
Without an IDE, you can rename the class/method/field, instead of deleting it and gradually, with several compilation runs, change all usages of the old name, where the compiler gives an error. Then grep for the new name.

Getting rid of "warning: dereferencing type-punned pointer"

I'm trying out OpenCobol with a simple Hello World example.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello World".
STOP RUN.
I compile with
cobc -x -free -o hello hello.cbl
And get a workable executable, but also a lot of these warnings from gcc
warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
From a Google search all I can find is that I can apparently just ignore these without ill effect. But for various reasons I'd like to actually get rid of them, if nothing else then at least suppressing them somehow. How can I do this?
Use -O to tone down optimisation.
I would have expected this was being applied to the C Code generation, and not passed through to gcc.
If you prefer absolute control over gcc,
Write a script to wrap your build.
(pass 1) To produce translated C code from cobc.
(pass 2) To compile (with lesser Optimisation) using gcc.
On a large project you can nest scripts for a full build ala make.
Excuse the late note;
For control over the C compiling phase, OpenCOBOL respects a few environment variables during the build chain.
See http://opencobol.add1tocobol.com/#does-opencobol-work-with-llvm for a list, including COB_CFLAGS and COB_LDFLAGS
From this OpenCobol forum thread it looks like you need to use the -fno-strict-aliasing option. Can't try it here because we don't use OpenCobol.
Somewhere under the covers you are invoking the gcc compiler. Try setting compiler options to turn the warning off as described here
The option should look something like: -Wno-strict

Where to find a full list of gcc warnings and errors?

Sometimes I need to use a gcc for cross-platform work, and sometimes gcc really amuses me with its warnings. For example:
#pragma once in a main file
This is very informative warning, but I really don't know what a 'main file' IS in terminology of gcc and WHY it must not contain #pragma once :). So, does any documentation exist that covers all gcc warnings and errors (mostly warnings, errors are trivial) with some comments on them?
The objective of '#pragma once' is to prevent a header from being reincluded. If you have it in a source file (usually a '.c' file), you won't be reading that twice (normally - I do know of a source file that reincludes itself [and I don't like it]; it does not use or want #pragma once, though!). So, a 'main file' in this context is one listed on the command line, for instance, rather than a header.
As to the subject matter of the question - the GCC manual does not seem to have a comprehensive list. I don't know whether there actually is one.

Selectively remove a warning message using GCC

This piece of code:
Int32 status;
printf("status: %x", status)
gives me the following warning:
jpegthread.c:157: warning: format '%x' expects type 'unsigned int', but argument 3 has type 'Int32'
I know I can get rid of it by casting the type, but is it possible with a GCC compiler flag to get rid of that particular type of warning, and still use -Wall?
If you need that code to work portable then you should cast the argument to unsigned int, as the int type may have a different size than Int32 on some platforms.
To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx.
From the GCC Warning Options:
You can request many specific warnings with options beginning -W, for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning -Wno- to turn off warnings; for example, -Wno-implicit. This manual lists only one of the two forms, whichever is not the default.
For your case the warning in question is -Wformat
-Wformat
Check calls to printf and scanf, etc., to make sure that the arguments supplied have types appropriate to the format string specified, and that the conversions specified in the format string make sense. This includes standard functions, and others specified by format attributes (see Function Attributes), in the printf, scanf, strftime and strfmon (an X/Open extension, not in the C standard) families (or other target-specific families). Which functions are checked without format attributes having been specified depends on the standard version selected, and such checks of functions without the attribute specified are disabled by -ffreestanding or -fno-builtin.
The formats are checked against the format features supported by GNU libc version 2.2. These include all ISO C90 and C99 features, as well as features from the Single Unix Specification and some BSD and GNU extensions. Other library implementations may not support all these features; GCC does not support warning about features that go beyond a particular library's limitations. However, if -pedantic is used with -Wformat, warnings will be given about format features not in the selected standard version (but not for strfmon formats, since those are not in any version of the C standard). See Options Controlling C Dialect.
It looks like the GCC manual does provide a way to do this with a #pragma, actually (contrary to what Aiden Bell says):
6.62.13 Diagnostic Pragmas
E.g., for the -Wuninitialized warning, you can do...
#pragma GCC diagnostic ignored "-Wuninitialized"
... to suppress the warning, or...
#pragma GCC diagnostic warning "-Wuninitialized"
... to treat it as a warning (not an error) even if you're building with -Werror.
I used the following CFLAGS:
-Wall -Wformat=0
I presume you are looking for the
#ifdef WIN32
#pragma warning (disable: #num of the warning)
#endif
Equivalent in GCC...
You can search for options such as -Wno-conversion -Wno-format-security that do the job in 3.8 Options to Request or Suppress Warnings.
But in terms of the #pragma directive:
I quote from the GCC mailing list from Google:
GCC does not, currently, provide the #pragma facility you are looking for.
Do not lose hope! There are viable
alternatives.
The first best way to fix the code so
it no longer emits the warning. Alas,
you've stated you cannot do this. :-(
NOTE: Have warnings turned up as
verbose as your team can tolerate!
[see below]
The next best way to ignore the
undesired warning is to post-process
the output of GCC to a script (such as
a Perl script) that strips out the
specific, exact warning you want to
ignore.
The next way to ignore the undesired
warning is to disable the warning for
that translation unit.
-Wno-foozle-mcgoogle, just for that particular translation unit. That's a
mighty big hammer, though. And if the
warning is in a header file, it may be
pervasive throughout your entire
project -- to which I'd direct you to
the post-processing script solution
(assuming you are disallowed from
fixing the code).
So currently no, there is no #pragma directive to disable specific warnings. Rather than using -Wall you could turn on as many warnings as you can minus specific ones.
http://www.network-theory.co.uk/docs/gccintro/gccintro_31.html
Or fix the code

Resources