|
|
View previous topic :: View next topic |
Author |
Message |
AdamWebber
Joined: 18 Jun 2014 Posts: 13
|
#EXPORT giving problems on version 5.045 and later |
Posted: Tue Dec 01, 2015 4:30 pm |
|
|
Is anybody else having problems with #export after version 5.044? I created #defines so that when I export the hex file, it automatically uses the defined values. After 5.044, it no longer uses the defined values. It now uses the label.
Code: |
#define SW_VER A
#define HW_VER REVB
#define DATE _2015-12-1
#define EXPNAME(a,b,c,d) a ##b ##c ##d
#export (HEX, FILE = EXPNAME(SW_VER,HW_VER,DATE,.hex) )
|
In the 5.044 and earlier, the filename would be "AREVB_2015-12-1.hex".
Anything later will be "SW_VERHW_VERDATE.hex".
Is there a fix for this before I go to the developer? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Dec 02, 2015 4:54 am |
|
|
It's actually working correctly now!....
This is a 'classic' C problem. When you use ## it precludes macro expansion of the arguments each side. So you have to 'double indirect', to get the expansion to occur.
Code: |
#define SW_VER A
#define HW_VER REVB
#define DATE _2015-12-1
#define CAT(a,b,c,d) a ## b ## c ## d
#define EXPNAME(a,b,c,d) CAT(a,b,c,d)
#export (HEX, FILE = EXPNAME(SW_VER,HW_VER,DATE,.hex))
|
Most C textbooks will have this explained, but what happens is that '##' will catenate the tokens without expanding them. So you have to force the macro expansion to take place before the catenation, by 'indirecting' this, calling it from another macro!...
It's interesting that CCS was expanding the macros (it shouldn't have).
Have a look at this:
<http://stackoverflow.com/questions/1489932/how-to-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-arg>
Note the 'PASTOR', and 'EVALUATOR' indirection, and the careful explanation of this.
It was done to allow 'unexpanded' values to be included in the macros, otherwise you have the problem of things being expanded 'accidentally', when they happen to contain a text value that is used elsewhere in a macro.... |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Dec 02, 2015 5:03 am |
|
|
I've had to do a similar thing for similar reasons with stringification. In my case also related to filenames:
Code: |
// Utility stringification macro. We have to use this as stringising can only happen
// in a function-like macro, and it needs this unexpected double stage thing.
#define STRINGER1(a) #a
#define STRINGER(a) STRINGER1(a)
// If we've been given a part number we should name the hex file based on that number. The string version allows code to printf it more easily.
#define PART_NUMBER_STRING STRINGER(PART_NUMBER)
#define HEX_FILENAME PART_NUMBER.hex
#export (HEX, FILE=HEX_FILENAME)
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Dec 02, 2015 5:07 am |
|
|
Yes. It applies to both the #, and the ## operators.
From the C spec:
Quote: |
6.10.3.1 Argument substitution
After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument’s preprocessing tokens are completely macro replaced as if they formed the rest of the preprocessing file; no other preprocessing tokens are available.
|
Critical bit is the phrase:
"unless preceded by a # or ## preprocessing token". On # though it doesn't apply to the token in front, but with ## this is affected as well:
"or followed by a ## preprocessing token". |
|
|
AdamWebber
Joined: 18 Jun 2014 Posts: 13
|
|
Posted: Wed Dec 02, 2015 12:13 pm |
|
|
This code now compiles the hex file name properly. I just added a secondary #define EXPNAME that defines the first #define EXPNAME1. I changed nothing else.
Thank you for your help, Captain Awesome-O!
Code: | #define SW_VER A
#define HW_VER REVB
#define DATE _2015-12-1
#define EXPNAME1(a,b,c,d) a ##b ##c ##d
#define EXPNAME(a,b,c,d) EXPNAME1(a,b,c,d)
#export (HEX, FILE = EXPNAME(SW_VER,HW_VER,DATE,.hex) ) |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Dec 03, 2015 1:35 am |
|
|
Good.
It is one of those things that once you understand it 'makes sense'. Problem is that if # or ## defaulted to doing a macro expansion before they joined things, you could not include stuff that contained keywords that were already #defined (so in your case for example the text 'DATE' could not be used, since it would expand). So they default to not expanding, and if you want an expansion, you have to use the indirect (which then does the expansion before the values are passed to the joining function). It is interesting that some of the older CCS versions were expanding. Would give problems with quite a few bits of code....
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|