Perhaps you can re use the infrastructure used in Clang for this.