Proof of concept: automatically import C header files

Jacob Carlborg doob at me.com
Wed Jul 17 23:17:06 PDT 2013


On 2013-07-18 00:59, H. S. Teoh wrote:

> IOW either you don't do it at all, or you have to go all the way and
> implement a fully-functional C frontend?
>
> If so, libclang is starting to sound rather attractive...

That's what I'm telling

> Hmm. We *could* pre-preprocess the code to do this conversion first to
> pick out these #define's, then suppress the #define's we understand from
> the input to the C preprocessor. Something like this:
>
> 	bool isSimpleValue(string s) {
> 		// basically, return true if s is something compilable
> 		// when put on the right side of "enum x = ...".
> 	}
>
> 	auto pipe = spawnCPreprocessor();
> 	string[string] manifestConstants;
> 	foreach (line; inputFile.byLine()) {
> 		if (auto m=match(line, `^\s*#define\s+(\w+)\s+(.*?)\s+`))
> 		{
> 			if (isSimpleValue(m.captures[2])) {
> 				manifestConstants[m.captures[1]] =
> 					m.captures[2];
>
> 				// Suppress enums that we picked out
> 				continue;
> 			}
> 			// whatever we don't understand, hand over to
> 			// the C preprocessor
> 		}
> 		pipe.writeln(line);
> 	}
>
> Basically, whatever #define's we can understand, we handle, and anything
> else we let the C preprocessor deal with. By suppressing the #define's
> we've picked out, we force the C preprocessor to leave any reference to
> them as unexpanded identifiers, so that later on we can just generate
> the enums and the resulting code will match up correctly.

You will just end up needing to build a full C preprocessor. Just use an 
existing one, that is libclang.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list