Skip to content

Commit e4ed050

Browse files
authored
Merge pull request #4970 from tamasvajk/feature/extraction-errors
C#: Fix extraction errors
2 parents 3ece8c3 + d05d4e2 commit e4ed050

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

csharp/extractor/Semmle.Extraction.CIL/Entities/CustomAttributeDecoder.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Reflection.Metadata;
34

45
namespace Semmle.Extraction.CIL.Entities
@@ -36,12 +37,26 @@ public PrimitiveTypeCode GetUnderlyingEnumType(Type type)
3637
}
3738

3839
var name = type.GetQualifiedName();
40+
41+
if (wellKnownEnums.TryGetValue(name, out var code))
42+
{
43+
cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Debug, $"Using hard coded underlying enum type for {name}");
44+
return code;
45+
}
46+
3947
cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Info, $"Couldn't get underlying enum type for {name}");
4048

4149
// We can't fall back to Int32, because the type returned here defines how many bytes are read from the
4250
// stream and how those bytes are interpreted.
4351
throw new NotImplementedException();
4452
}
53+
4554
public bool IsSystemType(Type type) => type.GetQualifiedName() == "System.Type";
55+
56+
private static readonly Dictionary<string, PrimitiveTypeCode> wellKnownEnums = new Dictionary<string, PrimitiveTypeCode>
57+
{
58+
{ "System.AttributeTargets", PrimitiveTypeCode.Int32 },
59+
{ "System.ComponentModel.EditorBrowsableState", PrimitiveTypeCode.Int32 }
60+
};
4661
}
4762
}

csharp/extractor/Semmle.Extraction.CIL/Entities/NoMetadataHandleType.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.IO;
44
using System.Linq;
55
using System.Reflection;
6-
using Semmle.Util;
76

87
namespace Semmle.Extraction.CIL.Entities
98
{
@@ -21,6 +20,7 @@ internal sealed partial class NoMetadataHandleType : Type
2120
private readonly Type[]? thisTypeArguments;
2221
private readonly Type unboundGenericType;
2322
private readonly Type? containingType;
23+
private readonly Namespace? containingNamespace;
2424

2525
private readonly NamedTypeIdWriter idWriter;
2626

@@ -53,13 +53,20 @@ public NoMetadataHandleType(Context cx, string originalName) : base(cx)
5353
? null
5454
: new NoMetadataHandleType(Cx, containerName);
5555

56+
containingNamespace = isContainerNamespace
57+
? containerName == Cx.GlobalNamespace.Name
58+
? Cx.GlobalNamespace
59+
: containerName == Cx.SystemNamespace.Name
60+
? Cx.SystemNamespace
61+
: new Namespace(Cx, containerName)
62+
: null;
63+
5664
Populate();
5765
}
5866

5967
private void Populate()
6068
{
61-
if (isContainerNamespace &&
62-
!ContainingNamespace!.IsGlobalNamespace)
69+
if (isContainerNamespace)
6370
{
6471
Cx.Populate(ContainingNamespace);
6572
}
@@ -100,11 +107,7 @@ public override IEnumerable<IExtractionProduct> Contents
100107

101108
public override string Name => GenericsHelper.GetNonGenericName(name);
102109

103-
public override Namespace? ContainingNamespace => isContainerNamespace
104-
? containerName == Cx.GlobalNamespace.Name
105-
? Cx.GlobalNamespace
106-
: new Namespace(Cx, containerName)
107-
: null;
110+
public override Namespace? ContainingNamespace => containingNamespace;
108111

109112
public override Type? ContainingType => containingType;
110113

csharp/ql/test/library-tests/cil/attributes/attribute.expected

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ attrNoArg
406406
| System.Resources.ResourceReader.<>c | [CompilerGeneratedAttribute(...)] |
407407
| System.Resources.ResourceReader.<>c__DisplayClass49_0`1 | [CompilerGeneratedAttribute(...)] |
408408
| System.Resources.SatelliteContractVersionAttribute.<Version>k__BackingField | [CompilerGeneratedAttribute(...)] |
409-
| System.Runtime.AssemblyTargetedPatchBandAttribute | [AttributeUsageAttribute(...)] |
410409
| System.Runtime.AssemblyTargetedPatchBandAttribute.<TargetedPatchBand>k__BackingField | [CompilerGeneratedAttribute(...)] |
411410
| System.Runtime.CompilerServices.AccessedThroughPropertyAttribute.<PropertyName>k__BackingField | [CompilerGeneratedAttribute(...)] |
412411
| System.Runtime.CompilerServices.AsyncMethodBuilderAttribute.<BuilderType>k__BackingField | [CompilerGeneratedAttribute(...)] |
@@ -431,21 +430,17 @@ attrNoArg
431430
| System.Runtime.CompilerServices.DefaultDependencyAttribute.<LoadHint>k__BackingField | [CompilerGeneratedAttribute(...)] |
432431
| System.Runtime.CompilerServices.DependencyAttribute.<DependentAssembly>k__BackingField | [CompilerGeneratedAttribute(...)] |
433432
| System.Runtime.CompilerServices.DependencyAttribute.<LoadHint>k__BackingField | [CompilerGeneratedAttribute(...)] |
434-
| System.Runtime.CompilerServices.EnumeratorCancellationAttribute | [AttributeUsageAttribute(...)] |
435433
| System.Runtime.CompilerServices.FixedBufferAttribute.<ElementType>k__BackingField | [CompilerGeneratedAttribute(...)] |
436434
| System.Runtime.CompilerServices.FixedBufferAttribute.<Length>k__BackingField | [CompilerGeneratedAttribute(...)] |
437435
| System.Runtime.CompilerServices.InternalsVisibleToAttribute.<AllInternalsVisible>k__BackingField | [CompilerGeneratedAttribute(...)] |
438436
| System.Runtime.CompilerServices.InternalsVisibleToAttribute.<AssemblyName>k__BackingField | [CompilerGeneratedAttribute(...)] |
439437
| System.Runtime.CompilerServices.JitHelpers | [ExtensionAttribute(...)] |
440438
| System.Runtime.CompilerServices.MethodImplAttribute.<Value>k__BackingField | [CompilerGeneratedAttribute(...)] |
441439
| System.Runtime.CompilerServices.MethodImplOptions | [FlagsAttribute(...)] |
442-
| System.Runtime.CompilerServices.NullableAttribute | [AttributeUsageAttribute(...)] |
443440
| System.Runtime.CompilerServices.NullableAttribute | [CompilerGeneratedAttribute(...)] |
444441
| System.Runtime.CompilerServices.NullableAttribute | [EmbeddedAttribute(...)] |
445-
| System.Runtime.CompilerServices.NullableContextAttribute | [AttributeUsageAttribute(...)] |
446442
| System.Runtime.CompilerServices.NullableContextAttribute | [CompilerGeneratedAttribute(...)] |
447443
| System.Runtime.CompilerServices.NullableContextAttribute | [EmbeddedAttribute(...)] |
448-
| System.Runtime.CompilerServices.NullablePublicOnlyAttribute | [AttributeUsageAttribute(...)] |
449444
| System.Runtime.CompilerServices.NullablePublicOnlyAttribute | [CompilerGeneratedAttribute(...)] |
450445
| System.Runtime.CompilerServices.NullablePublicOnlyAttribute | [EmbeddedAttribute(...)] |
451446
| System.Runtime.CompilerServices.ReferenceAssemblyAttribute.<Description>k__BackingField | [CompilerGeneratedAttribute(...)] |
@@ -463,7 +458,6 @@ attrNoArg
463458
| System.Runtime.CompilerServices.YieldAwaitable | [IsReadOnlyAttribute(...)] |
464459
| System.Runtime.CompilerServices.YieldAwaitable.YieldAwaiter | [IsReadOnlyAttribute(...)] |
465460
| System.Runtime.CompilerServices.YieldAwaitable.YieldAwaiter.<>c | [CompilerGeneratedAttribute(...)] |
466-
| System.Runtime.ConstrainedExecution.PrePrepareMethodAttribute | [AttributeUsageAttribute(...)] |
467461
| System.Runtime.ConstrainedExecution.ReliabilityContractAttribute.<Cer>k__BackingField | [CompilerGeneratedAttribute(...)] |
468462
| System.Runtime.ConstrainedExecution.ReliabilityContractAttribute.<ConsistencyGuarantee>k__BackingField | [CompilerGeneratedAttribute(...)] |
469463
| System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs.<Exception>k__BackingField | [CompilerGeneratedAttribute(...)] |
@@ -565,7 +559,6 @@ attrNoArg
565559
| System.Runtime.Serialization.SerializationInfo.<IsFullTypeNameSetExplicit>k__BackingField | [CompilerGeneratedAttribute(...)] |
566560
| System.Runtime.Serialization.StreamingContext | [IsReadOnlyAttribute(...)] |
567561
| System.Runtime.Serialization.StreamingContextStates | [FlagsAttribute(...)] |
568-
| System.Runtime.TargetedPatchingOptOutAttribute | [AttributeUsageAttribute(...)] |
569562
| System.Runtime.TargetedPatchingOptOutAttribute.<Reason>k__BackingField | [CompilerGeneratedAttribute(...)] |
570563
| System.RuntimeArgumentHandle | [IsByRefLikeAttribute(...)] |
571564
| System.RuntimeType.RuntimeTypeCache.Filter | [IsReadOnlyAttribute(...)] |
@@ -805,6 +798,7 @@ attrArgNamed
805798
| System.Reflection.ObfuscationAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
806799
| System.Resources.NeutralResourcesLanguageAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
807800
| System.Resources.SatelliteContractVersionAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
801+
| System.Runtime.AssemblyTargetedPatchBandAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
808802
| System.Runtime.CompilerServices.AsyncIteratorStateMachineAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
809803
| System.Runtime.CompilerServices.AsyncIteratorStateMachineAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
810804
| System.Runtime.CompilerServices.AsyncMethodBuilderAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
@@ -823,6 +817,7 @@ attrArgNamed
823817
| System.Runtime.CompilerServices.DependencyAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | True |
824818
| System.Runtime.CompilerServices.DisablePrivateReflectionAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
825819
| System.Runtime.CompilerServices.DisablePrivateReflectionAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
820+
| System.Runtime.CompilerServices.EnumeratorCancellationAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
826821
| System.Runtime.CompilerServices.FixedBufferAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
827822
| System.Runtime.CompilerServices.IndexerNameAttribute | [AttributeUsageAttribute(...)] | Inherited | True |
828823
| System.Runtime.CompilerServices.InternalsVisibleToAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | True |
@@ -850,6 +845,7 @@ attrArgNamed
850845
| System.Runtime.CompilerServices.TypeForwardedFromAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
851846
| System.Runtime.CompilerServices.TypeForwardedToAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | True |
852847
| System.Runtime.CompilerServices.TypeForwardedToAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
848+
| System.Runtime.ConstrainedExecution.PrePrepareMethodAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
853849
| System.Runtime.ConstrainedExecution.ReliabilityContractAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
854850
| System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
855851
| System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
@@ -887,6 +883,8 @@ attrArgNamed
887883
| System.Runtime.Serialization.OnSerializedAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
888884
| System.Runtime.Serialization.OnSerializingAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
889885
| System.Runtime.Serialization.OptionalFieldAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
886+
| System.Runtime.TargetedPatchingOptOutAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
887+
| System.Runtime.TargetedPatchingOptOutAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
890888
| System.Runtime.Versioning.NonVersionableAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
891889
| System.Runtime.Versioning.NonVersionableAttribute | [AttributeUsageAttribute(...)] | Inherited | False |
892890
| System.Runtime.Versioning.TargetFrameworkAttribute | [AttributeUsageAttribute(...)] | AllowMultiple | False |
@@ -1910,6 +1908,7 @@ attrArgPositional
19101908
| System.Runtime.AmbiguousImplementationException | [NullableAttribute(...)] | 0 | 0 |
19111909
| System.Runtime.AmbiguousImplementationException | [NullableContextAttribute(...)] | 0 | 2 |
19121910
| System.Runtime.AmbiguousImplementationException | [TypeForwardedFromAttribute(...)] | 0 | System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a |
1911+
| System.Runtime.AssemblyTargetedPatchBandAttribute | [AttributeUsageAttribute(...)] | 0 | 1 |
19131912
| System.Runtime.AssemblyTargetedPatchBandAttribute | [NullableAttribute(...)] | 0 | 0 |
19141913
| System.Runtime.AssemblyTargetedPatchBandAttribute | [NullableContextAttribute(...)] | 0 | 1 |
19151914
| System.Runtime.CompilerServices.AccessedThroughPropertyAttribute | [AttributeUsageAttribute(...)] | 0 | 256 |
@@ -1959,6 +1958,7 @@ attrArgPositional
19591958
| System.Runtime.CompilerServices.DependencyAttribute | [NullableAttribute(...)] | 0 | 0 |
19601959
| System.Runtime.CompilerServices.DependencyAttribute | [NullableContextAttribute(...)] | 0 | 1 |
19611960
| System.Runtime.CompilerServices.DisablePrivateReflectionAttribute | [AttributeUsageAttribute(...)] | 0 | 1 |
1961+
| System.Runtime.CompilerServices.EnumeratorCancellationAttribute | [AttributeUsageAttribute(...)] | 0 | 2048 |
19621962
| System.Runtime.CompilerServices.ExtensionAttribute | [AttributeUsageAttribute(...)] | 0 | 69 |
19631963
| System.Runtime.CompilerServices.FixedAddressValueTypeAttribute | [AttributeUsageAttribute(...)] | 0 | 256 |
19641964
| System.Runtime.CompilerServices.FixedBufferAttribute | [AttributeUsageAttribute(...)] | 0 | 256 |
@@ -2014,6 +2014,7 @@ attrArgPositional
20142014
| System.Runtime.CompilerServices.TypeForwardedToAttribute | [NullableAttribute(...)] | 0 | 0 |
20152015
| System.Runtime.CompilerServices.TypeForwardedToAttribute | [NullableContextAttribute(...)] | 0 | 1 |
20162016
| System.Runtime.CompilerServices.UnsafeValueTypeAttribute | [AttributeUsageAttribute(...)] | 0 | 8 |
2017+
| System.Runtime.ConstrainedExecution.PrePrepareMethodAttribute | [AttributeUsageAttribute(...)] | 0 | 96 |
20172018
| System.Runtime.ConstrainedExecution.ReliabilityContractAttribute | [AttributeUsageAttribute(...)] | 0 | 1133 |
20182019
| System.Runtime.ExceptionServices.ExceptionDispatchInfo | [NullableAttribute(...)] | 0 | 0 |
20192020
| System.Runtime.ExceptionServices.ExceptionDispatchInfo | [NullableContextAttribute(...)] | 0 | 1 |
@@ -2246,6 +2247,7 @@ attrArgPositional
22462247
| System.Runtime.Serialization.SerializationInfoEnumerator | [NullableContextAttribute(...)] | 0 | 1 |
22472248
| System.Runtime.Serialization.StreamingContext | [NullableAttribute(...)] | 0 | 0 |
22482249
| System.Runtime.Serialization.StreamingContext | [NullableContextAttribute(...)] | 0 | 2 |
2250+
| System.Runtime.TargetedPatchingOptOutAttribute | [AttributeUsageAttribute(...)] | 0 | 96 |
22492251
| System.Runtime.TargetedPatchingOptOutAttribute | [NullableAttribute(...)] | 0 | 0 |
22502252
| System.Runtime.TargetedPatchingOptOutAttribute | [NullableContextAttribute(...)] | 0 | 1 |
22512253
| System.Runtime.Versioning.NonVersionableAttribute | [AttributeUsageAttribute(...)] | 0 | 108 |

0 commit comments

Comments
 (0)