Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions build-tools/automation/guardian/source.gdnsuppress
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"default": {
"name": "default",
"createdDate": "2023-02-22 23:55:29Z",
"lastUpdatedDate": "2023-02-22 23:55:29Z"
"lastUpdatedDate": "2023-05-04 13:54:18Z"
}
},
"results": {
Expand Down Expand Up @@ -78,8 +78,8 @@
"expirationDate": null,
"type": null
},
"58fab4dfef38677720e955e546a6af108332c65daafb0d043ad9d93442300a30": {
"signature": "58fab4dfef38677720e955e546a6af108332c65daafb0d043ad9d93442300a30",
"6d1fb3a483eb491710d6a09ed0b4bab47f13942d0c6fc744e6683614a66604ab": {
"signature": "6d1fb3a483eb491710d6a09ed0b4bab47f13942d0c6fc744e6683614a66604ab",
"alternativeSignatures": [],
"target": "src/Mono.Android/Android.Util/Log.cs",
"memberOf": [
Expand Down Expand Up @@ -134,8 +134,8 @@
"expirationDate": null,
"type": null
},
"06af52be6b6f87455b1db2eb6e631e783f1dacaf607c9b5f34cdee669992c8b5": {
"signature": "06af52be6b6f87455b1db2eb6e631e783f1dacaf607c9b5f34cdee669992c8b5",
"1b38e026fae90da4ae2fe9151c9c1ebd73c8b3c2c5f072ceae390a3ceec2fb97": {
"signature": "1b38e026fae90da4ae2fe9151c9c1ebd73c8b3c2c5f072ceae390a3ceec2fb97",
"alternativeSignatures": [],
"target": "src/Mono.Android/Android.Util/Log.cs",
"memberOf": [
Expand Down
2 changes: 2 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.{cs,vb}]
dotnet_diagnostic.CA1305.severity = error # Specify IFormatProvider
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why enable this just in src and not repo-wide?

Copy link
Member Author

@jonathanpeppers jonathanpeppers May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the suggestion here: #7993 (comment)

There were a lot of tests, build-tools, Mono.Options, etc. that would also need to be addressed or use $(NoWarn).

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTyp
if (members [i] == null)
members [i] = attributeType.GetProperty (namedArgNames [i]);
if (members [i] == null)
throw new ArgumentException (String.Format ("Named argument {0} was not found in attribute type {1}", namedArgNames [i], attributeType));
throw new ArgumentException (FormattableString.Invariant ($"Named argument {namedArgNames [i]} was not found in attribute type {attributeType}"));
}

CodeLiteral [] args = new CodeLiteral [ctorArgs.Length];
Expand All @@ -64,7 +64,7 @@ public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTyp
ArrayList fvalues = new ArrayList ();
for (int i = 0; i < members.Length; i++) {
if (members [i] == null)
throw new ArgumentException (String.Format ("MemberInfo at {0} was null for type {1}.", i, attributeType));
throw new ArgumentException (FormattableString.Invariant ($"MemberInfo at {i} was null for type {attributeType}."));
if (members [i] is PropertyInfo) {
props.Add ((PropertyInfo) members [i]);
pvalues.Add (values [i].Value);
Expand Down
8 changes: 4 additions & 4 deletions src/Mono.Android/Android.Graphics/Color.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -64,7 +65,7 @@ public int ToArgb ()

public override string ToString ()
{
return String.Format ("Color [A={0}, R={1}, G={2}, B={3}]", A, R, G, B);
return FormattableString.Invariant ($"Color [A={A}, R={R}, G={G}, B={B}]");
}

public override bool Equals (object obj)
Expand Down Expand Up @@ -217,9 +218,8 @@ private static void CheckRGBValues (int red, int green, int blue)

private static ArgumentException CreateColorArgumentException (int value, string color)
{
return new ArgumentException (string.Format ("'{0}' is not a valid"
+ " value for '{1}'. '{1}' should be greater or equal to 0 and"
+ " less than or equal to 255.", value, color));
return new ArgumentException (FormattableString.Invariant (
$"'{value}' is not a valid value for '{color}'. '{color}' should be greater or equal to 0 and less than or equal to 255."));
}

public static Color ParseColor (string colorString)
Expand Down
11 changes: 4 additions & 7 deletions src/Mono.Android/Android.Runtime/AndroidEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ static bool TrustEvaluateSsl (List <byte[]> certsRawData)
if (certStore == null)
return null;

var alias = string.Format ("{0}:{1:x8}.0", userStore ? "user" : "system", hash);
var store = userStore ? "user" : "system";
var alias = FormattableString.Invariant ($"{store}:{hash:x8}.0");
var certificate = certStore.GetCertificate (alias);
if (certificate == null)
return null;
Expand Down Expand Up @@ -220,7 +221,7 @@ static void NotifyTimeZoneChanged ()
try {
clearInfo.Method ();
} catch (Exception e) {
Logger.Log (LogLevel.Warn, "MonoAndroid", string.Format ("Ignoring exception from {0}: {1}", clearInfo.Description, e));
Logger.Log (LogLevel.Warn, "MonoAndroid", FormattableString.Invariant ($"Ignoring exception from {clearInfo.Description}: {e}"));
}
}
}
Expand Down Expand Up @@ -426,11 +427,7 @@ public Uri GetProxy (Uri destination)
if (address == null) // FIXME
return destination;

#if ANDROID_19
return new Uri (string.Format ("http://{0}:{1}/", address.HostString, address.Port));
#else
return new Uri (string.Format ("http://{0}:{1}/", address.HostName, address.Port));
#endif
return new Uri (FormattableString.Invariant ($"http://{address.HostString}:{address.Port}/"));
}

public bool IsBypassed (Uri host)
Expand Down
17 changes: 8 additions & 9 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
Expand Down Expand Up @@ -165,7 +166,7 @@ public override IntPtr ReleaseLocalReference (ref JniObjectReference value, ref

public override void WriteGlobalReferenceLine (string format, params object?[] args)
{
RuntimeNativeMethods._monodroid_gref_log (string.Format (format, args));
RuntimeNativeMethods._monodroid_gref_log (string.Format (CultureInfo.InvariantCulture, format, args));
}

public override JniObjectReference CreateGlobalReference (JniObjectReference value)
Expand Down Expand Up @@ -530,7 +531,7 @@ public void RegisterNativeMembers (JniType nativeClass, Type type, ReadOnlySpan<
}

if (minfo == null)
throw new InvalidOperationException (String.Format ("Specified managed method '{0}' was not found. Signature: {1}", mname.ToString (), signature.ToString ()));
throw new InvalidOperationException (FormattableString.Invariant ($"Specified managed method '{mname.ToString ()}' was not found. Signature: {signature.ToString ()}"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this should either use {mname}…{signature} or use {mname.ToString(CultureInfo.InvariantCulture)}…{signature.ToString(CultureInfo.InvariantCulture). What is here will call .ToString() with whatever is the current culture, which is most likely not the Invariant culture, and then pass the output of .ToString() on to FormattableString.Invariant().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these two you have to call ToString() because they are both ReadonlySpan<char>.

For some reason you can't string interpolate these like $"Foo: {foo}"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, "playing around" in csharp:

CultureInfo.CurrentCulture = new CultureInfo("de-DE");
double d = 42.5;
d.ToString(); // "42,5"
FormattableString.Invariant($"{d}");            // "42.5"  
FormattableString.Invariant($"{d.ToString()}"); // "42,5"

d.ToString() is "immediate", and thus uses the current culture ("de-DE", German), which is not Invariant output.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, we'd get a CA1305 build error if these were wrong. ReadonlySpan<char> doesn't have an overload for ToString():

https://learn.microsoft.com/en-us/dotnet/api/system.readonlyspan-1.tostring?view=net-8.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In such cases we should use code like this

callback = CreateDynamicCallback (minfo);
needToRegisterNatives = true;
} else {
Expand Down Expand Up @@ -659,9 +660,8 @@ internal void AddPeer (IJavaPeerable value, JniObjectReference reference, IntPtr
if (JniEnvironment.Types.IsSameObject (value.PeerReference, target!.PeerReference)) {
found = true;
if (Logger.LogGlobalRef) {
Logger.Log (LogLevel.Info, "monodroid-gref",
string.Format ("warning: not replacing previous registered handle {0} with handle {1} for key_handle 0x{2}",
target.PeerReference.ToString (), reference.ToString (), hash.ToString ("x")));
Logger.Log (LogLevel.Info, "monodroid-gref", FormattableString.Invariant (
$"warning: not replacing previous registered handle {target.PeerReference} with handle {reference} for key_handle 0x{hash:x}"));
}
}
}
Expand Down Expand Up @@ -704,10 +704,9 @@ internal void AddPeer (IJavaPeerable value, IntPtr handle, JniHandleOwnership tr
}

if (Logger.LogGlobalRef) {
RuntimeNativeMethods._monodroid_gref_log ("handle 0x" + handleField.ToString ("x") +
"; key_handle 0x" + hash.ToString ("x") +
": Java Type: `" + JNIEnv.GetClassNameFromInstance (handleField) + "`; " +
"MCW type: `" + value.GetType ().FullName + "`\n");
RuntimeNativeMethods._monodroid_gref_log (
FormattableString.Invariant (
$"handle 0x{handleField:x}; key_handle 0x{hash:x}: Java Type: `{JNIEnv.GetClassNameFromInstance (handleField)}`; MCW type: `{value.GetType ().FullName}`\n"));
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -262,8 +263,8 @@ public static unsafe void InvokeConstructor (IntPtr instance, string jniCtorSign
try {
IntPtr ctor = JNIEnv.GetMethodID (lrefClass, "<init>", jniCtorSignature);
if (ctor == IntPtr.Zero)
throw new ArgumentException (string.Format ("Could not find constructor JNI signature '{0}' on type '{1}'.",
jniCtorSignature, Java.Interop.TypeManager.GetClassName (lrefClass)));
throw new ArgumentException (FormattableString.Invariant (
$"Could not find constructor JNI signature '{jniCtorSignature}' on type '{Java.Interop.TypeManager.GetClassName (lrefClass)}'."));
CallNonvirtualVoidMethod (instance, lrefClass, ctor, constructorParameters);
} finally {
DeleteLocalRef (lrefClass);
Expand All @@ -280,8 +281,8 @@ public static unsafe IntPtr CreateInstance (IntPtr jniClass, string signature, J
{
IntPtr ctor = JNIEnv.GetMethodID (jniClass, "<init>", signature);
if (ctor == IntPtr.Zero)
throw new ArgumentException (string.Format ("Could not find constructor JNI signature '{0}' on type '{1}'.",
signature, Java.Interop.TypeManager.GetClassName (jniClass)));
throw new ArgumentException (FormattableString.Invariant (
$"Could not find constructor JNI signature '{signature}' on type '{Java.Interop.TypeManager.GetClassName (jniClass)}'."));
return JNIEnv.NewObject (jniClass, ctor, constructorParameters);
}

Expand Down Expand Up @@ -627,9 +628,8 @@ static void AssertCompatibleArrayTypes (Type sourceType, IntPtr destArray)
IntPtr lrefDest = GetObjectClass (destArray);
try {
if (!IsAssignableFrom (grefSource, lrefDest)) {
throw new InvalidCastException (string.Format ("Unable to cast from '{0}' to '{1}'.",
Java.Interop.TypeManager.GetClassName (grefSource),
Java.Interop.TypeManager.GetClassName (lrefDest)));
throw new InvalidCastException (FormattableString.Invariant (
$"Unable to cast from '{Java.Interop.TypeManager.GetClassName (grefSource)}' to '{Java.Interop.TypeManager.GetClassName (lrefDest)}'."));
}
} finally {
DeleteGlobalRef (grefSource);
Expand All @@ -643,9 +643,8 @@ static void AssertCompatibleArrayTypes (IntPtr sourceArray, Type destType)
IntPtr lrefSource = GetObjectClass (sourceArray);
try {
if (!IsAssignableFrom (lrefSource, grefDest)) {
throw new InvalidCastException (string.Format ("Unable to cast from '{0}' to '{1}'.",
Java.Interop.TypeManager.GetClassName (lrefSource),
Java.Interop.TypeManager.GetClassName (grefDest)));
throw new InvalidCastException (FormattableString.Invariant (
$"Unable to cast from '{Java.Interop.TypeManager.GetClassName (lrefSource)}' to '{Java.Interop.TypeManager.GetClassName (grefDest)}'."));
}
} finally {
DeleteGlobalRef (grefDest);
Expand Down Expand Up @@ -1145,7 +1144,7 @@ static int _GetArrayLength (IntPtr array_ptr)
ret [i] = value;
ret [i] = targetType == null || targetType.IsInstanceOfType (value)
? value
: Convert.ChangeType (value, targetType);
: Convert.ChangeType (value, targetType, CultureInfo.InvariantCulture);
}

return ret;
Expand Down
5 changes: 3 additions & 2 deletions src/Mono.Android/Android.Runtime/XmlReaderPullParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,9 @@ public string PositionDescription {
var xi = r as IXmlLineInfo;
var loc = xi == null || !xi.HasLineInfo () ?
"(location N/A)" :
String.Format ("({0}, {1})", xi.LineNumber, xi.LinePosition);
return String.Format ("Node {0} at {1} {2}", r.NodeType, String.IsNullOrEmpty (r.BaseURI) ? null : r.BaseURI, loc);
FormattableString.Invariant ($"({xi.LineNumber}, {xi.LinePosition})");
var uri = string.IsNullOrEmpty (r.BaseURI) ? null : r.BaseURI;
return FormattableString.Invariant ($"Node {r.NodeType} at {uri} {loc}");
}
}

Expand Down
35 changes: 22 additions & 13 deletions src/Mono.Android/Android.Util/Log.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
using System;
using System.Globalization;
using Android.Runtime;

namespace Android.Util {

public partial class Log {

/// <summary>
/// IFormatProvider passed to any underlying string.Format() calls. Defaults to System.Globalization.CultureInfo.CurrentCulture.
/// </summary>
#if ANDROID_34
public
#endif // ANDROID_34
static IFormatProvider FormatProvider { get; set; } = CultureInfo.CurrentCulture;

public static int Debug (string tag, string format, params object[] args)
{
return Debug (tag, string.Format (format, args));
return Debug (tag, string.Format (FormatProvider, format, args));
}

public static int Debug (string tag, Java.Lang.Throwable tr, string msg)
Expand All @@ -17,12 +26,12 @@ public static int Debug (string tag, Java.Lang.Throwable tr, string msg)

public static int Debug (string tag, Java.Lang.Throwable tr, string format, params object[] args)
{
return Debug (tag, string.Format (format, args), tr);
return Debug (tag, string.Format (FormatProvider, format, args), tr);
}

public static int Error (string tag, string format, params object[] args)
{
return Error (tag, string.Format (format, args));
return Error (tag, string.Format (FormatProvider, format, args));
}

public static int Error (string tag, Java.Lang.Throwable tr, string msg)
Expand All @@ -32,12 +41,12 @@ public static int Error (string tag, Java.Lang.Throwable tr, string msg)

public static int Error (string tag, Java.Lang.Throwable tr, string format, params object[] args)
{
return Error (tag, string.Format (format, args), tr);
return Error (tag, string.Format (FormatProvider, format, args), tr);
}

public static int Info (string tag, string format, params object[] args)
{
return Info (tag, string.Format (format, args));
return Info (tag, string.Format (FormatProvider, format, args));
}

public static int Info (string tag, Java.Lang.Throwable tr, string msg)
Expand All @@ -47,17 +56,17 @@ public static int Info (string tag, Java.Lang.Throwable tr, string msg)

public static int Info (string tag, Java.Lang.Throwable tr, string format, params object[] args)
{
return Info (tag, string.Format (format, args), tr);
return Info (tag, string.Format (FormatProvider, format, args), tr);
}

public static int WriteLine (LogPriority priority, string tag, string format, params object[] args)
{
return WriteLine (priority, tag, string.Format (format, args));
return WriteLine (priority, tag, string.Format (FormatProvider, format, args));
}

public static int Verbose (string tag, string format, params object[] args)
{
return Verbose (tag, string.Format (format, args));
return Verbose (tag, string.Format (FormatProvider, format, args));
}

public static int Verbose (string tag, Java.Lang.Throwable tr, string msg)
Expand All @@ -67,12 +76,12 @@ public static int Verbose (string tag, Java.Lang.Throwable tr, string msg)

public static int Verbose (string tag, Java.Lang.Throwable tr, string format, params object[] args)
{
return Verbose (tag, string.Format (format, args), tr);
return Verbose (tag, string.Format (FormatProvider, format, args), tr);
}

public static int Warn (string tag, string format, params object[] args)
{
return Warn (tag, string.Format (format, args));
return Warn (tag, string.Format (FormatProvider, format, args));
}

public static int Warn (string tag, Java.Lang.Throwable tr, string msg)
Expand All @@ -82,12 +91,12 @@ public static int Warn (string tag, Java.Lang.Throwable tr, string msg)

public static int Warn (string tag, Java.Lang.Throwable tr, string format, params object[] args)
{
return Warn (tag, string.Format (format, args), tr);
return Warn (tag, string.Format (FormatProvider, format, args), tr);
}

public static int Wtf (string tag, string format, params object[] args)
{
return Wtf (tag, string.Format (format, args));
return Wtf (tag, string.Format (FormatProvider, format, args));
}

public static int Wtf (string tag, Java.Lang.Throwable tr, string msg)
Expand All @@ -97,7 +106,7 @@ public static int Wtf (string tag, Java.Lang.Throwable tr, string msg)

public static int Wtf (string tag, Java.Lang.Throwable tr, string format, params object[] args)
{
return Wtf (tag, string.Format (format, args), tr);
return Wtf (tag, string.Format (FormatProvider, format, args), tr);
}
}
}
Loading