Skip to content

Commit

Permalink
WIP - embed source files into PDB
Browse files Browse the repository at this point in the history
  • Loading branch information
noahfalk committed Sep 16, 2015
1 parent 78f1252 commit e4e20bf
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 70 deletions.
9 changes: 8 additions & 1 deletion src/Compilers/CSharp/Portable/CommandLine/CSharpCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ private SyntaxTree ParseFile(
}
else
{
return ParseFile(parseOptions, scriptParseOptions, content, file);
SyntaxTree tree = ParseFile(parseOptions, scriptParseOptions, content, file);
if(Arguments.EmbedSourceInPdb)
{
SyntaxNode root = tree.GetRoot();
root = root.WithAdditionalAnnotations(new SyntaxAnnotation("EmbedSourceInPdb"));
tree = tree.WithRootAndOptions(root, tree.Options);
}
return tree;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
bool concurrentBuild = true;
bool emitPdb = false;
string pdbPath = null;
bool embedSourceInPdb = false;
bool noStdLib = false;
string outputDirectory = baseDirectory;
string outputFileName = null;
Expand Down Expand Up @@ -971,6 +972,9 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar

additionalFiles.AddRange(ParseAdditionalFileArgument(value, baseDirectory, diagnostics));
continue;
case "embedSource":
embedSourceInPdb = true;
continue;
}
}

Expand Down Expand Up @@ -1102,6 +1106,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
OutputFileName = outputFileName,
PdbPath = pdbPath,
EmitPdb = emitPdb,
EmbedSourceInPdb = embedSourceInPdb,
OutputDirectory = outputDirectory,
DocumentationPath = documentationPath,
ErrorLogPath = errorLogPath,
Expand Down
13 changes: 12 additions & 1 deletion src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2612,9 +2612,20 @@ private static ImmutableArray<byte> MakeChecksumBytes(string bytesText)
return builder.ToImmutableAndFree();
}

private static bool IsSyntaxTreePdbEmbedded(SyntaxTree tree)
{
return tree.GetRoot().GetAnnotations().Any(a => a.Kind == "EmbedSourceInPdb");
}

private static Cci.DebugSourceDocument MakeDebugSourceDocumentForTree(string normalizedPath, SyntaxTree tree)
{
return new Cci.DebugSourceDocument(normalizedPath, Cci.DebugSourceDocument.CorSymLanguageTypeCSharp, () => tree.GetChecksumAndAlgorithm());
byte[] pdbEmbeddedSourceBytes = null;
if(IsSyntaxTreePdbEmbedded(tree))
{
SourceText text = tree.GetText();
pdbEmbeddedSourceBytes = text.Encoding.GetBytes(text.ToString());
}
return new Cci.DebugSourceDocument(normalizedPath, Cci.DebugSourceDocument.CorSymLanguageTypeCSharp, () => tree.GetChecksumAndAlgorithm(), pdbEmbeddedSourceBytes);
}

private void SetupWin32Resources(PEModuleBuilder moduleBeingBuilt, Stream win32Resources, DiagnosticBag diagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public abstract class CommandLineArguments
/// </summary>
public bool EmitPdb { get; internal set; }

/// <summary>
/// True to embed source files in the PDB.
/// </summary>
public bool EmbedSourceInPdb { get; internal set; }

/// <summary>
/// Absolute path of the output directory.
/// </summary>
Expand Down
13 changes: 12 additions & 1 deletion src/Compilers/Core/Portable/NativePdbWriter/PdbWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ internal enum PdbWriterOperation : byte
DefineKickoffMethod,
OpenMapTokensToSourceSpans,
MapTokenToSourceSpan,
CloseMapTokensToSourceSpans
CloseMapTokensToSourceSpans,
SetSource
}

public bool LogOperation(PdbWriterOperation op)
Expand Down Expand Up @@ -949,6 +950,16 @@ private ISymUnmanagedDocumentWriter GetDocumentWriter(DebugSourceDocument docume
_callLogger.LogArgument(vendor.ToByteArray());
_callLogger.LogArgument(type.ToByteArray());
}
if(document.EmbeddedSource != null)
{
writer.SetSource((uint)document.EmbeddedSource.Length, document.EmbeddedSource);
if (_callLogger.LogOperation(OP.SetSource))
{
//logging length is irrelevant, it has no additional entropy
//for performance it might be better to log ChecksumAndAlgorithm instead?
_callLogger.LogArgument(document.EmbeddedSource);
}
}
}
catch (Exception ex)
{
Expand Down
14 changes: 13 additions & 1 deletion src/Compilers/Core/Portable/PEWriter/DebugSourceDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal sealed class DebugSourceDocument
private readonly string _location;
private readonly Guid _language;
private readonly bool _isComputedChecksum;
private readonly byte[] _embeddedSource;

private readonly Task<ValueTuple<ImmutableArray<byte>, Guid>> _checksumAndAlgorithm;

Expand All @@ -33,11 +34,12 @@ public DebugSourceDocument(string location, Guid language)
/// <summary>
/// Use to create a document when checksum is computed based on actual source stream.
/// </summary>
public DebugSourceDocument(string location, Guid language, Func<ValueTuple<ImmutableArray<byte>, Guid>> checksumAndAlgorithm)
public DebugSourceDocument(string location, Guid language, Func<ValueTuple<ImmutableArray<byte>, Guid>> checksumAndAlgorithm, byte[] embeddedSource = null)
: this(location, language)
{
_checksumAndAlgorithm = Task.Run(checksumAndAlgorithm);
_isComputedChecksum = true;
_embeddedSource = embeddedSource;
}

/// <summary>
Expand Down Expand Up @@ -108,6 +110,14 @@ public ValueTuple<ImmutableArray<byte>, Guid> ChecksumAndAlgorithm
}
}

public byte[] EmbeddedSource
{
get
{
return _embeddedSource;
}
}

/// <summary>
/// returns true when checksum was computed base on an actual source stream
/// as opposed to be suggested via a checksum directive/pragma
Expand All @@ -119,5 +129,7 @@ internal bool IsComputedChecksum
return _isComputedChecksum;
}
}


}
}
1 change: 1 addition & 0 deletions src/Compilers/Core/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*REMOVED*static Microsoft.CodeAnalysis.SyntaxNodeExtensions.NormalizeWhitespace<TNode>(this TNode node, string indentation = " ", bool elasticTrivia = false) -> TNode
Microsoft.CodeAnalysis.CommandLineArguments.EmbedSourceInPdb.get -> bool
Microsoft.CodeAnalysis.CommandLineArguments.ScriptArguments.get -> System.Collections.Immutable.ImmutableArray<string>
Microsoft.CodeAnalysis.CommandLineParser.IsInteractive.get -> bool
Microsoft.CodeAnalysis.Compilation.GetSubmissionResultType(out bool hasValue) -> Microsoft.CodeAnalysis.ITypeSymbol
Expand Down
Loading

0 comments on commit e4e20bf

Please sign in to comment.