This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Macro Expansion with NANA

Hello All,

I am trying to use the GNU NANA in my project for Invariants and Quantifiers. I am facing some issues with macro expansion (the macros are already written by NANA people).

Whenever I use the MACRO (A, E, and C)in my code and compile it, I get an error as "error: #29: expected an expression". The way I am using it is as in following code snippet:

int main(void)
{
  int q[5] = {1,2,3,4,5};
  I(q[4] == 5);
  N(q[4] == 5);
  I(A(temp = 0, temp < 5, temp++, q[temp] <= 5));
  return 0;
}

I got above error for line "I(A(temp = 0, temp < 5, temp++, q[temp] <= 5));"

I have also seen the expanded macro in the preprocessed file and it look like this:

int main(void)
{
  int q[5] = {1,2,3,4,5};

  do { if((1)) { if(!(q[4] == 5)) { _I_default_handler("I(" "q[4] == 5" ")","test.c",8); } } } while(0);
  do { if((1)) { if(!((!(q[4] == 5)))) { _I_default_handler("N(" "q[4] == 5" ")","test.c",9); } } } while(0);

  do { if((1)) { if(!(({ int _A_result = 1; temp = 0; while(temp < 5) { if(!(q[temp] <= 5)) { _A_result = 0; break; } temp++; } _A_result; }))) { _I_default_handler("I(" "A(temp = 0, temp < 5, temp++, q[temp] <= 5)" ")","test.c",15); } } } while(0);

  return 0;
}

There is some problem while returning _A_result. I tried compliling this with CodeSourcery, and it works fine with that.

Does keil not work well with macros?

Thanks in advance

Parents
  • Does your code expand similarly with the GNU toolchain?

    do {
        if ((1)) {
            if (!(({ int _A_result = 1; temp = 0; while(temp < 5) { if(!(q[temp] <= 5)) { _A_result = 0; break; } temp++; } _A_result; }))) {
                _I_default_handler("I(" "A(temp = 0, temp < 5, temp++, q[temp] <= 5)" ")","test.c",15);
            }
        }
    } while(0);
    

    The above expanded line have a full {} block with local variables inside the conditions of the if statement.

    The block inside the if conditional looks like:

    {
        int _A_result = 1;
        temp = 0;
        while (temp < 5) {
            if (!(q[temp] <= 5)) {
                _A_result = 0; break;
            }
            temp++;
        }
        _A_result;
    }
    

    Quite an interesting "expression" for the if statement to evaluate...

    The GNU toolchain have a lot of extensions, so you have to verify what actual code that the GNU compiler would see. Besides the statement block as "expression", there is also a temp variable that is never declared anywhere.

Reply
  • Does your code expand similarly with the GNU toolchain?

    do {
        if ((1)) {
            if (!(({ int _A_result = 1; temp = 0; while(temp < 5) { if(!(q[temp] <= 5)) { _A_result = 0; break; } temp++; } _A_result; }))) {
                _I_default_handler("I(" "A(temp = 0, temp < 5, temp++, q[temp] <= 5)" ")","test.c",15);
            }
        }
    } while(0);
    

    The above expanded line have a full {} block with local variables inside the conditions of the if statement.

    The block inside the if conditional looks like:

    {
        int _A_result = 1;
        temp = 0;
        while (temp < 5) {
            if (!(q[temp] <= 5)) {
                _A_result = 0; break;
            }
            temp++;
        }
        _A_result;
    }
    

    Quite an interesting "expression" for the if statement to evaluate...

    The GNU toolchain have a lot of extensions, so you have to verify what actual code that the GNU compiler would see. Besides the statement block as "expression", there is also a temp variable that is never declared anywhere.

Children