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
2 changes: 1 addition & 1 deletion eng/submodules/silk.net-2.x
14 changes: 7 additions & 7 deletions sources/Core/Core/Annotations/NameAffixAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Silk.NET.Core;
/// </summary>
[AttributeUsage(
AttributeTargets.Class
| AttributeTargets.Delegate
| AttributeTargets.Enum
| AttributeTargets.Field
| AttributeTargets.Method
| AttributeTargets.Parameter
| AttributeTargets.Property
| AttributeTargets.Struct,
| AttributeTargets.Delegate
| AttributeTargets.Enum
| AttributeTargets.Field
| AttributeTargets.Method
| AttributeTargets.Parameter
| AttributeTargets.Property
| AttributeTargets.Struct,
AllowMultiple = true,
Inherited = true
)]
Expand Down
14 changes: 7 additions & 7 deletions sources/Core/Core/Annotations/NativeNameAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Silk.NET.Core;
/// </summary>
[AttributeUsage(
AttributeTargets.Class
| AttributeTargets.Delegate
| AttributeTargets.Enum
| AttributeTargets.Field
| AttributeTargets.Method
| AttributeTargets.Parameter
| AttributeTargets.Property
| AttributeTargets.Struct,
| AttributeTargets.Delegate
| AttributeTargets.Enum
| AttributeTargets.Field
| AttributeTargets.Method
| AttributeTargets.Parameter
| AttributeTargets.Property
| AttributeTargets.Struct,
AllowMultiple = false,
Inherited = true
)]
Expand Down
9 changes: 7 additions & 2 deletions sources/SilkTouch/SilkTouch/Mods/AddIncludes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ public async Task<List<ResponseFile>> BeforeScrapeAsync(string key, List<Respons
var rsp = rsps[i];
var cmdLineArgs = rsp.ClangCommandLineArgs.ToList();

cmdLineArgs.InsertRange(0, cfg.PriorityIncludes?.Select(x => $"--include-directory={x}") ?? []);
cmdLineArgs.AddRange(cfg.AdditionalIncludes?.Select(x => $"--include-directory={x}") ?? []);
cmdLineArgs.InsertRange(
0,
cfg.PriorityIncludes?.Select(x => $"--include-directory={x}") ?? []
);
cmdLineArgs.AddRange(
cfg.AdditionalIncludes?.Select(x => $"--include-directory={x}") ?? []
);

if (!cfg.SuppressStdIncludes)
{
Expand Down
8 changes: 6 additions & 2 deletions sources/SilkTouch/SilkTouch/Mods/AddVTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,13 @@ out var callConv
x.WithAttributes(
SeparatedList(
x.Attributes.Where(y =>
!y.IsAttribute("System.Runtime.InteropServices.DllImport")
!y.IsAttribute(
"System.Runtime.InteropServices.DllImport"
)
&& !y.IsAttribute("Silk.NET.Core.NativeFunction")
&& !y.IsAttribute("System.Runtime.CompilerServices.MethodImpl")
&& !y.IsAttribute(
"System.Runtime.CompilerServices.MethodImpl"
)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ node is BaseMethodDeclarationSyntax meth
node.AttributeLists.Select(x =>
x.WithAttributes(
SeparatedList(
x.Attributes.Where(y => y.IsAttribute("Silk.NET.Core.SupportedApiAttribute"))
x.Attributes.Where(y =>
y.IsAttribute("Silk.NET.Core.SupportedApiAttribute")
)
)
)
)
Expand Down
145 changes: 101 additions & 44 deletions sources/SilkTouch/SilkTouch/Mods/Common/AttributeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public static bool IsAttribute(this AttributeSyntax node, string fullNameWithout
var sep = node.Name.ToString().Split("::").Last();
var name = fullNameWithoutSuffix.Split('.').Last();
return sep == name
|| sep == $"{name}Attribute"
|| sep.EndsWith(fullNameWithoutSuffix)
|| sep.EndsWith($"{fullNameWithoutSuffix}Attribute");
|| sep == $"{name}Attribute"
|| sep.EndsWith(fullNameWithoutSuffix)
|| sep.EndsWith($"{fullNameWithoutSuffix}Attribute");
}

/// <summary>
Expand All @@ -41,8 +41,13 @@ public static bool IsAttribute(this AttributeSyntax node, string fullNameWithout
/// <param name="fullNameWithoutSuffix">
/// The fully-qualified attribute name including the namespace but without the <c>Attribute</c> suffix.
/// </param>
public static bool ContainsAttribute(this IEnumerable<AttributeListSyntax> attributeLists, string fullNameWithoutSuffix) =>
attributeLists.Any(list => list.Attributes.Any(attribute => attribute.IsAttribute(fullNameWithoutSuffix)));
public static bool ContainsAttribute(
this IEnumerable<AttributeListSyntax> attributeLists,
string fullNameWithoutSuffix
) =>
attributeLists.Any(list =>
list.Attributes.Any(attribute => attribute.IsAttribute(fullNameWithoutSuffix))
);

/// <summary>
/// Modifies the <see cref="DllImportAttribute"/>s the method may have to make them resistant to method identifier
Expand Down Expand Up @@ -209,8 +214,12 @@ out _
/// Use false if not (outside or appended to end).
/// True means that the attribute is added to the start of the attribute list, meaning that the affix is re-appended earlier.
/// </param>
public static SyntaxList<AttributeListSyntax> AddNamePrefix(this IEnumerable<AttributeListSyntax> attributeLists, string category, string prefix, bool addToInner = false)
=> attributeLists.AddNamePrefixOrSuffix("Prefix", category, prefix, addToInner);
public static SyntaxList<AttributeListSyntax> AddNamePrefix(
this IEnumerable<AttributeListSyntax> attributeLists,
string category,
string prefix,
bool addToInner = false
) => attributeLists.AddNamePrefixOrSuffix("Prefix", category, prefix, addToInner);

/// <summary>
/// Adds a name suffix attribute to the given attribute list.
Expand All @@ -223,23 +232,38 @@ public static SyntaxList<AttributeListSyntax> AddNamePrefix(this IEnumerable<Att
/// Use false if not (outside or appended to end).
/// True means that the attribute is added to the start of the attribute list, meaning that the affix is re-appended earlier.
/// </param>
public static SyntaxList<AttributeListSyntax> AddNameSuffix(this IEnumerable<AttributeListSyntax> attributeLists, string category, string suffix, bool addToInner = false)
=> attributeLists.AddNamePrefixOrSuffix("Suffix", category, suffix, addToInner);

private static SyntaxList<AttributeListSyntax> AddNamePrefixOrSuffix(this IEnumerable<AttributeListSyntax> attributeLists, string type, string category, string affix, bool addToInner = false)
public static SyntaxList<AttributeListSyntax> AddNameSuffix(
this IEnumerable<AttributeListSyntax> attributeLists,
string category,
string suffix,
bool addToInner = false
) => attributeLists.AddNamePrefixOrSuffix("Suffix", category, suffix, addToInner);

private static SyntaxList<AttributeListSyntax> AddNamePrefixOrSuffix(
this IEnumerable<AttributeListSyntax> attributeLists,
string type,
string category,
string affix,
bool addToInner = false
)
{
var typeArgument = AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal($"\"{type}\"", type)));
var categoryArgument = AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal($"\"{category}\"", category)));
var affixArgument = AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal($"\"{affix}\"", affix)));
var typeArgument = AttributeArgument(
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal($"\"{type}\"", type))
);
var categoryArgument = AttributeArgument(
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal($"\"{category}\"", category)
)
);
var affixArgument = AttributeArgument(
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal($"\"{affix}\"", affix))
);
var argumentList = AttributeArgumentList([typeArgument, categoryArgument, affixArgument]);

var attribute = AttributeList([
Attribute(IdentifierName("NameAffix"), argumentList),
]);
var attribute = AttributeList([Attribute(IdentifierName("NameAffix"), argumentList)]);

return addToInner
? [attribute, ..attributeLists]
: [..attributeLists, attribute];
return addToInner ? [attribute, .. attributeLists] : [.. attributeLists, attribute];
}

/// <summary>
Expand All @@ -248,7 +272,10 @@ private static SyntaxList<AttributeListSyntax> AddNamePrefixOrSuffix(this IEnume
/// <remarks>
/// The default usually should be the node's identifier.
/// </remarks>
public static string GetNativeNameOrDefault(this IEnumerable<AttributeListSyntax> attributeLists, SyntaxToken identifier)
public static string GetNativeNameOrDefault(
this IEnumerable<AttributeListSyntax> attributeLists,
SyntaxToken identifier
)
{
if (TryGetNativeName(attributeLists, out var nativeName))
{
Expand All @@ -264,7 +291,10 @@ public static string GetNativeNameOrDefault(this IEnumerable<AttributeListSyntax
/// <remarks>
/// The default usually should be the node's identifier.
/// </remarks>
public static string GetNativeNameOrDefault(this IEnumerable<AttributeListSyntax> attributeLists, string defaultName)
public static string GetNativeNameOrDefault(
this IEnumerable<AttributeListSyntax> attributeLists,
string defaultName
)
{
if (TryGetNativeName(attributeLists, out var nativeName))
{
Expand All @@ -277,9 +307,14 @@ public static string GetNativeNameOrDefault(this IEnumerable<AttributeListSyntax
/// <summary>
/// Gets the value of the native name attribute from the given attribute list.
/// </summary>
public static bool TryGetNativeName(this IEnumerable<AttributeListSyntax> attributeLists, [NotNullWhen(true)] out string? nativeName)
public static bool TryGetNativeName(
this IEnumerable<AttributeListSyntax> attributeLists,
[NotNullWhen(true)] out string? nativeName
)
{
var nativeNameAttribute = attributeLists.SelectMany(list => list.Attributes).FirstOrDefault(attribute => attribute.IsAttribute("Silk.NET.Core.NativeName"));
var nativeNameAttribute = attributeLists
.SelectMany(list => list.Attributes)
.FirstOrDefault(attribute => attribute.IsAttribute("Silk.NET.Core.NativeName"));
if (nativeNameAttribute == null)
{
nativeName = null;
Expand All @@ -295,25 +330,47 @@ public static bool TryGetNativeName(this IEnumerable<AttributeListSyntax> attrib
/// <summary>
/// Sets or replaces the native name attribute in the given attribute list.
/// </summary>
public static SyntaxList<AttributeListSyntax> WithNativeName(this IEnumerable<AttributeListSyntax> attributeLists, string nativeName)
public static SyntaxList<AttributeListSyntax> WithNativeName(
this IEnumerable<AttributeListSyntax> attributeLists,
string nativeName
)
{
var nativeNameAttribute = AttributeList([
Attribute(
IdentifierName("NativeName"),
AttributeArgumentList([
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal($"\"{nativeName}\"", nativeName))),
])),
]);

return List(attributeLists.Select(list => {
var attributes = list.Attributes;
attributes = [..attributes.Where(attribute => !attribute.IsAttribute("Silk.NET.Core.NativeName"))];

return attributes.Count == 0 ? null : list.WithAttributes(attributes);
})
.Where(list => list != null)
.Cast<AttributeListSyntax>()
.Prepend(nativeNameAttribute));
var nativeNameAttribute = AttributeList(
[
Attribute(
IdentifierName("NativeName"),
AttributeArgumentList(
[
AttributeArgument(
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal($"\"{nativeName}\"", nativeName)
)
),
]
)
),
]
);

return List(
attributeLists
.Select(list =>
{
var attributes = list.Attributes;
attributes =
[
.. attributes.Where(attribute =>
!attribute.IsAttribute("Silk.NET.Core.NativeName")
),
];

return attributes.Count == 0 ? null : list.WithAttributes(attributes);
})
.Where(list => list != null)
.Cast<AttributeListSyntax>()
.Prepend(nativeNameAttribute)
);
}

/// <summary>
Expand Down Expand Up @@ -368,7 +425,7 @@ out NativeTypeNameInfo info
switch (nativeTypeName)
{
// Handle case: "#define NAME VALUE"
case {} when nativeTypeName.StartsWith("#define "):
case { } when nativeTypeName.StartsWith("#define "):
{
// Trim off the #define
var nativeTypeSpan = nativeTypeName["#define ".Length..].Trim();
Expand Down Expand Up @@ -404,7 +461,7 @@ out NativeTypeNameInfo info
}

// Handle cases: "const NAME **", "NAME **"
case {}:
case { }:
{
// Trim off the const
var isConst = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public Dictionary<string, UsingDirectiveSyntax> UsingsToAdd
return ret;
}

foreach (var use in comp.Usings.Where(use => !use.GlobalKeyword.IsKind(SyntaxKind.GlobalKeyword)))
foreach (
var use in comp.Usings.Where(use => !use.GlobalKeyword.IsKind(SyntaxKind.GlobalKeyword))
)
{
AddUsing(use);
}
Expand Down Expand Up @@ -68,7 +70,9 @@ and not SyntaxKind.MultiLineCommentTrivia
FileScopedNamespaceDeclarationSyntax node
)
{
foreach (var use in node.Usings.Where(use => !use.GlobalKeyword.IsKind(SyntaxKind.GlobalKeyword)))
foreach (
var use in node.Usings.Where(use => !use.GlobalKeyword.IsKind(SyntaxKind.GlobalKeyword))
)
{
AddUsing(use);
}
Expand Down
10 changes: 8 additions & 2 deletions sources/SilkTouch/SilkTouch/Mods/Common/ModUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,20 @@ public static string FullPath(this Project proj, string relativePath) =>
/// <summary>
/// Searches and replaces all occurrences of the <paramref name="oldValue"/> with the <paramref name="newValue"/> in the document name and project *relative* file path.
/// </summary>
public static Document ReplaceNameAndPath(this Document document, string oldValue, string newValue)
public static Document ReplaceNameAndPath(
this Document document,
string oldValue,
string newValue
)
{
document = document.WithName(document.Name.Replace(oldValue, newValue));

var relativePath = document.RelativePath();
if (relativePath != null)
{
document = document.WithFilePath(FullPath(document.Project, relativePath.Replace(oldValue, newValue)));
document = document.WithFilePath(
FullPath(document.Project, relativePath.Replace(oldValue, newValue))
);
}

return document;
Expand Down
Loading