Matt Belle
Matt Belle

Reputation: 59

pre-compilation message in C

I'm writing a function that configures external interrupts in ATMEGA32 using C language

    typedef enum{
        LOW_LVL,
        CHANGE,
        FALLING,
        RISING
        }TRIGGER;

    void configExtInt(uint8_t ExtIntNo, TRIGGER trig){
        sei();
        if (ExtIntNo == INT0){

        }else if (ExtIntNo == INT1){

        }else if (ExtIntNo == INT2){

        }else{
            cli();
        /* warning message here*/
        }
    }

I want to display a warning message during compilation in case the user of my function provided parameter other than INT0, INT1, INT2 to my function.

For example :

configExtInt (INT3, FALLING);

Is that possible?

Upvotes: 1

Views: 97

Answers (1)

alk
alk

Reputation: 70921

[Not sure if I really understood your question correctly]

Assuming the INTxs are enums you could use a switch instead those several if-thens.

A decent GCC then would warn you about missing cases:

enum Ints
{
  INT0,
  INT1, 
  INT2
}

int main(void)
{
  enum Ints e = ...; /* Initialise to some value here. */

  switch (e)
  {
    case INT0:
      break;

    case INT1:
      break;
  }

  return;
}

Compile this using

gcc -g  -Wall  -Wextra -Wconversion -pedantic  main.c

and get:

main.c:12:3: warning: enumeration value ‘INT2’ not handled in switch [-Wswitch]
   switch (e)
   ^~~~~~

Upvotes: 1

Related Questions