(ARTICLE) Advice on creating a code generator
(ARTICLE) Advice on creating a code generator
I have been spending some time recently creating a custom code
generator that outputs C++ and C# code from a custom XML format. This
blog is about some of what I have learned while working on this.
Advice #1: Use '#line'
There is a really cheap way to get a debugging experience for your
custom language: #line. With #line, you get to change the debug info that
ilasm/csc/vbc/cl writes into the PDB so that the debug info points back to
the original source file instead of the one that your code generator
creates. This is quite handy if you custom language has at least parts
where there is a strong connection between the generated code and the
custom language. Notes:
1. There is not a similar technique to allow you to re-associate variables,
so I would advise using the original names of variables if possible.
2. If a line of the original source expands to multiple lines of the
generated code, just repeat the #line before each line of the generated
code.
3. There is an issue with this technique if your source language is
XML-based in VS 2008 RTM. Visual Studio hopes to correct this problem in
SP1.
4. In C#, you can use this technique with #pragma checksum to set the
checksum of the source file. This is useful if you expect to have a bunch of
source files with the same name (ex: default.aspx) or if you are already
grabbing the checksum for some other reason. Otherwise, it is probably
overkill for a custom code generator. Note that the GUID with #pragma
checksum identifies the hash algorithm (ex: MD5 is {
406ea660-64cf-4c82-b6f0-42d48172a799}).
Example:
Generated file:
static void Main(string[] args)
{
#line 1 "HelloWorld.ExampleLanguage"
Console.WriteLine("Hello World");
#line default
}
HelloWorld.ExampleLanguage (custom language source file):
Hello World
courtesy: blogs.msdn.com
- guru's blog
- Login or register to post comments

