Create own Warning / Error Message

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

Create own Warning / Error Message

Jump to solution
1,391 Views
Seppel
Contributor I

Hi,

 

how can i create my own warning or error message, which is used by the compiler?

 

What i want to do is to show an warning at compilation time f.e. when 2 Variables have certain values. Code should look like this:

if(var1 == var2)  {  #WARNING "attention, var1 and var2 have the same value"  }
Labels (1)
0 Kudos
1 Solution
917 Views
bigmac
Specialist III

Hello,

 

Since the preprocessing occurs prior to any compilation, in order to make use of the preprocessor error and warning commands would require that the initialisation constants for the static variables in question, be defined as preprocessor macros.  The macro values could then be compared by the preprocessor.

 

#define  VAR1_val  <expression>

#define  VAR2_val  <expression>

 

#if  VAR1_val==VAR2_val

#error "<string>

#endif

 

int var1 = VAR1_val;

int var2 = VAR2_val;

 

 

Regards,

Mac

 

View solution in original post

0 Kudos
10 Replies
917 Views
sebasira
Senior Contributor I

Hi Seppel!

 

As CrasyCat says, it depends on what compiler your using.

 

If it helps, I'm using CW 3.1 and CW 4.6, both for HCS12... Here's what I do:

 

#error "ERROR TEXT HERE"or#warning "WARNING TEXT HERE"

 

Maybe the problem is that you write it in UPPERCASE, try lowercase

 

Best Regards!

0 Kudos
917 Views
Seppel
Contributor I

Thanks for your replies Sebasira and CrasyCat!

 

I'm using a HCS08 Controller and Codewarrior 5.9.0. My Compiler accepts the suggestions from sebasira, so #error and #warning works. But i can't combine it with an IF ...THEN logic, how can i do this? The warning should only be showed, when an IF-condition is true. Therefor i have put in the #warning like in my first post. But this is warning always be shown, it don't cares of the IF condition.

0 Kudos
917 Views
CrasyCat
Specialist III

Hello

 

What are you trying to achieve here exactly?

 

#warning and #error generate preprocessor warnings or error messages. That means these messages are generated at compilation time.

You can have conditional error or warning messages generated if you enclose them into conditional preprocessing blocks.

 

For example

  #if   (MY_MACRO == 0)

    #warning "Incorrect definition of  MY_MACRO."

  #endif

 

The #warning or #error preprocessor commands cannot be used to generate warning or error messages at runtime.

 

CrasyCat

0 Kudos
917 Views
Seppel
Contributor I

I'm trying to implement a way to protect some vars from getting wrong init values.

F.e. i have some vars which are flags (bitpositions have the info), andthere are lots of possible flag state combinations. But there are few special flag state combinations, which are not allowed!

By developing the code i often switch between different flag-state-combinations and i want to protect myself to compile code with wrong flag-states.

 

I cannot test my Var with #if instruction like this:

#if(var1 == var2)    {    #error "Invalid Var State: var1 == var2"    }  #endif

Because #if expects a Macro-parameter. I try to generate a Macro like this

#define VAR_MACRO var1==var2

 

But this ends with "Warning C4443 Undefined Macro 'var1'"

 

 

 

0 Kudos
917 Views
sebasira
Senior Contributor I

If I don't understand wrong what you're trying to do is not possible, because values of var1 and var2 change at runtime. So the compiler won't know (at compilation time) if the value of var1 equals vars2. just like CrasyCat stated:

>The #warning or #error preprocessor commands cannot be used to generate warning or error messages at runtime

 

Is up to you (the programmer) to ensure that var1 won't equal var2. What you can do is put some "alarm" or something like that to notify that the not-allowed-condition took place. You can blink a LED, sound a buffer, send data trhough a port or display a message on screen. And that can help you debug your program and correct any wrong initialization.

 

0 Kudos
917 Views
Seppel
Contributor I

>You can blink a LED, sound a buffer, send data trhough a port or display a message on screen.

But these are all things that work at runtime. I don't want to recognize at runtime, but at compile time.

 

I init these vars as global vars in the top of the *.c-file and after these inits i want the compiler to check if the vars have wrong values.... isn't that possible?!?

I mean, the compiler f.e. also recognize if i want put a 32bit value in a 8bit unsigned char var, and show me a warning or error for trying that.

0 Kudos
918 Views
bigmac
Specialist III

Hello,

 

Since the preprocessing occurs prior to any compilation, in order to make use of the preprocessor error and warning commands would require that the initialisation constants for the static variables in question, be defined as preprocessor macros.  The macro values could then be compared by the preprocessor.

 

#define  VAR1_val  <expression>

#define  VAR2_val  <expression>

 

#if  VAR1_val==VAR2_val

#error "<string>

#endif

 

int var1 = VAR1_val;

int var2 = VAR2_val;

 

 

Regards,

Mac

 

0 Kudos
917 Views
sebasira
Senior Contributor I

At compilation time it's IMPOSSIBLE to know the values of the variables, because they change at runtime, that's why everything I suggest was at runtime.

 

Once I define a MACRO for every bitposition and then test it at compilation time, maybe it helps. I'll explain what I did:

 

Lets assume you have and 8bit register where every bitposition is a flag. It could be described as:

 

msb                                                       lsb

    | F7 | F6 | F5 | F4 | F3 | F2 | F1 | F0 |

 

Then you have a variable wich uses this structure (in your case it will be var1) , then when initializing var1 you do something like this:

var1.F7 = 1;var1.F4 = 1;var1.F3 = 1;

 

Now, what you can do is add some macro definitions, for every falg initialization, where the value of the macro is the mask for the corresponding flag:

// Every bit-mask definition for var1#define MACRO_var1_F7   0x80#define MACRO_var1_F6   0x40#define MACRO_var1_F5   0x20#define MACRO_var1_F4   0x10#define MACRO_var1_F3   0x08#define MACRO_var1_F2   0x04#define MACRO_var1_F1   0x02#define MACRO_var1_F0   0x01// Value of macro corresponding with initialization values#define MACRO_var1      (MACRO_var1_F7 + MACRO_var1_F4 + MACRO_var1_F3)   // Wrong initilization#define MACRO_var1_WRONG   (MACRO_var1_F7 + MACRO_var1_F4 + MACRO_var1_F3 + MACRO_var1_F0)   var1 = MACRO_var1;    // Replace everybit initialization with macro value#if (MACRO_var1 == MACRO_var1_WRONG)   #error "Wrong var1 initialization"#endif

 

I don't know if I made myself clear, but that's the closer you can get if you want to detect and init error at compilation time

 

Hope it helps!


 


0 Kudos
917 Views
Seppel
Contributor I

First thanks again for your detailed help sebasira and bigmac!

 

I didn't know / recognize that #warning etc are preprocessor instructions, which are used before compilation-time, so now i understand why i can't check my vars like that :smileyhappy:

I will take the solution that bigmac offers...really smart by the way:

1) put my flag values in MACROs

2) compare the MACROs

3) init vars with MACRO values

 

Thank you for your help!

 

0 Kudos
917 Views
CrasyCat
Specialist III

Hello

 

- Which CPU are you targeting (HC08, HC12, Coldfire, ..)
- Which version of CodeWarrior are you using?

#warning is not a ANSI C standard pre-processor macro. So answer here depends on the compiler you are using.

 

CrasyCat

 

0 Kudos