niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
Macro expansion in armcc
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
Keith Thompson
*nix forums Guru


Joined: 28 Feb 2005
Posts: 5173

PostPosted: Tue Jul 11, 2006 6:39 pm    Post subject: Re: Macro expansion in armcc Reply with quote

srinu.fsl@gmail.com writes:
Quote:
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) kst-u@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.
Back to top
Richard Bos
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1031

PostPosted: Tue Jul 11, 2006 11:40 am    Post subject: Re: Macro expansion in armcc Reply with quote

srinu.fsl@gmail.com wrote:

Quote:
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.

Quote:
#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.

Quote:
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.

Quote:
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
Back to top
srinu.fsl@gmail.com
*nix forums beginner


Joined: 11 Jul 2006
Posts: 1

PostPosted: Tue Jul 11, 2006 10:37 am    Post subject: Macro expansion in armcc Reply with quote

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?
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 2:41 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts expansion in master.cf mvmvm Postfix 0 Sat Mar 22, 2008 12:46 pm
No new posts C macro sgurminder@gmail.com C 11 Wed Jul 12, 2006 6:09 pm
No new posts Variable Number of Arguments in Macro Praveen.Kumar.SP@gmail.co C++ 10 Thu Jun 29, 2006 2:05 pm
No new posts String macro as wchar_t pointer? Heiner C 2 Fri Jun 23, 2006 9:11 pm
No new posts Keyboard macro in Linux Daniel Webb Debian 0 Tue Jun 13, 2006 3:40 am

Loan | Advertising | Remortgages | Remortgages | Mortgages
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.6243s ][ Queries: 20 (0.5366s) ][ GZIP on - Debug on ]