示例#1
0
        internal static void VerifyArguments(Diagnostic diagnostic, Compilation compilationOpt, Func <Diagnostic, bool> isSupportedDiagnostic)
        {
            if (diagnostic is DiagnosticWithInfo)
            {
                // Compiler diagnostic, skip validations.
                return;
            }

            if (diagnostic == null)
            {
                throw new ArgumentNullException(nameof(diagnostic));
            }

            if (compilationOpt != null)
            {
                VerifyDiagnosticLocationsInCompilation(diagnostic, compilationOpt);
            }

            if (!isSupportedDiagnostic(diagnostic))
            {
                throw new ArgumentException(string.Format(CodeAnalysisResources.UnsupportedDiagnosticReported, diagnostic.Id), nameof(diagnostic));
            }

            if (!UnicodeCharacterUtilities.IsValidIdentifier(diagnostic.Id))
            {
                // Disallow invalid diagnostic IDs.
                // Note that the parsing logic in Csc/Vbc MSBuild tasks to decode command line compiler output relies on diagnostics having a valid ID.
                // See https://github.com/dotnet/roslyn/issues/4376 for details.
                throw new ArgumentException(string.Format(CodeAnalysisResources.InvalidDiagnosticIdReported, diagnostic.Id), nameof(diagnostic));
            }
        }
示例#2
0
            private void GenerateMetadataNodes(
                MetadataNode parentNode,
                string nodeName,
                OrderPreservingMultiDictionary <string, MetadataDefinition> .ValueSet definitionsWithSameName)
            {
                if (!UnicodeCharacterUtilities.IsValidIdentifier(nodeName))
                {
                    return;
                }

                var childNode = MetadataNode.Allocate(nodeName);

                _parentToChildren.Add(parentNode, childNode);

                // Add all child members
                var definitionMap = OrderPreservingMultiDictionary <string, MetadataDefinition> .GetInstance();

                try
                {
                    foreach (var definition in definitionsWithSameName)
                    {
                        LookupMetadataDefinitions(definition, definitionMap);
                    }

                    foreach (var kvp in definitionMap)
                    {
                        GenerateMetadataNodes(childNode, kvp.Key, kvp.Value);
                    }
                }
                finally
                {
                    definitionMap.Free();
                }
            }
示例#3
0
        private static void GenerateMetadataNodes(
            MetadataReader reader,
            string name,
            int parentIndex,
            OrderPreservingMultiDictionary <string, MetadataDefinition> .ValueSet definitionsWithSameName,
            List <Node> unsortedNodes)
        {
            var node      = new Node(name, parentIndex);
            var nodeIndex = unsortedNodes.Count;

            unsortedNodes.Add(node);

            // Add all child members
            var definitionMap = OrderPreservingMultiDictionary <string, MetadataDefinition> .GetInstance();

            try
            {
                foreach (var definition in definitionsWithSameName)
                {
                    LookupMetadataDefinitions(reader, definition, definitionMap);
                }

                foreach (var kvp in definitionMap)
                {
                    if (UnicodeCharacterUtilities.IsValidIdentifier(kvp.Key))
                    {
                        GenerateMetadataNodes(reader, kvp.Key, nodeIndex, kvp.Value, unsortedNodes);
                    }
                }
            }
            finally
            {
                definitionMap.Free();
            }
        }
示例#4
0
            private void GenerateMetadataNodes(
                MetadataNode parentNode,
                string nodeName,
                OrderPreservingMultiDictionary <
                    string,
                    MetadataDefinition
                    > .ValueSet definitionsWithSameName
                )
            {
                if (!UnicodeCharacterUtilities.IsValidIdentifier(nodeName))
                {
                    return;
                }

                var childNode = MetadataNode.Allocate(nodeName);

                _parentToChildren.Add(parentNode, childNode);

                // Add all child members
                var definitionMap = OrderPreservingMultiDictionary <
                    string,
                    MetadataDefinition
                    > .GetInstance();

                try
                {
                    foreach (var definition in definitionsWithSameName)
                    {
                        if (definition.Kind == MetadataDefinitionKind.Member)
                        {
                            // We need to support having multiple methods with same name but different receiver type.
                            _extensionMethodToParameterTypeInfo.Add(
                                childNode,
                                definition.ReceiverTypeInfo
                                );
                        }

                        LookupMetadataDefinitions(definition, definitionMap);
                    }

                    foreach (var(name, definitions) in definitionMap)
                    {
                        GenerateMetadataNodes(childNode, name, definitions);
                    }
                }
                finally
                {
                    definitionMap.Free();
                }
            }
示例#5
0
        /// <summary>
        /// Old VS projects had some pretty messed-up looking values for the
        /// "DefineConstants" property.  It worked fine in the IDE, because it
        /// effectively munged up the string so that it ended up being valid for
        /// the compiler.  We do the equivalent munging here now.
        ///
        /// Basically, we take the incoming string, and split it on comma/semicolon/space.
        /// Then we look at the resulting list of strings, and remove any that are
        /// illegal identifiers, and pass the remaining ones through to the compiler.
        ///
        /// Note that CSharp doesn't support assigning a value to the constants ... in
        /// other words, a constant is either defined or not defined ... it can't have
        /// an actual value.
        /// </summary>
        internal static string?GetDefineConstantsSwitch(
            string?originalDefineConstants,
            TaskLoggingHelper log
            )
        {
            if (originalDefineConstants == null)
            {
                return(null);
            }

            StringBuilder finalDefineConstants = new StringBuilder();

            // Split the incoming string on comma/semicolon/space.
            string[] allIdentifiers = originalDefineConstants.Split(new char[] { ',', ';', ' ' });

            // Loop through all the parts, and for the ones that are legal C# identifiers,
            // add them to the outgoing string.
            foreach (string singleIdentifier in allIdentifiers)
            {
                if (UnicodeCharacterUtilities.IsValidIdentifier(singleIdentifier))
                {
                    // Separate them with a semicolon if there's something already in
                    // the outgoing string.
                    if (finalDefineConstants.Length > 0)
                    {
                        finalDefineConstants.Append(";");
                    }

                    finalDefineConstants.Append(singleIdentifier);
                }
                else if (singleIdentifier.Length > 0)
                {
                    log.LogWarningWithCodeFromResources(
                        "Csc_InvalidParameterWarning",
                        "/define:",
                        singleIdentifier
                        );
                }
            }

            if (finalDefineConstants.Length > 0)
            {
                return(finalDefineConstants.ToString());
            }
            else
            {
                // We wouldn't want to pass in an empty /define: switch on the csc.exe command-line.
                return(null);
            }
        }
示例#6
0
        private static void GenerateMetadataNodes(
            MetadataReader reader,
            NamespaceDefinition globalNamespace,
            List <Node> unsortedNodes)
        {
            var definitionMap = OrderPreservingMultiDictionary <string, MetadataDefinition> .GetInstance();

            try
            {
                LookupMetadataDefinitions(reader, globalNamespace, definitionMap);

                foreach (var kvp in definitionMap)
                {
                    if (UnicodeCharacterUtilities.IsValidIdentifier(kvp.Key))
                    {
                        GenerateMetadataNodes(reader, kvp.Key, 0 /*index of root node*/, kvp.Value, unsortedNodes);
                    }
                }
            }
            finally
            {
                definitionMap.Free();
            }
        }
示例#7
0
 /// <summary>
 /// Check that the name is a valid identifier.
 /// </summary>
 public static bool IsValidIdentifier(string name)
 {
     return(UnicodeCharacterUtilities.IsValidIdentifier(name));
 }
示例#8
0
 /// <summary>
 /// Check that the name is a valid identifier.
 /// </summary>
 public static bool IsValidIdentifier([NotNullWhen(true)] string?name)
 {
     return(UnicodeCharacterUtilities.IsValidIdentifier(name));
 }