LPCOpen bug : TRUE is false.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPCOpen bug : TRUE is false.

438 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by peufeu on Fri Aug 08 08:00:32 MST 2014
lpcopen2.12/lpc_chip_43xx/inc/lpc_types.h contains the following statement.

typedef enum {FALSE = 0, TRUE = !FALSE} Bool;


Compiling the following code with lpc_types.h included :

#if True
#pragma message "TRUE is TRUE"
#else
#pragma message "TRUE is FALSE"
#endif

#ifdef True
#pragma message "TRUE is Defined"
#else
#pragma message "TRUE is Not Defined"
#endif


Gives this result :

foo.c:40:9: note: #pragma message: TRUE is FALSE
foo.c:46:9: note: #pragma message: TRUE is Not Defined

Interesting. The preprecessor doesn't care about enums...

I have libraries with conditional compiles like :

// in config.h
#define USE_FEATURE_FOO TRUE

// in some other file
#if USE_FEATURE_FOO
 .. implement foo ..
#endif


This does not work if lpc_types is included...

Fix is to edit lpc_types.h and replace the typedef with the usual :

#define FALSE 0
#define TRUE  1

Labels (1)
0 Kudos
5 Replies

423 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hparracho on Wed Aug 13 03:22:36 MST 2014
The answer that TheFallGuy gave you basically sums it up perfectly.

The pre-processor is a macro processor and runs before compilation. It knows nothing about the language and it's types.
It only understands macros defined with the #define keyword and literal values.

So, if you want to test if a macro is defined you use #ifdef or #if defined(...)
If you want to test the value of a macro you use #if.

You can also test the value of literals like:
#if 1
or
#if 'a'

In C "TRUE/FALSE" are not literals, but in C++ "true/false" are.
0 Kudos

423 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by peufeu on Fri Aug 08 09:20:38 MST 2014
It's a copypaste mistake

Here's the real one :

typedef enum {FALSE = 0, TRUE = !FALSE} Bool;

#if TRUE
#pragma message "TRUE is TRUE"
#else
#pragma message "TRUE is FALSE"
#endif

#ifdef TRUE
#pragma message "TRUE is Defined"
#else
#pragma message "TRUE is Not Defined"
#endif


(It still says that TRUE is FALSE, but it now spits a warning, thanks to -Wundef now being activated).
0 Kudos

423 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hparracho on Fri Aug 08 08:44:41 MST 2014

Quote: peufeu

Compiling the following code with lpc_types.h included :

#if True
#pragma message "TRUE is TRUE"
#else
#pragma message "TRUE is FALSE"
#endif

#ifdef True
#pragma message "TRUE is Defined"
#else
#pragma message "TRUE is Not Defined"
#endif


Gives this result :

foo.c:40:9: note: #pragma message: TRUE is FALSE
foo.c:46:9: note: #pragma message: TRUE is Not Defined



C/C++ is case sensitive.
"TRUE" is defined, "True" is not. The output is ok to me.
0 Kudos

423 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by peufeu on Fri Aug 08 08:41:32 MST 2014

Quote: TheFallGuy
The pre-processor is a PRE_PROCESSOR - it processes the source code before the compiler gets to see it. It is simply a text processor that processes macros and knows nothing about enums, or any other C types or syntax.



Yep. This is why using enums for TRUE and FALSE constants is bad practice.
0 Kudos

423 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri Aug 08 08:12:33 MST 2014
That is a bug is your code and a misunderstanding of how the pre-processor works.

The pre-processor is a PRE_PROCESSOR - it processes the source code before the compiler gets to see it. It is simply a text processor that processes macros and knows nothing about enums, or any other C types or syntax.

To avoid the problems you are having when conditionally compiling code, you should be using the values 0 and 1 (or use 'defined') and not ANOTHER macro. If you try to do anything else, you will have problems with LPCOpen and MANY other libraries.

So, either do this
#define USE_FEATURE_FOO 1
#if USE_FEATURE_FOO
or
#define USE_FEATURE_FOO
#ifdef USE_FEATURE_FOO
or
#define USE_FEATURE_FOO
#if defined(USE_FEATURE_FOO)

Learn about the C pre-processor here:
https://gcc.gnu.org/onlinedocs/cpp/Overview.html#Overview
0 Kudos