Skip to content

Commit

Permalink
Merge pull request #62 from tmat/Docs
Browse files Browse the repository at this point in the history
Handle empty documents
  • Loading branch information
tmat authored May 18, 2017
2 parents fe84806 + ccbe812 commit 104d0e1
Showing 1 changed file with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,17 @@ public void Convert(PEReader peReader, Stream sourcePdbStream, Stream targetPdbS

var symSequencePoints = symMethod.GetSequencePoints().ToImmutableArray();

BlobHandle sequencePointsBlob = SerializeSequencePoints(metadataBuilder, localSignatureRowId, symSequencePoints, documentIndex, out var singleDocumentHandle);
// add a dummy document:
if (documentIndex.Count == 0 && symSequencePoints.Length > 0)
{
documentIndex.Add(string.Empty, metadataBuilder.AddDocument(
name: metadataBuilder.GetOrAddDocumentName(string.Empty),
hashAlgorithm: default(GuidHandle),
hash: default(BlobHandle),
language: default(GuidHandle)));
}

BlobHandle sequencePointsBlob = SerializeSequencePoints(metadataBuilder, localSignatureRowId, symSequencePoints, documentIndex, methodToken, out var singleDocumentHandle);

metadataBuilder.AddMethodDebugInformation(
document: singleDocumentHandle,
Expand Down Expand Up @@ -1022,11 +1032,12 @@ private static LocalVariableHandle NextHandle(LocalVariableHandle handle) =>
private static LocalConstantHandle NextHandle(LocalConstantHandle handle) =>
MetadataTokens.LocalConstantHandle(MetadataTokens.GetRowNumber(handle) + 1);

private static BlobHandle SerializeSequencePoints(
private BlobHandle SerializeSequencePoints(
MetadataBuilder metadataBuilder,
int localSignatureRowId,
ImmutableArray<SymUnmanagedSequencePoint> sequencePoints,
Dictionary<string, DocumentHandle> documentIndex,
IReadOnlyDictionary<string, DocumentHandle> documentIndex,
int methodIndex,
out DocumentHandle singleDocumentHandle)
{
if (sequencePoints.Length == 0)
Expand All @@ -1043,15 +1054,15 @@ private static BlobHandle SerializeSequencePoints(
// header:
writer.WriteCompressedInteger(localSignatureRowId);

DocumentHandle previousDocument = TryGetSingleDocument(sequencePoints, documentIndex);
DocumentHandle previousDocument = TryGetSingleDocument(sequencePoints, documentIndex, methodIndex);
singleDocumentHandle = previousDocument;

int previousOffset = -1;
for (int i = 0; i < sequencePoints.Length; i++)
{
var sequencePoint = SanitizeSequencePoint(sequencePoints[i], previousOffset);

var currentDocument = documentIndex[sequencePoint.Document.GetName()];
var currentDocument = GetDocumentHandle(sequencePoint.Document, documentIndex, methodIndex);
if (previousDocument != currentDocument)
{
// optional document in header or document record:
Expand Down Expand Up @@ -1143,12 +1154,12 @@ private static SymUnmanagedSequencePoint SanitizeSequencePoint(SymUnmanagedSeque
return new SymUnmanagedSequencePoint(offset, sequencePoint.Document, startLine, startColumn, endLine, endColumn);
}

private static DocumentHandle TryGetSingleDocument(ImmutableArray<SymUnmanagedSequencePoint> sequencePoints, Dictionary<string, DocumentHandle> documentIndex)
private DocumentHandle TryGetSingleDocument(ImmutableArray<SymUnmanagedSequencePoint> sequencePoints, IReadOnlyDictionary<string, DocumentHandle> documentIndex, int methodToken)
{
DocumentHandle singleDocument = documentIndex[sequencePoints[0].Document.GetName()];
DocumentHandle singleDocument = GetDocumentHandle(sequencePoints[0].Document, documentIndex, methodToken);
for (int i = 1; i < sequencePoints.Length; i++)
{
if (documentIndex[sequencePoints[i].Document.GetName()] != singleDocument)
if (GetDocumentHandle(sequencePoints[i].Document, documentIndex, methodToken) != singleDocument)
{
return default(DocumentHandle);
}
Expand All @@ -1157,6 +1168,28 @@ private static DocumentHandle TryGetSingleDocument(ImmutableArray<SymUnmanagedSe
return singleDocument;
}

private DocumentHandle GetDocumentHandle(ISymUnmanagedDocument document, IReadOnlyDictionary<string, DocumentHandle> documentIndex, int methodToken)
{
string name;
try
{
name = document.GetName();
}
catch (Exception)
{
ReportDiagnostic(PdbDiagnosticId.InvalidSequencePointDocument, methodToken);
return default(DocumentHandle);
}

if (documentIndex.TryGetValue(name, out var handle))
{
return handle;
}

ReportDiagnostic(PdbDiagnosticId.InvalidSequencePointDocument, methodToken);
return default(DocumentHandle);
}

private static void SerializeDeltaLinesAndColumns(BlobBuilder writer, SymUnmanagedSequencePoint sequencePoint)
{
int deltaLines = sequencePoint.EndLine - sequencePoint.StartLine;
Expand Down

0 comments on commit 104d0e1

Please sign in to comment.