Reputation: 41
I am trying to port a driver to a new application. I am looking for an efficient way to link definitions.
Example:
someCommonHeader.h
#define abc_1234 0x01
#define abc_5678 0x02
#define abc_9012 0x03
oldApplication.c
U8 x1,x2,x3;
x1 = abc_1234 ;
x2 = abc_5678 ;
x3 = abc_9012 ;
In the new application the "abc_" is replaced with "xyz_"
newApplication.c
x1 = xyx_1234;
x2 = xyz_5678;
x3 = xyz_9012;
Of course I can do something like:
compatibilityHeader.h
#define xyx_1234 abc_1234
#define xyz_5678 abc_5678
#define xyz_9012 abc_9012
However, I am looking to see if there is any other way. More specifically, I want the compiler to replace "xyz_" to "abc_" references. Is this possible?
Details:
HW: Embedded System / Micro-controller Compiler: gcc Programming language: C
Any suggestions / references are appreciated.
Upvotes: 0
Views: 103
Reputation: 552
Assuming I am understanding properly what you are after, you can use the preprocessor to substitute part of a string. So you could do something like this, but it is pretty ugly:
#define OLD_APPLICATION // Define if you need the old application
#ifdef OLD_APPLICATION
#define ABC_OR_XYZ abc
#else
#define ABC_OR_XYZ xyz
#endif
Now, everywhere in your code replace all your labels by their equivalent, such as:
xyx_1234 -> ABC_OR_XYZ##_1234
xyz_5678 -> ABC_OR_XYZ##_5678
xyz_9012 -> ABC_OR_XYZ##_9012
abc_1234 -> ABC_OR_XYZ##_1234
abc_5678 -> ABC_OR_XYZ##_5678
abc_9012 -> ABC_OR_XYZ##_9012
The preprocessor will then handle the string substitution for you.
Note that the ## is used to concatenate strings.
Another way could be for you to write your own pre-build parser.
Upvotes: 0
Reputation: 1
you need #undef
your old macro before you make new definition
#undef abc_xyz
#define abc_xyz xyz_xyz
(fill the dots)
Upvotes: 0
Reputation: 180181
More specifically, I want the compiler to replace "xyz_" to "abc_" references. Is this possible?
No.
We can simplify somewhat by observing that you would need this in the preprocessor, since it's macro names you want to manipulate, and those are resolved during preprocessing. With the scope thus reduced, it's within reason to simply read through section 6.10 of the standard to confirm that although there are mechanisms for combining multiple tokens into one, there are no mechanisms for splitting tokens apart or operating on token parts.
Depending on how many appearances of these tokens there are, and on what other identifiers are present it might be feasible to just perform a textual search & replace, with subsequent review. It might also be possible to extract the old macro definitions via a text-processing or other tool and programmatically generate the compatibility-macro definitions from them (credit: @Jean-FrançoisFabre).
Upvotes: 3