Skip to content

Commit c17ea1c

Browse files
authored
Merge branch 'master' into chrisnas/flacky_etw_tests
2 parents 5807594 + 2df7434 commit c17ea1c

File tree

8 files changed

+1301
-1199
lines changed

8 files changed

+1301
-1199
lines changed

tracer/build/_build/CodeGenerators/CallSitesGenerator.cs

+21-2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ static string GetArgument(Mono.Cecil.CustomAttributeArgument customAttributeArgu
251251
internal static void GenerateFile(Dictionary<string, AspectClass> aspectClasses, AbsolutePath outputPath)
252252
{
253253
var sb = new StringBuilder();
254+
bool inWin32Section = false;
254255
sb.AppendLine("""
255256
// <copyright company="Datadog">
256257
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
@@ -273,7 +274,25 @@ namespace trace
273274

274275
foreach (var method in aspectClass.Value.Aspects.OrderBy(static k => k.Key.ToString(), StringComparer.OrdinalIgnoreCase))
275276
{
276-
sb.AppendLine(Format(" " + method.Key + method.Value.Subfix()));
277+
bool win32Only = method.Value.Tfms.IsNetFxOnly();
278+
if (win32Only && !inWin32Section)
279+
{
280+
inWin32Section = true;
281+
sb.AppendLine("#if _WIN32");
282+
}
283+
else if (!win32Only && inWin32Section)
284+
{
285+
inWin32Section = false;
286+
sb.AppendLine("#endif");
287+
}
288+
289+
sb.AppendLine(Format(" " + method.Key + method.Value.Suffix()));
290+
}
291+
292+
if (inWin32Section)
293+
{
294+
inWin32Section = false;
295+
sb.AppendLine("#endif");
277296
}
278297
}
279298

@@ -319,7 +338,7 @@ public Aspect() { }
319338

320339
public TargetFrameworks Tfms = TargetFrameworks.None;
321340

322-
public string Subfix()
341+
public string Suffix()
323342
{
324343
return $" {((long)Tfms).ToString()}";
325344
}

tracer/build/_build/CodeGenerators/CallTargetsGenerator.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ namespace trace
546546

547547

548548
//Write all CallTargets
549+
bool inWin32Section = false;
549550
sb.AppendLine();
550551
sb.AppendLine("std::vector<CallTargetDefinition3> g_callTargets=");
551552
sb.AppendLine("{");
@@ -556,9 +557,27 @@ namespace trace
556557
.ThenBy(static x => x.Key.TargetTypeName)
557558
.ThenBy(static x => x.Key.TargetMethodName))
558559
{
560+
bool win32Only = definition.Value.IsNetFxOnly();
561+
if (win32Only && !inWin32Section)
562+
{
563+
inWin32Section = true;
564+
sb.AppendLine("#if _WIN32");
565+
}
566+
else if (!win32Only && inWin32Section)
567+
{
568+
inWin32Section = false;
569+
sb.AppendLine("#endif");
570+
}
571+
559572
sb.AppendLine(GetCallTarget(definition.Key, signatures[GetSignature(definition.Key)], definition.Value, x++));
560573
}
561574

575+
if (inWin32Section)
576+
{
577+
inWin32Section = false;
578+
sb.AppendLine("#endif");
579+
}
580+
562581
sb.AppendLine("""
563582
};
564583
}
@@ -639,7 +658,6 @@ internal static void GenerateJsonFile(Dictionary<CallTargetDefinitionSource, Tar
639658
Logger.Information("CallTarget definitions File saved: {File}", fileName);
640659
}
641660

642-
643661
internal static TargetFrameworks GetCategory(TargetFramework tfm)
644662
{
645663
return (TargetFrameworks)Enum.Parse<TargetFrameworks>(tfm.ToString().ToUpper().Replace('.', '_'));

tracer/build/_build/CodeGenerators/TargetFrameworks.cs

+12-10
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ internal enum TargetFrameworks : uint
1616
// NOTE : When modifying this file make sure to update the TargetFrameworks enum in the ClrProfiler folder inside the Datadog.Trace project
1717

1818
NET461 = 1,
19-
NET462 = 2,
20-
NETSTANDARD2_0 = 4,
21-
NETCOREAPP2_1 = 8,
22-
NETCOREAPP3_0 = 16,
23-
NETCOREAPP3_1 = 32,
24-
NET5_0 = 64,
25-
NET6_0 = 128,
26-
NET7_0 = 256,
27-
NET8_0 = 512,
28-
NET0_0 = 1024,
19+
NETSTANDARD2_0 = 2,
20+
NETCOREAPP3_1 = 4,
21+
NET6_0 = 8,
22+
}
23+
24+
25+
internal static class TargetFrameworksExtensions
26+
{
27+
public static bool IsNetFxOnly(this TargetFrameworks tfm)
28+
{
29+
return tfm == TargetFrameworks.NET461;
30+
}
2931
}

tracer/src/Datadog.Trace/ClrProfiler/TargetFrameworks.cs

+3-10
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@ internal enum TargetFrameworks : uint
1414
// NOTE : When modifying this file make sure to update the TargetFrameworks enum in the CodeGenerator folder inside the _build project
1515

1616
NET461 = 1,
17-
NET462 = 2,
18-
NETSTANDARD2_0 = 4,
19-
NETCOREAPP2_1 = 8,
20-
NETCOREAPP3_0 = 16,
21-
NETCOREAPP3_1 = 32,
22-
NET5_0 = 64,
23-
NET6_0 = 128,
24-
NET7_0 = 256,
25-
NET8_0 = 512,
26-
NET9_0 = 1024,
17+
NETSTANDARD2_0 = 2,
18+
NETCOREAPP3_1 = 4,
19+
NET6_0 = 8,
2720
}

tracer/src/Datadog.Trace/DuckTyping/DuckType.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ public static CreateTypeResult GetProxy(Type targetType)
13501350
return default;
13511351
}
13521352

1353-
return GetProxy(instance.GetType()).CreateInstance<T>(instance);
1353+
return instance is T tInst ? tInst : GetProxy(instance.GetType()).CreateInstance<T>(instance);
13541354
}
13551355

13561356
/// <summary>
@@ -1368,7 +1368,7 @@ public static CreateTypeResult GetProxy(Type targetType)
13681368
return default;
13691369
}
13701370

1371-
return GetProxy(typeof(TOriginal)).CreateInstance<T, TOriginal>(instance);
1371+
return instance is T tInst ? tInst : GetProxy(typeof(TOriginal)).CreateInstance<T, TOriginal>(instance);
13721372
}
13731373

13741374
/// <summary>
@@ -1384,7 +1384,7 @@ public static bool CanCreate(object? instance)
13841384
return false;
13851385
}
13861386

1387-
return GetProxy(instance.GetType()).CanCreate();
1387+
return instance is T || GetProxy(instance.GetType()).CanCreate();
13881388
}
13891389

13901390
/// <summary>

tracer/src/Datadog.Trace/DuckTyping/DuckTypeExtensions.cs

+35-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public static object DuckCast(this object instance, Type targetType)
4848
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4949
public static bool TryDuckCast<T>(this object? instance, [NotNullWhen(true)] out T? value)
5050
{
51+
if (instance is T tInst)
52+
{
53+
value = tInst;
54+
return true;
55+
}
56+
5157
if (instance is not null &&
5258
DuckType.CreateCache<T>.GetProxy(instance.GetType()) is { Success: true } proxyResult)
5359
{
@@ -71,11 +77,20 @@ public static bool TryDuckCast(this object? instance, Type targetType, [NotNullW
7177
{
7278
if (targetType is null) { ThrowHelper.ThrowArgumentNullException(nameof(targetType)); }
7379

74-
if (instance is not null &&
75-
DuckType.GetOrCreateProxyType(targetType, instance.GetType()) is { Success: true } proxyResult)
80+
if (instance is not null)
7681
{
77-
value = proxyResult.CreateInstance(instance);
78-
return true;
82+
var instanceType = instance.GetType();
83+
if (instanceType == targetType)
84+
{
85+
value = instance;
86+
return true;
87+
}
88+
89+
if (DuckType.GetOrCreateProxyType(targetType, instanceType) is { Success: true } proxyResult)
90+
{
91+
value = proxyResult.CreateInstance(instance);
92+
return true;
93+
}
7994
}
8095

8196
value = default;
@@ -92,6 +107,11 @@ public static bool TryDuckCast(this object? instance, Type targetType, [NotNullW
92107
public static T? DuckAs<T>(this object? instance)
93108
where T : class
94109
{
110+
if (instance is T tInst)
111+
{
112+
return tInst;
113+
}
114+
95115
if (instance is not null &&
96116
DuckType.CreateCache<T>.GetProxy(instance.GetType()) is { Success: true } proxyResult)
97117
{
@@ -112,10 +132,18 @@ public static bool TryDuckCast(this object? instance, Type targetType, [NotNullW
112132
{
113133
if (targetType is null) { ThrowHelper.ThrowArgumentNullException(nameof(targetType)); }
114134

115-
if (instance is not null &&
116-
DuckType.GetOrCreateProxyType(targetType, instance.GetType()) is { Success: true } proxyResult)
135+
if (instance is not null)
117136
{
118-
return proxyResult.CreateInstance(instance);
137+
var instanceType = instance.GetType();
138+
if (instanceType == targetType)
139+
{
140+
return instance;
141+
}
142+
143+
if (DuckType.GetOrCreateProxyType(targetType, instanceType) is { Success: true } proxyResult)
144+
{
145+
return proxyResult.CreateInstance(instance);
146+
}
119147
}
120148

121149
return null;

0 commit comments

Comments
 (0)