Skip to content

Commit

Permalink
Redo pubternalyzer with Roslyn Operations (#21996)
Browse files Browse the repository at this point in the history
* Rewrite most of the analyzer using Operations, for much better
  perf and VB.NET support.
* Support some extra language scenarios which weren't being detected.

Fixes #19648
Fixes #20206
  • Loading branch information
roji authored Aug 16, 2020
1 parent 1dc1788 commit bc68329
Show file tree
Hide file tree
Showing 16 changed files with 470 additions and 145 deletions.
2 changes: 1 addition & 1 deletion benchmark/EFCore.Benchmarks/EFCore.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup Condition=" '$(Configuration)' == 'Release' Or '$(Configuration)' == 'Debug' ">
<ProjectReference Include="..\..\src\EFCore.Relational\EFCore.Relational.csproj" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion)" />
</ItemGroup>

<ItemGroup Condition=" '$(Configuration)' == 'Release22' Or '$(Configuration)' == 'Debug22' ">
Expand Down
3 changes: 3 additions & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
<MicrosoftExtensionsHostFactoryResolverSourcesVersion>5.0.0-rc.1.20409.3</MicrosoftExtensionsHostFactoryResolverSourcesVersion>
<MicrosoftExtensionsLoggingVersion>5.0.0-rc.1.20409.3</MicrosoftExtensionsLoggingVersion>
</PropertyGroup>
<PropertyGroup Label="Other dependencies">
<MicrosoftCodeAnalysisVersion>3.7.0</MicrosoftCodeAnalysisVersion>
</PropertyGroup>
</Project>
18 changes: 18 additions & 0 deletions src/EFCore.Analyzers/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Release 2.1.0

### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
EF1000 | Usage | Warning | RawSqlStringInjectionDiagnosticAnalyzer, [Documentation](https://docs.microsoft.com/ef/core/querying/raw-sql)

## Release 3.0.0

### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
EF1001 | Usage | Warning | InternalUsageDiagnosticAnalyzer

### Removed Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
EF1000 | Security | Disabled | RawSqlStringInjectionDiagnosticAnalyzer, [Documentation](https://docs.microsoft.com/ef/core/querying/raw-sql)
Empty file.
8 changes: 7 additions & 1 deletion src/EFCore.Analyzers/EFCore.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

<ItemGroup>
<!-- NB: Version affects the minimum required Visual Studio version -->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.3.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>

Expand All @@ -32,4 +33,9 @@
</ItemGroup>
</Target>

<ItemGroup>
<AdditionalFiles Include="AnalyzerReleases.Shipped.md" />
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />
</ItemGroup>

</Project>
322 changes: 261 additions & 61 deletions src/EFCore.Analyzers/InternalUsageDiagnosticAnalyzer.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,12 @@ private void AddInclude(
var includeMethod = navigation.IsCollection ? _includeCollectionMethodInfo : _includeReferenceMethodInfo;
var includingClrType = navigation.DeclaringEntityType.ClrType;
var relatedEntityClrType = navigation.TargetEntityType.ClrType;
#pragma warning disable EF1001 // Internal EF Core API usage.
var entityEntryVariable = _trackQueryResults
? shaperBlock.Variables.Single(v => v.Type == typeof(InternalEntityEntry))
: (Expression)Expression.Constant(null, typeof(InternalEntityEntry));
#pragma warning restore EF1001 // Internal EF Core API usage.

var concreteEntityTypeVariable = shaperBlock.Variables.Single(v => v.Type == typeof(IEntityType));
var inverseNavigation = navigation.Inverse;
var fixup = GenerateFixup(
Expand All @@ -394,7 +397,9 @@ private static readonly MethodInfo _includeReferenceMethodInfo
.GetDeclaredMethod(nameof(IncludeReference));

private static void IncludeReference<TIncludingEntity, TIncludedEntity>(
#pragma warning disable EF1001 // Internal EF Core API usage.
InternalEntityEntry entry,
#pragma warning restore EF1001 // Internal EF Core API usage.
object entity,
IEntityType entityType,
TIncludedEntity relatedEntity,
Expand Down Expand Up @@ -437,7 +442,9 @@ private static readonly MethodInfo _includeCollectionMethodInfo
.GetDeclaredMethod(nameof(IncludeCollection));

private static void IncludeCollection<TIncludingEntity, TIncludedEntity>(
#pragma warning disable EF1001 // Internal EF Core API usage.
InternalEntityEntry entry,
#pragma warning restore EF1001 // Internal EF Core API usage.
object entity,
IEntityType entityType,
IEnumerable<TIncludedEntity> relatedEntities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,28 +259,28 @@ private bool TryGenerateIdFromKeys(IProperty idProperty, out object value)
{
var entityEntry = Activator.CreateInstance(_readItemExpression.EntityType.ClrType);

#pragma warning disable EF1001
#pragma warning disable EF1001 // Internal EF Core API usage.
var internalEntityEntry = new InternalEntityEntryFactory().Create(
_cosmosQueryContext.Context.GetDependencies().StateManager, _readItemExpression.EntityType, entityEntry);
#pragma warning restore EF1001
#pragma warning restore EF1001 // Internal EF Core API usage.

foreach (var keyProperty in _readItemExpression.EntityType.FindPrimaryKey().Properties)
{
var property = _readItemExpression.EntityType.FindProperty(keyProperty.Name);

if (TryGetParameterValue(property, out var parameterValue))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
internalEntityEntry[property] = parameterValue;
#pragma warning restore EF1001 // Internal EF Core API usage.
}
}

#pragma warning disable EF1001 // Internal EF Core API usage.
internalEntityEntry.SetEntityState(EntityState.Added);
#pragma warning restore EF1001 // Internal EF Core API usage.

value = internalEntityEntry[idProperty];

#pragma warning disable EF1001 // Internal EF Core API usage.
internalEntityEntry.SetEntityState(EntityState.Detached);
#pragma warning restore EF1001 // Internal EF Core API usage.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ public class MigrationsModelDiffer : IMigrationsModelDiffer
public MigrationsModelDiffer(
[NotNull] IRelationalTypeMappingSource typeMappingSource,
[NotNull] IMigrationsAnnotationProvider migrationsAnnotations,
#pragma warning disable EF1001 // Internal EF Core API usage.
[NotNull] IChangeDetector changeDetector,
#pragma warning restore EF1001 // Internal EF Core API usage.
[NotNull] IUpdateAdapterFactory updateAdapterFactory,
[NotNull] CommandBatchPreparerDependencies commandBatchPreparerDependencies)
{
Check.NotNull(typeMappingSource, nameof(typeMappingSource));
Check.NotNull(migrationsAnnotations, nameof(migrationsAnnotations));
#pragma warning disable EF1001 // Internal EF Core API usage.
Check.NotNull(changeDetector, nameof(changeDetector));
#pragma warning restore EF1001 // Internal EF Core API usage.
Check.NotNull(updateAdapterFactory, nameof(updateAdapterFactory));
Check.NotNull(commandBatchPreparerDependencies, nameof(commandBatchPreparerDependencies));

Expand Down Expand Up @@ -137,7 +141,9 @@ public MigrationsModelDiffer(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
#pragma warning disable EF1001 // Internal EF Core API usage.
protected virtual IChangeDetector ChangeDetector { get; }
#pragma warning restore EF1001 // Internal EF Core API usage.

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class SqlServerAnnotationProvider : RelationalAnnotationProvider
/// Initializes a new instance of this class.
/// </summary>
/// <param name="dependencies"> Parameter object containing dependencies for this service. </param>
#pragma warning disable EF1001 // Internal EF Core API usage.
public SqlServerAnnotationProvider([NotNull] RelationalAnnotationProviderDependencies dependencies)
#pragma warning restore EF1001 // Internal EF Core API usage.
: base(dependencies)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class SqlServerMigrationsAnnotationProvider : MigrationsAnnotationProvide
/// Initializes a new instance of this class.
/// </summary>
/// <param name="dependencies"> Parameter object containing dependencies for this service. </param>
#pragma warning disable EF1001 // Internal EF Core API usage.
public SqlServerMigrationsAnnotationProvider([NotNull] MigrationsAnnotationProviderDependencies dependencies)
#pragma warning restore EF1001 // Internal EF Core API usage.
: base(dependencies)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class SqliteAnnotationProvider : RelationalAnnotationProvider
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
#pragma warning disable EF1001 // Internal EF Core API usage.
public SqliteAnnotationProvider([NotNull] RelationalAnnotationProviderDependencies dependencies)
#pragma warning restore EF1001 // Internal EF Core API usage.
: base(dependencies)
{
}
Expand Down
3 changes: 2 additions & 1 deletion test/EFCore.Analyzers.Tests/EFCore.Analyzers.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<ProjectReference Include="..\..\src\EFCore.Analyzers\EFCore.Analyzers.csproj" />
<ProjectReference Include="..\..\src\EFCore.Relational\EFCore.Relational.csproj" />
<ProjectReference Include="..\EFCore.Tests\EFCore.Tests.csproj" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="$(MicrosoftExtensionsDependencyModelVersion)" />
</ItemGroup>

Expand Down
Loading

0 comments on commit bc68329

Please sign in to comment.