Skip to content

Commit

Permalink
Add ChecksumAlgorithm to DocumentAttributes and ProjectAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jul 25, 2022
1 parent 2b1499b commit 0b6ad0c
Show file tree
Hide file tree
Showing 99 changed files with 1,071 additions and 562 deletions.
11 changes: 11 additions & 0 deletions eng/config/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ 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
M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.SyntaxTree(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.ParseOptions,System.String,System.Text.Encoding); Use overload with checksum algorithm
M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseSyntaxTree(System.String,Microsoft.CodeAnalysis.ParseOptions,System.String,System.Text.Encoding,System.Threading.CancellationToken)
M:Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(System.String,Microsoft.CodeAnalysis.CSharp.CSharpParseOptions,System.String,System.Text.Encoding,System.Threading.CancellationToken); Use API that takes SourceText
M:Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(System.String,Microsoft.CodeAnalysis.CSharp.CSharpParseOptions,System.String,System.Text.Encoding,System.Collections.Immutable.ImmutableDictionary{System.String,Microsoft.CodeAnalysis.ReportDiagnostic},System.Nullable{System.Boolean},System.Threading.CancellationToken); Use API that takes SourceText
M:Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.SyntaxTree(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.ParseOptions,System.String,System.Text.Encoding); Use overload with checksum algorithm
M:Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.ParseSyntaxTree(System.String,Microsoft.CodeAnalysis.ParseOptions,System.String,System.Text.Encoding,System.Threading.CancellationToken); Use overload with SourceText
M:Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.ParseSyntaxTree(System.String,Microsoft.CodeAnalysis.ParseOptions,System.String,System.Text.Encoding,System.Collections.Immutable.ImmutableDictionary{System.String,Microsoft.CodeAnalysis.ReportDiagnostic},System.Threading.CancellationToken); Use overload with SourceText
M:Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.CreateWithoutClone(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxNode); Use VisualBasicSyntaxTree.Create
M:Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.Create(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxNode,Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions,System.String,System.Text.Encoding,System.Collections.Immutable.ImmutableDictionary{System.String,Microsoft.CodeAnalysis.ReportDiagnostic}); Use overload with checksum algorithm
M:Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(System.String,Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions,System.String,System.Text.Encoding,System.Collections.Immutable.ImmutableDictionary{System.String,Microsoft.CodeAnalysis.ReportDiagnostic},System.Threading.CancellationToken); Use overload with SourceText
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 = null, string? path = "", System.Text.Encoding? encoding = null, Microsoft.CodeAnalysis.Text.SourceHashAlgorithm checksumAlgorithm = Microsoft.CodeAnalysis.Text.SourceHashAlgorithm.Sha1) -> Microsoft.CodeAnalysis.SyntaxTree!
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.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
33 changes: 14 additions & 19 deletions src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,11 @@ private void BuildPreprocessorStateChangeMap()

#region Factories

// The overload that has more parameters is itself obsolete, as an intentional break to allow future
// expansion
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.

/// <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
}

#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.
public static SyntaxTree Create(CSharpSyntaxNode root, CSharpParseOptions? options, string? path, Encoding? encoding)
=> CreateImpl(root, diagnosticOptions: null, options, path, encoding, SourceHashAlgorithm.Sha1);

/// <summary>
/// Creates a new syntax tree from a syntax node.
Expand All @@ -332,15 +322,20 @@ public static SyntaxTree Create(
CSharpParseOptions? options,
string? path,
Encoding? encoding,
// obsolete parameter -- unused
ImmutableDictionary<string, ReportDiagnostic>? diagnosticOptions,
// obsolete parameter -- unused
bool? isGeneratedCode)
bool? isGeneratedCode) // unused
=> CreateImpl(root, diagnosticOptions, options, path, encoding, SourceHashAlgorithm.Sha1);

/// <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, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1)
=> CreateImpl(root, diagnosticOptions: null, options, path, encoding, checksumAlgorithm);

private static SyntaxTree CreateImpl(CSharpSyntaxNode root, ImmutableDictionary<string, ReportDiagnostic>? diagnosticOptions, CSharpParseOptions? options, string? path, Encoding? encoding, SourceHashAlgorithm checksumAlgorithm)
{
if (root == null)
{
throw new ArgumentNullException(nameof(root));
}

var directives = root.Kind() == SyntaxKind.CompilationUnit ?
((CompilationUnitSyntax)root).GetConditionalDirectivesStack() :
Expand All @@ -349,7 +344,7 @@ public static SyntaxTree Create(
return new ParsedSyntaxTree(
textOpt: null,
encodingOpt: encoding,
checksumAlgorithm: SourceHashAlgorithm.Sha1,
checksumAlgorithm: checksumAlgorithm,
path: path,
options: options ?? CSharpParseOptions.Default,
root: root,
Expand Down Expand Up @@ -442,7 +437,7 @@ public static SyntaxTree ParseText(
bool? isGeneratedCode,
CancellationToken cancellationToken)
{
return ParseText(SourceText.From(text, encoding), options, path, diagnosticOptions, isGeneratedCode, cancellationToken);
return ParseText(SourceText.From(text, encoding, SourceHashAlgorithm.Sha1), options, path, diagnosticOptions, isGeneratedCode, cancellationToken);
}

// The overload that has more parameters is itself obsolete, as an intentional break to allow future
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Syntax/SyntaxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ public static IdentifierNameSyntax IdentifierName(string name)
/// </summary>
public static SyntaxTree SyntaxTree(SyntaxNode root, ParseOptions? options = null, string path = "", Encoding? encoding = null)
{
return CSharpSyntaxTree.Create((CSharpSyntaxNode)root, (CSharpParseOptions?)options, path, encoding);
return CSharpSyntaxTree.Create((CSharpSyntaxNode)root, (CSharpParseOptions?)options, path, encoding, SourceHashAlgorithm.Sha1);
}

#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

#nullable disable
#pragma warning disable RS0030 // Do not used banned APIs

using System;
using System.Globalization;
Expand Down
28 changes: 19 additions & 9 deletions src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.

#nullable disable
#pragma warning disable RS0030 // Do not used banned APIs
#pragma warning disable CS0618 // Obsolete APIs

using System.Collections.Immutable;
using System.Text;
Expand All @@ -21,18 +23,26 @@ public class SyntaxTreeTests : ParsingTests
{
public SyntaxTreeTests(ITestOutputHelper output) : base(output) { }

// Diagnostic options on syntax trees are now obsolete
#pragma warning disable CS0618
[Fact]
public void CreateTreeWithDiagnostics()
public void Create()
{
var root = SyntaxFactory.ParseCompilationUnit("");

var tree = CSharpSyntaxTree.Create(root);
Assert.Equal(SourceHashAlgorithm.Sha1, tree.GetText().ChecksumAlgorithm);

tree = CSharpSyntaxTree.Create(root, checksumAlgorithm: SourceHashAlgorithm.Sha256);
Assert.Equal(SourceHashAlgorithm.Sha256, tree.GetText().ChecksumAlgorithm);
}

[Fact]
public void Create_WithDiagnosticOptions()
{
var options = CreateImmutableDictionary(("CS0078", ReportDiagnostic.Suppress));
var tree = CSharpSyntaxTree.Create(SyntaxFactory.ParseCompilationUnit(""),
options: null,
path: "",
encoding: null,
diagnosticOptions: options);
var tree = CSharpSyntaxTree.Create(SyntaxFactory.ParseCompilationUnit(""), options: null, path: null, encoding: null, diagnosticOptions: options);

Assert.Same(options, tree.DiagnosticOptions);
Assert.Equal(SourceHashAlgorithm.Sha1, tree.GetText().ChecksumAlgorithm);
}

[Fact]
Expand Down Expand Up @@ -230,7 +240,7 @@ public void WithFilePath_Null()
oldTree = SyntaxFactory.ParseSyntaxTree("", path: "old.cs");
Assert.Equal(string.Empty, oldTree.WithFilePath(null).FilePath);
Assert.Equal(string.Empty, SyntaxFactory.ParseSyntaxTree("", path: null).FilePath);
Assert.Equal(string.Empty, CSharpSyntaxTree.Create((CSharpSyntaxNode)oldTree.GetRoot(), path: null).FilePath);
Assert.Equal(string.Empty, CSharpSyntaxTree.Create((CSharpSyntaxNode)oldTree.GetRoot(), checksumAlgorithm: SourceHashAlgorithm.Sha256).FilePath);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
' taken to ensure these are compatible with 2.0 runtimes so there is no danger
' with allowing the newer syntax here.
Dim options = parseOptions.WithLanguageVersion(LanguageVersion.Default)
tree = VisualBasicSyntaxTree.ParseText(text, options:=options, isMyTemplate:=True)
tree = VisualBasicSyntaxTree.ParseText(
SourceText.From(text, encoding:=Nothing, SourceHashAlgorithm.Sha256),
isMyTemplate:=True,
options,
path:=Nothing)

If tree.GetDiagnostics().Any() Then
Throw ExceptionUtilities.Unreachable
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*REMOVED*Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilationOptions.GetHashCode() -> Integer
Shared Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.Create(root As Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxNode, options As Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions = Nothing, path As String = "", encoding As System.Text.Encoding = Nothing, checksumAlgorithm As Microsoft.CodeAnalysis.Text.SourceHashAlgorithm = Microsoft.CodeAnalysis.Text.SourceHashAlgorithm.Sha1) -> Microsoft.CodeAnalysis.SyntaxTree
Shared Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.Create(root As Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxNode, options As Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions, path As String, encoding As System.Text.Encoding, diagnosticOptions As System.Collections.Immutable.ImmutableDictionary(Of String, Microsoft.CodeAnalysis.ReportDiagnostic)) -> Microsoft.CodeAnalysis.SyntaxTree
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Optional path As String = "",
Optional encoding As Encoding = Nothing) As SyntaxTree

Return VisualBasicSyntaxTree.Create(DirectCast(root, VisualBasicSyntaxNode), DirectCast(options, VisualBasicParseOptions), path, encoding)
Return VisualBasicSyntaxTree.Create(DirectCast(root, VisualBasicSyntaxNode), DirectCast(options, VisualBasicParseOptions), path, encoding, SourceHashAlgorithm.Sha1)
End Function

''' <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic

If tree Is Nothing Then
Debug.Assert(rootCandidate IsNot Nothing)
#Disable Warning RS0030 ' This method is only intended to be used from this call site
tree = VisualBasicSyntaxTree.CreateWithoutClone(DirectCast(rootCandidate, VisualBasicSyntaxNode))
#Enable Warning RS0030
End If

Debug.Assert(tree IsNot Nothing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Friend Class DummySyntaxTree
Inherits VisualBasicSyntaxTree

Private Const ChecksumAlgorithm As SourceHashAlgorithm = SourceHashAlgorithm.Sha256

Private ReadOnly _node As CompilationUnitSyntax

Public Sub New()
Expand All @@ -24,11 +26,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Function

Public Overrides Function GetText(Optional cancellationToken As CancellationToken = Nothing) As SourceText
Return SourceText.From(String.Empty, Encoding.UTF8)
Return SourceText.From(String.Empty, Me.Encoding, ChecksumAlgorithm)
End Function

Public Overrides Function TryGetText(ByRef text As SourceText) As Boolean
text = SourceText.From(String.Empty, Encoding.UTF8)
text = SourceText.From(String.Empty, Me.Encoding, ChecksumAlgorithm)
Return True
End Function

Expand Down Expand Up @@ -86,11 +88,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Property

Public Overrides Function WithRootAndOptions(root As SyntaxNode, options As ParseOptions) As SyntaxTree
Return SyntaxFactory.SyntaxTree(root, options:=options, path:=FilePath, encoding:=Nothing)
Return VisualBasicSyntaxTree.Create(DirectCast(root, VisualBasicSyntaxNode), DirectCast(options, VisualBasicParseOptions), FilePath, Me.Encoding, ChecksumAlgorithm)
End Function

Public Overrides Function WithFilePath(path As String) As SyntaxTree
Return SyntaxFactory.SyntaxTree(_node, options:=Me.Options, path:=path, encoding:=Nothing)
Return VisualBasicSyntaxTree.Create(_node, Options, path, Me.Encoding, ChecksumAlgorithm)
End Function

Public Overrides Function WithDiagnosticOptions(options As ImmutableDictionary(Of String, ReportDiagnostic)) As SyntaxTree
Expand Down
Loading

0 comments on commit 0b6ad0c

Please sign in to comment.