473,287 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Macro expansion in armcc

there's a MACRO call :

MACRO1(cnf)

and its expansion is :

#define MACRO1(cnf) (((cnf) != TRUE)? (CLEANUP(FAIL)):(err =
SUCCESS));

#define CLEANUP(a)
\
{
\
if ((err = (a)) != SUCCESS)
\
{
\
goto cleanup;
\
}
\
}
\
Its giving me error:
line 392: Error: expected an expression
MACRO1(cnf)
^

Same code is getting compiled in gcc but armcc gives the above error..
Can anybody pls comment on this?

Jul 11 '06 #1
2 3464
sr*******@gmail.com wrote:
there's a MACRO call :

MACRO1(cnf)

and its expansion is :

#define MACRO1(cnf) (((cnf) != TRUE)? (CLEANUP(FAIL)):(err =
SUCCESS));
I assume that in your code it's all on one line; if not, it should have
given an error for this declaration, not for your call.
#define CLEANUP(a)
{
if ((err = (a)) != SUCCESS)
{
goto cleanup;
}
}
Ditto here; you provided line continuation backslashes, but they
themselves had wrapped to the next line. I've snipped them here for
brevity.
Its giving me error:
line 392: Error: expected an expression
MACRO1(cnf)
^
The CLEANUP macro is declared to be equivalent to an entire block
statement containing an if statement. However, in MACRO1, you use it
essentially like this:

value? CLEANUP(FAIL): expr;

In ISO C, the ternary operator takes three expressions, not one
expression and three bits of whatever executable code. IOW, this:

value? { if (x) y; }: expr;

is invalid, but that is what you are declaring.

Since your CLEANUP macro involves a goto statement (see the castigations
for that flying in from a dozen posters, btw), you can't turn CLEANUP
itself into a ?: expression, which would have solved this problem; so
you're left with only one solution: change your MACRO1 to use an if
statement as well, so that you get something like

if (!cnf) {
if ((err=FAIL)!=SUCCESS) { goto cleanup; }
} else err=SUCCESS;

It's still ugly, and you will probably want to consult the FAQ on how to
write a statement macro more solidly, but it should work - FSVO.
Same code is getting compiled in gcc but armcc gives the above error..
That's because gcc supports complete statements in ?: (and IIRC in all
expression contexts). Beats me why; its main use is to create illegible
code.

Richard
Jul 11 '06 #2
sr*******@gmail.com writes:
there's a MACRO call :

MACRO1(cnf)

and its expansion is :

#define MACRO1(cnf) (((cnf) != TRUE)? (CLEANUP(FAIL)):(err =
SUCCESS));

#define CLEANUP(a)
\
{
\
if ((err = (a)) != SUCCESS)
\
{
\
goto cleanup;
\
}
\
}
\
Its giving me error:
line 392: Error: expected an expression
MACRO1(cnf)
^

Same code is getting compiled in gcc but armcc gives the above error..
Can anybody pls comment on this?
I see a number of problems.

As Richard Bos has mentioned, some of your lines wrapped when you
posted this.

Stylistically, there's way too much vertical space in the definition
of CLEANUP, and some of the '\'s wrapped to the next line.

It's easy enough to fix the lin wrapping in MACRO1, either by
re-joining the line or by splitting it and adding a '\'.

Here's a re-formatted version of CLEANUP:

#define CLEANUP(a) \
{ \
if ((err = (a)) != SUCCESS) \
{ \
goto cleanup; \
} \
}

I presume TRUE is defined somewhere else. It's almost never a good
idea to compare a boolean expression for equality with TRUE or FALSE.
Remember than an expression with the value zero is considered false
when used as a condition, and an expression with *any* non-zero value
is considered true. Your test
((cnf) != TRUE)
will fail if cnf is true (non-zero) but doesn't happen to have the
same value as TRUE (presumably 1). cnf is already a condition; just
use it directly. ((cnf) != TRUE) can be replaced by (!(cnf)). See
section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

A macro usually expands to either an expression or a statement. An
expression can be used as a statement by adding a ';', but a statement
cannot be used in an expression context.

Your MACRO1 definition *almost* expands to an expression, but you've
added a semicolon, so you can't use MACRO1 in an expression context.
That's what the error message is telling you.

I might define MACRO1 something like this:

#define MACRO1(cnf) ( (cnf) ? err = SUCCESS : CLEANUP(FAIL) )

(but I'd give it a better name!)

Your CLEANUP macro expands to a compound statement. This means, of
course, that you can't use it in an expression context. Presumably
you haven't made that mistake, but since you only showed us the
definitions of your macros and not the code that invokes them, we
can't really tell. But there's still a problem using CLEANUP even in
a statement context; see question 10.4 in the FAQ.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Caleb Hattingh | last post by:
Hi Here is a script I want to be able to write (explanation appears after): *** start of script *** import MyCustomMacroLib # This does the magic I would like help for. # This is not...
1
by: me | last post by:
Hi guys I want to insert a load of pieces of data into a map The map has an std::string representing a field name as the key, and the value is a struct with 2 members - the field length and a...
3
by: Ark | last post by:
Hello, NG, Please, help on this snippet: #define CAT(a,b) a##b #define COMMENT CAT(/,/) COMMENT This is a comment Should it compile? It passes MS C/C++ 13.0 (Visual Studio 2002) and fails...
7
by: reppisch | last post by:
Hi Ng, i am looking for a method of expanding a macro while the rest of the code remains untouched. I have some code which does macro voodo / ifdef's which i would like to strip and simplify. ...
4
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
3
by: casul | last post by:
Hi All, I was told there were a few macro gurus on this group :) I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE )
0
by: borophyll | last post by:
Hi all Can anyone explain to me the algorithm for macro expansion in the C++ preprocessor. I am confused why the following code works like it does: #define A(x) #x #define B(x) A(x)...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
5
by: Srinivas Mudireddy | last post by:
Hi, We have bunch of message levels and depending on whether that level is turned on, messages of that level are printed. For example, we have levels like MSG_LOW, MSG_MED etc. We want to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.