473,722 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Token pasting and what does the result need to be?

I'm running two different compilers against some hairy macros and one,
gcc, doesn't like my token pasting result so much. It says, " "." and
"foo" does not give a valid preprocessing token ". Some further reading
suggests that since .foo is not a valid preproc. token that I can't
really do what I want, portably. The macro looks like this:

#define DMA_CHECK_SR_BI T(reg, REG, ch) do \
{ \
DmaSr sr; \
\
sr.all = 0; \
sr.bit.##reg##c h = 1; \
\
printf("SR[" #REG "] = 0x%08X", sr.all); \
if (sr.all == DMA0_SR_##REG## _CHAN(ch)) \
{ \
printf(", okay\n"); \
} \
else \
{ \
printf(" != 0x%08X (FAIL)\n", DMA0_SR_##REG## _CHAN(ch)); \
} \
} while (0)

and DMA_CHECK_SR_BI T(cs, CS, 0) ends up creating what looks like
exactly what I want (sorry for the long line):

do { DmaSr sr; sr.all = 0; sr.bit.cs0 = 1; printf("SR[" "CS" "] =
0x%08X", sr.all); if (sr.all == DMA0_SR_CS_CHAN (0)) { printf(",
okay\n"); } else { printf(" != 0x%08X (FAIL)\n", DMA0_SR_CS_CHAN (0)); }
} while (0);

So I fear that gcc and the other compiler do the right thing, for me,
but I should re-write the macro to be more portable.

Comments? Thanks,
--
- Mark

May 1 '06 #1
7 2642


Mark Odell wrote On 05/01/06 14:21,:
I'm running two different compilers against some hairy macros and one,
gcc, doesn't like my token pasting result so much. It says, " "." and
"foo" does not give a valid preprocessing token ". Some further reading
suggests that since .foo is not a valid preproc. token that I can't
really do what I want, portably. The macro looks like this:

#define DMA_CHECK_SR_BI T(reg, REG, ch) do \
{ \
DmaSr sr; \
\
sr.all = 0; \
sr.bit.##reg##c h = 1; \

^^
Lose these. You don't want a token made
up of . and reg and ch, you want the two tokens . and
regch.

--
Er*********@sun .com

May 1 '06 #2
In article <1146509939.717 664@news1nwk>,
Eric Sosman <Er*********@su n.com> wrote:
Mark Odell wrote On 05/01/06 14:21,:
I'm running two different compilers against some hairy macros and one,
gcc, doesn't like my token pasting result so much. It says, " "." and
"foo" does not give a valid preprocessing token " sr.bit.##reg##c h = 1; \

^^
Lose these. You don't want a token made
up of . and reg and ch, you want the two tokens . and
regch.


Also, since . is an operator, one can have whitespace around it.
If one were to add a space after the sr.bit. portion then it would
perhaps become clearer as to what kind of token-pasting one needed to do.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
May 1 '06 #3
Eric Sosman wrote:
Mark Odell wrote On 05/01/06 14:21,:
I'm running two different compilers against some hairy macros and one,
gcc, doesn't like my token pasting result so much. It says, " "." and
"foo" does not give a valid preprocessing token ". Some further reading
suggests that since .foo is not a valid preproc. token that I can't
really do what I want, portably. The macro looks like this:

#define DMA_CHECK_SR_BI T(reg, REG, ch) do \
{ \
DmaSr sr; \
\
sr.all = 0; \
sr.bit.##reg##c h = 1; \

^^
Lose these. You don't want a token made
up of . and reg and ch, you want the two tokens . and
regch.


Thanks for the reply. However, trying what you suggest seems to
engender the same warning, .e.g.

#define DMA_CHECK_SR_BI T(regCh, REG, ch) do \
{ \
DmaSr sr; \
\
sr.all = 0; \
sr.bit.##regCh = 1; \
[snip]

invoked with DMA_CHECK_SR_BI T(cs0, CS, 0); gives me:

pasting "." and "cs0" does not give a valid preprocessing token
do { DmaSr sr; sr.all = 0; sr.bit.cs0 = 1;
[snip]

I don't think I understood your suggestion correctly.

Regards,
--
- Mark

May 1 '06 #4


Mark Odell wrote On 05/01/06 15:20,:
Eric Sosman wrote:
Mark Odell wrote On 05/01/06 14:21,:
I'm running two different compilers against some hairy macros and one,
gcc, doesn't like my token pasting result so much. It says, " "." and
"foo" does not give a valid preprocessing token ". Some further reading
suggests that since .foo is not a valid preproc. token that I can't
really do what I want, portably. The macro looks like this:

#define DMA_CHECK_SR_BI T(reg, REG, ch) do \
{ \
DmaSr sr; \
\
sr.all = 0; \
sr.bit.##reg##c h = 1; \


^^
Lose these. You don't want a token made
up of . and reg and ch, you want the two tokens . and
regch.

Thanks for the reply. However, trying what you suggest seems to
engender the same warning, .e.g.

#define DMA_CHECK_SR_BI T(regCh, REG, ch) do \
{ \
DmaSr sr; \
\
sr.all = 0; \
sr.bit.##regCh = 1; \
[snip]

invoked with DMA_CHECK_SR_BI T(cs0, CS, 0); gives me:

pasting "." and "cs0" does not give a valid preprocessing token
do { DmaSr sr; sr.all = 0; sr.bit.cs0 = 1;
[snip]

I don't think I understood your suggestion correctly.


Perhaps the up-arrows don't line up in your newsreader
as they do in my newswriter. You should lose the first ##,
not the second, so the offending line looks like

sr.bit.reg##ch = 1;

Or, following Walter Roberson's sensible suggestion

sr.bit. reg##ch = 1;

--
Er*********@sun .com

May 1 '06 #5
Eric Sosman wrote:
Mark Odell wrote On 05/01/06 15:20,:
Eric Sosman wrote:
Mark Odell wrote On 05/01/06 14:21,:

I'm running two different compilers against some hairy macros and one,
gcc, doesn't like my token pasting result so much. It says, " "." and
"foo" does not give a valid preprocessing token ". Some further reading
suggests that since .foo is not a valid preproc. token that I can't
really do what I want, portably. The macro looks like this:

#define DMA_CHECK_SR_BI T(reg, REG, ch) do \
{ \
DmaSr sr; \
\
sr.all = 0; \
sr.bit.##reg##c h = 1; \

^^
Lose these. You don't want a token made
up of . and reg and ch, you want the two tokens . and
regch.

Thanks for the reply. However, trying what you suggest seems to
engender the same warning, .e.g.

#define DMA_CHECK_SR_BI T(regCh, REG, ch) do \
{ \
DmaSr sr; \
\
sr.all = 0; \
sr.bit.##regCh = 1; \
[snip]

invoked with DMA_CHECK_SR_BI T(cs0, CS, 0); gives me:

pasting "." and "cs0" does not give a valid preprocessing token
do { DmaSr sr; sr.all = 0; sr.bit.cs0 = 1;
[snip]

I don't think I understood your suggestion correctly.


Perhaps the up-arrows don't line up in your newsreader
as they do in my newswriter. You should lose the first ##,
not the second, so the offending line looks like

sr.bit.reg##ch = 1;

Or, following Walter Roberson's sensible suggestion

sr.bit. reg##ch = 1;


Oh. But then I can't write one macro to handle multiple registers via a
macro argument. I did not think you meant that, despite the ^^ markers
because it was not what I wished to have. I agree with you that your
proposed solution will work and be correct WRT ISO C, it's just not
what I was hoping for. Thanks to you both for your replies.
--
- Mark

May 1 '06 #6
In article <11************ *********@i39g2 000cwa.googlegr oups.com>,
Mark Odell <mr********@gma il.com> wrote:
>>Mark Odell wrote On 05/01/06 14:21,: >>>#define DMA_CHECK_SR_BI T(reg, REG, ch) do \
[...]
Oh. But then I can't write one macro to handle multiple registers via a
macro argument. I did not think you meant that, despite the ^^ markers
because it was not what I wished to have.


You are apparently passing in both reg and ch to the macro.
For example, if you passed in A and 17, you would want A17 to
be the result? Should the combined register name then be
eligable for further substitution -- for example,

#define A7 SP
DMA_CHECK_SR_BI T(A,something,7 )

would you then want the code to be generated in terms of A7 or would
you want the completed token A7 to then be looked up and SP substituted?
I suspect that what you want to do is something like this:

#define PASTE(a,b) a##b

then

sr.bit. PASTE(reg,ch)
There are some subtle semantics about exactly what is substituted
when you use ## and usually you can get around the semantics by
using an extra layer of #define to do the pasting.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
May 1 '06 #7
Walter Roberson wrote:
In article <11************ *********@i39g2 000cwa.googlegr oups.com>,
Mark Odell <mr********@gma il.com> wrote:
>>Mark Odell wrote On 05/01/06 14:21,:>>#define DMA_CHECK_SR_BI T(reg, REG, ch) do \

[...]
Oh. But then I can't write one macro to handle multiple registers via a
macro argument. I did not think you meant that, despite the ^^ markers
because it was not what I wished to have.


You are apparently passing in both reg and ch to the macro.
For example, if you passed in A and 17, you would want A17 to
be the result? Should the combined register name then be
eligable for further substitution -- for example,

#define A7 SP
DMA_CHECK_SR_BI T(A,something,7 )

would you then want the code to be generated in terms of A7 or would
you want the completed token A7 to then be looked up and SP substituted?
I suspect that what you want to do is something like this:

#define PASTE(a,b) a##b

then

sr.bit. PASTE(reg,ch)


Thanks again for the reply. Sadly, I completely missed the mark on
Eric's reply. Had I done what he had instructed on the correct source
file in the directory under test, instead of some other file. The
warning would have gone away and I would have had the desired result. I
get it now and it works. The answer was just as Eric said, remove the
first pair of #'s. E.g. sr.bit.reg##ch works w/o warning and
sr.bit.##reg##c h worked but is (now) weird looking and produces a
warning.

Best regards,
--
- Mark

May 1 '06 #8

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

Similar topics

1
2484
by: Cronus | last post by:
Hi the following code the g++ (g++ (GCC) 3.3.3 (Debian 20040422)) emits the error message that pasting of :: and hello is no valid preprocessor token. The g++ 2.95.3 accepts the code. I know that handling of the ## preprocessing operator has changed. How I have to change the macro definiton of A(P)? NO changes at macro calling should be necessary!
3
22954
by: Frodo Baggins | last post by:
Hi All, I have a piece of code (not written by me) that is failing on compile with the error: pasting "xdr_ndmp_connect_open_request" and "," does not give a valid preprocessing token The relevant line from the header is: #define XDR_AND_SIZE(func) (bool_t(*)(XDR*, ...))xdr_##func##,sizeof(func)
5
8651
by: Chris | last post by:
Hi all We have a strange problem with macros: #define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O=i##Tbl##_##Fld; SQLST_MAP_IND_I(RAGREEJ1,FORMFROMTMSTP); => gcc 3.3.4 gives the following error: source.c:173:40: pasting "[" and "RAGREEJ1" does not give a valid
3
3170
by: Wessi | last post by:
Hi, token pasting means, that normally whitespaces and comments are deleted before and after the ## operator. I want the preprocessor to hold an existing whitespace while replacing the argument of a macro. Example: The following define and macro #define ISR_CAT1(NAME) __interrupt void ##NAME(void)
4
3445
by: Nelson Hsu | last post by:
Is there anything like the token pasting operator (##) in C#? I'm trying to port some unmanaged C++ code into C# and I'm not sure how to mimic this behavior.
8
1624
by: Mark Odell | last post by:
I've read the FAQ and I still can't see how to get what I want. Here's what I'd like to do: I have a list of register numbers that the CPU's instruction set requires be literal constants, e.g. #define DMA_CR0 0x100 #define DMA_CR1 0x108 #define DMA_CR2 0x110 #define DMA_CR3 0x118
13
2934
by: Henry Townsend | last post by:
I hope this is on-topic in c.l.c - it's about the C preprocessor more than the language per se, more generally about the K&R behavior, and most specifically about the Sun cpp which is why I've cross posted there. The test case below is taken from an Imake setup (yes, old, I know). There's a Concat() macro which uses old-style /**/ for token pasting (we are not allowed to assume ANSI so ## isn't allowed). I'm trying to paste AND expand...
2
5733
by: mark.bergman | last post by:
I am porting some old code from Digital Unix to Linux using token pasting, which is failing to compile (code simplified): #define DEBUG(strg,v) printf("debugoutput: "##strg##".\n", v); main() { int x = 5; DEBUG("variable x is %d",x); }
1
4226
by: jeff | last post by:
Given: ===Program <=== $ cat macro_delay_expansion.c #define args(a,b) a, b #define myprintf(a, b, c) do { \ printf("%d %d %d\n", a, b, c );\ } while(0)
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.