Skip to content

Commit

Permalink
Support ignoring types by regex (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
lithiumtoast authored Jan 17, 2025
1 parent aa98d5f commit 37c7d31
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/c/tests/functions/function_ignored/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"ignoredIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"ignoredFunctions": [
"ignoredNames": [
"function_not_allowed",
"function_ignored*"
],
Expand Down
2 changes: 1 addition & 1 deletion src/c/tests/macro_objects/macro_object_ignored/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"ignoredIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"ignoredMacroObjects": [
"ignoredNames": [
"MACRO_OBJECT_NOT_ALLOWED",
"MACRO_OBJECT_IGNORED*"
],
Expand Down
2 changes: 1 addition & 1 deletion src/c/tests/variables/variable_ignored/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"ignoredIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"ignoredVariables": [
"ignoredNames": [
"variable_not_allowed",
"variable_ignored*"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ private CType VisitTypeInternal(
return type;
}

foreach (var regex in ParseContext.InputSanitized.IgnoreNameRegexes)
{
if (regex.IsMatch(typeName))
{
return type;
}
}

var info = CreateNodeInfo(type.NodeKind, type.Name, type.Name, clangCursor, clangType, parentInfo);
TryEnqueueNode(info);
return type;
Expand Down
7 changes: 1 addition & 6 deletions src/cs/production/c2ffi.Tool/Extract/Explore/Explorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ private void VisitTranslationUnit(ExploreContext exploreContext, ParseContext pa
{
LogVisitingTranslationUnit(parseContext.FilePath);

var isMultipleHeaders = !exploreContext.ParseContext.InputSanitized.IsSingleHeader;
if (isMultipleHeaders)
{
VisitIncludes(exploreContext, parseContext);
}

VisitIncludes(exploreContext, parseContext);
VisitFunctions(exploreContext, parseContext);
VisitVariables(exploreContext, parseContext);
VisitMacroObjects(exploreContext, parseContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@ public sealed class InputSanitizedTargetPlatform

public bool IsEnabledFindSystemHeaders { get; init; }

public bool IsSingleHeader { get; init; }

public ImmutableHashSet<string> IncludedNames { get; init; } = ImmutableHashSet<string>.Empty;

public ImmutableArray<Regex> IgnoreMacroObjectsRegexes { get; init; } = ImmutableArray<Regex>.Empty;

public ImmutableArray<Regex> IgnoreVariableRegexes { get; init; } = ImmutableArray<Regex>.Empty;

public ImmutableArray<Regex> IgnoreFunctionRegexes { get; init; } = ImmutableArray<Regex>.Empty;
public ImmutableArray<Regex> IgnoreNameRegexes { get; init; } = ImmutableArray<Regex>.Empty;

public override string ToString()
{
Expand Down
19 changes: 3 additions & 16 deletions src/cs/production/c2ffi.Tool/Extract/InputSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,8 @@ private InputSanitizedTargetPlatform SanitizeTargetPlatformInput(
MacroObjectDefines = ClangDefines(input, targetPlatformInput),
AdditionalArguments = ClangArguments(targetPlatformInput),
IsEnabledFindSystemHeaders = input.IsEnabledAutomaticallyFindSystemHeaders ?? true,
IsSingleHeader = input.IsSingleHeader ?? false,
IncludedNames = IncludedNames(input),
IgnoreMacroObjectsRegexes = IgnoredMacroObjects(input),
IgnoreVariableRegexes = IgnoredVariables(input),
IgnoreFunctionRegexes = IgnoredFunctions(input)
IgnoreNameRegexes = IgnoredNames(input),
};

return options;
Expand Down Expand Up @@ -174,19 +171,9 @@ private ImmutableArray<string> ClangArguments(InputUnsanitizedTargetPlatform tar
return SanitizeStrings(targetPlatformInput.ClangArguments);
}

private ImmutableArray<Regex> IgnoredMacroObjects(InputUnsanitized input)
private ImmutableArray<Regex> IgnoredNames(InputUnsanitized input)
{
return SanitizeRegexes(input.IgnoredMacroObjects);
}

private ImmutableArray<Regex> IgnoredVariables(InputUnsanitized input)
{
return SanitizeRegexes(input.IgnoredVariables);
}

private ImmutableArray<Regex> IgnoredFunctions(InputUnsanitized input)
{
return SanitizeRegexes(input.IgnoredFunctions);
return SanitizeRegexes(input.IgnoredNames);
}

private ImmutableHashSet<string> IncludedNames(InputUnsanitized input)
Expand Down
42 changes: 7 additions & 35 deletions src/cs/production/c2ffi.Tool/Extract/InputUnsanitized.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,55 +69,27 @@ public sealed class InputUnsanitized : ToolUnsanitizedInput
public bool? IsEnabledAutomaticallyFindSystemHeaders { get; set; }

/// <summary>
/// Gets or sets a value that determines whether the C code is parsed as a single header or multiple headers.
/// </summary>
/// <para>
/// Default is <c>false</c>. Use <c>true</c> to parse the C code as a single header. Use <c>false</c> to parse
/// the C code as multiple headers.
/// </para>
[JsonPropertyName("isSingleHeader")]
public bool? IsSingleHeader { get; set; }

/// <summary>
/// Gets or sets the cursor names to be treated as opaque types.
/// Gets or sets the Clang cursor names to be treated as opaque types.
/// </summary>
[JsonPropertyName("opaqueTypeNames")]
public ImmutableArray<string>? OpaqueTypeNames { get; set; }

/// <summary>
/// Gets or sets the name of macro objects that are not allowed. Use <c>null</c> to allow all macro objects.
/// </summary>
[JsonPropertyName("ignoredMacroObjects")]
public ImmutableArray<string>? IgnoredMacroObjects { get; set; }

/// <summary>
/// Gets or sets the name of variables that are not allowed. Use <c>null</c> to allow all variables.
/// Gets or sets the name of Clang cursors / Clang types that are not allowed.
/// </summary>
[JsonPropertyName("ignoredVariables")]
public ImmutableArray<string>? IgnoredVariables { get; set; }
[JsonPropertyName("ignoredNames")]
public ImmutableArray<string>? IgnoredNames { get; set; }

/// <summary>
/// Gets or sets the name of functions that are not allowed. Use <c>null</c> to allow all functions.
/// Gets or sets the name of Clang cursors / Clang types that are explicitly allowed.
/// </summary>
[JsonPropertyName("ignoredFunctions")]
public ImmutableArray<string>? IgnoredFunctions { get; set; }
[JsonPropertyName("includedNames")]
public ImmutableArray<string>? IncludedNames { get; set; }

/// <summary>
/// Gets or sets the target platform configurations for extracting the FFIs per desktop host
/// operating system.
/// </summary>
[JsonPropertyName("targetPlatforms")]
public ImmutableDictionary<string, ImmutableDictionary<string, InputUnsanitizedTargetPlatform>>? TargetPlatforms { get; set; }

/// <summary>
/// Gets or sets the names of libraries and/or interfaces for macOS, iOS, tvOS or watchOS.
/// </summary>
[JsonPropertyName("appleFrameworks")]
public ImmutableArray<string>? AppleFrameworks { get; set; }

/// <summary>
/// Gets or sets the name of enums that are explicitly allowed.
/// </summary>
[JsonPropertyName("includedNames")]
public ImmutableArray<string>? IncludedNames { get; set; }
}
6 changes: 3 additions & 3 deletions src/cs/production/c2ffi.Tool/Extract/Parse/ParseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static bool IsExternalFunction(ParseContext parseContext, clang.CXCursor cursor,
}

var name = cursor.Spelling();
var isIgnored = IsIgnored(name, parseContext.InputSanitized.IgnoreFunctionRegexes);
var isIgnored = IsIgnored(name, parseContext.InputSanitized.IgnoreNameRegexes);
return !isIgnored;
}
}
Expand All @@ -225,7 +225,7 @@ static bool IsExternalVariable(ParseContext parseContext, clang.CXCursor cursor,
}

var name = cursor.Spelling();
var isIgnored = IsIgnored(name, parseContext.InputSanitized.IgnoreVariableRegexes);
var isIgnored = IsIgnored(name, parseContext.InputSanitized.IgnoreNameRegexes);
return !isIgnored;
}
}
Expand Down Expand Up @@ -254,7 +254,7 @@ static bool IsMacroObject(ParseContext parseContext, clang.CXCursor cursor, clan
}

var name = cursor.Spelling();
var isIgnored = IsIgnored(name, parseContext.InputSanitized.IgnoreMacroObjectsRegexes);
var isIgnored = IsIgnored(name, parseContext.InputSanitized.IgnoreNameRegexes);
return !isIgnored;
}
}
Expand Down

0 comments on commit 37c7d31

Please sign in to comment.