Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jul 23, 2022
1 parent 2b1499b commit f173424
Show file tree
Hide file tree
Showing 71 changed files with 796 additions and 428 deletions.
1 change: 1 addition & 0 deletions eng/config/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAna
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.SyntaxAnnotation,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.Text.TextSpan,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,System.Collections.Generic.IEnumerable{Microsoft.CodeAnalysis.Text.TextSpan},Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.FileTextLoader.#ctor(System.String,System.Text.Encoding); Use overload with checksum algorithm
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Microsoft.CodeAnalysis.CSharp.SyntaxKind.RequiredKeyword = 8447 -> Microsoft.Cod
Microsoft.CodeAnalysis.CSharp.SyntaxKind.ScopedKeyword = 8448 -> Microsoft.CodeAnalysis.CSharp.SyntaxKind
Microsoft.CodeAnalysis.CSharp.SyntaxKind.FileKeyword = 8449 -> Microsoft.CodeAnalysis.CSharp.SyntaxKind
Microsoft.CodeAnalysis.CSharp.SyntaxKind.SingleLineRawStringLiteralToken = 8518 -> Microsoft.CodeAnalysis.CSharp.SyntaxKind
static Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.Create(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode! root, Microsoft.CodeAnalysis.CSharp.CSharpParseOptions? options, string? path, System.Text.Encoding? encoding) -> Microsoft.CodeAnalysis.SyntaxTree!
static Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.Create(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode! root, Microsoft.CodeAnalysis.Text.SourceHashAlgorithm checksumAlgorithm, Microsoft.CodeAnalysis.CSharp.CSharpParseOptions? options = null, string? path = "", System.Text.Encoding? encoding = null) -> Microsoft.CodeAnalysis.SyntaxTree!
static Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ConstructorDeclaration(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.AttributeListSyntax!> attributeLists, Microsoft.CodeAnalysis.SyntaxTokenList modifiers, Microsoft.CodeAnalysis.SyntaxToken identifier, Microsoft.CodeAnalysis.CSharp.Syntax.ParameterListSyntax! parameterList, Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorInitializerSyntax? initializer, Microsoft.CodeAnalysis.CSharp.Syntax.BlockSyntax! body) -> Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorDeclarationSyntax!
*REMOVED*static Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ConstructorDeclaration(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.AttributeListSyntax!> attributeLists, Microsoft.CodeAnalysis.SyntaxTokenList modifiers, Microsoft.CodeAnalysis.SyntaxToken identifier, Microsoft.CodeAnalysis.CSharp.Syntax.ParameterListSyntax! parameterList, Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorInitializerSyntax! initializer, Microsoft.CodeAnalysis.CSharp.Syntax.BlockSyntax! body) -> Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorDeclarationSyntax!
static Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ConstructorDeclaration(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.AttributeListSyntax!> attributeLists, Microsoft.CodeAnalysis.SyntaxTokenList modifiers, Microsoft.CodeAnalysis.SyntaxToken identifier, Microsoft.CodeAnalysis.CSharp.Syntax.ParameterListSyntax! parameterList, Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorInitializerSyntax? initializer, Microsoft.CodeAnalysis.CSharp.Syntax.BlockSyntax? body, Microsoft.CodeAnalysis.SyntaxToken semicolonToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorDeclarationSyntax!
Expand Down
27 changes: 13 additions & 14 deletions src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,9 @@ private void BuildPreprocessorStateChangeMap()
/// <summary>
/// Creates a new syntax tree from a syntax node.
/// </summary>
public static SyntaxTree Create(CSharpSyntaxNode root, CSharpParseOptions? options = null, string? path = "", Encoding? encoding = null)
{
#pragma warning disable CS0618 // We are calling into the obsolete member as that's the one that still does the real work
return Create(root, options, path, encoding, diagnosticOptions: null);
#pragma warning restore CS0618
}
[Obsolete("Specify checksumAlgorithm")]
public static SyntaxTree Create(CSharpSyntaxNode root, CSharpParseOptions? options, string? path, Encoding? encoding)
=> Create(root, SourceHashAlgorithm.Sha1, options, path, encoding);

#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.

Expand All @@ -332,15 +329,17 @@ public static SyntaxTree Create(
CSharpParseOptions? options,
string? path,
Encoding? encoding,
// obsolete parameter -- unused
ImmutableDictionary<string, ReportDiagnostic>? diagnosticOptions,
// obsolete parameter -- unused
bool? isGeneratedCode)
ImmutableDictionary<string, ReportDiagnostic>? diagnosticOptions, // unused
bool? isGeneratedCode) // unused
=> Create(root , SourceHashAlgorithm.Sha1, options, path, encoding);

/// <summary>
/// Creates a new syntax tree from a syntax node.
/// </summary>
public static SyntaxTree Create(CSharpSyntaxNode root, SourceHashAlgorithm checksumAlgorithm, CSharpParseOptions? options = null, string? path = "", Encoding? encoding = null)
{
if (root == null)
{
throw new ArgumentNullException(nameof(root));
}

var directives = root.Kind() == SyntaxKind.CompilationUnit ?
((CompilationUnitSyntax)root).GetConditionalDirectivesStack() :
Expand All @@ -349,12 +348,12 @@ public static SyntaxTree Create(
return new ParsedSyntaxTree(
textOpt: null,
encodingOpt: encoding,
checksumAlgorithm: SourceHashAlgorithm.Sha1,
checksumAlgorithm: checksumAlgorithm,
path: path,
options: options ?? CSharpParseOptions.Default,
root: root,
directives: directives,
diagnosticOptions,
diagnosticOptions: null,
cloneRoot: true);
}

Expand Down
1 change: 1 addition & 0 deletions src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*REMOVED*Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilationOptions.GetHashCode() -> Integer
Shared Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.Create(root As Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxNode, checksumAlgorithm As Microsoft.CodeAnalysis.Text.SourceHashAlgorithm, options As Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions = Nothing, path As String = "", encoding As System.Text.Encoding = Nothing) -> Microsoft.CodeAnalysis.SyntaxTree
26 changes: 26 additions & 0 deletions src/Compilers/VisualBasic/Portable/Syntax/VisualBasicSyntaxTree.vb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
''' Creates a new syntax tree from a syntax node.
''' </summary>
''' <param name="diagnosticOptions">An obsolete parameter. Diagnostic options should now be passed with <see cref="CompilationOptions.SyntaxTreeOptionsProvider"/></param>
<EditorBrowsable(EditorBrowsableState.Never)>
<Obsolete("The diagnosticOptions parameter is obsolete due to performance problems, if you are using it use CompilationOptions.SyntaxTreeOptionsProvider instead", [error]:=False)>
Public Shared Function Create(root As VisualBasicSyntaxNode,
Optional options As VisualBasicParseOptions = Nothing,
Optional path As String = "",
Expand All @@ -178,6 +180,30 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
diagnosticOptions)
End Function

''' <summary>
''' Creates a new syntax tree from a syntax node.
''' </summary>
Public Shared Function Create(root As VisualBasicSyntaxNode,
checksumAlgorithm As SourceHashAlgorithm,
Optional options As VisualBasicParseOptions = Nothing,
Optional path As String = "",
Optional encoding As Encoding = Nothing) As SyntaxTree
If root Is Nothing Then
Throw New ArgumentNullException(NameOf(root))
End If

Return New ParsedSyntaxTree(
textOpt:=Nothing,
encodingOpt:=encoding,
checksumAlgorithm:=checksumAlgorithm,
path:=path,
options:=If(options, VisualBasicParseOptions.Default),
syntaxRoot:=root,
isMyTemplate:=False,
diagnosticOptions:=Nothing,
cloneRoot:=True)
End Function

''' <summary>
''' <para>
''' Internal helper for <see cref="VisualBasicSyntaxNode"/> class to create a new syntax tree rooted at the given root node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static async Task<Document> FormatDocumentAsync(Document document, Syntax
text += logger.ToString();
text += "#endif" + Environment.NewLine;

return document.WithText(SourceText.From(text));
return document.WithText(SourceText.From(text, encoding: null, checksumAlgorithm: SourceHashAlgorithm.Sha256));
}

private static async Task<Document> AddAssemblyInfoRegionAsync(Document document, ISymbol symbol, CancellationToken cancellationToken)
Expand Down
19 changes: 11 additions & 8 deletions src/EditorFeatures/Core/Interactive/InteractiveSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private void AddSubmissionProjectNoLock(ITextBuffer submissionBuffer, string lan
solution = initProject.Solution.AddDocument(
DocumentId.CreateNewId(initializationScriptProjectId, debugName: initializationScriptPath),
Path.GetFileName(initializationScriptPath),
new FileTextLoader(initializationScriptPath, defaultEncoding: null));
new FileTextLoader(initializationScriptPath, defaultEncoding: null, initProject.State.ChecksumAlgorithm));
}
var newSubmissionProject = CreateSubmissionProjectNoLock(solution, _currentSubmissionProjectId, _lastSuccessfulSubmissionProjectId, languageName, imports, references);
Expand Down Expand Up @@ -289,18 +289,21 @@ private Project CreateSubmissionProjectNoLock(Solution solution, ProjectId newSu

solution = solution.AddProject(
ProjectInfo.Create(
newSubmissionProjectId,
VersionStamp.Create(),
name: name,
assemblyName: name,
language: languageName,
new ProjectInfo.ProjectAttributes(
id: newSubmissionProjectId,
version: VersionStamp.Create(),
name: name,
assemblyName: name,
language: languageName,
compilationOutputFilePaths: default,
checksumAlgorithm: SourceHashAlgorithm.Sha256,
isSubmission: true),
compilationOptions: compilationOptions,
parseOptions: _languageInfo.ParseOptions,
documents: null,
projectReferences: null,
metadataReferences: references,
hostObjectType: typeof(InteractiveScriptGlobals),
isSubmission: true));
hostObjectType: typeof(InteractiveScriptGlobals)));

if (previousSubmissionProjectId != null)
{
Expand Down
16 changes: 8 additions & 8 deletions src/EditorFeatures/Core/Workspaces/EditorTextFactoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace Microsoft.CodeAnalysis.Editor.Implementation.Workspaces
{
[ExportWorkspaceService(typeof(ITextFactoryService), ServiceLayer.Editor), Shared]
internal class EditorTextFactoryService : ITextFactoryService
internal sealed class EditorTextFactoryService : ITextFactoryService
{
private readonly ITextBufferCloneService _textBufferCloneService;
private readonly ITextBufferFactoryService _textBufferFactory;
Expand All @@ -37,7 +37,7 @@ public EditorTextFactoryService(

private static readonly Encoding s_throwingUtf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

public SourceText CreateText(Stream stream, Encoding? defaultEncoding, CancellationToken cancellationToken = default)
public SourceText CreateText(Stream stream, Encoding? defaultEncoding, SourceHashAlgorithm checksumAlgorithm, CancellationToken cancellationToken)
{
// this API is for a case where user wants us to figure out encoding from the given stream.
// if defaultEncoding is given, we will use it if we couldn't figure out encoding used in the stream ourselves.
Expand All @@ -50,7 +50,7 @@ public SourceText CreateText(Stream stream, Encoding? defaultEncoding, Cancellat
// Try UTF-8
try
{
return CreateTextInternal(stream, s_throwingUtf8Encoding, cancellationToken);
return CreateTextInternal(stream, s_throwingUtf8Encoding, checksumAlgorithm, cancellationToken);
}
catch (DecoderFallbackException)
{
Expand All @@ -61,7 +61,7 @@ public SourceText CreateText(Stream stream, Encoding? defaultEncoding, Cancellat

try
{
return CreateTextInternal(stream, defaultEncoding, cancellationToken);
return CreateTextInternal(stream, defaultEncoding, checksumAlgorithm, cancellationToken);
}
catch (DecoderFallbackException)
{
Expand All @@ -70,27 +70,27 @@ public SourceText CreateText(Stream stream, Encoding? defaultEncoding, Cancellat
}
}

public SourceText CreateText(TextReader reader, Encoding? encoding, CancellationToken cancellationToken = default)
public SourceText CreateText(TextReader reader, Encoding? encoding, SourceHashAlgorithm checksumAlgorithm, CancellationToken cancellationToken)
{
// this API is for a case where user just wants to create a source text with explicit encoding.
var buffer = CreateTextBuffer(reader);

// use the given encoding as it is.
return buffer.CurrentSnapshot.AsRoslynText(_textBufferCloneService, encoding);
return buffer.CurrentSnapshot.AsRoslynText(_textBufferCloneService, encoding, checksumAlgorithm);
}

private ITextBuffer CreateTextBuffer(TextReader reader)
=> _textBufferFactory.CreateTextBuffer(reader, _unknownContentType);

private SourceText CreateTextInternal(Stream stream, Encoding encoding, CancellationToken cancellationToken)
private SourceText CreateTextInternal(Stream stream, Encoding encoding, SourceHashAlgorithm checksumAlgorithm, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
stream.Seek(0, SeekOrigin.Begin);

using var reader = new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true);

var buffer = CreateTextBuffer(reader);
return buffer.CurrentSnapshot.AsRoslynText(_textBufferCloneService, reader.CurrentEncoding ?? Encoding.UTF8);
return buffer.CurrentSnapshot.AsRoslynText(_textBufferCloneService, reader.CurrentEncoding ?? Encoding.UTF8, checksumAlgorithm);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,28 +495,28 @@ public async Task StartDebuggingSession_CapturingDocuments(bool captureAllDocume
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdA,
name: "A",
loader: new FileTextLoader(sourceFileA.Path, encodingA),
loader: new FileTextLoader(sourceFileA.Path, encodingA, SourceHashAlgorithm.Sha256),
filePath: sourceFileA.Path));

var documentIdB = DocumentId.CreateNewId(projectP.Id, debugName: "B");
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdB,
name: "B",
loader: new FileTextLoader(sourceFileB.Path, encodingB),
loader: new FileTextLoader(sourceFileB.Path, encodingB, SourceHashAlgorithm.Sha256),
filePath: sourceFileB.Path));

var documentIdC = DocumentId.CreateNewId(projectP.Id, debugName: "C");
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdC,
name: "C",
loader: new FileTextLoader(sourceFileC.Path, encodingC),
loader: new FileTextLoader(sourceFileC.Path, encodingC, SourceHashAlgorithm.Sha256),
filePath: sourceFileC.Path));

var documentIdE = DocumentId.CreateNewId(projectP.Id, debugName: "E");
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdE,
name: "E",
loader: new FileTextLoader(sourceFileE.Path, encodingE),
loader: new FileTextLoader(sourceFileE.Path, encodingE, SourceHashAlgorithm.Sha256),
filePath: sourceFileE.Path));

// check that are testing documents whose hash algorithm does not match the PDB (but the hash itself does):
Expand Down Expand Up @@ -556,7 +556,7 @@ public async Task StartDebuggingSession_CapturingDocuments(bool captureAllDocume

// change content of B on disk again:
sourceFileB.WriteAllText(sourceB3, encodingB);
solution = solution.WithDocumentTextLoader(documentIdB, new FileTextLoader(sourceFileB.Path, encodingB), PreservationMode.PreserveValue);
solution = solution.WithDocumentTextLoader(documentIdB, new FileTextLoader(sourceFileB.Path, encodingB, SourceHashAlgorithm.Sha256), PreservationMode.PreserveValue);

EnterBreakState(debuggingSession);

Expand Down Expand Up @@ -4257,7 +4257,7 @@ public async Task MultiSession()
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdA,
name: "A",
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8),
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8, SourceHashAlgorithm.Sha256),
filePath: sourceFileA.Path));

var tasks = Enumerable.Range(0, 10).Select(async i =>
Expand Down Expand Up @@ -4342,7 +4342,7 @@ public async Task WatchHotReloadServiceTest()
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdA,
name: "A",
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8),
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8, SourceHashAlgorithm.Sha256),
filePath: sourceFileA.Path));

var hotReload = new WatchHotReloadService(workspace.Services, ImmutableArray.Create("Baseline", "AddDefinitionToExistingType", "NewTypeDefinition"));
Expand Down Expand Up @@ -4409,7 +4409,7 @@ public async Task UnitTestingHotReloadServiceTest()
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdA,
name: "A",
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8),
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8, SourceHashAlgorithm.Sha256),
filePath: sourceFileA.Path));

var hotReload = new UnitTestingHotReloadService(workspace.Services);
Expand Down
Loading

0 comments on commit f173424

Please sign in to comment.