CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Preprocessor __LINE__ broken in V5

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

Preprocessor __LINE__ broken in V5
PostPosted: Tue Aug 13, 2019 10:35 pm     Reply with quote

Hello everybody,

I am trying to make this macro work on version 5. Version 4.100+ preprocesses it correctly, but the later compilers do not.

There is nothing about it in the recent software changes so I wonder if anybody else is using it.
Code:

#define MT_LABEL_HELP(line) _l##line##_
#define MT_LABEL MT_LABEL_HELP(__LINE__)

void main() {
 void *plabel = label_address(MT_LABEL);
 MT_LABEL: ;
}

Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19217

View user's profile Send private message

PostPosted: Wed Aug 14, 2019 1:38 am     Reply with quote

Er. __LINE__ is not broken, but what you are trying to do is.

__LINE__ returns the line number. How can this have a 'label_address'?.
Label_address requires a C label. Something inside the code. The line number
is not a C label.
If it worked in the past, it was then 'wrong'.
A line number is not an 'address' in the code, it is a simple number.
If you want the line number in the code then simply:

int32 line_number=(__LINE__);

will give this.

If you want to know where a particular line is in processor space, then
simply put a label there.

marker:

Will be a label at the address in the code where it is placed. This can
then be used as an address as you show.
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Thu Aug 15, 2019 9:46 pm     Reply with quote

Actually the idea is to construct a label like: "_l34_:"
Code:

#define MT_LABEL_HELP(line) _l##line##_
#define MT_LABEL MT_LABEL_HELP(__LINE__)

It uses the concatenation operator to create the label at compile time.

V5 is not working it out.
Code:

31 void main() {
32 do {
33     void *plabel = label_address(MT_LABEL);
34     MT_LABEL: ;
35     goto_address(plabel);
36  } while(0);
37 }
Ttelmah



Joined: 11 Mar 2010
Posts: 19217

View user's profile Send private message

PostPosted: Thu Aug 15, 2019 11:18 pm     Reply with quote

Problem is that when you access MT_LABEL in the pointer declaration
line, it tries to construct a line number 'inside' the line where you declare
the pointer, not access the label constructed further down the code.

Do it like this:
Code:

#define MT_LABEL_HELP(line) _l##line##_ //concatenate
#define MT_LABELCAT(x) MT_LABEL_HELP(x) //expand
#define MT_LABEL MT_LABELCAT(__LINE__) //call

void main() {

MT_LABEL: void *plabel = label_address(MT_LABEL);

}


Then you declare plabel to equal MT_LABEL in the same line that MT_LABEL
is created. So the declaration then works. Done a line apart, the expansion
gives a different line number to the two locations, so it can't work....

V5 is a lot more careful than V4 on things like this. However it would have
expanded 'wrong' on any compiler I can think of....

Just went back and tried the original syntax on two V4 compilers (4.141, and
4.104), and it failed on both. However it does incorrectly expand the
actual __LINE__ into the line number. Incorrectly, since in the C
preprocessor this should only happen when it is passed into another
define (as I show).
It is awful, but this is the way that the C preprocessor works.....
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Fri Aug 16, 2019 6:41 pm     Reply with quote

Ttelmah,

Thank you very much for the hint. The extra expand step fixed for V5 while V4 did without it.

I actually made a typo: An extra '\' is part of the original macro.
Code:

31 void main() {
32 do {
33     void *plabel = label_address(MT_LABEL); \
34     MT_LABEL: ;
35     goto_address(plabel);
36  } while(0);
37 }
Ttelmah



Joined: 11 Mar 2010
Posts: 19217

View user's profile Send private message

PostPosted: Sat Aug 17, 2019 12:42 am     Reply with quote

I remember hitting a similar problem when V5 first launched, but then did
some research and realised that V4 was letting an incorrect syntax work...
Have fun... Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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