Suppressing warning about #pragma pack in included file - c++

I'm building some code with clang. Here's a cut-down version of what I'm doing. (Please bear in mind that during the cutting-down process, I've cut out every irrelevant detail I can, including anything that might make it obvious why I might actually want to do this.)
push.h:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragma-pack"
#pragma pack(push)
#pragma pack(1)
pop.h:
#pragma pack(pop)
#pragma GCC diagnostic pop
main.c:
#include "push.h"
struct Fred {char x;};
#include "pop.h"
Compile it like this:
clang -Wall -pedantic -c main.c
Doing this, I get a warning:
tmbp ~/tmp/pushpack % clang -Wall -pedantic -c main.c
main.c:3:10: warning: the current #pragma pack aligment value is modified in the included file [-Wpragma-pack]
#include "pop.h"
^
note: previous '#pragma pack' directive that modifies alignment is here
1 warning generated.
tmbp ~/tmp/pushpack %
How can I suppress this warning in this case? I'd like to do this by adding something to pop.h, if possible.
I don't really want to suppress the warning globally, because it seems like it might be kind of useful in the long run (even if not when I'm including push.h and pop.h).
I don't want to add additional junk to every include of pop.h, because in my non-cut-down actual program, because there's tons of them.
I don't want to have just a naked #pragma pack(pop) instead, because I'd prefer things to be symmetrical.
I don't want to make them symmetrical by inlining the contents of push.h, because in practice there's a bit more to it than is shown here.
What, if any, are my options?
Clang version: (this is the one that comes with Xcode 10 - don't think this warning was there in Xcode 9)
tmbp ~/tmp/pushpack % clang --version
Apple LLVM version 10.0.0 (clang-1000.11.45.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

You cannot restore the warning options in pop.h if you don't want to see the warning generated for main.c resulting from including pop.h. Restore the warning options after `#include "pop.h".

Related

How can I avoid `#pragma once in main file` in GCC when using precompiled headers? [duplicate]

This question already has answers here:
gcc precompiled header: pragma once in main file
(3 answers)
Closed last year.
Here is a minimal example:
// pch.h
#pragma once
#include <iostream>
And I run:
g++ -x c++-header -o pch.h.gch -c pch.hpp
When I run the command, I get
pch.h:1:9: warning: #pragma once in main file
1 | #pragma once
|
From my understanding, this behavior is intended by GCC after reading their bugzilla bug tracker; so, this is not a bug but a feature.
How can I disable this warning? Is there a warning number or name that I can suppress by adding it as a pragma statement:
#pragma GCC diagnostic ignored "-Wcan-i-set-something-here"
From my understanding, this behavior is intended by GCC
As far as I can tell, it's a bug.
How can I disable this warning?
Unfortunately, it appears that you cannot since the warning cannot be controlled by an option. In my opinion, this is a bug as well.
You can circumvent the issue by using a header guard instead of #pragma once.

Error: Initializer provided for function, __THROW __asm

I am trying to port an ARM-C library to be compiled with x86_64 C++, and I am getting the following error:
In file included from /usr/include/c++/5/cwchar:44:0,
from /usr/include/c++/5/bits/postypes.h:40,
from /usr/include/c++/5/bits/char_traits.h:40,
from /usr/include/c++/5/string:40,
from MyFile.h:19,
/usr/include/wchar.h:226:20: error: initializer provided for function
__THROW __asm ("wcschr") __attribute_pure__;
^
where MyFile.h has the following structure
// comments
#pragma once
// comments
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string> //<<< line 19
…
Initially, instead of it used to be which gave me a similar error:
In file included from MyFile.h:19:
/usr/include/string.h:73:21: error: initializer provided for function
__THROW __asm ("memchr") __attribute_pure__ __nonnull ((1));
^
Compiler version:
GNU C++14 (Ubuntu 5.4.0-6ubuntu1~16.04.11) version 5.4.0 20160609 (x86_64-linux-gnu)
compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
ldd (Ubuntu GLIBC 2.23-0ubuntu11) 2.23
Compilation flags:
#g++ -O3 -std=c++14 -fpermissive -Wno-system-headers -w
UPDATE 1:
I've been modifying the Makefile, and the original version contains $#.via. For instance:
#$(COMPILE) -M -MF $(subst .o,.d.tmp,$#) -MT $# -E $(C_FLAGS) $#.via $< -o $#.preprocessed.c
and I changed the $#.via for #$#.via because I saw that in an older project they did it like that. However, if I leave as $#.via I just get:
SomeFile.c:1:1 fatal error: OneHeader.h: No such file or directory
I am starting to think that my Makefile is somewhere wrong...
I misunderstood the compiler option... Few lines above, my makefile creates the #.via files passing DEFINES and INCLUDES
#echo $(patsubst %, '%', $(C_DEFINES)) > $#.via
#echo $(C_INCLUDE) >> $#.via
and those #.via files are passed as additional arguments for the compilation. While for armcc the --via is supported see here, I found that for g++ -according to the gcc doc- the syntax is #<your_file>. Thus, what #$#.via does is simply to parse the $#.via to <your_file>.via.
Now I am still getting the initializer provided for function error message.
UPDATE 2:
I found the problem and I explained what happened in the answer section. See below.
Root cause
The problem was originated because I redefined __asm to be replaced by nothing (e.g. #define __asm) since I didn't want to touch the assembly code yet. Remember that I said I am porting ARM to x86, so I thought that easiest way to get rid of the compile errors was to remove all those __asm instructions, but not considering the effects of doing such a thing.
In other words, when I included the string.h header, the header itself uses assembly call as the error messaged pointed out:
/usr/include/wchar.h:226:20: error: initializer provided for function
__THROW __asm ("wcschr") __attribute_pure__;
and when the preprocessor changed the __asm("wcschr") for ("wcschr") the compiler hits the error -- which makes sense.
Moral of the history
Do not redefine qualifiers since it will also affect other modules that you are not seeing directly and prefer creating a macro to just change them (e.g. __asm for /*__asm*/) or just run sed in you code base.

How can I suppress a "-fpermissive" error using modern GCC?

I am trying to compile some nonconforming code in C++17, but I am stuck with the following issue.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-fpermissive"
Some code that compiles only when with -fpermissive flag is set:
#pragma GCC diagnostic pop
It compiles fine on GCC version 4.6.4 through 4.7.4, but all later versions of GCC are giving me the following warning and don't suppress the error.
warning: ‘-fpermissive’ is not an option that controls warnings [-Wpragmas]
#pragma GCC diagnostic ignored "-fpermissive"
When I write (out of desperation)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-fpermissive"
Some code that compiles only when with -fpermissive flag is set:
#pragma GCC diagnostic pop
I am back at square one. Currently I'd like to continue using GCC 7.1 for the project. I can compile the entire project with -fpermissive flag set as a compile option, but this means that some other section of code causing a -fpermissive error could compile.
Condensed example https://godbolt.org/g/KFd5Ke
This question is not a duplicate of In GCC, how can I mute the '-fpermissive' warning? as this is directed toward newer versions of GCC where the solution provided in the aforementioned Stack Overflow question does not work. I even included an example.

How to disable all warnings using pragma directives in GCC

I am seeking for a way to suppress all possible warnings that i may get with Gcc with pragma directives. I had made some guard macros that help me silence 3rd party headers from warnings, and for now they work like charm for msvc and clang. I am still missing the correct way to use Gcc diagnostic pragmas in order to suppress every warning in a section. Let me give you some examples:
In msvc we can do this:
#pragma warning(push, 0)
// Code that produces warnings...
#pragma warning(pop)
And in clang we can do this:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wall"
#pragma clang diagnostic ignored "-Wextra"
// Code that produces warnings...
#pragma clang diagnostic pop
And the code that is in the middle is now being silenced from warnings for good.
And in Gcc we also have similar pragma directives with clang and i thought i could try something like this:
#pragma GCC diagnostic push
#pramga GCC diagnostic ignored "-Wall"
#pragma GCC diagnostic ignored "-Wextra"
// Code that produces warnings...
#pramga GCC diagnostic pop
But passing -Wall and -Wextra in diagnostic ignored pragma in GCC does not work like clang, and does not disable all the possible warnings. Instead of this passing a specific warning to disable works:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
void foo (int x) // No longer getting "unused parameter 'x'" warning
{
}
#pragma GCC diagnostic pop
So the only workaround i can think so far is to make a long list with all the GCC warning flags and use them like above. Is there a more elegant solution?
If not where i can get the complete Gcc warning flag list (favorably in a plain list)?
Documentation says:
At the moment only warnings (normally controlled by ‘-W...’) can be controlled, and not all of them. Use -fdiagnostics-show-option to determine which diagnostics are controllable and which option controls them.

How to disable Wmaybe-uninitialized warning

I want to disable "Wmaybe-uninitialized" warning.
I use omnet++ ( base on c++ ) to simulate my project.
Is there anyway to disable this warning ?
I also found the following code , but it doesn't work.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#pragma GCC diagnostic pop
Adding the flag -Wno-maybe-uninitialized works, but should be used with care. See the thread GCC -Wuninitialized / -Wmaybe-uninitialized issues