Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-25-2013, 08:20 PM
|
#1
|
|
Member
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 223
Rep:
|
Templating preprocessor for C
One of the (many) things I truly hate about the C preprocessor and C++ templates instantiation is although they create input to the compiler....
Their output (the compilers input) is neither visible (by default or to gdb) or readable (if you intercept it).
In principle, there is no reason why a C / C++ file cannot be run through some sort of templating pre-processor and output perfectly human readable C/C++ as an intermediate file.
It would be trivial to knock up ruby pre-processor that does something like this... but a couple of things are awkward to manage... (limiting scope, variable instantiation, token-pasting, stringification, handling expansion tokens in comments and strings...)
Not impossible, I could do something.... but hey, I'd hate to reinvent the wheel.
Does anyone know of an open source templating / macro engine that inputs beautiful human readable template definitions and outputs beautiful human readable C.
I don't think m4 qualifies as beautiful human readable input.
If all else fails cpp -P -C | indent might work.
All suggestions welcome - thanks.
|
|
|
|
02-26-2013, 02:25 AM
|
#2
|
|
Senior Member
Registered: May 2005
Posts: 4,386
|
Quote:
Originally Posted by cyent
One of the (many) things I truly hate about the C preprocessor and C++ templates instantiation is although they create input to the compiler....
Their output (the compilers input) is neither visible (by default or to gdb) or readable (if you intercept it).
In principle, there is no reason why a C / C++ file cannot be run through some sort of templating pre-processor and output perfectly human readable C/C++ as an intermediate file.
It would be trivial to knock up ruby pre-processor that does something like this... but a couple of things are awkward to manage... (limiting scope, variable instantiation, token-pasting, stringification, handling expansion tokens in comments and strings...)
Not impossible, I could do something.... but hey, I'd hate to reinvent the wheel.
Does anyone know of an open source templating / macro engine that inputs beautiful human readable template definitions and outputs beautiful human readable C.
I don't think m4 qualifies as beautiful human readable input.
If all else fails cpp -P -C | indent might work.
All suggestions welcome - thanks.
|
Have a look at https://www.linuxquestions.org/quest...e-like-674387/ and ask me further questions if interested  . If the source isn't there, I'll send it to you or place somewhere on the web.
|
|
|
1 members found this post helpful.
|
02-26-2013, 09:05 AM
|
#3
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,015
|
FYI, the latest gcc (4.8) can show macro context of errors:
Quote:
http://gcc.gnu.org/gcc-4.8/changes.html
The option -ftrack-macro-expansion=2 is now enabled by default. This allows the compiler to display the macro expansion stack in diagnostics. Combined with the caret information, an example diagnostic showing these two features is:
Code:
t.c:1:94: error: invalid operands to binary < (have ‘struct mystruct’ and ‘float’)
#define MYMAX(A,B) __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
^
t.c:7:7: note: in expansion of macro 'MYMAX'
X = MYMAX(P, F);
^
|
And with debug level 3 (-g3) includes macro information:
No help for C++ templates though.
|
|
|
|
02-26-2013, 10:23 AM
|
#4
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,672
|
Quote:
Originally Posted by cyent
One of the (many) things I truly hate about the C preprocessor and C++ templates instantiation is although they create input to the compiler....
Their output (the compilers input) is neither visible (by default or to gdb) or readable (if you intercept it).
|
The "preprocessor" output is very well defined and about as readable as such a thing could be and is in the source language C or C++.
I don't think template instantiation output is available in the source language or in any readable form.
Quote:
|
In principle, there is no reason why a C / C++ file cannot be run through some sort of templating pre-processor and output perfectly human readable C/C++ as an intermediate file.
|
I think such a "templating pre-processor" would need to include the entire front end of a C++ compiler, plus extra functions to get back to source code format.
The C preprocessor was intentionally designed with nasty (from a C programmer's viewpoint) limitations to make it easy to separate a C preprocessor from a C compiler.
Templating has no similar restrictions. The whole front end of a C++ compiler is needed to understand C++ code well enough to do template expansion.
Quote:
|
It would be trivial to knock up ruby pre-processor that does something like this
|
I doubt it.
Quote:
|
but a couple of things are awkward to manage.
|
Understatement.
Quote:
|
token-pasting, stringification, handling expansion tokens in comments and strings.
|
Those aren't trivial on their own. But in comparison to the hard problems of template expansion, the fact that you list those at all means you are missing the real work.
|
|
|
|
02-26-2013, 09:56 PM
|
#5
|
|
Member
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by johnsfine
I think such a "templating pre-processor" would need to include the entire front end of a C++ compiler, plus extra functions to get back to source code format.
>snip<
Templating has no similar restrictions. The whole front end of a C++ compiler is needed to understand C++ code well enough to do template expansion.
|
Hmm. Interesting.
Apart from the complexity of understanding the scope of a template parameters.... I thought it was a pretty much mechanical replace all template parameters with whatever it is instantiated with.
Thereafter much of the dark magic is about function overloading.
Which parts of template instantiation step (prior to handling overloading) do you feel requires a full compiler?
Quote:
|
Those aren't trivial on their own. But in comparison to the hard problems of template expansion, the fact that you list those at all means you are missing the real work.
|
Well those "easier" problems turned out to be pretty much non-problems. I now have 120 lines of ruby that does 90% of what I want.
The hard problem now seems to be generating separate header and .c files in a DRY fashion. Which sort of implies I need to create a facility to #include .template files and track the dependencies. Hmm.
Last edited by cyent; 02-26-2013 at 10:00 PM.
|
|
|
|
02-26-2013, 10:00 PM
|
#6
|
|
Member
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by Sergei Steshenko
|
Ok, interesting design... Pretty much like the erb (Embedded Ruby) / PHP thing - except with perl.
Hmm. Sort of aiming for something simpler.... closer to cpp -E -P | indent.
but I will keep that in mind.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:51 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|