Example #1
0
        private IEnumerable <LocalizedString> ParseLocalizedStringInvocation(LocalizedStringInvocation invocation)
        {
            var strings = new Stack <KeyValuePair <string, string> > ();

            Log("+ {0}", invocation.Method);
            if (invocation.SequencePoint != null)
            {
                Log("  @ {0}:{1}", RelativeDocumentUrl(invocation.SequencePoint.Document.Url),
                    invocation.SequencePoint.StartLine);
            }

            foreach (var instruction in invocation.AllInstructions)
            {
                Log(true, "  | {0}", instruction);
            }

            Log(true, "  |".PadRight(70, '-'));

            int i = invocation.Instructions.Count;

            foreach (var param in invocation.Method.Parameters.Reverse())
            {
                if (param.ParameterType.FullName == "System.String" && i > 0)
                {
                    var instruction = invocation.Instructions [--i];
                    if (instruction.OpCode == OpCodes.Ldnull || instruction.OpCode == OpCodes.Ldstr)
                    {
                        strings.Push(new KeyValuePair <string, string> (
                                         param.Name,
                                         instruction.Operand as string
                                         ));
                    }

                    Log("  | [{0}]: {1}", param.Name, instruction);
                }
            }

            Log(true, "  |".PadRight(70, '-'));

            foreach (var @string in GenerateLocalizedStrings(invocation.SequencePoint, strings))
            {
                Log("  | {0}", @string);
                yield return(@string);
            }

            Log();
        }
Example #2
0
        private IEnumerable<LocalizedString> ParseLocalizedStringInvocation (LocalizedStringInvocation invocation)
        {
            var strings = new Stack<KeyValuePair<string, string>> ();

            Log ("+ {0}", invocation.Method);
            if (invocation.SequencePoint != null) {
                Log ("  @ {0}:{1}", RelativeDocumentUrl (invocation.SequencePoint.Document.Url),
                    invocation.SequencePoint.StartLine);
            }

            foreach (var instruction in invocation.AllInstructions) {
                Log (true, "  | {0}", instruction);
            }

            Log (true, "  |".PadRight (70, '-'));

            int i = invocation.Instructions.Count;
            foreach (var param in invocation.Method.Parameters.Reverse ()) {
                if (param.ParameterType.FullName == "System.String" && i > 0) {
                    var instruction = invocation.Instructions [--i];
                    if (instruction.OpCode == OpCodes.Ldnull || instruction.OpCode == OpCodes.Ldstr) {
                        strings.Push (new KeyValuePair<string, string> (
                            param.Name,
                            instruction.Operand as string
                        ));
                    }

                    Log ("  | [{0}]: {1}", param.Name, instruction);
                }
            }

            // Detect if the GetString call is nested (an argument to) in a
            // String.Format call, which is a warning (Catalog.Format should be
            // used since it will never throw an exception).
            var call_instruction = invocation.AllInstructions [invocation.AllInstructions.Count - 1];
            var is_string_format = false;

            if (call_instruction.Next != null &&
                call_instruction.Next.Next != null &&
                call_instruction.Next.OpCode == OpCodes.Ldarg_0 &&
                call_instruction.Next.Next.OpCode == OpCodes.Call) {
                var string_format_call = call_instruction.Next.Next.Operand as MethodReference;
                if (string_format_call != null &&
                    string_format_call.DeclaringType.FullName == "System.String" &&
                    string_format_call.Name == "Format") {
                    is_string_format = true;
                }
            }

            Log (true, "  |".PadRight (70, '-'));

            foreach (var @string in GenerateLocalizedStrings (invocation.SequencePoint, strings,
                invocation.Method.Name.Contains("Gender"))) {
                Log ("  | {0}", @string);
                if (is_string_format) {
                    @string.Warnings.Add ("String.Format is unsafe - use Catalog.Format instead");
                }
                yield return @string;
            }

            Log ();
        }
Example #3
0
        private IEnumerable <LocalizedString> ParseLocalizedStringInvocation(LocalizedStringInvocation invocation)
        {
            var strings = new Stack <KeyValuePair <string, string> > ();

            Log("+ {0}", invocation.Method);
            if (invocation.SequencePoint != null)
            {
                Log("  @ {0}:{1}", RelativeDocumentUrl(invocation.SequencePoint.Document.Url),
                    invocation.SequencePoint.StartLine);
            }

            foreach (var instruction in invocation.AllInstructions)
            {
                Log(true, "  | {0}", instruction);
            }

            Log(true, "  |".PadRight(70, '-'));

            int i = invocation.Instructions.Count;

            foreach (var param in invocation.Method.Parameters.Reverse())
            {
                if (param.ParameterType.FullName == "System.String" && i > 0)
                {
                    var instruction = invocation.Instructions [--i];
                    if (instruction.OpCode == OpCodes.Ldnull || instruction.OpCode == OpCodes.Ldstr)
                    {
                        strings.Push(new KeyValuePair <string, string> (
                                         param.Name,
                                         instruction.Operand as string
                                         ));
                    }

                    Log("  | [{0}]: {1}", param.Name, instruction);
                }
            }

            // Detect if the GetString call is nested (an argument to) in a
            // String.Format call, which is a warning (Catalog.Format should be
            // used since it will never throw an exception).
            var call_instruction = invocation.AllInstructions [invocation.AllInstructions.Count - 1];
            var is_string_format = false;

            if (call_instruction.Next != null &&
                call_instruction.Next.Next != null &&
                call_instruction.Next.OpCode == OpCodes.Ldarg_0 &&
                call_instruction.Next.Next.OpCode == OpCodes.Call)
            {
                var string_format_call = call_instruction.Next.Next.Operand as MethodReference;
                if (string_format_call != null &&
                    string_format_call.DeclaringType.FullName == "System.String" &&
                    string_format_call.Name == "Format")
                {
                    is_string_format = true;
                }
            }

            Log(true, "  |".PadRight(70, '-'));

            foreach (var @string in GenerateLocalizedStrings(invocation.SequencePoint, strings,
                                                             invocation.Method.Name.Contains("Gender")))
            {
                Log("  | {0}", @string);
                if (is_string_format)
                {
                    @string.Warnings.Add("String.Format is unsafe - use Catalog.Format instead");
                }
                yield return(@string);
            }

            Log();
        }