Beispiel #1
0
        private AttributeSyntax TransformExplicitAttribute(AttributeSyntax node)
        {
            var location = node.GetLocation();
            var original = node.ToFullString();

            // MSTest V2 does not support "[Explicit]".
            // Convert "[Explicit]" to "[Ignore("EXPLICIT")]"
            // Convert "[Explicit("yadayada")]" to "[Ignore("EXPLICIT: yadayada")]"

            string text        = "EXPLICIT";
            var    description = node.GetPositionExpression(0);

            if (description != null)
            {
                text += ": " + description.GetFirstToken().ValueText;
            }

            var literalExpression = SyntaxFactory.LiteralExpression(
                SyntaxKind.StringLiteralExpression,
                SyntaxFactory.Literal("\"" + text + "\"", text));

            var arguments = new SeparatedSyntaxList <AttributeArgumentSyntax>();

            arguments = arguments.Add(SyntaxFactory.AttributeArgument(literalExpression));

            node = node.WithName(SyntaxFactory.IdentifierName("Ignore")).WithArgumentList(
                SyntaxFactory.AttributeArgumentList(arguments));

            m_diagnostics.Add(Diagnostic.Create(DiagnosticsDescriptors.TransformedUnsupported, location, original,
                                                node.ToFullString()));

            return(node);
        }
Beispiel #2
0
 private static int GetAttributeArgumentIntValue(AttributeSyntax attr, int index, SemanticModel semanticModel)
 {
     if (attr.ArgumentList.Arguments.Count < index + 1)
     {
         throw new ShaderGenerationException(
                   "Too few arguments in attribute " + attr.ToFullString() + ". Required + " + (index + 1));
     }
     return(GetConstantIntFromExpression(attr.ArgumentList.Arguments[index].Expression, semanticModel));
 }
Beispiel #3
0
        private static uint GetAttributeArgumentUIntValue(AttributeSyntax attr, int index)
        {
            if (attr.ArgumentList.Arguments.Count < index + 1)
            {
                throw new ShaderGenerationException(
                          "Too few arguments in attribute " + attr.ToFullString() + ". Required + " + (index + 1));
            }
            string fullArg0 = attr.ArgumentList.Arguments[index].ToFullString();

            if (uint.TryParse(fullArg0, out uint ret))
            {
                return(ret);
            }
            else
            {
                throw new ShaderGenerationException("Incorrectly formatted attribute: " + attr.ToFullString());
            }
        }
        public static AttributeSyntax WithoutArgumentList(
            this AttributeSyntax node, List <Diagnostic> diagnostics = null, Location location = null,
            params string[] ignore)
        {
            if (node.ArgumentList?.Arguments.Count > 0)
            {
                int count = node.ArgumentList.Arguments.Count(a => ignore.All(i => i != a.NameEquals?.Name?.ToFullString()?.Trim()));
                if (count > 0)
                {
                    diagnostics?.Add(Diagnostic.Create(DiagnosticsDescriptors.IgnoredAllArguments,
                                                       node.GetLocation(location), node.ToFullString()));
                }

                node = node.WithArgumentList(null);
            }

            return(node);
        }
Beispiel #5
0
        private static SemanticType GetSemanticType(VariableDeclaratorSyntax vds)
        {
            AttributeSyntax[] attrs = Utilities.GetMemberAttributes(vds, "VertexSemantic");
            if (attrs.Length == 1)
            {
                AttributeSyntax semanticTypeAttr = attrs[0];
                string          fullArg0         = semanticTypeAttr.ArgumentList.Arguments[0].ToFullString();
                if (fullArg0.Contains("."))
                {
                    fullArg0 = fullArg0.Substring(fullArg0.LastIndexOf('.') + 1);
                }
                if (Enum.TryParse(fullArg0, out SemanticType ret))
                {
                    return(ret);
                }
                else
                {
                    throw new ShaderGenerationException("Incorrectly formatted attribute: " + semanticTypeAttr.ToFullString());
                }
            }
            else if (attrs.Length > 1)
            {
                throw new ShaderGenerationException("Too many vertex semantics applied to field: " + vds.ToFullString());
            }

            if (CheckSingleAttribute(vds, "SystemPositionSemantic"))
            {
                return(SemanticType.SystemPosition);
            }
            else if (CheckSingleAttribute(vds, "PositionSemantic"))
            {
                return(SemanticType.Position);
            }
            else if (CheckSingleAttribute(vds, "NormalSemantic"))
            {
                return(SemanticType.Normal);
            }
            else if (CheckSingleAttribute(vds, "TextureCoordinateSemantic"))
            {
                return(SemanticType.TextureCoordinate);
            }
            else if (CheckSingleAttribute(vds, "ColorSemantic"))
            {
                return(SemanticType.Color);
            }
            else if (CheckSingleAttribute(vds, "TangentSemantic"))
            {
                return(SemanticType.Tangent);
            }
            else if (CheckSingleAttribute(vds, "ColorTargetSemantic"))
            {
                return(SemanticType.ColorTarget);
            }

            return(SemanticType.None);
        }
Beispiel #6
0
        private SyntaxNode HandleAttribute(AttributeSyntax node)
        {
            var existing = m_semanticModel.GetSymbolInfo(node.Name);

            string existingTypeName = existing.Symbol?.ContainingType?.ToDisplayString();
            var    location         = node.GetLocation();
            var    originalNode     = node;

            try
            {
                switch (existingTypeName)
                {
                case "NUnit.Framework.SetUpAttribute":
                    WarnIfNotSuitableForTestInitialize("TestInitialize", location, true);
                    node = node.WithName(SyntaxFactory.IdentifierName("TestInitialize"));
                    break;

                case "NUnit.Framework.TearDownAttribute":
                    WarnIfNotSuitableForTestInitialize("TestCleanup", location, false);
                    node = node.WithName(SyntaxFactory.IdentifierName("TestCleanup"));
                    break;

                case "NUnit.Framework.OneTimeSetUpAttribute":
                    WarnIfNotSuitableForClassInitialize("ClassInitialize", location, true);
                    node = node.WithName(SyntaxFactory.IdentifierName("ClassInitialize"));
                    break;

                case "NUnit.Framework.OneTimeTearDownAttribute":
                    WarnIfNotSuitableForClassInitialize("ClassCleanup", location, false);
                    node = node.WithName(SyntaxFactory.IdentifierName("ClassCleanup"));
                    break;

                case "NUnit.Framework.PropertyAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("TestProperty"))
                           .ConvertArgumentsToString(m_diagnostics, location);
                    break;

                case "NUnit.Framework.TimeoutAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("Timeout"))
                           .WithoutArgumentList(m_diagnostics, location);
                    Changed = true;
                    break;

                case "NUnit.Framework.TestFixtureAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("TestClass"))
                           .WithoutArgumentList(m_diagnostics, location);
                    Changed = true;
                    break;

                case "NUnit.Framework.TestCaseAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("DataRow"))
                           .RenameNameEquals("TestName", "DisplayName");
                    m_perMethodState.DataRowSeen = true;
                    Changed = true;
                    break;

                case "NUnit.Framework.TestCaseSourceAttribute":
                    node    = TransformTestCaseSourceAttribute(node);
                    Changed = true;
                    break;

                case "NUnit.Framework.TestAttribute":
                    m_perMethodState.Description = node.GetNameEqualsExpression("Description");
                    node = node.WithName(SyntaxFactory.IdentifierName("TestMethod"))
                           .WithoutArgumentList(m_diagnostics, location, "Description");
                    Changed = true;
                    break;

                case "NUnit.Framework.CategoryAttribute":
                    node    = node.WithName(SyntaxFactory.IdentifierName("TestCategory"));
                    Changed = true;
                    break;

                case "NUnit.Framework.ExplicitAttribute":
                    node    = TransformExplicitAttribute(node);
                    Changed = true;
                    break;

                case "NUnit.Framework.IgnoreAttribute":
                    node = node.WithName(SyntaxFactory.IdentifierName("Ignore"))
                           .WithoutNameEquals("Until", m_diagnostics, location);
                    Changed = true;
                    break;

                case "NUnit.Framework.DescriptionAttribute"
                    // With MSTest DescriptionAttribute only supported on methods
                    when node.GetParentKind() == SyntaxKind.MethodDeclaration:
                    node = node.WithName(SyntaxFactory.IdentifierName("Description"));

                    Changed = true;
                    break;

                default:
                {
                    if (existingTypeName != null && existingTypeName.StartsWith("NUnit."))
                    {
                        // Replace (potential) unqualified name with qualified name.
                        // Otherwise, an attribute whose unqualified name is accidentally the same
                        // as that of some other, unrelated, attribute could semantically change (since we
                        // replace the "using NUnit.Framework" with "using <MSTest>").
                        var fullQualifiedName = SyntaxFactory.ParseName(existingTypeName);
                        m_diagnostics.Add(Diagnostic.Create(DiagnosticsDescriptors.UnsupportedAttribute, location,
                                                            node.ToFullString()));
                        node    = node.WithName(fullQualifiedName);
                        Changed = true;
                    }

                    break;
                }
                }

                return(node);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Failed to process '{originalNode}' [{location}]: {ex.Message}", ex);
            }
        }
Beispiel #7
0
        private static int GetAttributeFirstArgumentIntValue(AttributeSyntax attr)
        {
            string fullArg0 = attr.ArgumentList.Arguments[0].ToFullString();

            if (int.TryParse(fullArg0, out int ret))
            {
                return(ret);
            }
            else
            {
                throw new ShaderGenerationException("Incorrectly formatted attribute: " + attr.ToFullString());
            }
        }
        private static int GetArrayCountValue(VariableDeclaratorSyntax vds)
        {
            AttributeSyntax[] arraySizeAttrs = Utilities.GetMemberAttributes(vds, "ArraySize");
            if (arraySizeAttrs.Length != 1)
            {
                throw new ShaderGenerationException(
                          "Array fields in structs must have a constant size specified by an ArraySizeAttribute.");
            }
            AttributeSyntax arraySizeAttr = arraySizeAttrs[0];
            string          fullArg0      = arraySizeAttr.ArgumentList.Arguments[0].ToFullString();

            if (int.TryParse(fullArg0, out int ret))
            {
                return(ret);
            }
            else
            {
                throw new ShaderGenerationException("Incorrectly formatted attribute: " + arraySizeAttr.ToFullString());
            }
        }
Beispiel #9
0
        private static GeometrySemantic GetGeometrySemantic(VariableDeclaratorSyntax vds)
        {
            AttributeSyntax[] attrs = Utilities.GetMemberAttributes(vds, "GeometrySemantic");
            if (attrs.Length == 1)
            {
                AttributeSyntax semanticTypeAttr = attrs[0];
                string          fullArg0         = semanticTypeAttr.ArgumentList.Arguments[0].ToFullString();
                if (fullArg0.Contains("."))
                {
                    fullArg0 = fullArg0.Substring(fullArg0.LastIndexOf('.') + 1);
                }
                if (Enum.TryParse(fullArg0, out GeometrySemantic ret))
                {
                    return(ret);
                }
                else
                {
                    throw new ShaderGenerationException("Incorrectly formatted attribute: " + semanticTypeAttr.ToFullString());
                }
            }
            else if (attrs.Length > 1)
            {
                throw new ShaderGenerationException("Too many geometry semantics applied to field: " + vds.ToFullString());
            }

            return(GeometrySemantic.None);
        }