Skip to content

Commit

Permalink
Merge branch 'master' into features/upgrade_maxlangver
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzngard committed Jul 26, 2019
2 parents 64a50ae + e7f0317 commit 34a64db
Show file tree
Hide file tree
Showing 109 changed files with 2,185 additions and 417 deletions.
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<RoslynDiagnosticsNugetPackageVersion>2.9.3</RoslynDiagnosticsNugetPackageVersion>
<CodeStyleLayerCodeAnalysisVersion>2.8.2</CodeStyleLayerCodeAnalysisVersion>
<MicrosoftCodeAnalysisTestingVersion>1.0.0-beta1-63310-01</MicrosoftCodeAnalysisTestingVersion>
<CodeStyleAnalyzerVersion>3.1.0-beta1-19113-04</CodeStyleAnalyzerVersion>
<CodeStyleAnalyzerVersion>3.3.0-beta2-19376-02</CodeStyleAnalyzerVersion>
<VisualStudioEditorPackagesVersion>16.1.101</VisualStudioEditorPackagesVersion>
</PropertyGroup>
<!--
Expand Down
8 changes: 5 additions & 3 deletions src/CodeStyle/Core/Analyzers/AbstractFormattingAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.IO;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.CodingConventions;

namespace Microsoft.CodeAnalysis.CodeStyle
Expand All @@ -27,8 +26,11 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics

protected override void InitializeWorker(AnalysisContext context)
{
var codingConventionsManager = CodingConventionsManagerFactory.CreateCodingConventionsManager();
context.RegisterSyntaxTreeAction(c => AnalyzeSyntaxTree(c, codingConventionsManager));
context.RegisterSyntaxTreeAction(c =>
{
var codingConventionsManager = new AnalyzerConfigCodingConventionsManager(c.Tree, c.Options);
AnalyzeSyntaxTree(c, codingConventionsManager);
});
}

protected abstract OptionSet ApplyFormattingOptions(OptionSet optionSet, ICodingConventionContext codingConventionContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.VisualStudio.CodingConventions;

namespace Microsoft.CodeAnalysis
{
public class AnalyzerConfigCodingConventionsContext : ICodingConventionContext, ICodingConventionsSnapshot
{
private static readonly Func<object, string, string?> TryGetAnalyzerConfigValue;

private readonly object _analyzerConfigOptions;

static AnalyzerConfigCodingConventionsContext()
{
TryGetAnalyzerConfigValue = CreateTryGetAnalyzerConfigValueAccessor();

static Func<object, string, string?> CreateTryGetAnalyzerConfigValueAccessor()
{
var analyzerConfigOptions = typeof(AnalyzerOptions).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions", throwOnError: false, ignoreCase: false);
var method = analyzerConfigOptions?.GetRuntimeMethod("TryGetValue", new[] { typeof(string), typeof(string).MakeByRefType() });
if (method is null)
{
return (_1, _2) => null;
}

var instance = Expression.Parameter(typeof(object), "instance");
var key = Expression.Parameter(typeof(string), "key");
var value = Expression.Variable(typeof(string), "value");
var accessor = Expression.Lambda<Func<object, string, string>>(
Expression.Block(
typeof(string),
new[] { value },
Expression.Call(
Expression.Convert(instance, analyzerConfigOptions),
method,
key,
value),
value),
instance,
key);
return accessor.Compile();
}
}

public AnalyzerConfigCodingConventionsContext(object analyzerConfigOptions)
{
_analyzerConfigOptions = analyzerConfigOptions;
}

public ICodingConventionsSnapshot CurrentConventions => this;

IUniversalCodingConventions ICodingConventionsSnapshot.UniversalConventions => throw new NotSupportedException();
IReadOnlyDictionary<string, object> ICodingConventionsSnapshot.AllRawConventions => throw new NotSupportedException();
int ICodingConventionsSnapshot.Version => 0;

event CodingConventionsChangedAsyncEventHandler ICodingConventionContext.CodingConventionsChangedAsync
{
add { }
remove { }
}

public void Dispose()
{
}

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task WriteConventionValueAsync(string conventionName, string conventionValue, CancellationToken cancellationToken)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
throw new NotSupportedException();
}

bool ICodingConventionsSnapshot.TryGetConventionValue<T>(string conventionName, [MaybeNullWhen(returnValue: false)] out T conventionValue)
{
if (typeof(T) != typeof(string))
{
conventionValue = default!;
return false;
}

conventionValue = (T)(object?)TryGetAnalyzerConfigValue(_analyzerConfigOptions, conventionName)!;
return conventionValue is object;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.VisualStudio.CodingConventions;

namespace Microsoft.CodeAnalysis
{
internal class AnalyzerConfigCodingConventionsManager : ICodingConventionsManager
{
private static readonly Func<AnalyzerOptions, object?> GetAnalyzerConfigOptionsProvider;
private static readonly Func<object, SyntaxTree, object> GetAnalyzerConfigOptions;

private readonly SyntaxTree _tree;
private readonly AnalyzerOptions _options;
private readonly ICodingConventionsManager? _codingConventionsManager;

static AnalyzerConfigCodingConventionsManager()
{
GetAnalyzerConfigOptionsProvider = CreateAnalyzerConfigOptionsProviderAccessor();
GetAnalyzerConfigOptions = CreateAnalyzerConfigOptionsAccessor();

static Func<AnalyzerOptions, object?> CreateAnalyzerConfigOptionsProviderAccessor()
{
var property = typeof(AnalyzerOptions).GetTypeInfo().GetDeclaredProperty("AnalyzerConfigOptionsProvider");
if (property is null)
{
return _ => null;
}

var options = Expression.Parameter(typeof(AnalyzerOptions), "options");
var accessor = Expression.Lambda<Func<AnalyzerOptions, object>>(
Expression.Call(options, property.GetMethod),
options);
return accessor.Compile();
}

static Func<object, SyntaxTree, object> CreateAnalyzerConfigOptionsAccessor()
{
var analyzerConfigOptionsProvider = typeof(AnalyzerOptions).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptionsProvider", throwOnError: false, ignoreCase: false);
var method = analyzerConfigOptionsProvider?.GetRuntimeMethod("GetOptions", new[] { typeof(SyntaxTree) });
if (method is null)
{
return (_1, _2) => throw new NotImplementedException();
}

var provider = Expression.Parameter(typeof(object), "provider");
var tree = Expression.Parameter(typeof(SyntaxTree), "tree");
var accessor = Expression.Lambda<Func<object, SyntaxTree, object>>(
Expression.Call(
Expression.Convert(provider, analyzerConfigOptionsProvider),
method,
tree),
provider,
tree);
return accessor.Compile();
}
}

public AnalyzerConfigCodingConventionsManager(SyntaxTree tree, AnalyzerOptions options)
{
_tree = tree;
_options = options;
if (GetAnalyzerConfigOptionsProvider(options) is null)
{
_codingConventionsManager = CodingConventionsManagerFactory.CreateCodingConventionsManager();
}
}

public Task<ICodingConventionContext> GetConventionContextAsync(string filePathContext, CancellationToken cancellationToken)
{
if (_codingConventionsManager is object)
{
return _codingConventionsManager.GetConventionContextAsync(filePathContext, cancellationToken);
}

var analyzerConfigOptionsProvider = GetAnalyzerConfigOptionsProvider(_options);
var analyzerConfigOptions = GetAnalyzerConfigOptions(analyzerConfigOptionsProvider!, _tree);
return Task.FromResult<ICodingConventionContext>(new AnalyzerConfigCodingConventionsContext(analyzerConfigOptions));
}
}
}
4 changes: 2 additions & 2 deletions src/CodeStyle/Core/CodeFixes/FormattingCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.CodingConventions;

namespace Microsoft.CodeAnalysis.CodeStyle
Expand Down Expand Up @@ -62,7 +61,8 @@ private async Task<OptionSet> GetOptionsAsync(Document document, CancellationTok
// in testing requires manual handling of .editorconfig.
if (File.Exists(document.FilePath ?? document.Name))
{
var codingConventionsManager = CodingConventionsManagerFactory.CreateCodingConventionsManager();
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var codingConventionsManager = new AnalyzerConfigCodingConventionsManager(tree, document.Project.AnalyzerOptions);
var codingConventionContext = await codingConventionsManager.GetConventionContextAsync(document.FilePath ?? document.Name, cancellationToken).ConfigureAwait(false);
options = ApplyFormattingOptions(options, codingConventionContext);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/BoundTree/BoundNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ private enum BoundNodeAttributes : short
TopLevelFlowStateMaybeNull = 1 << 3,
TopLevelNotAnnotated = 1 << 4,
TopLevelAnnotated = 1 << 5,
TopLevelDisabled = TopLevelAnnotated | TopLevelNotAnnotated,
TopLevelAnnotationMask = TopLevelDisabled,
TopLevelNone = TopLevelAnnotated | TopLevelNotAnnotated,
TopLevelAnnotationMask = TopLevelNone,

/// <summary>
/// Captures the fact that consumers of the node already checked the state of the WasCompilerGenerated bit.
Expand Down Expand Up @@ -199,7 +199,7 @@ public void ResetCompilerGenerated(bool newCompilerGenerated)
/// <summary>
/// Top level nullability for the node. This should not be used by flow analysis.
/// </summary>
[DebuggerHidden]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected NullabilityInfo TopLevelNullability
{
get
Expand All @@ -225,7 +225,7 @@ protected NullabilityInfo TopLevelNullability
{
CodeAnalysis.NullableAnnotation.Annotated => BoundNodeAttributes.TopLevelAnnotated,
CodeAnalysis.NullableAnnotation.NotAnnotated => BoundNodeAttributes.TopLevelNotAnnotated,
CodeAnalysis.NullableAnnotation.Disabled => BoundNodeAttributes.TopLevelDisabled,
CodeAnalysis.NullableAnnotation.None => BoundNodeAttributes.TopLevelNone,
var a => throw ExceptionUtilities.UnexpectedValue(a),
};

Expand Down Expand Up @@ -262,7 +262,7 @@ private NullabilityInfo TopLevelNullabilityCore
{
BoundNodeAttributes.TopLevelAnnotated => CodeAnalysis.NullableAnnotation.Annotated,
BoundNodeAttributes.TopLevelNotAnnotated => CodeAnalysis.NullableAnnotation.NotAnnotated,
BoundNodeAttributes.TopLevelDisabled => CodeAnalysis.NullableAnnotation.Disabled,
BoundNodeAttributes.TopLevelNone => CodeAnalysis.NullableAnnotation.None,
var mask => throw ExceptionUtilities.UnexpectedValue(mask)
};

Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CodeGen/EmitExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ private void EmitCallExpression(BoundCall call, UseKind useKind)
// otherwise we should not use direct 'call' and must use constrained call;

// calling a method defined in a value type
Debug.Assert(TypeSymbol.Equals(receiverType, methodContainingType, TypeCompareKind.ConsiderEverything2));
Debug.Assert(TypeSymbol.Equals(receiverType, methodContainingType, TypeCompareKind.ObliviousNullableModifierMatchesAny));
tempOpt = EmitReceiverRef(receiver, receiverAddresskind);
callKind = CallKind.Call;
}
Expand Down
Loading

0 comments on commit 34a64db

Please sign in to comment.