Toggle warnings with fsyntax-only parameter - c++

I have a question concerning compilation between g++ and gcc.
If I write this code:
int main(int args, char* argv[]){
return 0;
}
and compile it with: g++ -fsyntax-only -Wall -Wextra -Werror
whether the file has a .c or a .cpp extension it won't complain about unused paramaters(args and argv).
The -Wunused option will only work if I compile the file with a .c extension and gcc.
So my question is: is it possible to enable warnings with fsyntax-only parameter in all other cases ?
Thank you in advance for any reponse

No, it is not, -fsyntax-only only checks the syntax. – nos
Options to Request or Suppress Warnings:
-fsyntax-only Check the code for syntax
errors, but don’t do anything beyond that.

This anomaly appears to have been a g++ compiler bug. The OP's
observations are confirmed with g++ 5.4 but g++ 6.3 gives
the expect warnings:
$ g++-6 -fsyntax-only -Wall -Wextra -Werror test.cpp
test.cpp: In function ‘int main(int, char**)’:
test.cpp:1:14: error: unused parameter ‘args’ [-Werror=unused-parameter]
int main(int args, char* argv[]){
^~~~
test.cpp:1:31: error: unused parameter ‘argv’ [-Werror=unused-parameter]
int main(int args, char* argv[]){
^
cc1plus: all warnings being treated as errors

Related

G++ raise compile errors instead of warnings for narrowing conversions

I want to get compile errors instead of warnings for this code:
#include <iostream>
int main(int argc, char ** argv)
{
float a = 1.3f;
int b = 2.0 * a;
std::cout << b << "\n";
}
If I compile it with:
g++ test.cpp -o test
I have no errors.
But If I compile the same code with:
g++ test.cpp -o test -Wconversion
I got the following warning:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:6:17: warning: conversion from ‘double’ to ‘int’ may change value [-Wfloat-conversion]
6 | int b = 2.0 * a;
I'm looking for a way to get compile errors instead of warnings only for this particular type of warning.
Obs.1: -Werror can make all warnings become errors but it is not what I am looking for
Obs.2: I'm using g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Use -Werror= to treat specific warnings as errors only:
g++ test.cpp -o test -Werror=conversion

g++ compile options with -std=c++14 compile error showing Werror=c++11-compat

My environment Arch Linux, gcc 7.2
I'm learning C++ and I'm using keyword constexpr to define a constant, while compile, it give me an error message
error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
I can compile my program with default g++, but cannot compile with -std=c++14 and -Werror
The command I'm using is:
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
I believe the -Werror option caused the issue. but what is the issue? can someone tell me please?
#include <iostream>
int main() {
constexpr double yen_dollar = 0.107;
std::cout << yen_dollar << std::endl;
return 0;
}
test.cpp:4:5: error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
constexpr double yen_dollar = 0.107;
^~~~~~~~~
test.cpp: In function ‘int main()’:
test.cpp:4:5: error: ‘constexpr’ was not declared in this scope
test.cpp:5:16: error: ‘yen_dollar’ was not declared in this scope
std::cout << yen_dollar << std::endl;
From the GCC documentation §3.4 Options Controlling C Dialect, one can read:
-ansi
In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98.
Since, you compiled with
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
-ansi overwrites -std=c++14 with -std=c++98. This is why constexpr is not recognized.
Solution: get rid of the -ansi flag.

Gcc is ignoring -Wno-unused-variable

I'm using both gcc version 4.8.3 20140624 and gcc4.9.1 compiled from trunk. However, the flag doesn't seem to be ignored on online compilers. Here is the test program:
#include <iostream>
int main()
{
int i;
}
And shell output:
g++ -std=c++11 -O2 -Wall -Wextra -pedantic -Wno-unused-variable -I./ -c -o test.o test.cpp
test.cpp:6:5: warning: unused parameter ‘argc’ [-Wunused-parameter]
int main(int argc, char* argv[])
^
test.cpp:6:5: warning: unused parameter ‘argv’ [-Wunused-parameter]
With the following program
int main(int argc, char** argv)
{
int i;
}
with g++ -std=c++11 -O2 -Wall -Wextra -pedantic -Wno-unused-variable
You don't have warning for unused-variable i.
if you don't want warning for parameter argc, argv,
add also the compiler flag -Wno-unused-parameter
Live example

Missing "missing sentinel" warning for exec in C++11

GCC will helpfully warn you if you forget to include the NULL sentinel at the end of a call to one of the exec(3) functions:
#include <unistd.h>
int main(int argc, char **argv)
{
execlp("test", "test", "arg1");
}
Sample compiler output for GCC 4.8:
$ g++ test.cc -Wformat
test.cc: In function ‘int main(int, char**)’:
test.cc:4:32: warning: missing sentinel in function call [-Wformat=]
execlp("test", "test", "arg1");
^
$
However, if you compile in C++11 mode, no diagnostic is printed:
$ g++ test.cc -std=c++11 -Wformat
$
Why is this warning not available in C++11? Is there any way to get it back?
execlp is not a standard C function. For the compiler to recognize it as a "standard" function, for which it knows what the arguments should look like, you need -std=gnu++11 instead of -std=c++11. Note that the default is -std=gnu++98. Glibc could improve the situation by specifying the sentinel attribute on the declaration of execlp.

Forcing GCC 4.x to treat -Wreturn-type as an error without enabling -Werror?

Suppose we have the following code:
#if !defined(__cplusplus)
# error This file should be compiled as C++
#endif
#include <stdio.h>
#include <string>
//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}
std::string GetSomeString()
{
// case #1
}
};
#endif // USE_CXX_CLASS
int foo()
{
// case #2
}
int
main (int argc, char *argv[])
{
(void)argc;
(void)argv;
#ifdef USE_CXX_CLASS
SomeClass someInstance;
someInstance.GetSomeString();
#endif // USE_CXX_CLASS
foo();
return 0;
}
And suppose that it were to be compiled the C++ compiler (and not the C compiler) from GCC version 4.2.1 with the options -Wreturn-type -Werror=return-type. If the above code is compiled as is without first uncommenting the //#define USE_CXX_CLASS line above, then you will see a warning but no error:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function
But if the //#define USE_CXX_CLASS line is uncommented, then the warning is treated as an error:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1
Yes, one is a non-member function (case #2), and the other is a C++ function (case #1). IMO, that should not matter. I want both conditions treated as an error, and I don't want to add -Werror or -Wall at this point in time (probably will do so later, but that is out of scope of this question).
My sub-questions are:
Is there some GCC switch that I am missing that should work? (No I do not want to use #pragma's.)
Is this a bug that has been addressed in a more recent version of GCC?
For reference, I have already poured through other similar questions already, including the following:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
C question: no warning?
Is a return statement mandatory for C++ functions that do not return void?
It has been fixed, it works well with g++ 9.3: both member functions and free functions are treated as error with -Wall -Werror=return-type
I do see an error even w/o the USE_CXX_CLASS flag. i.e. g++ is consistent with the error for both class member functions and non member functions.
g++ (GCC) 4.4.3 20100127 (Red Hat 4.4.3-4)
It seems to me that what you need is a shell script wrapper around gcc.
Name it something like gcc-wrapper and g++-wrapper.
In your Makefile set CC and CXX to the wrappers.
Have the wrapper invoke GCC and pipe its output to another program which will search for your desired warning strings.
Have the search program exit with an error when it finds the warning.