LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 02-25-2013, 08:20 PM   #1
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 245

Rep: Reputation: 42
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.
 
Old 02-26-2013, 02:25 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by cyent View Post
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.
Old 02-26-2013, 09:05 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,398

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
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:
Quote:
http://gcc.gnu.org/onlinedocs/gcc/De...g-Options.html

Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.
No help for C++ templates though.
 
Old 02-26-2013, 10:23 AM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,055

Rep: Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104
Quote:
Originally Posted by cyent View Post
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.
 
Old 02-26-2013, 09:56 PM   #5
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 245

Original Poster
Rep: Reputation: 42
Quote:
Originally Posted by johnsfine View Post
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.
 
Old 02-26-2013, 10:00 PM   #6
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 245

Original Poster
Rep: Reputation: 42
Quote:
Originally Posted by Sergei Steshenko View Post
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.
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.
 
  


Reply

Tags
c_language, preprocessor, templates, templating


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Obfuscated C++ Templating jhwilliams Programming 2 08-27-2011 02:35 PM
PHP Templating delite Programming 0 10-19-2010 04:39 AM
C pre-preprocessor or preprocessor replacement Tischbein Programming 3 02-11-2007 11:38 AM
LXer: Smarty templating and presentation library for PHP LXer Syndicated Linux News 0 12-09-2005 02:36 PM
Creating a PHP templating system - any ideas? vharishankar Programming 1 07-06-2005 10:51 PM


All times are GMT -5. The time now is 05:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration