Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eng/config/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ M:Microsoft.CodeAnalysis.Editing.SyntaxEditor.#ctor(Microsoft.CodeAnalysis.Synta
M:Microsoft.CodeAnalysis.FileTextLoader.#ctor(System.String,System.Text.Encoding); use WorkspaceFileTextLoader that calls on ITextFactoryService to create SourceText
M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.SyntaxTree(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.ParseOptions,System.String,System.Text.Encoding); Use CSharpSyntaxTree sublass that takes checksum algorithm
M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseSyntaxTree(System.String,Microsoft.CodeAnalysis.ParseOptions,System.String,System.Text.Encoding,System.Threading.CancellationToken); Use CSharpSyntaxTree sublass that takes checksum algorithm
M:Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.CreateWithoutClone(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode); Use CSharpSyntaxTree sublass that takes checksum algorithm
M:Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.CreateWithoutClone(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode,Microsoft.CodeAnalysis.CSharp.CSharpParseOptions); Use CSharpSyntaxTree sublass that takes checksum algorithm
M:Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.Create(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode, Microsoft.CodeAnalysis.CSharp.CSharpParseOptions options, System.String path, System.Text.Encoding encoding); Use CSharpSyntaxTree sublass that takes checksum algorithm
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 VisualBasicSyntaxTree sublass that takes 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 sublass that takes checksum algorithm
M:Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.CreateWithoutClone(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxNode,Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions); Use VisualBasicSyntaxTree sublass that takes checksum algorithm
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 VisualBasicSyntaxTree sublass that takes checksum algorithm
M:Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.Create(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxNode,Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions,System.String,System.Text.Encoding); Use VisualBasicSyntaxTree sublass that takes 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
Expand Down
6 changes: 2 additions & 4 deletions src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp
{
Expand Down Expand Up @@ -73,8 +71,8 @@ private static SyntaxTree ComputeSyntaxTree(CSharpSyntaxNode node)
if (parent == null)
{
// set the tree on the root node atomically
#pragma warning disable RS0030 // Do not use banned APIs (CreateWithoutClone is intended to be used from this call site only)
Interlocked.CompareExchange(ref node._syntaxTree, CSharpSyntaxTree.CreateWithoutClone(node), null);
#pragma warning disable RS0030 // Do not use banned APIs (CreateWithoutClone is intended to be used from this call site)
Interlocked.CompareExchange(ref node._syntaxTree, CSharpSyntaxTree.CreateWithoutClone(node, CSharpParseOptions.Default), null);
#pragma warning restore
tree = node._syntaxTree;
break;
Expand Down
8 changes: 3 additions & 5 deletions src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -390,10 +388,10 @@ internal static SyntaxTree CreateForDebugger(CSharpSyntaxNode root, SourceText t
/// Internal helper for <see cref="CSharpSyntaxNode"/> class to create a new syntax tree rooted at the given root node.
/// This method does not create a clone of the given root, but instead preserves it's reference identity.
/// </para>
/// <para>NOTE: This method is only intended to be used from <see cref="CSharpSyntaxNode.SyntaxTree"/> property.</para>
/// <para>NOTE: This method is only intended to be used from <see cref="CSharpSyntaxNode.SyntaxTree"/> property and <c>SyntaxFactory.Parse*</c> methods.</para>
/// <para>NOTE: Do not use this method elsewhere, instead use <see cref="Create(CSharpSyntaxNode, CSharpParseOptions, string, Encoding)"/> method for creating a syntax tree.</para>
/// </summary>
internal static SyntaxTree CreateWithoutClone(CSharpSyntaxNode root)
internal static SyntaxTree CreateWithoutClone(CSharpSyntaxNode root, CSharpParseOptions options)
{
Debug.Assert(root != null);

Expand All @@ -402,7 +400,7 @@ internal static SyntaxTree CreateWithoutClone(CSharpSyntaxNode root)
encodingOpt: null,
checksumAlgorithm: SourceHashAlgorithm.Sha1,
path: "",
options: CSharpParseOptions.Default,
options: options,
root: root,
directives: default,
diagnosticOptions: null,
Expand Down
33 changes: 22 additions & 11 deletions src/Compilers/CSharp/Portable/Syntax/SyntaxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ public static NameSyntax ParseName(string text, int offset = 0, bool consumeFull
{
var node = parser.ParseName();
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (NameSyntax)node.CreateRed();
return CreateRed<NameSyntax>(node, lexer.Options);
}
}

Expand All @@ -1718,7 +1718,7 @@ public static TypeSyntax ParseTypeName(string text, int offset = 0, ParseOptions
{
var node = parser.ParseTypeName();
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (TypeSyntax)node.CreateRed();
return CreateRed<TypeSyntax>(node, lexer.Options);
}
}

Expand All @@ -1737,7 +1737,7 @@ public static ExpressionSyntax ParseExpression(string text, int offset = 0, Pars
{
var node = parser.ParseExpression();
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (ExpressionSyntax)node.CreateRed();
return CreateRed<ExpressionSyntax>(node, lexer.Options);
}
}

Expand All @@ -1756,7 +1756,7 @@ public static StatementSyntax ParseStatement(string text, int offset = 0, ParseO
{
var node = parser.ParseStatement();
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (StatementSyntax)node.CreateRed();
return CreateRed<StatementSyntax>(node, lexer.Options);
}
}

Expand All @@ -1780,7 +1780,7 @@ public static StatementSyntax ParseStatement(string text, int offset = 0, ParseO
return null;
}

return (MemberDeclarationSyntax)(consumeFullText ? parser.ConsumeUnexpectedTokens(node) : node).CreateRed();
return CreateRed<MemberDeclarationSyntax>(consumeFullText ? parser.ConsumeUnexpectedTokens(node) : node, lexer.Options);
}
}

Expand All @@ -1800,7 +1800,7 @@ public static CompilationUnitSyntax ParseCompilationUnit(string text, int offset
using (var parser = MakeParser(lexer))
{
var node = parser.ParseCompilationUnit();
return (CompilationUnitSyntax)node.CreateRed();
return CreateRed<CompilationUnitSyntax>(node, lexer.Options);
}
}

Expand All @@ -1819,7 +1819,7 @@ public static ParameterListSyntax ParseParameterList(string text, int offset = 0
{
var node = parser.ParseParenthesizedParameterList(forExtension: false);
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (ParameterListSyntax)node.CreateRed();
return CreateRed<ParameterListSyntax>(node, lexer.Options);
}
}

Expand All @@ -1838,7 +1838,7 @@ public static BracketedParameterListSyntax ParseBracketedParameterList(string te
{
var node = parser.ParseBracketedParameterList();
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (BracketedParameterListSyntax)node.CreateRed();
return CreateRed<BracketedParameterListSyntax>(node, lexer.Options);
}
}

Expand All @@ -1857,7 +1857,7 @@ public static ArgumentListSyntax ParseArgumentList(string text, int offset = 0,
{
var node = parser.ParseParenthesizedArgumentList();
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (ArgumentListSyntax)node.CreateRed();
return CreateRed<ArgumentListSyntax>(node, lexer.Options);
}
}

Expand All @@ -1876,7 +1876,7 @@ public static BracketedArgumentListSyntax ParseBracketedArgumentList(string text
{
var node = parser.ParseBracketedArgumentList();
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (BracketedArgumentListSyntax)node.CreateRed();
return CreateRed<BracketedArgumentListSyntax>(node, lexer.Options);
}
}

Expand All @@ -1902,7 +1902,18 @@ public static BracketedArgumentListSyntax ParseBracketedArgumentList(string text
annotations: null);
if (consumeFullText)
node = parser.ConsumeUnexpectedTokens(node);
return (AttributeArgumentListSyntax)node.CreateRed();
return CreateRed<AttributeArgumentListSyntax>(node, lexer.Options);
}

private static TSyntax CreateRed<TSyntax>(InternalSyntax.CSharpSyntaxNode green, CSharpParseOptions options)
where TSyntax : CSharpSyntaxNode
{
var red = (TSyntax)green.CreateRed();
Debug.Assert(red._syntaxTree is null);
#pragma warning disable RS0030 // Do not use banned APIs (CreateWithoutClone is intended to be used from this call site)
red._syntaxTree = CSharpSyntaxTree.CreateWithoutClone(red, options);
#pragma warning restore
return red;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@

#nullable disable

using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.Semantic.UnitTests.Semantics
{
Expand All @@ -32,7 +27,7 @@ public void SyntaxTreeCreateAcceptsAnySyntaxNode()
public void SyntaxTreeCreateWithoutCloneAcceptsAnySyntaxNode()
{
var node = SyntaxFactory.CatchClause(SyntaxFactory.CatchDeclaration(SyntaxFactory.ParseTypeName(typeof(InvalidOperationException).Name)), null, SyntaxFactory.Block());
var tree = CSharpSyntaxTree.CreateWithoutClone(node);
var tree = CSharpSyntaxTree.CreateWithoutClone(node, CSharpParseOptions.Default);
CheckTree(tree);
}

Expand Down
38 changes: 37 additions & 1 deletion src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Xunit;
using InternalSyntax = Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
Expand Down Expand Up @@ -678,5 +678,41 @@ public void TestParseNameWithOptions()
// unsafe class C { delegate*<void> x; }
Diagnostic(ErrorCode.WRN_UnreferencedField, "x").WithArguments("C.x").WithLocation(1, 34));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78510")]
public void TestParseMethodsKeepParseOptionsInTheTree()
{
var parseOptions = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Latest);

var argList = SyntaxFactory.ParseArgumentList("", options: parseOptions);
Assert.Same(parseOptions, argList.SyntaxTree.Options);

var attrArgList = SyntaxFactory.ParseAttributeArgumentList("", options: parseOptions);
Assert.Same(parseOptions, attrArgList.SyntaxTree.Options);

var bracketedArgList = SyntaxFactory.ParseBracketedArgumentList("", options: parseOptions);
Assert.Same(parseOptions, bracketedArgList.SyntaxTree.Options);

var bracketedParamList = SyntaxFactory.ParseBracketedParameterList("", options: parseOptions);
Assert.Same(parseOptions, bracketedParamList.SyntaxTree.Options);

var compUnit = SyntaxFactory.ParseCompilationUnit("", options: parseOptions);
Assert.Same(parseOptions, compUnit.SyntaxTree.Options);

var expr = SyntaxFactory.ParseExpression("", options: parseOptions);
Assert.Same(parseOptions, expr.SyntaxTree.Options);

var memberDecl = SyntaxFactory.ParseMemberDeclaration("public", options: parseOptions);
Assert.Same(parseOptions, memberDecl.SyntaxTree.Options);

var paramList = SyntaxFactory.ParseParameterList("", options: parseOptions);
Assert.Same(parseOptions, paramList.SyntaxTree.Options);

var statement = SyntaxFactory.ParseStatement("", options: parseOptions);
Assert.Same(parseOptions, statement.SyntaxTree.Options);

var typeName = SyntaxFactory.ParseTypeName("", options: parseOptions);
Assert.Same(parseOptions, typeName.SyntaxTree.Options);
}
}
}
6 changes: 6 additions & 0 deletions src/Compilers/VisualBasic/Portable/Parser/Parser.vb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
End If
End Sub

Friend ReadOnly Property Options As VisualBasicParseOptions
Get
Return _scanner.Options
End Get
End Property

Friend ReadOnly Property IsScript As Boolean
Get
Return _scanner.Options.Kind = SourceCodeKind.Script
Expand Down
Loading