public void ShouldNotReportErrorFromDecorationEvaluationWhenIsDecorated()
        {
            //GIVEN
            var declaration = new MethodDeclarationInfo(Any.String(), new List <string> {
                Any.String()
            });
            var violationFactory = Substitute.For <IProjectScopedRuleViolationFactory>();
            var report           = Substitute.For <IAnalysisReportInProgress>();
            var parentClassName  = Any.String();
            var description      = Any.Instance <RuleDescription>();
            var violation        = Any.Instance <RuleViolation>();

            var cSharpMethod = new CSharpMethod(declaration, violationFactory);

            violationFactory.ProjectScopedRuleViolation(
                description,
                $"Method {declaration.Name} in class {parentClassName} does not have any attribute")
            .Returns(violation);

            //WHEN
            cSharpMethod.EvaluateMethodsHavingCorrectAttributes(report, parentClassName, description);

            //THEN
            report.DidNotReceive().Add(Arg.Any <RuleViolation>());
        }
        public void ShouldBeAbleToSayWhetherItsNameMatchesAPattern(string pattern, string methodName, bool expectedResult)
        {
            //GIVEN
            var declaration = new MethodDeclarationInfo(methodName, Any.ReadOnlyList <string>());

            var method = new CSharpMethod(declaration, Any.Instance <IProjectScopedRuleViolationFactory>());

            //WHEN
            var nameMatches = method.NameMatches(Pattern.WithoutExclusion(pattern));

            //THEN
            nameMatches.Should().Be(expectedResult);
        }
        public static CSharpMethod CreateGetContentMethodForCreatingReference(string referenceUrlParameterName)
        {
            // Create the method definition
            string methodName = nameof(ODataCmdletBase.GetContent);
            Type   returnType = typeof(object);
            string methodBody = $"return this.{nameof(ODataCmdletBase.GetReferenceRequestContent)}({referenceUrlParameterName});";

            // Create the method object
            CSharpMethod result = new CSharpMethod(methodName, returnType, methodBody)
            {
                Override       = true,
                AccessModifier = CSharpAccessModifier.Internal,
            };

            return(result);
        }
        private IEnumerable <CSharpMethod> GetMethods(IEnumerable <OperationDefinition> operations)
        {
            foreach (OperationDefinition operation in operations)
            {
                CSharpDocumentationComment docComment = new CSharpDocumentationComment(summary: null, rawNotes: operation.Description);
                bool isAsync = operation.ReturnsTask(out Type returnType);

                CSharpMethod result = new CSharpMethod(
                    name: operation.Name,
                    returnType: operation.ReturnType.GetCSharpName(),
                    body: this.GetMethodBody(operation, isAsync, returnType),
                    parameters: this.GetMethodParameters(operation),
                    isAsync: isAsync,
                    documentationComment: docComment);

                yield return(result);
            }
        }
        public static CSharpMethod CreateGetHttpMethodMethod(string httpMethod)
        {
            if (httpMethod == null)
            {
                throw new ArgumentNullException(nameof(httpMethod));
            }

            // Create the method definition
            string methodName = nameof(ODataCmdletBase.GetHttpMethod);
            Type   returnType = typeof(string);
            string methodBody = $"return \"{httpMethod}\";";

            // Create the method object
            CSharpMethod result = new CSharpMethod(methodName, returnType, methodBody)
            {
                Override       = true,
                AccessModifier = CSharpAccessModifier.Internal,
            };

            return(result);
        }
Exemple #6
0
        private IEnumerable <CSharpMethod> GetMethods(ContractDefinition contract)
        {
            IEnumerable <CSharpParameter> methodParameters = new CSharpParameter[]
            {
                new CSharpParameter(ImplementationParameterName, contract.FullName),
                new CSharpParameter(OperationInvocationParameterName, typeof(OperationInvocation).GetCSharpName()),
            };

            foreach (OperationDefinition operation in contract.Operations)
            {
                CSharpMethod result = new CSharpMethod(
                    name: operation.Name,
                    returnType: typeof(Task <object>).GetCSharpName(),
                    body: this.GetMethodBody(operation),
                    accessModifier: CSharpAccessModifier.Private,
                    parameters: methodParameters,
                    isStatic: true,
                    isAsync: true);

                yield return(result);
            }
        }
        public static CSharpMethod CreateGetResourcePathMethod(string url, bool isFunction = false)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }

            // Create the method definition
            string methodName = nameof(ODataCmdletBase.GetResourcePath);
            Type   returnType = typeof(string);
            string methodBody = isFunction
                ? $"return $\"{url}({{this.{nameof(GetCmdlet.GetFunctionUrlSegment)}()}})\";"
                : $"return $\"{url}\";";

            // Create the method object
            CSharpMethod result = new CSharpMethod(methodName, returnType, methodBody)
            {
                Override       = true,
                AccessModifier = CSharpAccessModifier.Internal,
            };

            return(result);
        }
Exemple #8
0
        private static void ProcessElfEnum(CSharpConverterOptions cppOptions, CSharpCompilation csCompilation, string enumPrefix, string enumClassName)
        {
            var ns = csCompilation.Members.OfType <CSharpGeneratedFile>().First().Members.OfType <CSharpNamespace>().First();

            var rawElfClass = ns.Members.OfType <CSharpClass>().First();

            var enumRawFields = rawElfClass.Members.OfType <CSharpField>().Where(x => (x.Modifiers & CSharpModifiers.Const) != 0 && x.Name.StartsWith(enumPrefix)).ToList();

            var enumClass = new CSharpStruct(enumClassName)
            {
                Modifiers = CSharpModifiers.Partial | CSharpModifiers.ReadOnly
            };

            ns.Members.Add(enumClass);

            bool isReloc = enumPrefix == "R_";

            var filteredFields = new List <CSharpField>();

            foreach (var enumRawField in enumRawFields)
            {
                var rawName = enumRawField.Name;

                string relocArch = null;

                if (isReloc)
                {
                    foreach (var mapReloc in MapRelocMachineToArch)
                    {
                        if (rawName.StartsWith(mapReloc.Key))
                        {
                            relocArch = mapReloc.Value;
                            break;
                        }
                    }

                    if (relocArch == null)
                    {
                        continue;
                    }
                }

                // NUM fields
                if (rawName.EndsWith("_NUM"))
                {
                    continue;
                }

                filteredFields.Add(enumRawField);

                var csFieldName = isReloc ? rawName : rawName.Substring(enumPrefix.Length); // discard EM_
                if (csFieldName.StartsWith("386"))
                {
                    csFieldName = $"I{csFieldName}";
                }
                else
                {
                    switch (csFieldName)
                    {
                    case "88K":
                        csFieldName = "M88K";
                        break;

                    case "860":
                        csFieldName = "I860";
                        break;

                    case "960":
                        csFieldName = "I960";
                        break;

                    default:
                        // assume Motorola
                        if (csFieldName.StartsWith("68"))
                        {
                            csFieldName = $"M{csFieldName}";
                        }

                        break;
                    }
                }

                if (char.IsDigit(csFieldName[0]))
                {
                    throw new InvalidOperationException($"The enum name `{rawName}` starts with a number and needs to be modified");
                }

                csFieldName = CSharpHelper.EscapeName(csFieldName);

                var enumField = new CSharpField(csFieldName)
                {
                    Modifiers  = CSharpModifiers.Static | CSharpModifiers.ReadOnly,
                    FieldType  = enumClass,
                    Visibility = CSharpVisibility.Public,
                    Comment    = enumRawField.Comment,
                    InitValue  = relocArch != null ?
                                 $"new {enumClass.Name}(ElfArch.{relocArch}, {cppOptions.DefaultClassLib}.{rawName})" :
                                 $"new {enumClass.Name}({cppOptions.DefaultClassLib}.{rawName})"
                };

                enumClass.Members.Add(enumField);
            }

            var toStringInternal = new CSharpMethod()
            {
                Name       = "ToStringInternal",
                Visibility = CSharpVisibility.Private,
                ReturnType = CSharpPrimitiveType.String
            };

            enumClass.Members.Add(toStringInternal);

            toStringInternal.Body = (writer, element) =>
            {
                var values = new HashSet <object>();
                if (isReloc)
                {
                    writer.WriteLine("switch (((ulong)Value << 16) | Arch.Value)");
                }
                else
                {
                    writer.WriteLine("switch (Value)");
                }
                writer.OpenBraceBlock();
                foreach (var rawField in filteredFields)
                {
                    var cppField = ((CppField)rawField.CppElement);
                    if (isReloc)
                    {
                        string relocMachine = null;
                        foreach (var mapReloc in MapRelocMachineToMachine)
                        {
                            if (rawField.Name.StartsWith(mapReloc.Key))
                            {
                                relocMachine = mapReloc.Value;
                                break;
                            }
                        }

                        if (relocMachine == null)
                        {
                            continue;
                        }

                        if (!values.Add(relocMachine + "$" + cppField.InitValue.Value))
                        {
                            continue;
                        }

                        writer.WriteLine($"case ((ulong){cppOptions.DefaultClassLib}.{rawField.Name} << 16) | {cppOptions.DefaultClassLib}.{relocMachine} : return \"{rawField.Name}\";");
                    }
                    else
                    {
                        if (!values.Add(cppField.InitValue.Value))
                        {
                            continue;
                        }

                        string descriptionText = rawField.Name;

                        if (cppField.Comment != null)
                        {
                            descriptionText += " - " + cppField.Comment.ToString().Replace("\"", "\\\"");
                        }
                        descriptionText = descriptionText.Replace("\r\n", "").Replace("\n", "");
                        writer.WriteLine($"case {cppOptions.DefaultClassLib}.{rawField.Name}: return \"{descriptionText}\";");
                    }
                }

                writer.WriteLine($"default: return \"Unknown {enumClassName}\";");
                writer.CloseBraceBlock();
            };
        }
Exemple #9
0
        private static void GenerateDwarfDIE(CSharpNamespace ns)
        {
            var file = File.ReadAllLines(@"C:\code\LibObjectFile\ext\dwarf-specs\attributesbytag.tex");

            var regexDWTag = new Regex(@"^\\(DWTAG\w+)");
            var regexDWAT  = new Regex(@"^&\\(DWAT\w+)");

            int state = 0;

            string      currentCompactTagName = null;
            CSharpClass currentDIE            = null;

            var dieClasses = new List <CSharpClass>();
            var dieTags    = new List <string>();

            foreach (var line in file)
            {
                if (state == 0)
                {
                    if (line.StartsWith(@"\begin{longtable}"))
                    {
                        continue;
                    }
                    else
                    {
                        state = 1;
                    }
                }
                var match = regexDWTag.Match(line);
                if (match.Success)
                {
                    var compactTagName = match.Groups[1].Value;
                    if (compactTagName == currentCompactTagName)
                    {
                        continue;
                    }
                    currentCompactTagName = compactTagName;
                    var fullTagName = MapTagCompactNameToFullName[compactTagName];
                    dieTags.Add(fullTagName);
                    var csDIEName = fullTagName.Substring("DW_TAG_".Length);
                    csDIEName  = CSharpifyName(csDIEName);
                    currentDIE = new CSharpClass($"DwarfDIE{csDIEName}");
                    currentDIE.BaseTypes.Add(new CSharpFreeType("DwarfDIE"));
                    ns.Members.Add(currentDIE);

                    var csConstructor = new CSharpMethod();
                    csConstructor.IsConstructor = true;
                    csConstructor.Body          = (writer, element) => writer.WriteLine($"this.Tag = (DwarfTag)DwarfNative.{fullTagName};");
                    currentDIE.Members.Add(csConstructor);

                    dieClasses.Add(currentDIE);
                }
                else
                {
                    match = regexDWAT.Match(line);
                    if (match.Success)
                    {
                        var compactAttrName = match.Groups[1].Value;
                        var csProperty      = CreatePropertyFromDwarfAttributeName(compactAttrName);
                        currentDIE.Members.Add(csProperty);

                        // The DW_AT_description attribute can be used on any debugging information
                        // entry that may have a DW_AT_name attribute. For simplicity, this attribute is
                        // not explicitly shown.
                        if (compactAttrName == "DWATname")
                        {
                            csProperty = CreatePropertyFromDwarfAttributeName("DWATdescription");
                            currentDIE.Members.Add(csProperty);
                        }
                    }
                    else if (currentDIE != null && line.Contains("{DECL}"))
                    {
                        currentDIE.BaseTypes[0] = new CSharpFreeType("DwarfDIEDeclaration");
                    }
                }
            }

            // Generate the DIEHelper class
            var dieHelperClass = new CSharpClass("DIEHelper")
            {
                Modifiers  = CSharpModifiers.Partial | CSharpModifiers.Static,
                Visibility = CSharpVisibility.Internal
            };

            ns.Members.Add(dieHelperClass);
            var dieHelperMethod = new CSharpMethod()
            {
                Name       = "ConvertTagToDwarfDIE",
                Modifiers  = CSharpModifiers.Static,
                Visibility = CSharpVisibility.Public
            };

            dieHelperClass.Members.Add(dieHelperMethod);

            dieHelperMethod.Parameters.Add(new CSharpParameter("tag")
            {
                ParameterType = CSharpPrimitiveType.UShort
            });
            dieHelperMethod.ReturnType = new CSharpFreeType("DwarfDIE");

            dieHelperMethod.Body = (writer, element) => {
                writer.WriteLine("switch (tag)");
                writer.OpenBraceBlock();

                for (var i = 0; i < dieClasses.Count; i++)
                {
                    var dieCls = dieClasses[i];
                    var dieTag = dieTags[i];
                    writer.WriteLine($"case DwarfNative.{dieTag}:");
                    writer.Indent();
                    writer.WriteLine($"return new {dieCls.Name}();");
                    writer.UnIndent();
                }

                writer.CloseBraceBlock();
                writer.WriteLine("return new DwarfDIE();");
            };
        }
 public CSharpMethodChange(ChangeType typeOfChange, CSharpMethod method)
 {
     _typeOfChange = typeOfChange;
       _method = method;
 }
Exemple #11
0
 public CSharpMethodChange(ChangeType typeOfChange, CSharpMethod method)
 {
     _typeOfChange = typeOfChange;
     _method       = method;
 }