Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Not all languages support java-like annotations or C#-like attributes or code metadata in general, however that doesn't mean it is not possible to have in languages that don't have this.

An example is PHP with Stubbles and the Doctrine annotation library.

My question is, is there anything like this for C?, or are there any reliable ways of doing reflection with extended code metadata in C?

Ideally, I'm looking for something that reads javadoc-like comments.

Edit: The reason for me *needing* as opposed to just wanting, is that I need to generate C code and code-metadata from a database, as well as being able to edit that metadada and update the database. The volume of the work (~15,000 variables/structures/functions to generate from this database) justifies the solution.

share|improve this question
10  
Dear god, why? If you're using C, write C. Don't try to shoehorn idioms from one language into another. –  Telastyn Nov 15 '12 at 15:07
    
What do you want to do with javadoc like comments? –  MichaelT Nov 15 '12 at 15:10
    
I searched on Google for "c metaprogamming" and found this as the first result: maier-komor.de/metac –  FrustratedWithFormsDesigner Nov 15 '12 at 16:06
    
AGreed with Telastyn, I don't think C was ment to be used like that. It has other advantages, but this is not one of them. –  Shivan Dragon Nov 15 '12 at 21:40
1  
Perhaps you could give an example for the kind of metadata you have in mind and what you are trying to accomplish? –  Doc Brown Nov 15 '12 at 21:56
show 1 more comment

2 Answers

up vote 3 down vote accepted
+50

The nearest thing I can think of is MetaC:

http://www.maier-komor.de/metac/

(Already mentioned by FrustratedWithFormsDesigner a few days ago)

Put aside this, there are a number of other systems based on the C preprocessor that can be used (or abused) for this task. Have a look at Boost.Preprocessor, for example:

http://www.boost.org/doc/libs/1_52_0/libs/preprocessor/doc/index.html

Or P99 and ABCPP:

http://p99.gforge.inria.fr/

http://code.google.com/p/abcpp/

Despite this, I strongly suggest you to use of some kind of full-blown code-generation tool for this task (as Brian already did).

All of the above-mentioned preprocessor-based tools, at the very end, are just convoluted ways to perform some kind of code generation. You could just implement your own full-blown, well-behaved code-generation tool instead. Ruby, Python and Perl offer a lot of good tools for this. For example, Ruby has its own Modelling and Code Generation framework:

http://ruby-gen.org/

http://rgen.rubyforge.org/

Many other exists for other scripting languages. You could even use a template engine normally used for developing web applications, like Cheetah (Python-based):

http://www.cheetahtemplate.org/

Have a look at the general-purpose Ruby-based WLang framework, as well:

https://www.ruby-toolbox.com/projects/wlang

Using a general-purpose scripting language and a code-generation framework, you will end up developing a real, stand-alone application that will query your DB and generate metadata and code accordingly. It can look like a big and complicated solution but it is actually easier to write and much more maintaineable than a preprocessor-based one (because a full-blown scripting language is much more powerful and flexible than the C preprocessor).

Moreover, from your "feature-requests list" I'm getting the feeling that none of the existing preprocessor-based tools can be used for your task "as is" because the C preprocessor cannot easily deal with a DB and because your task will most likely require a two-pass process (one pass for quering the DB and generate the required metadata and a second one for generating the C code from the metadata).

This can be another reason for using a full-blown scripting language.

share|improve this answer
    
MetaC does look interesting indeed. How did I not know that existed? –  Tim Post Nov 22 '12 at 12:13
    
If you like MetaC, consider our DMS Software Reengineering Toolkit with its full C front end, used to build custom program analysis and transformation tools. Parses C code, builds symbol tables, extracts control and data flow, allow surface syntax pattern matching and rewriting. Comments are captured and can be processed specially by the rewriting rules, thus providing arbitrary "annotation" like effects. –  Ira Baxter Feb 22 '13 at 23:30
add comment

Have you considered generating code??

Attributes and their associated behavior are just code if you could expand them. It would take a ton of setup but you'd have full control. If you're generating this from a database, I think you have an extremely strong case--this is what ORMs do.

share|improve this answer
    
I have to generate both, the metadata validates ranges and values for the generated code when it goes into another environment. This is for embedded software, so an ORM would be way too much overhead. –  dukeofgaming Nov 15 '12 at 20:11
    
Seems like all you need to generate is the struct and setter operations. The setters will have hardcoded constraints in them (but can be regenerated). How is this not good enough? So you could have some markup that says <struct><Name="Person"><MaxNameLength="60"></struct> and all you'd need to do is write a program to consume this format to generate C data structures and functions that don't allow Name to be greater than 60 characters. –  brian Nov 15 '12 at 21:07
    
The metadata needs to stay as metadata for another tool to generate other stuff at the same time with the code. –  dukeofgaming Nov 15 '12 at 21:27
    
Yes it's a tough sale if all your tools can't consume the metadata in a uniform way. My solution would only work if you had full control. –  brian Nov 15 '12 at 21:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.