コード例 #1
0
        public void TestPointerLocation()
        {
            var source  = @"
unsafe public struct S
{ 
    public S* F;
}
";
            var tree    = Parse(source, "file.cs");
            var options = OptionsDll.WithAllowUnsafe(true);

            var libRef = CreateCompilationWithMscorlib(tree, assemblyName: "Metadata", compOptions: options).EmitToImageReference();
            var comp   = CreateCompilationWithMscorlib(tree, new[] { libRef }, assemblyName: "Source", compOptions: options);

            var sourceAssembly     = comp.SourceAssembly;
            var referencedAssembly = (AssemblySymbol)comp.GetAssemblyOrModuleSymbol(libRef);

            var sourceType     = sourceAssembly.GlobalNamespace.GetMember <NamedTypeSymbol>("S").GetMember <FieldSymbol>("F").Type;
            var referencedType = referencedAssembly.GlobalNamespace.GetMember <NamedTypeSymbol>("S").GetMember <FieldSymbol>("F").Type;
            var distinguisher  = new SymbolDistinguisher(comp, sourceType, referencedType);

            // NOTE: Locations come from element types.
            Assert.Equal("S* [file.cs(2)]", distinguisher.First.ToString());
            Assert.Equal("S* [Metadata, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]", distinguisher.Second.ToString());
        }
コード例 #2
0
        public void TestWinMdExpData_AnonymousTypes()
        {
            #region "Source"
            var text = @"
namespace X
{ 
	public sealed class TestCase
	{
		public void M() 
		{ 
			var a = new { x = 1, y = new { a = 1 } };
			var b = new { t = new { t = new { t = new { t = new { a = 1 } } } } };
		}
	}
}";
            #endregion

            string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<token-map>
  <token-location token=""0x02xxxxxx"" file=""source.cs"" start-line=""4"" start-column=""22"" end-line=""4"" end-column=""30"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""4"" start-column=""22"" end-line=""4"" end-column=""30"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""6"" start-column=""15"" end-line=""6"" end-column=""16"" />
</token-map>";

            var compilation = CreateCompilationWithMscorlib45(
                text,
                compOptions: OptionsDll.WithOutputKind(OutputKind.WindowsRuntimeMetadata),
                sourceFileName: "source.cs").VerifyDiagnostics();

            string actual = GetTokenToLocationMap(compilation, true);
            AssertXmlEqual(expected, actual);
        }
コード例 #3
0
        void TestGetEffectiveDiagnosticsGlobal()
        {
            var noneDiagDesciptor    = new DiagnosticDescriptor("XX0001", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Hidden, isEnabledByDefault: true);
            var infoDiagDesciptor    = new DiagnosticDescriptor("XX0002", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Info, isEnabledByDefault: true);
            var warningDiagDesciptor = new DiagnosticDescriptor("XX0003", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Warning, isEnabledByDefault: true);
            var errorDiagDesciptor   = new DiagnosticDescriptor("XX0004", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Error, isEnabledByDefault: true);

            var noneDiag    = Microsoft.CodeAnalysis.Diagnostic.Create(noneDiagDesciptor, Location.None);
            var infoDiag    = Microsoft.CodeAnalysis.Diagnostic.Create(infoDiagDesciptor, Location.None);
            var warningDiag = Microsoft.CodeAnalysis.Diagnostic.Create(warningDiagDesciptor, Location.None);
            var errorDiag   = Microsoft.CodeAnalysis.Diagnostic.Create(errorDiagDesciptor, Location.None);

            var diags = new [] { noneDiag, infoDiag, warningDiag, errorDiag };

            var options        = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Default);
            var comp           = CreateCompilationWithMscorlib45("", compOptions: options);
            var effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray();

            Assert.Equal(4, effectiveDiags.Length);

            options        = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Error);
            comp           = CreateCompilationWithMscorlib45("", compOptions: options);
            effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray();
            Assert.Equal(4, effectiveDiags.Length);
            Assert.Equal(1, effectiveDiags.Count(d => d.IsWarningAsError));

            options        = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Warn);
            comp           = CreateCompilationWithMscorlib45("", compOptions: options);
            effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray();
            Assert.Equal(4, effectiveDiags.Length);
            Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Error));
            Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Warning));

            options        = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Info);
            comp           = CreateCompilationWithMscorlib45("", compOptions: options);
            effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray();
            Assert.Equal(4, effectiveDiags.Length);
            Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Error));
            Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Info));

            options        = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Hidden);
            comp           = CreateCompilationWithMscorlib45("", compOptions: options);
            effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray();
            Assert.Equal(4, effectiveDiags.Length);
            Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Error));
            Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Hidden));

            options        = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Suppress);
            comp           = CreateCompilationWithMscorlib45("", compOptions: options);
            effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray();
            Assert.Equal(2, effectiveDiags.Length);
            Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Error));
            Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Hidden));
        }
コード例 #4
0
        public void TestWinMdExpData_Empty()
        {
            #region "Source"
            var text = @"";
            #endregion

            string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<token-map>
</token-map>";

            var compilation = CreateCompilationWithMscorlib45(
                text,
                compOptions: OptionsDll.WithOutputKind(OutputKind.WindowsRuntimeMetadata),
                sourceFileName: "source.cs").VerifyDiagnostics();

            string actual = GetTokenToLocationMap(compilation, true);
            AssertXmlEqual(expected, actual);
        }
コード例 #5
0
        public void Bug693206()
        {
            #region "Source"
            var text = @"
namespace X
{ 
	class DynamicMembers
	{
        enum HRESULT : int
        {
            S_OK = 0x0000,
            S_FALSE = 0x0001,
            S_PT_NO_CONFLICT = 0x40001,
            E_INVALID_DATA = unchecked((int)0x8007000D),
            E_INVALIDARG = unchecked((int)0x80070057),
            E_OUTOFMEMORY = unchecked((int)0x8007000E),
            ERROR_NOT_FOUND = unchecked((int)0x80070490)
        }
    }
}";
            #endregion

            string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<token-map>
  <token-location token=""0x02xxxxxx"" file=""source.cs"" start-line=""4"" start-column=""8"" end-line=""4"" end-column=""22"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""4"" start-column=""8"" end-line=""4"" end-column=""22"" />
  <token-location token=""0x02xxxxxx"" file=""source.cs"" start-line=""6"" start-column=""14"" end-line=""6"" end-column=""21"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""8"" start-column=""13"" end-line=""8"" end-column=""17"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""9"" start-column=""13"" end-line=""9"" end-column=""20"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""10"" start-column=""13"" end-line=""10"" end-column=""29"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""11"" start-column=""13"" end-line=""11"" end-column=""27"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""12"" start-column=""13"" end-line=""12"" end-column=""25"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""13"" start-column=""13"" end-line=""13"" end-column=""26"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""14"" start-column=""13"" end-line=""14"" end-column=""28"" />
</token-map>";

            var compilation = CreateCompilationWithMscorlib45(
                text,
                compOptions: OptionsDll.WithOutputKind(OutputKind.WindowsRuntimeMetadata),
                sourceFileName: "source.cs").VerifyDiagnostics();

            string actual = GetTokenToLocationMap(compilation, true);
            AssertXmlEqual(expected, actual);
        }
コード例 #6
0
ファイル: NameLengthTests.cs プロジェクト: SkightTeam/roslyn
        public void ConstantLocals()
        {
            var sourceTemplate = @"
class C
{{
    int M() 
    {{
        const int {0} = 1;
        const int {0}1 = 1;
        return {0} + {0}1;
    }}
}}
";

            var source = string.Format(sourceTemplate, LongLocalName);
            var comp   = CreateCompilationWithMscorlib(source, compOptions: OptionsDll.WithDebugInformationKind(DebugInformationKind.Full));

            comp.VerifyDiagnostics();
            comp.VerifyEmitDiagnostics(
                // (7,19): warning CS8029: Local name 'LongSymbolName + 1' is too long for PDB.  Consider shortening or compiling without /debug.
                //         const int LongSymbolName + 1 = 1;
                Diagnostic(ErrorCode.WRN_PdbLocalNameTooLong, LongLocalName + 1).WithArguments(LongLocalName + 1).WithLocation(7, 19));
        }
コード例 #7
0
        public void TestWinMdExpData_Basic()
        {
            #region "Source"
            var text = @"using System;
using System.Threading;
using System.Threading.Tasks;

namespace X
{ 
	class DynamicMembers
	{
		public Func<Task<int>> Prop { get; set; }
	}
	public sealed class TestCase
	{
		private static int Count = 0;
		public async void Run()
		{
			DynamicMembers dc2 = new DynamicMembers();
			dc2.Prop = async () => { await Task.Delay(10000); return 3; };
			var rez2 = await dc2.Prop();
			if (rez2 == 3) Count++;
	 
			Driver.Result = TestCase.Count - 1;
			//When test complete, set the flag.
			Driver.CompletedSignal.Set();
		}
	}
	class Driver
	{
		public static int Result = -1;
		public static AutoResetEvent CompletedSignal = new AutoResetEvent(false);
		static int Main()
		{
			var t = new TestCase();
			t.Run();
	 
			CompletedSignal.WaitOne();
			return Driver.Result;
		}
	}
}";
            #endregion

            string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<token-map>
  <token-location token=""0x02xxxxxx"" file=""source.cs"" start-line=""7"" start-column=""8"" end-line=""7"" end-column=""22"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""7"" start-column=""8"" end-line=""7"" end-column=""22"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""9"" start-column=""26"" end-line=""9"" end-column=""30"" />
  <token-location token=""0x17xxxxxx"" file=""source.cs"" start-line=""9"" start-column=""26"" end-line=""9"" end-column=""30"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""9"" start-column=""33"" end-line=""9"" end-column=""36"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""9"" start-column=""38"" end-line=""9"" end-column=""41"" />
  <token-location token=""0x02xxxxxx"" file=""source.cs"" start-line=""11"" start-column=""22"" end-line=""11"" end-column=""30"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""11"" start-column=""22"" end-line=""11"" end-column=""30"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""11"" start-column=""22"" end-line=""11"" end-column=""30"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""13"" start-column=""22"" end-line=""13"" end-column=""27"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""14"" start-column=""21"" end-line=""14"" end-column=""24"" />
  <token-location token=""0x02xxxxxx"" file=""source.cs"" start-line=""26"" start-column=""8"" end-line=""26"" end-column=""14"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""26"" start-column=""8"" end-line=""26"" end-column=""14"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""26"" start-column=""8"" end-line=""26"" end-column=""14"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""28"" start-column=""21"" end-line=""28"" end-column=""27"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""29"" start-column=""32"" end-line=""29"" end-column=""47"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""30"" start-column=""14"" end-line=""30"" end-column=""18"" />
</token-map>";

            var compilation = CreateCompilationWithMscorlib45(
                text,
                compOptions: OptionsDll.WithOutputKind(OutputKind.WindowsRuntimeMetadata),
                sourceFileName: "source.cs").VerifyDiagnostics();

            string actual = GetTokenToLocationMap(compilation, true);
            AssertXmlEqual(expected, actual);
        }
コード例 #8
0
        public void TestWinMdExpData_Property_Event()
        {
            #region "Source"
            var text = @"
using System;

namespace X
{ 
	public delegate void D(int k);

	public sealed class TestCase
	{
		static TestCase()
		{
		}

		public TestCase(int rr)
		{
		}
		
		public event D E;
		
		public event Action E2;
		
		public int P { get; set; }
		
		public int P2 
		{ 
			get{ return 1; }
			set{}
		}
		
		public int this[int a]
		{
			get{ return 1; }
			set{}
		}
	}
}";
            #endregion

            string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<token-map>
  <token-location token=""0x02xxxxxx"" file=""source.cs"" start-line=""6"" start-column=""23"" end-line=""6"" end-column=""24"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""6"" start-column=""23"" end-line=""6"" end-column=""24"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""6"" start-column=""23"" end-line=""6"" end-column=""24"" />
  <token-location token=""0x02xxxxxx"" file=""source.cs"" start-line=""8"" start-column=""22"" end-line=""8"" end-column=""30"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""10"" start-column=""10"" end-line=""10"" end-column=""18"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""14"" start-column=""10"" end-line=""14"" end-column=""18"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""18"" start-column=""18"" end-line=""18"" end-column=""19"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""18"" start-column=""18"" end-line=""18"" end-column=""19"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""18"" start-column=""18"" end-line=""18"" end-column=""19"" />
  <token-location token=""0x14xxxxxx"" file=""source.cs"" start-line=""18"" start-column=""18"" end-line=""18"" end-column=""19"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""20"" start-column=""23"" end-line=""20"" end-column=""25"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""20"" start-column=""23"" end-line=""20"" end-column=""25"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""20"" start-column=""23"" end-line=""20"" end-column=""25"" />
  <token-location token=""0x14xxxxxx"" file=""source.cs"" start-line=""20"" start-column=""23"" end-line=""20"" end-column=""25"" />
  <token-location token=""0x04xxxxxx"" file=""source.cs"" start-line=""22"" start-column=""14"" end-line=""22"" end-column=""15"" />
  <token-location token=""0x17xxxxxx"" file=""source.cs"" start-line=""22"" start-column=""14"" end-line=""22"" end-column=""15"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""22"" start-column=""18"" end-line=""22"" end-column=""21"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""22"" start-column=""23"" end-line=""22"" end-column=""26"" />
  <token-location token=""0x17xxxxxx"" file=""source.cs"" start-line=""24"" start-column=""14"" end-line=""24"" end-column=""16"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""26"" start-column=""4"" end-line=""26"" end-column=""7"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""27"" start-column=""4"" end-line=""27"" end-column=""7"" />
  <token-location token=""0x17xxxxxx"" file=""source.cs"" start-line=""30"" start-column=""14"" end-line=""30"" end-column=""18"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""32"" start-column=""4"" end-line=""32"" end-column=""7"" />
  <token-location token=""0x06xxxxxx"" file=""source.cs"" start-line=""33"" start-column=""4"" end-line=""33"" end-column=""7"" />
</token-map>";

            var compilation = CreateCompilationWithMscorlib45(
                text,
                compOptions: OptionsDll.WithOutputKind(OutputKind.WindowsRuntimeMetadata),
                sourceFileName: "source.cs").VerifyDiagnostics(
                Diagnostic(ErrorCode.WRN_UnreferencedEvent, "E").WithArguments("X.TestCase.E"),
                Diagnostic(ErrorCode.WRN_UnreferencedEvent, "E2").WithArguments("X.TestCase.E2"));

            string actual = GetTokenToLocationMap(compilation, true);
            AssertXmlEqual(expected, actual);
        }
コード例 #9
0
        public void CS0204_ERR_TooManyLocals()
        {
            var builder = new System.Text.StringBuilder();

            builder.Append(@"
public class A
{
    public static int Main ()
        {
");
            for (int i = 0; i < 65536; i++)
            {
                builder.AppendLine(string.Format("    int i{0} = {0};", i));
            }

            builder.Append(@"
        return 1;
        }
}
");

            //Compiling this with optimizations enabled causes the stack scheduler to eliminate a bunch of these locals.
            //It could eliminate 'em all, but doesn't.
            var warnOpts = new System.Collections.Generic.Dictionary <string, ReportDiagnostic>();

            warnOpts.Add(MessageProvider.Instance.GetIdForErrorCode((int)ErrorCode.WRN_UnreferencedVarAssg), ReportDiagnostic.Suppress);
            var compilation1 = CreateCompilationWithMscorlib(builder.ToString(), null, OptionsDll.WithSpecificDiagnosticOptions(warnOpts).WithOptimizations(false));

            compilation1.VerifyEmitDiagnostics(
                // (4,23): error CS0204: Only 65534 locals, including those generated by the compiler, are allowed
                //     public static int Main ()
                Diagnostic(ErrorCode.ERR_TooManyLocals, "Main"));
        }
コード例 #10
0
ファイル: NameLengthTests.cs プロジェクト: SkightTeam/roslyn
        public void TestFixedSizeBuffers()
        {
            var    sourceTemplate = @"
unsafe struct S
{{
    fixed int {0}[1];
    fixed int {0}1[1];
}}
";
            int    padding        = GeneratedNames.MakeFixedFieldImplementationName("A").Length - 1;
            string longName       = LongSymbolName.Substring(padding);
            var    source         = string.Format(sourceTemplate, longName);
            var    comp           = CreateCompilationWithMscorlib(source, compOptions: OptionsDll.WithAllowUnsafe(true));

            comp.VerifyDiagnostics();
            // CONSIDER: Location would light up if synthesized methods had them.
            comp.VerifyEmitDiagnostics(
                // error CS7013: Name '<longName1>e__FixedBuffer' exceeds the maximum length allowed in metadata.
                Diagnostic(ErrorCode.ERR_MetadataNameTooLong).WithArguments("<" + longName + 1 + ">e__FixedBuffer").WithLocation(1, 1));
        }