Skip to content

Commit

Permalink
Warning if /refout and /out filenames don't match
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored and jcouv committed May 11, 2017
1 parent 97ad221 commit 6996101
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5060,6 +5060,12 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_VoidAssignment" xml:space="preserve">
<value>A value of type 'void' may not be assigned.</value>
</data>
<data name="WRN_RefOutFilenameDoesNotMatchOut" xml:space="preserve">
<value>The filenames for the main output '{0}' and reference output '{1}' don't match. That is not recommended.</value>
</data>
<data name="WRN_RefOutFilenameDoesNotMatchOut_Title" xml:space="preserve">
<value>The filenames for the main output and reference output don't match. That is not recommended.</value>
</data>
<data name="WRN_Experimental" xml:space="preserve">
<value>'{0}' is for evaluation purposes only and is subject to change or removal in future updates.</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ internal enum ErrorCode

ERR_NoRefOutWhenRefOnly = 8308,
ERR_NoNetModuleOutputWhenRefOutOrRefOnly = 8309,
// Available = 8310,
WRN_RefOutFilenameDoesNotMatchOut = 8310,
ERR_BadDynamicMethodArgDefaultLiteral = 8311,
ERR_DefaultLiteralNotValid = 8312,
WRN_DefaultInSwitch = 8313,
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_TupleLiteralNameMismatch:
case ErrorCode.WRN_Experimental:
case ErrorCode.WRN_DefaultInSwitch:
case ErrorCode.WRN_RefOutFilenameDoesNotMatchOut:
return 1;
default:
return 0;
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/MessageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ public override ReportDiagnostic GetDiagnosticReport(DiagnosticInfo diagnosticIn
public override int ERR_MetadataReferencesNotSupported => (int)ErrorCode.ERR_MetadataReferencesNotSupported;
public override int ERR_LinkedNetmoduleMetadataMustProvideFullPEImage => (int)ErrorCode.ERR_LinkedNetmoduleMetadataMustProvideFullPEImage;

// refout:
public override int WRN_RefOutFilenameDoesNotMatchOut => (int)ErrorCode.WRN_RefOutFilenameDoesNotMatchOut;

public override void ReportDuplicateMetadataReferenceStrong(DiagnosticBag diagnostics, Location location, MetadataReference reference, AssemblyIdentity identity, MetadataReference equivalentReference, AssemblyIdentity equivalentIdentity)
{
diagnostics.Add(ErrorCode.ERR_DuplicateImport, location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public static bool IsWarning(ErrorCode code)
case ErrorCode.WRN_AttributeIgnoredWhenPublicSigning:
case ErrorCode.WRN_TupleLiteralNameMismatch:
case ErrorCode.WRN_Experimental:
case ErrorCode.WRN_RefOutFilenameDoesNotMatchOut:
case ErrorCode.WRN_DefaultInSwitch:
return true;
default:
Expand Down
27 changes: 27 additions & 0 deletions src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9130,6 +9130,33 @@ private static void PrivateMethod()
CleanupAllGeneratedFiles(refDir.Path);
}

[Fact]
public void RefOutWithFilenameMismatch()
{
var dir = Temp.CreateDirectory();
var refDir = dir.CreateDirectory("ref");

var src = dir.CreateFile("a.cs");
src.WriteAllText(@"
public class C
{
}");

var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var csc = new MockCSharpCompiler(null, dir.Path,
new[] { "/nologo", "/refout:ref/b.dll", "/target:library", "/deterministic", "a.cs" });

int exitCode = csc.Run(outWriter);

Assert.Equal("error CS8310: The filenames for the main output 'a.dll' and reference output 'b.dll' don't match. That is not recommended.",
outWriter.ToString().Trim());
Assert.Equal(0, exitCode);

// Clean up temp files
CleanupAllGeneratedFiles(dir.Path);
CleanupAllGeneratedFiles(refDir.Path);
}

[Fact]
public void RefOutWithError()
{
Expand Down
17 changes: 17 additions & 0 deletions src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ private int RunCore(TextWriter consoleOutput, ErrorLogger errorLogger, Cancellat
var pdbStreamProviderOpt = emitPdbFile ? new CompilerEmitStreamProvider(this, finalPdbFilePath) : null;

string finalRefPeFilePath = Arguments.OutputRefFilePath;
ReportRefFilenameMismatch(finalPeFilePath, finalRefPeFilePath, diagnosticBag);
var refPeStreamProviderOpt = finalRefPeFilePath != null ? new CompilerEmitStreamProvider(this, finalRefPeFilePath) : null;

try
Expand Down Expand Up @@ -861,6 +862,22 @@ private int RunCore(TextWriter consoleOutput, ErrorLogger errorLogger, Cancellat
return Succeeded;
}

private void ReportRefFilenameMismatch(string finalPeFilePath, string finalRefPeFilePath, DiagnosticBag diagnosticBag)
{
if (finalPeFilePath == null || finalRefPeFilePath == null)
{
return;
}

var primaryOutput = Path.GetFileName(finalPeFilePath);
var secondaryOutput = Path.GetFileName(finalRefPeFilePath);
if (!primaryOutput.Equals(secondaryOutput, StringComparison.OrdinalIgnoreCase))
{
diagnosticBag.Add(MessageProvider.CreateDiagnostic(MessageProvider.WRN_RefOutFilenameDoesNotMatchOut,
Location.None, primaryOutput, secondaryOutput));
}
}

private bool WriteTouchedFiles(TextWriter consoleOutput, TouchedFileLogger touchedFilesLogger, string finalXmlFilePath)
{
if (Arguments.TouchedFilesPath != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ public DiagnosticInfo FilterDiagnosticInfo(DiagnosticInfo diagnosticInfo, Compil
public abstract int ERR_ModuleEmitFailure { get; }
public abstract int ERR_EncUpdateFailedMissingAttribute { get; }

// Refout:
public abstract int WRN_RefOutFilenameDoesNotMatchOut { get; }

/// <summary>
/// Takes an exception produced while writing to a file stream and produces a diagnostic.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Compilers/VisualBasic/Portable/Errors/Errors.vb
Original file line number Diff line number Diff line change
Expand Up @@ -1955,9 +1955,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic

WRN_AttributeIgnoredWhenPublicSigning = 42379
WRN_Experimental = 42380
WRN_RefOutFilenameDoesNotMatchOut = 42381

' // AVAILABLE 42381 - 49998
ERRWRN_Last = WRN_Experimental
' // AVAILABLE 42382 - 49998
ERRWRN_Last = WRN_RefOutFilenameDoesNotMatchOut

'// HIDDENS AND INFOS BEGIN HERE
HDN_UnusedImportClause = 50000
Expand Down
6 changes: 6 additions & 0 deletions src/Compilers/VisualBasic/Portable/Errors/MessageProvider.vb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Get
End Property

Public Overrides ReadOnly Property WRN_RefOutFilenameDoesNotMatchOut As Integer
Get
Return ERRID.WRN_RefOutFilenameDoesNotMatchOut
End Get
End Property

Public Overrides ReadOnly Property INF_UnableToLoadSomeTypesInAnalyzer As Integer
Get
Return ERRID.INF_UnableToLoadSomeTypesInAnalyzer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
ERRID.WRN_NoAnalyzerInAssembly,
ERRID.WRN_UnableToLoadAnalyzer,
ERRID.WRN_AttributeIgnoredWhenPublicSigning,
ERRID.WRN_Experimental
ERRID.WRN_Experimental,
ERRID.WRN_RefOutFilenameDoesNotMatchOut
Return True
Case Else
Return False
Expand Down
18 changes: 18 additions & 0 deletions src/Compilers/VisualBasic/Portable/VBResources.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Compilers/VisualBasic/Portable/VBResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5492,4 +5492,10 @@
<data name="WRN_Experimental_Title" xml:space="preserve">
<value>Type is for evaluation purposes only and is subject to change or removal in future updates.</value>
</data>
<data name="WRN_RefOutFilenameDoesNotMatchOut" xml:space="preserve">
<value>The filenames for the main output '{0}' and reference output '{1}' don't match. That is not recommended.</value>
</data>
<data name="WRN_RefOutFilenameDoesNotMatchOut_Title" xml:space="preserve">
<value>The filenames for the main output and reference output don't match. That is not recommended.</value>
</data>
</root>
24 changes: 24 additions & 0 deletions src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -8252,6 +8252,30 @@ a
CleanupAllGeneratedFiles(refDir.Path)
End Sub

<Fact>
Public Sub RefOutWithFilenameMismatch()
Dim dir = Temp.CreateDirectory()
Dim refDir = dir.CreateDirectory("ref")

Dim src = dir.CreateFile("a.vb")
src.WriteAllText("
Public Class C
End Class")

Dim outWriter = New StringWriter(CultureInfo.InvariantCulture)
Dim vbc = New MockVisualBasicCompiler(Nothing, dir.Path,
{"/define:_MYTYPE=""Empty"" ", "/nologo", "/refout:ref/b.dll", "/target:library", "/deterministic", "a.vb"})

Dim exitCode = vbc.Run(outWriter)
Assert.Equal("vbc : warning BC42381: The filenames for the main output 'a.dll' and reference output 'b.dll' don't match. That is not recommended.",
outWriter.ToString().Trim())
Assert.Equal(0, exitCode)

' Clean up temp files
CleanupAllGeneratedFiles(dir.Path)
CleanupAllGeneratedFiles(refDir.Path)
End Sub

<Fact>
Public Sub RefOutWithError()
Dim dir = Temp.CreateDirectory()
Expand Down
12 changes: 10 additions & 2 deletions src/Test/Utilities/Portable/Mocks/TestMessageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@ public override int ERR_TooManyUserStrings
throw new NotImplementedException();
}
}

public override int ERR_PeWritingFailure
{
get
{
throw new NotImplementedException();
}
}

public override int ERR_ModuleEmitFailure
{
get
Expand Down Expand Up @@ -441,5 +441,13 @@ public override int ERR_BadDocumentationMode
throw new NotImplementedException();
}
}

public override int WRN_RefOutFilenameDoesNotMatchOut
{
get
{
throw new NotImplementedException();
}
}
}
}

0 comments on commit 6996101

Please sign in to comment.