I need to disable all warnings from a directory of a project which uses the external library ProtocolBuffers, which produces its own warnings.
These are some of the 151 warnings I get:
my.pb.cc: warning C4512: 'google::protobuf::FatalException' : assignment operator could not be generated
my.pb.cc: warning C4244: 'initializing' : conversion from '__int64' to 'int', possible loss of data
my.pb.cc: warning C4125: decimal digit terminates octal escape sequence
my.pb.cc: warning C4127: conditional expression is constant
my.pb.cc
xutility(2132): warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS.
Where xutility is a Visual Studio include file.
If I add these lines in my CMakeLists.txt file:
# Use a lower warning level threshold for autogenerated files
set(PROTOBUF_FILES_PATH "${CMAKE_CURRENT_BINARY_DIR}/src/*.pb.*")
file(GLOB PROTOBUF_FILES ${PROTOBUF_FILES_PATH})
set_source_files_properties(${PROTOBUF_FILES} PROPERTIES COMPILE_FLAGS /W2)
I get all warnings from my generated pb.cc files disabled, but I still have 15 warnings left from ProtoBuf's internal code, of the type:
my.pb.cc
xutility(2132): warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS.
How can I disable these last warnings, possibly adjusting my CMakeLists.txt file adding the -D_SCL_SECURE_NO_WARNINGS option, just for files with extension *.pb.*, considering I cannot modify ProtoBuf itself?
Without knowing much about xutility or how it is used by Protobuf, I would suggest trying to expand your set_source_files_properties() call to include COMPILE_DEFINITIONS:
set_source_files_properties(${PROTOBUF_FILES} PROPERTIES
COMPILE_DEFINITIONS _SCL_SECURE_NO_WARNINGS
COMPILE_FLAGS /W2
)
Related
While developing a plug-in for Catia using the CAA C++-Interface, I need to do a dynamic_cast:
DerivedClass *derived = dynamic_cast<*derived>(base);
When Build with mkmk, the compiler gives the warning:
[MkMk] warning C4541: 'dynamic_cast' used on polymorphic type '<DerivedClass>' with /GR-; unpredictable behavior may result
I tried to enable the run-time type information by adding LOCAL_CCFLAGS = /GR to the Imakefile.mk of the module. But the compiler overwrites this flag and disables the RTTI:
[MkMk] Command line warning D9025 : overriding '/GR' with '/GR-'
[MkMk] warning C4541: 'dynamic_cast' used on polymorphic type '<DerivedClass>' with /GR-; unpredictable behavior may result
How do I enable the RTTI correctly?
I recently found a header file which included the following, but didn't include a corresponding "pop" operation.
#pragma warning(push, 1)
#pragma warning(disable: 4503)
This had the effect of setting warning level to 1 for source files which include the header (we normally compile at warning level 3 with VS2013).
I'm looking for a way to detect mismatched push/pop operations so they can be removed from our code base. Ideally the detection would be part of our build process so once removed, no new instances would creep into the code.
One option would be to preprocess the source files and parse the output looking for mismatches. However, I'm wondering what other options might be available.
Any ideas?
Thanks!
Say there is a single warning such as path_statements, unused_variables. Is there a way to ignore a single instant of this, without isolating them into a code block or function?
To be clear, when there is a single warning in the code. I would like the ability to quiet only that warning, without having to do special changes addressing the particular warning.
And without this quieting warnings anywhere else, even later on in the same function.
With GCC this can be done as follows:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
/* Isolated region which doesn't raise warnings! */
this_would_raise_Wformat(args);
#pragma GCC diagnostic pop
Does Rust have the equivalent capability?
Note, am asking about the general case of how to quiet warnings.
Am aware there are ways to resolve unused var warning for eg.
To silence warnings you have to add the allow(warning_type) attribute to the affected expression or any of its parents. If you only want to silence the warning on one specific expression, you can add the attribute to that expression/statement:
fn main() {
#[allow(unused_variables)]
let not_used = 27;
#[allow(path_statements)]
std::io::stdin;
println!("hi!");
}
However, the feature of adding attributes to statements/expressions (as opposed to items, like functions) is still a bit broken. In particular, in the above code, the std::io::stdin line still triggers a warning. You can read the ongoing discussion about this feature here.
Often it is not necessary to use an attribute though. Many warnings (like unused_variables and unused_must_use) can be silenced by using let _ = as the left side of your statement. In general, any variable that starts with an underscore won't trigger unused-warnings.
If you want to silence all warnings of a kind in a module, write e.g. #![allow(dead_code)] (note the exclamation mark) at the top of the module. This will disable all warnings of this kind in the whole module. You can also call rustc with e.g. -A dead_code.
You can disable all warnings by writing #![allow(warnings)] at the top of the module.
You can insert a module (as described in the Rust book) where the specific warnings are ignored.
As Lukas said, you can also write e.g. #[allow(dead_code)] on a statement or an expression.
When compiling my project as a 64bit target (this isn't considered a warning on 32bit target) I get this warning:
5 enumeration values not handled in switch: 'kCreated', 'kLoaded', 'kConnected'...
I have somehow managed to turn off the error/warning message numbers, so I don't know what number to suppress in my code with #pragma warn.
I need to use #pragma warn because I only want to suppress approved places in my code (turning the warning off and on again).
Bonus question: Does anyone know how to get back the error/warning numbers again?
The 64bit compiler is based on CLang, which does not use warning numbers, which is why you do not see them. The following info comes from Bruneau Babet, one of C++Builder's chief developers, in the Embarcadero forums:
How do I suppress 64bit XE3 warnings?
Warnings in clang, hence bcc64, don't have the Wxxxx numbers. Behind the
scene there is a unique id generated for each warning but it's
auto-generated and cannot be assumed to remain constant across builds.
Instead each warning has a group. Related warnings are often in the same
group. Some groups have just one warning. To disable warnings of a group
you can use "-Wno-" on the command line, or via something like
the following in code:
#pragma clang diagnostic ignored "-W<groupname>".
For example, the first warning you listed is under the group "float-equal".
So, "-Wno-float-equal" should disable that warning. And to disable the one
about an enumerator not handled in the switch you can use the following in
code:
#pragma clang diagnostic ignored "-Wswitch"
So the next obvious question is how to find out about each group. The
"-fdiagnostics-show-option" should trigger the compiler to display the
option but unfortunately the IDE does not honor that option. So you must
either use the command line to find out about the group ach warning belongs
to, or you can peek at the Warning declarations here:
https://github.com/llvm/llvm-project/tree/main/clang/include/clang/Basic
The *.td files declare the various warnings. The ones mentioned above are
https://github.com/llvm/llvm-project/tree/main/clang/include/clang/Basic/DiagnosticSemaKinds.td
Oddly, #pragma clang is still not documented on the Embarcadero DocWiki.
We've updated to VS2012 recently and also changed our PostSharp version to 3.0.26. Generally this combination works fine, but I get warnings for my unit test project such as
The module "MyModule.Fakes.dll" does not contain any aspect or other transformation. For improved build-time performance, consider disabling PostSharp for this module by setting the compilation symbol (aka constant) "SkipPostSharp" in your project or set the MSBuild property "SkipPostSharp=True".
Now usually I can disable this warning by changing the project settings accordingly (SkipPostSharp = True), but I've already done that for my MyProjectTest project containing the unit tests.
When I add <SkipPostSharp>True</SkipPostSharp> to my MyModule.Fakes file in the Fakes folder, the warning about PostSharp disappears, however, I get another error message:
The element "Fakes" in namespace "http://schemas.microsoft.com/fakes/2011/" has invalid child element "SkipPostSharp" in namespace "http://schemas.microsoft.com/fakes/2011/". List of possible elements expected: "StubGeneration, ShimGeneration, Compilation" in namespace "http://schemas.microsoft.com/fakes/2011".
Update
I also tried editing the fakes.xsd file and add an extra SkipPostSharp element, but a) this still isn't recognised as a valid element by Visual Studio and b) I'm not sure whether this would be the right approach anyway.
Any ideas how to get rid of these warnings?
You can specify additional properties for the project file Fakes will generate to build the fakes assembly by placing Property elements inside of the Compilation element at the end of the .FAKES file. Here is an example based on your description.
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="MyModule"/>
<Compilation>
<Property Name="SkipPostSharp">True</Property>
</Compilation>
</Fakes>