Skip to content

Commit ab05661

Browse files
authored
Order PDB document table to match compiler arguments (#50615)
1 parent 749283b commit ab05661

11 files changed

+426
-324
lines changed

src/Compilers/CSharp/Test/Emit/PDB/CSharpDeterministicBuildCompilationTests.cs

+95-12
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ private static void VerifyCompilationOptions(
3131
Compilation compilation,
3232
EmitOptions emitOptions,
3333
BlobReader compilationOptionsBlobReader,
34-
string langVersion)
34+
string langVersion,
35+
int sourceFileCount)
3536
{
3637
var pdbOptions = DeterministicBuildCompilationTestHelpers.ParseCompilationOptions(compilationOptionsBlobReader);
3738

@@ -43,12 +44,19 @@ private static void VerifyCompilationOptions(
4344
pdbOptions.VerifyPdbOption("unsafe", originalOptions.AllowUnsafe);
4445

4546
Assert.Equal(langVersion, pdbOptions["language-version"]);
47+
Assert.Equal(sourceFileCount.ToString(), pdbOptions["source-file-count"]);
4648

4749
var firstSyntaxTree = (CSharpSyntaxTree)compilation.SyntaxTrees.FirstOrDefault();
4850
pdbOptions.VerifyPdbOption("define", firstSyntaxTree.Options.PreprocessorSymbolNames, isDefault: v => v.IsEmpty(), toString: v => string.Join(",", v));
4951
}
5052

51-
private static void TestDeterministicCompilationCSharp(string langVersion, SyntaxTree[] syntaxTrees, CSharpCompilationOptions compilationOptions, EmitOptions emitOptions, params TestMetadataReferenceInfo[] metadataReferences)
53+
private static void TestDeterministicCompilationCSharp(
54+
string langVersion,
55+
SyntaxTree[] syntaxTrees,
56+
CSharpCompilationOptions compilationOptions,
57+
EmitOptions emitOptions,
58+
TestMetadataReferenceInfo[] metadataReferences,
59+
int? debugDocumentsCount = null)
5260
{
5361
var originalCompilation = CreateCompilation(
5462
syntaxTrees,
@@ -74,11 +82,12 @@ private static void TestDeterministicCompilationCSharp(string langVersion, Synta
7482
using (var embeddedPdb = peReader.ReadEmbeddedPortablePdbDebugDirectoryData(embedded))
7583
{
7684
var pdbReader = embeddedPdb.GetMetadataReader();
77-
7885
var metadataReferenceReader = DeterministicBuildCompilationTestHelpers.GetSingleBlob(PortableCustomDebugInfoKinds.CompilationMetadataReferences, pdbReader);
7986
var compilationOptionsReader = DeterministicBuildCompilationTestHelpers.GetSingleBlob(PortableCustomDebugInfoKinds.CompilationOptions, pdbReader);
8087

81-
VerifyCompilationOptions(compilationOptions, originalCompilation, emitOptions, compilationOptionsReader, langVersion);
88+
Assert.Equal(debugDocumentsCount ?? syntaxTrees.Length, pdbReader.Documents.Count);
89+
90+
VerifyCompilationOptions(compilationOptions, originalCompilation, emitOptions, compilationOptionsReader, langVersion, syntaxTrees.Length);
8291
DeterministicBuildCompilationTestHelpers.VerifyReferenceInfo(metadataReferences, metadataReferenceReader);
8392
}
8493
}
@@ -98,17 +107,77 @@ public static void Main()
98107
Console.WriteLine();
99108
}
100109
}
101-
", options: parseOptions, encoding: Encoding.UTF8);
110+
", filename: "a.cs", options: parseOptions, encoding: Encoding.UTF8);
111+
112+
var sourceTwo = Parse(@"
113+
class TypeTwo
114+
{
115+
}", filename: "b.cs", options: parseOptions, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
116+
117+
var sourceThree = Parse(@"
118+
class TypeThree
119+
{
120+
}", filename: "c.cs", options: parseOptions, encoding: Encoding.Unicode);
121+
122+
var referenceOneCompilation = CreateCompilation(
123+
@"public struct StructWithReference
124+
{
125+
string PrivateData;
126+
}
127+
public struct StructWithValue
128+
{
129+
int PrivateData;
130+
}", options: TestOptions.DebugDll);
131+
132+
var referenceTwoCompilation = CreateCompilation(
133+
@"public class ReferenceTwo
134+
{
135+
}", options: TestOptions.DebugDll);
136+
137+
using var referenceOne = TestMetadataReferenceInfo.Create(
138+
referenceOneCompilation,
139+
fullPath: "abcd.dll",
140+
emitOptions: emitOptions);
141+
142+
using var referenceTwo = TestMetadataReferenceInfo.Create(
143+
referenceTwoCompilation,
144+
fullPath: "efgh.dll",
145+
emitOptions: emitOptions);
146+
147+
var testSource = new[] { sourceOne, sourceTwo, sourceThree };
148+
TestDeterministicCompilationCSharp(
149+
parseOptions.LanguageVersion.MapSpecifiedToEffectiveVersion().ToDisplayString(),
150+
testSource,
151+
compilationOptions,
152+
emitOptions,
153+
new[] { referenceOne, referenceTwo });
154+
}
155+
156+
[Theory]
157+
[ClassData(typeof(CSharpDeterministicBuildCompilationTests))]
158+
public void PortablePdb_DeterministicCompilation_DuplicateFilePaths(CSharpCompilationOptions compilationOptions, EmitOptions emitOptions, CSharpParseOptions parseOptions)
159+
{
160+
var sourceOne = Parse(@"
161+
using System;
162+
163+
class MainType
164+
{
165+
public static void Main()
166+
{
167+
Console.WriteLine();
168+
}
169+
}
170+
", filename: "a.cs", options: parseOptions, encoding: Encoding.UTF8);
102171

103172
var sourceTwo = Parse(@"
104173
class TypeTwo
105174
{
106-
}", options: parseOptions, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
175+
}", filename: "b.cs", options: parseOptions, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
107176

108177
var sourceThree = Parse(@"
109178
class TypeThree
110179
{
111-
}", options: parseOptions, encoding: Encoding.Unicode);
180+
}", filename: "a.cs", options: parseOptions, encoding: Encoding.Unicode);
112181

113182
var referenceOneCompilation = CreateCompilation(
114183
@"public struct StructWithReference
@@ -136,7 +205,16 @@ public struct StructWithValue
136205
emitOptions: emitOptions);
137206

138207
var testSource = new[] { sourceOne, sourceTwo, sourceThree };
139-
TestDeterministicCompilationCSharp(parseOptions.LanguageVersion.MapSpecifiedToEffectiveVersion().ToDisplayString(), testSource, compilationOptions, emitOptions, referenceOne, referenceTwo);
208+
209+
// Note that only one debug document can be present for each distinct source path.
210+
// So if more than one syntax tree has the same file path, it won't be possible to do a rebuild from the DLL+PDB.
211+
TestDeterministicCompilationCSharp(
212+
parseOptions.LanguageVersion.MapSpecifiedToEffectiveVersion().ToDisplayString(),
213+
testSource,
214+
compilationOptions,
215+
emitOptions,
216+
new[] { referenceOne, referenceTwo },
217+
debugDocumentsCount: 2);
140218
}
141219

142220
[ConditionalTheory(typeof(DesktopOnly))]
@@ -153,17 +231,17 @@ public static void Main()
153231
Console.WriteLine();
154232
}
155233
}
156-
", options: parseOptions, encoding: Encoding.UTF8);
234+
", filename: "a.cs", options: parseOptions, encoding: Encoding.UTF8);
157235

158236
var sourceTwo = Parse(@"
159237
class TypeTwo
160238
{
161-
}", options: parseOptions, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
239+
}", filename: "b.cs", options: parseOptions, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
162240

163241
var sourceThree = Parse(@"
164242
class TypeThree
165243
{
166-
}", options: parseOptions, encoding: Encoding.GetEncoding(932)); // SJIS encoding
244+
}", filename: "c.cs", options: parseOptions, encoding: Encoding.GetEncoding(932)); // SJIS encoding
167245

168246
var referenceOneCompilation = CreateCompilation(
169247
@"public struct StructWithReference
@@ -191,7 +269,12 @@ public struct StructWithValue
191269
emitOptions: emitOptions);
192270

193271
var testSource = new[] { sourceOne, sourceTwo, sourceThree };
194-
TestDeterministicCompilationCSharp(parseOptions.LanguageVersion.MapSpecifiedToEffectiveVersion().ToDisplayString(), testSource, compilationOptions, emitOptions, referenceOne, referenceTwo);
272+
TestDeterministicCompilationCSharp(
273+
parseOptions.LanguageVersion.MapSpecifiedToEffectiveVersion().ToDisplayString(),
274+
testSource,
275+
compilationOptions,
276+
emitOptions,
277+
new[] { referenceOne, referenceTwo });
195278
}
196279

197280
public IEnumerator<object[]> GetEnumerator() => GetTestParameters().GetEnumerator();

src/Compilers/CSharp/Test/Emit/PDB/CheckSumTest.cs

+16-23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Immutable;
88
using System.Text;
99
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
10+
using Microsoft.CodeAnalysis.Emit;
1011
using Microsoft.CodeAnalysis.Test.Utilities;
1112
using Microsoft.CodeAnalysis.Text;
1213
using Roslyn.Test.Utilities;
@@ -193,6 +194,7 @@ class C1
193194
}
194195

195196
[Fact]
197+
[WorkItem(50611, "https://github.com/dotnet/roslyn/issues/50611")]
196198
public void TestPartialClassFieldInitializers()
197199
{
198200
var text1 = WithWindowsLineBreaks(@"
@@ -232,27 +234,22 @@ static void Main()
232234
compilation.VerifyPdb("C.Main", @"
233235
<symbols>
234236
<files>
235-
<file id=""1"" name=""USED1.cs"" language=""C#"" checksumAlgorithm=""406ea660-64cf-4c82-b6f0-42d48172a799"" checksum=""AB-00-7F-1D-23-D9"" />
236-
<file id=""2"" name=""USED2.cs"" language=""C#"" checksumAlgorithm=""406ea660-64cf-4c82-b6f0-42d48172a799"" checksum=""AB-00-7F-1D-23-D9"" />
237-
<file id=""3"" name=""b.cs"" language=""C#"" checksumAlgorithm=""SHA1"" checksum=""C0-51-F0-6F-D3-ED-44-A2-11-4D-03-70-89-20-A6-05-11-62-14-BE"" />
238-
<file id=""4"" name=""a.cs"" language=""C#"" checksumAlgorithm=""SHA1"" checksum=""F0-C4-23-63-A5-89-B9-29-AF-94-07-85-2F-3A-40-D3-70-14-8F-9B"" />
237+
<file id=""1"" name=""a.cs"" language=""C#"" checksumAlgorithm=""SHA1"" checksum=""F0-C4-23-63-A5-89-B9-29-AF-94-07-85-2F-3A-40-D3-70-14-8F-9B"" />
238+
<file id=""2"" name=""b.cs"" language=""C#"" checksumAlgorithm=""SHA1"" checksum=""C0-51-F0-6F-D3-ED-44-A2-11-4D-03-70-89-20-A6-05-11-62-14-BE"" />
239+
<file id=""3"" name=""USED1.cs"" language=""C#"" checksumAlgorithm=""406ea660-64cf-4c82-b6f0-42d48172a799"" checksum=""AB-00-7F-1D-23-D9"" />
240+
<file id=""4"" name=""USED2.cs"" language=""C#"" checksumAlgorithm=""406ea660-64cf-4c82-b6f0-42d48172a799"" checksum=""AB-00-7F-1D-23-D9"" />
239241
<file id=""5"" name=""UNUSED.cs"" language=""C#"" checksumAlgorithm=""406ea660-64cf-4c82-b6f0-42d48172a799"" checksum=""AB-00-7F-1D-23-D9"" />
240242
</files>
241243
<methods>
242244
<method containingType=""C"" name=""Main"">
243-
<customDebugInfo>
244-
<using>
245-
<namespace usingCount=""0"" />
246-
</using>
247-
</customDebugInfo>
248245
<sequencePoints>
249-
<entry offset=""0x0"" startLine=""112"" startColumn=""9"" endLine=""112"" endColumn=""23"" document=""1"" />
250-
<entry offset=""0x6"" startLine=""112"" startColumn=""9"" endLine=""112"" endColumn=""24"" document=""2"" />
251-
<entry offset=""0xc"" startLine=""19"" startColumn=""5"" endLine=""19"" endColumn=""6"" document=""3"" />
246+
<entry offset=""0x0"" startLine=""112"" startColumn=""9"" endLine=""112"" endColumn=""23"" document=""3"" />
247+
<entry offset=""0x6"" startLine=""112"" startColumn=""9"" endLine=""112"" endColumn=""24"" document=""4"" />
248+
<entry offset=""0xc"" startLine=""19"" startColumn=""5"" endLine=""19"" endColumn=""6"" document=""2"" />
252249
</sequencePoints>
253250
</method>
254251
</methods>
255-
</symbols>");
252+
</symbols>", format: DebugInformationFormat.PortablePdb);
256253
}
257254

258255
[WorkItem(729235, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/729235")]
@@ -292,6 +289,7 @@ void M()
292289
}
293290

294291
[ConditionalFact(typeof(WindowsOnly))]
292+
[WorkItem(50611, "https://github.com/dotnet/roslyn/issues/50611")]
295293
public void NoResolver()
296294
{
297295
var comp = CSharpCompilation.Create(
@@ -309,23 +307,18 @@ class C { void M() { } }
309307
comp.VerifyPdb(@"
310308
<symbols>
311309
<files>
312-
<file id=""1"" name=""a\..\a.cs"" language=""C#"" checksumAlgorithm=""406ea660-64cf-4c82-b6f0-42d48172a799"" checksum=""AB-00-7F-1D-23-D5"" />
313-
<file id=""2"" name=""C:\a\..\b.cs"" language=""C#"" checksumAlgorithm=""SHA1"" checksum=""36-39-3C-83-56-97-F2-F0-60-95-A4-A0-32-C6-32-C7-B2-4B-16-92"" />
310+
<file id=""1"" name=""C:\a\..\b.cs"" language=""C#"" checksumAlgorithm=""SHA1"" checksum=""36-39-3C-83-56-97-F2-F0-60-95-A4-A0-32-C6-32-C7-B2-4B-16-92"" />
311+
<file id=""2"" name=""a\..\a.cs"" language=""C#"" checksumAlgorithm=""406ea660-64cf-4c82-b6f0-42d48172a799"" checksum=""AB-00-7F-1D-23-D5"" />
314312
</files>
315313
<methods>
316314
<method containingType=""C"" name=""M"">
317-
<customDebugInfo>
318-
<using>
319-
<namespace usingCount=""0"" />
320-
</using>
321-
</customDebugInfo>
322315
<sequencePoints>
323-
<entry offset=""0x0"" startLine=""10"" startColumn=""20"" endLine=""10"" endColumn=""21"" document=""1"" />
324-
<entry offset=""0x1"" startLine=""10"" startColumn=""22"" endLine=""10"" endColumn=""23"" document=""1"" />
316+
<entry offset=""0x0"" startLine=""10"" startColumn=""20"" endLine=""10"" endColumn=""21"" document=""2"" />
317+
<entry offset=""0x1"" startLine=""10"" startColumn=""22"" endLine=""10"" endColumn=""23"" document=""2"" />
325318
</sequencePoints>
326319
</method>
327320
</methods>
328-
</symbols>");
321+
</symbols>", format: DebugInformationFormat.PortablePdb);
329322
}
330323

331324
[WorkItem(729235, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/729235")]

0 commit comments

Comments
 (0)