Skip to content

Commit 45d6e88

Browse files
committed
Addressed feedback
1 parent e666538 commit 45d6e88

File tree

9 files changed

+60
-20
lines changed

9 files changed

+60
-20
lines changed

src/DotNet/Pdb/Dss/SymbolDocumentImpl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,7 @@ public override PdbCustomDebugInfo[] CustomDebugInfos {
8989
}
9090
}
9191
PdbCustomDebugInfo[] customDebugInfos;
92+
93+
public override MDToken? MDToken => null;
9294
}
9395
}

src/DotNet/Pdb/Managed/DbiDocument.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public override PdbCustomDebugInfo[] CustomDebugInfos {
3838
}
3939
PdbCustomDebugInfo[] customDebugInfos;
4040

41+
public override MDToken? MDToken => null;
42+
4143
public DbiDocument(string url) {
4244
this.url = url;
4345
documentType = SymDocumentType.Text;

src/DotNet/Pdb/PdbCustomDebugInfo.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Threading;
56
using dnlib.DotNet.Emit;
67

78
namespace dnlib.DotNet.Pdb {
@@ -1115,7 +1116,7 @@ public sealed class PdbCompilationOptionsCustomDebugInfo : PdbCustomDebugInfo {
11151116
/// <summary>
11161117
/// Links a TypeDef with no method IL with a PDB document.
11171118
/// </summary>
1118-
public sealed class PdbTypeDefinitionDocumentsDebugInfo : PdbCustomDebugInfo {
1119+
public class PdbTypeDefinitionDocumentsDebugInfo : PdbCustomDebugInfo {
11191120
/// <summary>
11201121
/// Returns <see cref="PdbCustomDebugInfoKind.TypeDefinitionDocuments"/>
11211122
/// </summary>
@@ -1127,16 +1128,39 @@ public sealed class PdbTypeDefinitionDocumentsDebugInfo : PdbCustomDebugInfo {
11271128
public override Guid Guid => CustomDebugInfoGuids.TypeDefinitionDocuments;
11281129

11291130
/// <summary>
1130-
/// Document tokens. Only resolvable in debug metadata.
1131-
/// A token like this can be resolved by subtracting 1 from the RID and using it as an index into the
1132-
/// PdbState.Documents enumerable.
1131+
/// List of documents associated with the type
11331132
/// </summary>
1134-
public List<MDToken> DocumentTokens { get; }
1133+
public IList<PdbDocument> Documents {
1134+
get {
1135+
if (documents is null)
1136+
InitializeDocuments();
1137+
return documents;
1138+
}
1139+
}
1140+
/// <summary/>
1141+
protected IList<PdbDocument> documents;
1142+
/// <summary>Initializes <see cref="documents"/></summary>
1143+
protected virtual void InitializeDocuments() =>
1144+
Interlocked.CompareExchange(ref documents, new List<PdbDocument>(), null);
1145+
}
11351146

1136-
/// <summary>
1137-
/// Constructor
1138-
/// </summary>
1139-
public PdbTypeDefinitionDocumentsDebugInfo() => DocumentTokens = new List<MDToken>();
1147+
class PdbTypeDefinitionDocumentsDebugInfoMD : PdbTypeDefinitionDocumentsDebugInfo {
1148+
readonly ModuleDef readerModule;
1149+
readonly IList<MDToken> documentTokens;
1150+
1151+
protected override void InitializeDocuments() {
1152+
var list = new List<PdbDocument>(documentTokens.Count);
1153+
for (var i = 0; i < documentTokens.Count; i++) {
1154+
if (readerModule.PdbState.tokenToDocument.TryGetValue(documentTokens[i], out var document))
1155+
list.Add(document);
1156+
}
1157+
Interlocked.CompareExchange(ref documents, list, null);
1158+
}
1159+
1160+
public PdbTypeDefinitionDocumentsDebugInfoMD(ModuleDef readerModule, IList<MDToken> documentTokens) {
1161+
this.readerModule = readerModule;
1162+
this.documentTokens = documentTokens;
1163+
}
11401164
}
11411165

11421166
/// <summary>

src/DotNet/Pdb/PdbState.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace dnlib.DotNet.Pdb {
1515
public sealed class PdbState {
1616
readonly SymbolReader reader;
1717
readonly Dictionary<PdbDocument, PdbDocument> docDict = new Dictionary<PdbDocument, PdbDocument>();
18+
internal readonly Dictionary<MDToken, PdbDocument> tokenToDocument = new Dictionary<MDToken, PdbDocument>();
1819
MethodDef userEntryPoint;
1920
readonly Compiler compiler;
2021
readonly PdbFileKind originalPdbFileKind;
@@ -64,7 +65,7 @@ public bool HasDocuments {
6465
#if THREAD_SAFE
6566
} finally { theLock.ExitWriteLock(); }
6667
#endif
67-
68+
6869
}
6970
}
7071

@@ -133,6 +134,8 @@ PdbDocument Add_NoLock(SymbolDocument symDoc) {
133134
// Expensive part, can read source code etc
134135
doc.Initialize(symDoc);
135136
docDict.Add(doc, doc);
137+
if (symDoc.MDToken.HasValue)
138+
tokenToDocument.Add(symDoc.MDToken.Value, doc);
136139
return doc;
137140
}
138141

src/DotNet/Pdb/Portable/PortablePdbCustomDebugInfoReader.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,11 @@ PdbCustomDebugInfo ReadCompilationOptions() {
233233
}
234234

235235
PdbCustomDebugInfo ReadTypeDefinitionDocuments() {
236-
var cdi = new PdbTypeDefinitionDocumentsDebugInfo();
237-
236+
var docList = new List<MDToken>();
238237
while (reader.BytesLeft > 0)
239-
cdi.DocumentTokens.Add(new MDToken(Table.Document, reader.ReadCompressedUInt32()));
238+
docList.Add(new MDToken(Table.Document, reader.ReadCompressedUInt32()));
240239

241-
return cdi;
240+
return new PdbTypeDefinitionDocumentsDebugInfoMD(module, docList);
242241
}
243242

244243
PdbCustomDebugInfo ReadEncStateMachineStateMap() {

src/DotNet/Pdb/Portable/PortablePdbCustomDebugInfoWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ void WriteCompilationOptions(PdbCompilationOptionsCustomDebugInfo cdi) {
329329
}
330330

331331
void WriteTypeDefinitionDocuments(PdbTypeDefinitionDocumentsDebugInfo cdi) {
332-
foreach (var docToken in cdi.DocumentTokens)
333-
writer.WriteCompressedUInt32(docToken.Rid);
332+
foreach (var document in cdi.Documents)
333+
writer.WriteCompressedUInt32(systemMetadata.GetRid(document));
334334
}
335335

336336
void WriteEditAndContinueStateMachineStateMap(PdbEditAndContinueStateMachineStateMapDebugInfo cdi) {

src/DotNet/Pdb/Portable/PortablePdbReader.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ SymbolDocument[] ReadDocuments() {
6161
var custInfos = ListCache<PdbCustomDebugInfo>.AllocList();
6262
var gpContext = new GenericParamContext();
6363
for (int i = 0; i < docs.Length; i++) {
64-
bool b = pdbMetadata.TablesStream.TryReadDocumentRow((uint)i + 1, out var row);
64+
uint rid = (uint)i + 1;
65+
bool b = pdbMetadata.TablesStream.TryReadDocumentRow(rid, out var row);
6566
Debug.Assert(b);
6667
var url = nameReader.ReadDocumentName(row.Name);
6768
var language = pdbMetadata.GuidStream.Read(row.Language) ?? Guid.Empty;
@@ -70,12 +71,13 @@ SymbolDocument[] ReadDocuments() {
7071
var checkSumAlgorithmId = pdbMetadata.GuidStream.Read(row.HashAlgorithm) ?? Guid.Empty;
7172
var checkSum = pdbMetadata.BlobStream.ReadNoNull(row.Hash);
7273

73-
var token = new MDToken(Table.Document, i + 1).ToInt32();
74+
var mdToken = new MDToken(Table.Document, rid);
75+
var token = mdToken.ToInt32();
7476
custInfos.Clear();
7577
GetCustomDebugInfos(token, gpContext, custInfos);
7678
var custInfosArray = custInfos.Count == 0 ? Array2.Empty<PdbCustomDebugInfo>() : custInfos.ToArray();
7779

78-
docs[i] = new SymbolDocumentImpl(url, language, languageVendor, documentType, checkSumAlgorithmId, checkSum, custInfosArray);
80+
docs[i] = new SymbolDocumentImpl(url, language, languageVendor, documentType, checkSumAlgorithmId, checkSum, custInfosArray, mdToken);
7981
}
8082
ListCache<PdbCustomDebugInfo>.Free(ref custInfos);
8183
return docs;

src/DotNet/Pdb/Portable/SymbolDocumentImpl.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ sealed class SymbolDocumentImpl : SymbolDocument {
1515
/*readonly*/ Guid checkSumAlgorithmId;
1616
readonly byte[] checkSum;
1717
readonly PdbCustomDebugInfo[] customDebugInfos;
18+
MDToken mdToken;
1819

1920
string GetDebuggerString() {
2021
var sb = new StringBuilder();
@@ -45,15 +46,17 @@ string GetDebuggerString() {
4546
public override Guid CheckSumAlgorithmId => checkSumAlgorithmId;
4647
public override byte[] CheckSum => checkSum;
4748
public override PdbCustomDebugInfo[] CustomDebugInfos => customDebugInfos;
49+
public override MDToken? MDToken => mdToken;
4850

49-
public SymbolDocumentImpl(string url, Guid language, Guid languageVendor, Guid documentType, Guid checkSumAlgorithmId, byte[] checkSum, PdbCustomDebugInfo[] customDebugInfos) {
51+
public SymbolDocumentImpl(string url, Guid language, Guid languageVendor, Guid documentType, Guid checkSumAlgorithmId, byte[] checkSum, PdbCustomDebugInfo[] customDebugInfos, MDToken mdToken) {
5052
this.url = url;
5153
this.language = language;
5254
this.languageVendor = languageVendor;
5355
this.documentType = documentType;
5456
this.checkSumAlgorithmId = checkSumAlgorithmId;
5557
this.checkSum = checkSum;
5658
this.customDebugInfos = customDebugInfos;
59+
this.mdToken = mdToken;
5760
}
5861
}
5962
}

src/DotNet/Pdb/Symbols/SymbolDocument.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@ public abstract class SymbolDocument {
4141
/// Gets the custom debug infos
4242
/// </summary>
4343
public abstract PdbCustomDebugInfo[] CustomDebugInfos { get; }
44+
45+
/// <summary>
46+
/// Gets the Metadata token of the document if available.
47+
/// </summary>
48+
public abstract MDToken? MDToken { get; }
4449
}
4550
}

0 commit comments

Comments
 (0)