Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CommonCompiler.RunCore #23059

Merged
merged 5 commits into from
Nov 13, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ protected override void ResolveEmbeddedFilesFromExternalSourceDirectives(
SyntaxTree tree,
SourceReferenceResolver resolver,
OrderedSet<string> embeddedFiles,
IList<Diagnostic> diagnostics)
DiagnosticBag diagnostics)
{
foreach (LineDirectiveTriviaSyntax directive in tree.GetRoot().GetDirectives(
d => d.IsActive && !d.HasErrors && d.Kind() == SyntaxKind.LineDirectiveTrivia))
Expand Down
11 changes: 8 additions & 3 deletions src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2075,6 +2075,13 @@ internal MergedNamespaceDeclaration MergedRootDeclaration
}

internal ImmutableArray<Diagnostic> GetDiagnostics(CompilationStage stage, bool includeEarlierStages, CancellationToken cancellationToken)
{
var diagnostics = DiagnosticBag.GetInstance();
GetDiagnostics(stage, includeEarlierStages, diagnostics, cancellationToken);
return diagnostics.ToReadOnlyAndFree();
}

internal override void GetDiagnostics(CompilationStage stage, bool includeEarlierStages, DiagnosticBag diagnostics, CancellationToken cancellationToken = default)
{
var builder = DiagnosticBag.GetInstance();

Expand Down Expand Up @@ -2153,9 +2160,7 @@ internal ImmutableArray<Diagnostic> GetDiagnostics(CompilationStage stage, bool

// Before returning diagnostics, we filter warnings
// to honor the compiler options (e.g., /nowarn, /warnaserror and /warn) and the pragmas.
var result = DiagnosticBag.GetInstance();
FilterAndAppendAndFreeDiagnostics(result, ref builder);
return result.ToReadOnlyAndFree<Diagnostic>();
FilterAndAppendAndFreeDiagnostics(diagnostics, ref builder);
}

private static void AppendLoadDirectiveDiagnostics(DiagnosticBag builder, SyntaxAndDeclarationManager syntaxAndDeclarations, SyntaxTree syntaxTree, Func<IEnumerable<Diagnostic>, IEnumerable<Diagnostic>> locationFilterOpt = null)
Expand Down
581 changes: 314 additions & 267 deletions src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Compilers/Core/Portable/Compilation/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,8 @@ protected abstract INamedTypeSymbol CommonCreateAnonymousTypeSymbol(
/// </summary>
public abstract ImmutableArray<Diagnostic> GetDiagnostics(CancellationToken cancellationToken = default(CancellationToken));

internal abstract void GetDiagnostics(CompilationStage stage, bool includeEarlierStages, DiagnosticBag diagnostics, CancellationToken cancellationToken = default);

internal void EnsureCompilationEventQueueCompleted()
{
Debug.Assert(EventQueue != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,9 @@ public DiagnosticInfo FilterDiagnosticInfo(DiagnosticInfo diagnosticInfo, Compil
/// <summary>
/// Takes an exception produced while writing to a file stream and produces a diagnostic.
/// </summary>
public void ReportStreamWriteException(Exception e, string filePath, TextWriter consoleOutput)
public void ReportStreamWriteException(Exception e, string filePath, DiagnosticBag diagnostics)
{
if (consoleOutput != null)
{
var diagnostic = new DiagnosticInfo(this, ERR_OutputWriteFailed, filePath, e.Message);
consoleOutput.WriteLine(diagnostic.ToString(consoleOutput.FormatProvider));
}
diagnostics.Add(CreateDiagnostic(ERR_OutputWriteFailed, Location.None, filePath, e.Message));
}

public abstract void ReportInvalidAttributeArgument(DiagnosticBag diagnostics, SyntaxNode attributeSyntax, int parameterIndex, AttributeData attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class NoThrowStreamDisposer : IDisposable
{
private bool? _failed; // Nullable to assert that this is only checked after dispose
private readonly string _filePath;
private readonly TextWriter _writer;
private readonly DiagnosticBag _diagnostics;
private readonly CommonMessageProvider _messageProvider;

/// <summary>
Expand All @@ -40,13 +40,13 @@ public bool HasFailedToDispose
public NoThrowStreamDisposer(
Stream stream,
string filePath,
TextWriter writer,
DiagnosticBag diagnostics,
CommonMessageProvider messageProvider)
{
Stream = stream;
_failed = null;
_filePath = filePath;
_writer = writer;
_diagnostics = diagnostics;
_messageProvider = messageProvider;
}

Expand All @@ -63,7 +63,7 @@ public void Dispose()
}
catch (Exception e)
{
_messageProvider.ReportStreamWriteException(e, _filePath, _writer);
_messageProvider.ReportStreamWriteException(e, _filePath, _diagnostics);
// Record if any exceptions are thrown during dispose
_failed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
tree As SyntaxTree,
resolver As SourceReferenceResolver,
embeddedFiles As OrderedSet(Of String),
diagnostics As IList(Of Diagnostic))
diagnostics As DiagnosticBag)

For Each directive As ExternalSourceDirectiveTriviaSyntax In tree.GetRoot().GetDirectives(
Function(d) d.Kind() = SyntaxKind.ExternalSourceDirectiveTrivia)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
''' should sort the errors as desired.
''' </returns>
Friend Overloads Function GetDiagnostics(stage As CompilationStage, Optional includeEarlierStages As Boolean = True, Optional cancellationToken As CancellationToken = Nothing) As ImmutableArray(Of Diagnostic)
Dim diagnostics = DiagnosticBag.GetInstance()
GetDiagnostics(stage, includeEarlierStages, diagnostics, cancellationToken)
Return diagnostics.ToReadOnlyAndFree()
End Function

Friend Overrides Sub GetDiagnostics(stage As CompilationStage,
includeEarlierStages As Boolean,
diagnostics As DiagnosticBag,
Optional cancellationToken As CancellationToken = Nothing)

Dim builder = DiagnosticBag.GetInstance()

' Add all parsing errors.
Expand Down Expand Up @@ -1971,10 +1981,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic

' Before returning diagnostics, we filter some of them
' to honor the compiler options (e.g., /nowarn and /warnaserror)
Dim result = DiagnosticBag.GetInstance()
FilterAndAppendAndFreeDiagnostics(result, builder)
Return result.ToReadOnlyAndFree(Of Diagnostic)()
End Function
FilterAndAppendAndFreeDiagnostics(diagnostics, builder)
End Sub

Private Function GetClsComplianceDiagnostics(cancellationToken As CancellationToken, Optional filterTree As SyntaxTree = Nothing, Optional filterSpanWithinTree As TextSpan? = Nothing) As ImmutableArray(Of Diagnostic)
If filterTree IsNot Nothing Then
Expand Down Expand Up @@ -2161,7 +2169,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
additionalTypes As ImmutableArray(Of NamedTypeSymbol),
cancellationToken As CancellationToken) As CommonPEModuleBuilder

Debug.Assert(diagnostics.IsEmptyWithoutResolution) ' True, but not required.
Debug.Assert(Not IsSubmission OrElse HasCodeToEmit())

' Get the runtime metadata version from the cor library. If this fails we have no reasonable value to give.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8577,7 +8577,7 @@ End Module

Dim outWriter = New StringWriter(CultureInfo.InvariantCulture)
Assert.Equal(1, csc.Run(outWriter))
Assert.Equal($"error BC2012: can't open '{xmlPath}' for writing: Fake IOException{Environment.NewLine}", outWriter.ToString())
Assert.Equal($"vbc : error BC2012: can't open '{xmlPath}' for writing: Fake IOException{Environment.NewLine}", outWriter.ToString())
End Sub

<Theory>
Expand Down Expand Up @@ -8605,7 +8605,7 @@ End Module

Dim outWriter = New StringWriter(CultureInfo.InvariantCulture)
Assert.Equal(1, csc.Run(outWriter))
Assert.Equal($"error BC2012: can't open '{sourceLinkPath}' for writing: Fake IOException{Environment.NewLine}", outWriter.ToString())
Assert.Equal($"vbc : error BC2012: can't open '{sourceLinkPath}' for writing: Fake IOException{Environment.NewLine}", outWriter.ToString())
End Sub

<Fact>
Expand Down