Hello,
I'd like to know how I can enable warnings for incorrect enum assignment or being passed as function parameter. Here is an example
typedef enum
{
TEST_1A = 0,
TEST_2A = 1
} test_1_t;
typedef enum
{
TEST_1B = 0,
TEST_2B = 1
} test_2_t;
If I declare you variables,
test_1_t Var1;
test_2_t Var2;
Var2 = TEST_1A; // this line should cause a warning.
Can someone please let me know if how to enable this? I've already enabled -Wall but that doesn't seem to work.
Thank you
Regards
Santhosh
Solved! Go to Solution.
Hi Santhosh,
In ANSI-C enumeration are of type ‘int’. So there is no reason for the compiler to have a warning for what
Enums in C are really like int's like
int i = 3;
Likewise, you can do any things with enumeration exactly what you can do with ints.
e.g.
Var2 = (TEST_1A+TEST_2B)/TEST_2A;
Is legal and allowed in the C programming language. Again: enumerations are just ‘int’s by the rule of the language.
The mentioned PC-lint checker is a good suggestion. If you want to have a warning (actually: an error without a proper cast), then he should write and use this in C++.
In C++ enumerations are not of type ‘int’, but a distinct type (for each enumeration). Actually that’s the main benefit for using C++: it provides stronger type checking and flags many things like the above one as an error. I think this is what you could consider if you want to catch things like this. Another option is to compiler the C sources as C++ ones, but this will likely flag other things too (which might be a good thing).
I hope this helps,
Erich
Hello Everyone,
Thank you so much for the detailed explanation - I came to the same conclusion that has been suggested in the posts. The static analysis tool is a good option. But the way I've solved it is by compiling the code with C++ compiler.
I had to make few changes to have the code compatible both in C and C++, the most notable is the ISR. Here is a link for changing the ISR - https://community.nxp.com/message/630632
Thanks once again for all your help.
Regards
Santhosh
Hi Santhosh,
In ANSI-C enumeration are of type ‘int’. So there is no reason for the compiler to have a warning for what
Enums in C are really like int's like
int i = 3;
Likewise, you can do any things with enumeration exactly what you can do with ints.
e.g.
Var2 = (TEST_1A+TEST_2B)/TEST_2A;
Is legal and allowed in the C programming language. Again: enumerations are just ‘int’s by the rule of the language.
The mentioned PC-lint checker is a good suggestion. If you want to have a warning (actually: an error without a proper cast), then he should write and use this in C++.
In C++ enumerations are not of type ‘int’, but a distinct type (for each enumeration). Actually that’s the main benefit for using C++: it provides stronger type checking and flags many things like the above one as an error. I think this is what you could consider if you want to catch things like this. Another option is to compiler the C sources as C++ ones, but this will likely flag other things too (which might be a good thing).
I hope this helps,
Erich
-Wpedantic and -Wextra will enable other warnings beyond -Wall.
However C has limited type system compared to that of C++.
What you are really after is Lint.
The Leader in Static Analysis for C/C++ -- PC-lint and FlexeLint
which is what I use myself.
# -Wenum-compare:
# Warn about a comparison between values of different enumerated
# types. In C++ enumeral mismatches in conditional expressions are
# also diagnosed and the warning is enabled by default. In C this
# warning is enabled by -Wall.
CFLAGS += -Wextra
CPPFLAGS += -Wextra
# -Wextra:
# This enables some extra warning flags that are not enabled by
# -Wall.
#
# -Wclobbered
# -Wempty-body
# -Wignored-qualifiers
# -Wmissing-field-initializers
# -Wmissing-parameter-type (C only)
# -Wold-style-declaration (C only)
# -Woverride-init
# -Wsign-compare
# -Wtype-limits
# -Wuninitialized
# -Wunused-parameter (only with -Wunused or -Wall)
# -Wunused-but-set-parameter (only with -Wunused or -Wall)
#
# The option -Wextra also prints warning messages for the following cases:
#
# A pointer is compared against integer zero with ‘<’, ‘<=’, ‘>’, or ‘>=’.
# (C++ only) An enumerator and a non-enumerator both appear in a conditional expression.
# (C++ only) Ambiguous virtual bases.
# (C++ only) Subscripting an array that has been declared ‘register’.
# (C++ only) Taking the address of a variable that has been declared ‘register’.
# (C++ only) A base class is not initialized in a derived class's copy constructor.
#CFLAGS += -Wpedantic
#CPPFLAGS += -Wpedantic
#-Wpedantic
# Issue all the warnings demanded by strict ISO C and ISO C++; reject
# all programs that use forbidden extensions, and some other programs
# that do not follow ISO C and ISO C++. For ISO C, follows the version
# of the ISO C standard specified by any -std option used. Valid ISO
# C and ISO C++ programs should compile properly with or without this
# option (though a rare few require -ansi or a -std option specifying
# the required version of ISO C). However, without this option,
# certain GNU extensions and traditional C and C++ features are
# supported as well. With this option, they are rejected.
#
# Some users try to use -Wpedantic to check programs for strict ISO C
# conformance. They soon find that it does not do quite what they
# want: it finds some non-ISO practices, but not all—only those for
# which ISO C requires a diagnostic, and some others for which
# diagnostics have been added.
#CFLAGS += -pedantic-errors
#CPPFLAGS += -pedantic-errors
# Like -Wpedantic, except that errors are produced rather than warnings.