C#, Expressions and source locations

Topics: Metadata Model, PDB Writer, Source Model
May 17, 2010 at 2:12 AM
Edited May 18, 2010 at 11:00 AM

Not being C# a (purely) functional language, its designers thought they could not add the source location of expressions in the debug informations. The compiler associates a source location only to statements.

That's why debugging lambdas and linq expressions is a pain.

Is this a limitation of the debug file format or just a limitation of the C# compiler?

How does debugging work with F# or nemerle?

I thought I found a smart way to associate to a IObjectWithLocations the source location of the piece of (C#) source code that synthesized it so that a verifier could point me to the right place when it finds a problem. It looks like this:


block.Statements.Add(new ExpressionStatement { // XXX: The resulting location for the method call below is here
    Expression = new Assignment {
	Target = new TargetExpression {
	    Definition = validationResultLocalDefinition
	Source = new MethodCall {
	    Locations = { MetaLocationSource.Get("A5401890-F632-46CA-8A8C-B37522B319AE") }, // XXX: not here
	    MethodToCall = validateMethod,
	    //IsStaticCall = validateMethod.IsStatic,
	    Arguments = {
		new ThisReference(),
		new BoundExpression {
		    Definition = ValueParameterDefinition


But - as explained in the XXX comments - it doesn't work very well. :(


May 17, 2010 at 2:52 AM

The notion of a "statement" is deeply embedded into the VS debugger. The PDB format seems very flexible, but .NET compilers such as C# only put as much information into a PDB file as is required to enable the debugging scenarios.