Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

Commit

Permalink
Make much of the Workspaces layer Portable.
Browse files Browse the repository at this point in the history
This change splits the Workspaces layer into two parts, mirroring the Portable/Desktop split that the compilers did. The core parts of the Workspaces layer (managing documents and projects, formatting, some refactoring, code fixes) is kept in the portable subset, with a few non-portable pieces remaining, notably MSBuild support.

This change has a major impact on how MEF now works in Roslyn. Traditional MEF (“MEFv1”) is not portable, and so we must move the Workspaces layer over to using the Microsoft.Composition NuGet package (“MEFv2”) which is. The APIs are distinct in that each has its own namespace, but the concepts are more or less identical. It requires some care though: the workspaces layer is simple in that it only references MEFv2, but higher layers have to reference both versions to use metadata attributes. Exports using metadata attributes from the editor (say, ContentTypeAttribute) must use MEFv1 attributes to export, whereas exports for the workspaces layer must use MEFv2 attributes. The rule is subtle and yet simple, and so a diagnostic is provided which catches any offenses to prevent confusion.

This also has some impact in how we create MEF hosts: if you wish to host just the base workspaces layer, we can use MEFv2 to compose everything. The HostServices implementation (MefV1HostServices) that consumes a MEFv1 ExportProvider. If you’re in Visual Studio, you can use this implementation to get the full set of host services provided in Visual Studio.

Otherwise, most of the changes in here are minor: we react to some APIs that have been moved/renamed in the portable subset we are targeting, and also align our various exception helper utilities with the compiler’s precedent. (changeset 1349276)
  • Loading branch information
jasonmalinowski committed Oct 7, 2014
1 parent d0ea086 commit e76a29a
Show file tree
Hide file tree
Showing 1,646 changed files with 43,381 additions and 2,723 deletions.
4 changes: 2 additions & 2 deletions Src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private Task CompileNamespaceAsTask(NamespaceSymbol symbol)
CompileNamespace(symbol);
return (object)null;
}
catch (Exception e) if (CompilerFatalError.ReportUnlessCanceled(e))
catch (Exception e) if (FatalError.ReportUnlessCanceled(e))
{
throw ExceptionUtilities.Unreachable;
}
Expand Down Expand Up @@ -342,7 +342,7 @@ private Task CompileNamedTypeAsTask(NamedTypeSymbol symbol)
CompileNamedType(symbol);
return (object)null;
}
catch (Exception e) if (CompilerFatalError.Report(e))
catch (Exception e) if (FatalError.Report(e))
{
throw ExceptionUtilities.Unreachable;
}
Expand Down
2 changes: 1 addition & 1 deletion Src/Compilers/CSharp/csc/Csc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal Csc(string responseFile, string baseDirectory, string[] args)
internal static int Run(string[] args)
{

CompilerFatalError.Handler = FailFast.OnFatalException;
FatalError.Handler = FailFast.OnFatalException;

Csc compiler = new Csc(CSharpResponseFileName, Directory.GetCurrentDirectory(), args);

Expand Down
3 changes: 1 addition & 2 deletions Src/Compilers/Core/Portable/CodeAnalysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
<Compile Include="Instrumentation\RoslynCompilerEventSource.cs" />
<Compile Include="InternalUtilities\ArrayExtensions.cs" />
<Compile Include="InternalUtilities\BitArithmeticUtilities.cs" />
<Compile Include="InternalUtilities\CompilerFatalError.cs" />
<Compile Include="InternalUtilities\FatalError.cs" />
<Compile Include="InternalUtilities\ComStreamWrapper.cs" />
<Compile Include="InternalUtilities\ConcurrentDictionaryExtensions.cs" />
<Compile Include="InternalUtilities\ConcurrentLruCache.cs" />
Expand Down Expand Up @@ -269,7 +269,6 @@
<Compile Include="InternalUtilities\SuppressUnmanagedCodeSecurityAttribute.cs" />
<Compile Include="InternalUtilities\TextChangeRangeExtensions.cs" />
<Compile Include="InternalUtilities\TextKeyedCache.cs" />
<Compile Include="InternalUtilities\ThreadingUtilities.cs" />
<Compile Include="InternalUtilities\ThreadSafeFlagOperations.cs" />
<Compile Include="InternalUtilities\ThreeState.cs" />
<Compile Include="InternalUtilities\ValueTuple.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ namespace Microsoft.CodeAnalysis.Instrumentation
/// <summary>
/// This EventSource exposes our events to ETW.
/// RoslynCompilerEventSource GUID is {9f93daf9-7fee-5301-ebea-643b538889b4}.
/// CodeSense.RoslynCompilerEventSource GUID is {08e567fa-f66d-52c7-4e58-d802264cc8db}.
/// </summary>
#if CODESENSE
[EventSource(Name = "CodeSense.RoslynCompilerEventSource")]
#else
[EventSource(Name = "RoslynCompilerEventSource")]
#endif
internal sealed class RoslynCompilerEventSource : EventSource
{
// We might not be "enabled" but we always have this singleton alive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

namespace Microsoft.CodeAnalysis
{
internal static class CompilerFatalError
internal static class FatalError
{
private static Action<Exception> handler;
private static Exception reportedException;
private static string reportedExceptionMessage;

// Set by the host to a fail fast trigger,
// if the host desires to crash the process on a fatal exception.
Expand All @@ -18,6 +20,7 @@ public static Action<Exception> Handler
{
return handler;
}

set
{
if (handler != value)
Expand Down Expand Up @@ -70,6 +73,10 @@ public static bool ReportUnlessNotImplemented(Exception exception)
[DebuggerHidden]
public static bool Report(Exception exception)
{
// hold onto last exception to make investigation easier
reportedException = exception;
reportedExceptionMessage = exception.ToString();

var localHandler = handler;
if (localHandler != null)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public async Task ServeConnection()
{
return handler.HandleRequest(request, cancellationTokenSource.Token);
}
catch (Exception e) if (CompilerFatalError.ReportUnlessCanceled(e))
catch (Exception e) if (FatalError.ReportUnlessCanceled(e))
{
throw ExceptionUtilities.Unreachable;
}
Expand Down
4 changes: 2 additions & 2 deletions Src/Compilers/Core/VBCSCompiler/ServerDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static int Main(string[] args)
CompilerServerLogger.LogException(e, "Could not read AppSettings");
}

CompilerFatalError.Handler = FailFast.OnFatalException;
FatalError.Handler = FailFast.OnFatalException;

var dispatcher = new ServerDispatcher(BuildProtocolConstants.PipeName,
new CompilerRequestHandler(),
Expand Down Expand Up @@ -263,7 +263,7 @@ private async Task DispatchConnection(NamedPipeServerStream pipeStream)
}
ConnectionCompleted();
}
catch (Exception e) if (CompilerFatalError.Report(e))
catch (Exception e) if (FatalError.Report(e))
{
throw ExceptionUtilities.Unreachable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<DebugSymbols>true</DebugSymbols>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
<DefineConstants>PERFORMANCE_DIAGNOSTICS</DefineConstants>
<RemoveIntegerChecks>true</RemoveIntegerChecks>
<Optimize>false</Optimize>
<DebugType>full</DebugType>
Expand All @@ -52,7 +51,6 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<DefineTrace>true</DefineTrace>
<DefineConstants>PERFORMANCE_DIAGNOSTICS</DefineConstants>
<RemoveIntegerChecks>true</RemoveIntegerChecks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
Expand Down
2 changes: 0 additions & 2 deletions Src/Compilers/VisualBasic/Portable/BasicCodeAnalysis.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
<DebugSymbols>true</DebugSymbols>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
<DefineConstants>PERFORMANCE_DIAGNOSTICS</DefineConstants>
<RemoveIntegerChecks>true</RemoveIntegerChecks>
<Optimize>false</Optimize>
<DebugType>full</DebugType>
Expand All @@ -63,7 +62,6 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<DefineTrace>true</DefineTrace>
<DefineConstants>PERFORMANCE_DIAGNOSTICS</DefineConstants>
<RemoveIntegerChecks>true</RemoveIntegerChecks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
If Not _frozen Then
' First time we're called, get the thread id. Subsequent times, check it hasn't changed.
If _threadIdForDeclaration = -1 Then
Interlocked.CompareExchange(_threadIdForDeclaration, ThreadingUtilities.GetCurrentThreadId(), -1)
Interlocked.CompareExchange(_threadIdForDeclaration, Environment.CurrentManagedThreadId, -1)
End If

Debug.Assert(_threadIdForDeclaration = ThreadingUtilities.GetCurrentThreadId())
Debug.Assert(_threadIdForDeclaration = Environment.CurrentManagedThreadId)
End If
End Sub
#End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Sub()
Try
CompileNamespace(symbol)
Catch e As Exception When CompilerFatalError.ReportUnlessCanceled(e)
Catch e As Exception When FatalError.ReportUnlessCanceled(e)
Throw ExceptionUtilities.Unreachable
End Try
End Sub,
Expand Down Expand Up @@ -487,7 +487,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Sub()
Try
CompileNamedType(symbol, filter)
Catch e As Exception When CompilerFatalError.ReportUnlessCanceled(e)
Catch e As Exception When FatalError.ReportUnlessCanceled(e)
Throw ExceptionUtilities.Unreachable
End Try
End Sub,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols

Private Function BuildMembersAndInitializers(diagBag As DiagnosticBag) As MembersAndInitializers
#If DEBUG Then
Dim threadId = ThreadingUtilities.GetCurrentThreadId()
Dim threadId = Environment.CurrentManagedThreadId
Debug.Assert(m_computingMembersThreadId <> threadId)
Interlocked.CompareExchange(m_computingMembersThreadId, threadId, 0)
#End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Sub()
Try
visitor(symbol)
Catch e As Exception When CompilerFatalError.ReportUnlessCanceled(e)
Catch e As Exception When FatalError.ReportUnlessCanceled(e)
Throw ExceptionUtilities.Unreachable
End Try
End Sub,
Expand Down
2 changes: 1 addition & 1 deletion Src/Compilers/VisualBasic/vbc/Vbc.vb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CommandLine

Dim compiler = New Vbc(BasicResponseFileName, Directory.GetCurrentDirectory(), args)

CompilerFatalError.Handler = AddressOf FailFast.OnFatalException
FatalError.Handler = AddressOf FailFast.OnFatalException

' We store original encoding and restore it later to revert
' the changes that might be done by /utf8output options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<Project>{36755424-5267-478C-9434-37A507E22711}</Project>
<Name>FxCopRulesDiagnosticAnalyzers</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Workspaces\Core\Workspaces.csproj">
<ProjectReference Include="..\..\..\Workspaces\Core\Portable\Workspaces.csproj">
<Project>{5F8D2414-064A-4B3A-9B42-8E2A04246BE5}</Project>
<Name>Workspaces</Name>
</ProjectReference>
Expand All @@ -47,7 +47,26 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Collections" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Composition.AttributedModel, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.AttributedModel.dll</HintPath>
</Reference>
<Reference Include="System.Composition.Convention, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Convention.dll</HintPath>
</Reference>
<Reference Include="System.Composition.Hosting, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Hosting.dll</HintPath>
</Reference>
<Reference Include="System.Composition.Runtime, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Runtime.dll</HintPath>
</Reference>
<Reference Include="System.Composition.TypedParts, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.TypedParts.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand All @@ -14,7 +15,7 @@ namespace Microsoft.CodeAnalysis.CSharp.FxCopAnalyzers.Design
/// <summary>
/// CA1001: Types that own disposable fields should be disposable
/// </summary>
[ExportCodeFixProvider(CA1001DiagnosticAnalyzer.RuleId, LanguageNames.CSharp)]
[ExportCodeFixProvider(CA1001DiagnosticAnalyzer.RuleId, LanguageNames.CSharp), Shared]
public class CA1001CSharpCodeFixProvider : CA1001CodeFixProviderBase
{
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using System.Linq;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -10,7 +11,7 @@ namespace Microsoft.CodeAnalysis.CSharp.FxCopAnalyzers.Design
/// <summary>
/// CA1008: Enums should have zero value
/// </summary>
[ExportCodeFixProvider("CA1008", LanguageNames.CSharp)]
[ExportCodeFixProvider("CA1008", LanguageNames.CSharp), Shared]
public class CA1008CSharpCodeFixProvider : CA1008CodeFixProviderBase
{
internal override SyntaxNode GetFieldInitializer(IFieldSymbol field)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
Expand All @@ -14,7 +15,7 @@

namespace Microsoft.CodeAnalysis.CSharp.FxCopAnalyzers.Design
{
[ExportCodeFixProvider(StaticTypeRulesDiagnosticAnalyzer.RuleNameForExportAttribute, LanguageNames.CSharp)]
[ExportCodeFixProvider(StaticTypeRulesDiagnosticAnalyzer.RuleNameForExportAttribute, LanguageNames.CSharp), Shared]
public class CA1052CSharpCodeFixProvider : CodeFixProvider
{
public sealed override ImmutableArray<string> GetFixableDiagnosticIds()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using System.Threading;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -12,7 +13,7 @@ namespace Microsoft.CodeAnalysis.CSharp.FxCopAnalyzers.Design
/// CA1027: Mark enums with FlagsAttribute
/// CA2217: Do not mark enums with FlagsAttribute
/// </summary>
[ExportCodeFixProvider(EnumWithFlagsDiagnosticAnalyzer.RuleNameForExportAttribute, LanguageNames.CSharp)]
[ExportCodeFixProvider(EnumWithFlagsDiagnosticAnalyzer.RuleNameForExportAttribute, LanguageNames.CSharp), Shared]
public class EnumWithFlagsCSharpCodeFixProvider : EnumWithFlagsCodeFixProviderBase
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
Expand All @@ -12,7 +13,7 @@

namespace Microsoft.CodeAnalysis.CSharp.FxCopAnalyzers.Globalization
{
[ExportCodeFixProvider(CA1309DiagnosticAnalyzer.RuleId, LanguageNames.CSharp)]
[ExportCodeFixProvider(CA1309DiagnosticAnalyzer.RuleId, LanguageNames.CSharp), Shared]
public class CA1309CSharpCodeFixProvider : CA1309CodeFixProviderBase
{
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -14,7 +15,7 @@

namespace Microsoft.CodeAnalysis.CSharp.FxCopAnalyzers.Globalization
{
[ExportCodeFixProvider(PInvokeDiagnosticAnalyzer.CA2101, LanguageNames.CSharp)]
[ExportCodeFixProvider(PInvokeDiagnosticAnalyzer.CA2101, LanguageNames.CSharp), Shared]
public class CA2101CSharpCodeFixProvider : CA2101CodeFixProviderBase
{
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
Expand All @@ -12,7 +13,7 @@ namespace Microsoft.CodeAnalysis.CSharp.FxCopAnalyzers.Performance
/// <summary>
/// CA1813: Seal attribute types for improved performance. Sealing attribute types speeds up performance during reflection on custom attributes.
/// </summary>
[ExportCodeFixProvider(CA1813DiagnosticAnalyzer.RuleId, LanguageNames.CSharp)]
[ExportCodeFixProvider(CA1813DiagnosticAnalyzer.RuleId, LanguageNames.CSharp), Shared]
public class CA1813CSharpCodeFixProvider : CA1813CodeFixProviderBase
{
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -16,7 +17,7 @@ namespace Microsoft.CodeAnalysis.CSharp.FxCopAnalyzers.Usage
/// <summary>
/// CA2213: Disposable fields should be disposed
/// </summary>
[ExportCodeFixProvider(CA2213DiagnosticAnalyzer.RuleId, LanguageNames.CSharp)]
[ExportCodeFixProvider(CA2213DiagnosticAnalyzer.RuleId, LanguageNames.CSharp), Shared]
public class CA2213CSharpCodeFixProvider : CA2213CodeFixProviderBase
{
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
Expand Down

0 comments on commit e76a29a

Please sign in to comment.