コード例 #1
0
        public void LogWarning(string text, int code, MethodIL origin, int ilOffset, string subcategory = MessageSubCategory.None)
        {
            string document   = null;
            int?   lineNumber = null;

            IEnumerable <ILSequencePoint> sequencePoints = origin.GetDebugInfo()?.GetSequencePoints();

            if (sequencePoints != null)
            {
                foreach (var sequencePoint in sequencePoints)
                {
                    if (sequencePoint.Offset <= ilOffset)
                    {
                        document   = sequencePoint.Document;
                        lineNumber = sequencePoint.LineNumber;
                    }
                }
            }

            MethodDesc warnedMethod = CompilerGeneratedState.GetUserDefinedMethodForCompilerGeneratedMember(origin.OwningMethod) ?? origin.OwningMethod;

            MessageOrigin messageOrigin = new MessageOrigin(warnedMethod, document, lineNumber, null);

            LogWarning(text, code, messageOrigin, subcategory);
        }
コード例 #2
0
        public void LogWarning(MethodIL origin, int ilOffset, DiagnosticId id, params string[] args)
        {
            string document   = null;
            int?   lineNumber = null;

            IEnumerable <ILSequencePoint> sequencePoints = origin.GetDebugInfo()?.GetSequencePoints();

            if (sequencePoints != null)
            {
                foreach (var sequencePoint in sequencePoints)
                {
                    if (sequencePoint.Offset <= ilOffset)
                    {
                        document   = sequencePoint.Document;
                        lineNumber = sequencePoint.LineNumber;
                    }
                }
            }

            MethodDesc warnedMethod = CompilerGeneratedState.GetUserDefinedMethodForCompilerGeneratedMember(origin.OwningMethod) ?? origin.OwningMethod;

            MessageOrigin messageOrigin = new MessageOrigin(warnedMethod, document, lineNumber, null);

            LogWarning(messageOrigin, id, args);
        }
コード例 #3
0
 public FlowAnnotations(Logger logger, ILProvider ilProvider, CompilerGeneratedState compilerGeneratedState)
 {
     _hashtable = new TypeAnnotationsHashtable(logger, ilProvider, compilerGeneratedState);
     _logger = logger;
     ILProvider = ilProvider;
     CompilerGeneratedState = compilerGeneratedState;
 }
コード例 #4
0
        internal bool IsWarningSuppressed(int code, MessageOrigin origin)
        {
            // This is causing too much noise
            // https://github.com/dotnet/runtimelab/issues/1591
            if (code == 2110 || code == 2111 || code == 2113 || code == 2115)
            {
                return(true);
            }

            if (_suppressedWarnings.Contains(code))
            {
                return(true);
            }

            IEnumerable <CustomAttributeValue <TypeDesc> > suppressions = null;

            // TODO: Suppressions with different scopes

            if (origin.MemberDefinition is TypeDesc type)
            {
                var ecmaType = type.GetTypeDefinition() as EcmaType;
                suppressions = ecmaType?.GetDecodedCustomAttributes("System.Diagnostics.CodeAnalysis", "UnconditionalSuppressMessageAttribute");
            }

            if (origin.MemberDefinition is MethodDesc method)
            {
                method = CompilerGeneratedState.GetUserDefinedMethodForCompilerGeneratedMember(method) ?? method;

                var ecmaMethod = method.GetTypicalMethodDefinition() as EcmaMethod;
                suppressions = ecmaMethod?.GetDecodedCustomAttributes("System.Diagnostics.CodeAnalysis", "UnconditionalSuppressMessageAttribute");
            }

            if (suppressions != null)
            {
                foreach (CustomAttributeValue <TypeDesc> suppression in suppressions)
                {
                    if (suppression.FixedArguments.Length != 2 ||
                        suppression.FixedArguments[1].Value is not string warningId ||
                        warningId.Length < 6 ||
                        !warningId.StartsWith("IL") ||
                        (warningId.Length > 6 && warningId[6] != ':') ||
                        !int.TryParse(warningId.Substring(2, 4), out int suppressedCode))
                    {
                        continue;
                    }

                    if (code == suppressedCode)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
コード例 #5
0
 public Logger(
     ILogWriter writer,
     ILProvider ilProvider,
     bool isVerbose,
     IEnumerable <int> suppressedWarnings,
     bool singleWarn,
     IEnumerable <string> singleWarnEnabledModules,
     IEnumerable <string> singleWarnDisabledModules)
 {
     _logWriter = writer;
     _compilerGeneratedState = ilProvider == null ? null : new CompilerGeneratedState(ilProvider, this);
     IsVerbose                     = isVerbose;
     _suppressedWarnings           = new HashSet <int>(suppressedWarnings);
     _isSingleWarn                 = singleWarn;
     _singleWarnEnabledAssemblies  = new HashSet <string>(singleWarnEnabledModules, StringComparer.OrdinalIgnoreCase);
     _singleWarnDisabledAssemblies = new HashSet <string>(singleWarnDisabledModules, StringComparer.OrdinalIgnoreCase);
 }
コード例 #6
0
ファイル: ILCompilerDriver.cs プロジェクト: mikem8361/runtime
        public void Trim(ILCompilerOptions options, ILogWriter logWriter)
        {
            ComputeDefaultOptions(out var targetOS, out var targetArchitecture);
            var targetDetails = new TargetDetails(targetArchitecture, targetOS, TargetAbi.NativeAot);
            CompilerTypeSystemContext typeSystemContext =
                new CompilerTypeSystemContext(targetDetails, SharedGenericsMode.CanonicalReferenceTypes, DelegateFeature.All);

            typeSystemContext.InputFilePaths     = options.InputFilePaths;
            typeSystemContext.ReferenceFilePaths = options.ReferenceFilePaths;
            typeSystemContext.SetSystemModule(typeSystemContext.GetModuleForSimpleName(DefaultSystemModule));

            List <EcmaModule> inputModules = new List <EcmaModule> ();

            foreach (var inputFile in typeSystemContext.InputFilePaths)
            {
                EcmaModule module = typeSystemContext.GetModuleFromPath(inputFile.Value);
                inputModules.Add(module);
            }

            CompilationModuleGroup compilationGroup = new TestInfraMultiFileSharedCompilationModuleGroup(typeSystemContext, inputModules);

            List <ICompilationRootProvider> compilationRoots = new List <ICompilationRootProvider> ();
            EcmaModule?entrypointModule = null;

            foreach (var inputFile in typeSystemContext.InputFilePaths)
            {
                EcmaModule module = typeSystemContext.GetModuleFromPath(inputFile.Value);

                if (module.PEReader.PEHeaders.IsExe)
                {
                    if (entrypointModule != null)
                    {
                        throw new Exception("Multiple EXE modules");
                    }
                    entrypointModule = module;
                }

                compilationRoots.Add(new ExportedMethodsRootProvider(module));
            }

            compilationRoots.Add(new MainMethodRootProvider(entrypointModule, CreateInitializerList(typeSystemContext, options)));

            ILProvider ilProvider = new NativeAotILProvider();

            ilProvider = new FeatureSwitchManager(ilProvider, options.FeatureSwitches);

            Logger logger = new Logger(logWriter, ilProvider, isVerbose: true);
            CompilerGeneratedState compilerGeneratedState = new CompilerGeneratedState(ilProvider, logger);

            UsageBasedMetadataManager metadataManager = new UsageBasedMetadataManager(
                compilationGroup,
                typeSystemContext,
                new NoMetadataBlockingPolicy(),
                new ManifestResourceBlockingPolicy(options.FeatureSwitches),
                logFile: null,
                new NoStackTraceEmissionPolicy(),
                new NoDynamicInvokeThunkGenerationPolicy(),
                new FlowAnnotations(logger, ilProvider, compilerGeneratedState),
                UsageBasedMetadataGenerationOptions.ReflectionILScanning,
                logger,
                Array.Empty <KeyValuePair <string, bool> > (),
                Array.Empty <string> (),
                options.TrimAssemblies.ToArray());

            CompilationBuilder builder = new RyuJitCompilationBuilder(typeSystemContext, compilationGroup)
                                         .UseILProvider(ilProvider)
                                         .UseCompilationUnitPrefix("");

            IILScanner scanner = builder.GetILScannerBuilder()
                                 .UseCompilationRoots(compilationRoots)
                                 .UseMetadataManager(metadataManager)
                                 .UseParallelism(System.Diagnostics.Debugger.IsAttached ? 1 : -1)
                                 .ToILScanner();

            ILScanResults results = scanner.Scan();
        }
コード例 #7
0
 public DataflowAnalyzedMethodNode(MethodIL methodIL)
 {
     Debug.Assert(methodIL.OwningMethod.IsTypicalMethodDefinition);
     Debug.Assert(!CompilerGeneratedState.IsNestedFunctionOrStateMachineMember(methodIL.OwningMethod));
     _methodIL = methodIL;
 }
コード例 #8
0
        public void TestDependencyGraphInvariants(EcmaMethod method)
        {
            //
            // Scan the input method
            //

            var context = (CompilerTypeSystemContext)method.Context;
            CompilationModuleGroup compilationGroup = new SingleFileCompilationModuleGroup();

            NativeAotILProvider    ilProvider             = new NativeAotILProvider();
            CompilerGeneratedState compilerGeneratedState = new CompilerGeneratedState(ilProvider, Logger.Null);

            UsageBasedMetadataManager metadataManager = new UsageBasedMetadataManager(compilationGroup, context,
                                                                                      new FullyBlockedMetadataBlockingPolicy(), new FullyBlockedManifestResourceBlockingPolicy(),
                                                                                      null, new NoStackTraceEmissionPolicy(), new NoDynamicInvokeThunkGenerationPolicy(),
                                                                                      new ILLink.Shared.TrimAnalysis.FlowAnnotations(Logger.Null, ilProvider, compilerGeneratedState), UsageBasedMetadataGenerationOptions.None,
                                                                                      Logger.Null, Array.Empty <KeyValuePair <string, bool> >(), Array.Empty <string>(), Array.Empty <string>());

            CompilationBuilder builder = new RyuJitCompilationBuilder(context, compilationGroup)
                                         .UseILProvider(ilProvider);

            IILScanner scanner = builder.GetILScannerBuilder()
                                 .UseCompilationRoots(new ICompilationRootProvider[] { new SingleMethodRootProvider(method) })
                                 .UseMetadataManager(metadataManager)
                                 .ToILScanner();

            ILScanResults results = scanner.Scan();

            //
            // Check invariants
            //

            const string assetsNamespace       = "ILCompiler.Compiler.Tests.Assets";
            bool         foundSomethingToCheck = false;

            foreach (var attr in method.GetDecodedCustomAttributes(assetsNamespace, "GeneratesConstructedEETypeAttribute"))
            {
                foundSomethingToCheck = true;
                Assert.Contains((TypeDesc)attr.FixedArguments[0].Value, results.ConstructedEETypes);
            }

            foreach (var attr in method.GetDecodedCustomAttributes(assetsNamespace, "NoConstructedEETypeAttribute"))
            {
                foundSomethingToCheck = true;
                Assert.DoesNotContain((TypeDesc)attr.FixedArguments[0].Value, results.ConstructedEETypes);
            }

            foreach (var attr in method.GetDecodedCustomAttributes(assetsNamespace, "GeneratesMethodBodyAttribute"))
            {
                foundSomethingToCheck = true;
                MethodDesc methodToCheck = GetMethodFromAttribute(attr);
                Assert.Contains(methodToCheck.GetCanonMethodTarget(CanonicalFormKind.Specific), results.CompiledMethodBodies);
            }

            foreach (var attr in method.GetDecodedCustomAttributes(assetsNamespace, "NoMethodBodyAttribute"))
            {
                foundSomethingToCheck = true;
                MethodDesc methodToCheck = GetMethodFromAttribute(attr);
                Assert.DoesNotContain(methodToCheck.GetCanonMethodTarget(CanonicalFormKind.Specific), results.CompiledMethodBodies);
            }

            //
            // Make sure we checked something
            //

            Assert.True(foundSomethingToCheck, "No invariants to check?");
        }