-
Notifications
You must be signed in to change notification settings - Fork 169
Adding new config options for ClangSharp #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ae4af5a
ba8c7a5
c8ea7bc
1fbcd81
24f9398
f4e16bc
f1a7fc8
dd2a7a7
e0951a6
417fb65
695190c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -431,11 +431,13 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) | |
| type = attributedType.ModifiedType; | ||
| callConv = attributedType.Handle.FunctionTypeCallingConv; | ||
| } | ||
| var functionType = (FunctionType)type; | ||
|
|
||
| if (callConv == CXCallingConv.CXCallingConv_Invalid) | ||
| if (type is FunctionType functionType) | ||
| { | ||
| callConv = functionType.CallConv; | ||
| if (callConv == CXCallingConv.CXCallingConv_Invalid) | ||
| { | ||
| callConv = functionType.CallConv; | ||
| } | ||
| } | ||
|
|
||
| var cxxMethodDecl = functionDecl as CXXMethodDecl; | ||
|
|
@@ -740,6 +742,7 @@ void ForFunctionDecl(ParmVarDecl parmVarDecl, FunctionDecl functionDecl) | |
| } | ||
|
|
||
| AddNativeTypeNameAttribute(nativeTypeName, prefix: "", postfix: " "); | ||
| AddCppAttributes(parmVarDecl, prefix: "", postfix: " "); | ||
|
|
||
| _outputBuilder.Write(typeName); | ||
| _outputBuilder.Write(' '); | ||
|
|
@@ -775,6 +778,7 @@ void ForTypedefDecl(ParmVarDecl parmVarDecl, TypedefDecl typedefDecl) | |
| var type = parmVarDecl.Type; | ||
| var typeName = GetRemappedTypeName(parmVarDecl, context: null, type, out var nativeTypeName); | ||
| AddNativeTypeNameAttribute(nativeTypeName, prefix: "", postfix: " "); | ||
| AddCppAttributes(parmVarDecl, prefix: "", postfix: " "); | ||
|
|
||
| _outputBuilder.Write(typeName); | ||
| _outputBuilder.Write(' '); | ||
|
|
@@ -934,6 +938,7 @@ private void VisitRecordDecl(RecordDecl recordDecl) | |
| } | ||
|
|
||
| AddNativeTypeNameAttribute(nativeTypeNameBuilder.ToString()); | ||
| AddNativeInheritanceAttribute(GetCursorName(cxxRecordDecl.Bases.Last().Referenced)); | ||
| } | ||
|
|
||
| _outputBuilder.WriteIndented(GetAccessSpecifierName(recordDecl)); | ||
|
|
@@ -1590,6 +1595,11 @@ void VisitAnonymousRecordDecl(RecordDecl recordDecl, RecordDecl nestedRecordDecl | |
|
|
||
| void VisitAnonymousRecordDeclFields(RecordDecl rootRecordDecl, RecordDecl anonymousRecordDecl, string contextType, string contextName) | ||
| { | ||
| if (_config.ExcludeAnonymousFieldHelpers) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think this should be a general |
||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var declaration in anonymousRecordDecl.Decls) | ||
| { | ||
| if (declaration is FieldDecl fieldDecl) | ||
|
|
@@ -2489,9 +2499,26 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType) | |
| { | ||
| ForPointeeType(typedefDecl, parentType: null, referenceType.PointeeType); | ||
| } | ||
| else if (underlyingType is TagType) | ||
| else if (underlyingType is TagType underlyingTagType) | ||
| { | ||
| // Nothing to do for tag types | ||
| // See if there's a potential typedef remapping we want to log | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Just for consistency, I think this logic should be in a |
||
| if (_config.LogPotentialTypedefRemappings) | ||
| { | ||
| var typedefName = typedefDecl.UnderlyingDecl.Name; | ||
| var possibleNamesToRemap = new string[] { "_" + typedefName, "_tag" + typedefName, "tag" + typedefName }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just looking at the Windows bindings I've done for my own project, what about |
||
| var underlyingName = underlyingTagType.AsString; | ||
|
|
||
| foreach (var possibleNameToRemap in possibleNamesToRemap) | ||
| { | ||
| if (!_config.RemappedNames.ContainsKey(possibleNameToRemap)) | ||
| { | ||
| if (possibleNameToRemap == underlyingName) | ||
| { | ||
| AddDiagnostic(DiagnosticLevel.Info, $"Potential remap: {possibleNameToRemap}={typedefName}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else if (underlyingType is TypedefType typedefType) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -295,6 +295,94 @@ private void AddDiagnostic(DiagnosticLevel level, string message, Cursor cursor) | |
| _diagnostics.Add(diagnostic); | ||
| } | ||
|
|
||
| private void AddCppAttributes(ParmVarDecl parmVarDecl, string prefix = null, string postfix = null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just want to confirm. This basically saves all the C++ attributes separated by |
||
| { | ||
| if (!_config.GenerateCppAttributes) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (parmVarDecl.Attrs.Count == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (prefix is null) | ||
| { | ||
| _outputBuilder.WriteIndentation(); | ||
| } | ||
| else | ||
| { | ||
| _outputBuilder.WriteNewlineIfNeeded(); | ||
| _outputBuilder.Write(prefix); | ||
| } | ||
|
|
||
| _outputBuilder.Write($"[CppAttributeList(\""); | ||
|
|
||
| _outputBuilder.Write(EscapeString(parmVarDecl.Attrs[0].Spelling)); | ||
| for (int i = 1; i < parmVarDecl.Attrs.Count; i++) | ||
| { | ||
| // Separator char between attributes | ||
| _outputBuilder.Write('^'); | ||
|
|
||
| _outputBuilder.Write(EscapeString(parmVarDecl.Attrs[i].Spelling)); | ||
| } | ||
|
|
||
| _outputBuilder.Write($"\")]"); | ||
|
|
||
| if (postfix is null) | ||
| { | ||
| _outputBuilder.NeedsNewline = true; | ||
| } | ||
| else | ||
| { | ||
| _outputBuilder.Write(postfix); | ||
| } | ||
| } | ||
|
|
||
| private void AddNativeInheritanceAttribute(string inheritedFromName, string prefix = null, string postfix = null, string attributePrefix = null) | ||
| { | ||
| if (!_config.GenerateNativeInheritanceAttribute) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (prefix is null) | ||
| { | ||
| _outputBuilder.WriteIndentation(); | ||
| } | ||
| else | ||
| { | ||
| _outputBuilder.WriteNewlineIfNeeded(); | ||
| _outputBuilder.Write(prefix); | ||
| } | ||
|
|
||
| _outputBuilder.Write('['); | ||
|
|
||
| if (attributePrefix != null) | ||
| { | ||
| _outputBuilder.Write(attributePrefix); | ||
| } | ||
|
|
||
| _outputBuilder.Write("NativeInheritance"); | ||
| _outputBuilder.Write('('); | ||
|
|
||
| _outputBuilder.Write('"'); | ||
| _outputBuilder.Write(EscapeString(inheritedFromName)); | ||
| _outputBuilder.Write('"'); | ||
| _outputBuilder.Write(')'); | ||
| _outputBuilder.Write(']'); | ||
|
|
||
| if (postfix is null) | ||
| { | ||
| _outputBuilder.NeedsNewline = true; | ||
| } | ||
| else | ||
| { | ||
| _outputBuilder.Write(postfix); | ||
| } | ||
| } | ||
|
|
||
| private void AddNativeTypeNameAttribute(string nativeTypeName, string prefix = null, string postfix = null, string attributePrefix = null) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(nativeTypeName)) | ||
|
|
@@ -991,10 +1079,10 @@ private string GetRemappedCursorName(NamedDecl namedDecl) | |
| } | ||
| else if ((namedDecl is RecordDecl recordDecl) && name.StartsWith("__AnonymousRecord_")) | ||
| { | ||
| remappedName = "_Anonymous"; | ||
|
|
||
| if (recordDecl.Parent is RecordDecl parentRecordDecl) | ||
| { | ||
| remappedName = "_Anonymous"; | ||
|
|
||
| var matchingField = parentRecordDecl.Fields.Where((fieldDecl) => fieldDecl.Type.CanonicalType == recordDecl.TypeForDecl.CanonicalType).FirstOrDefault(); | ||
|
|
||
| if (matchingField != null) | ||
|
|
@@ -1007,8 +1095,9 @@ private string GetRemappedCursorName(NamedDecl namedDecl) | |
| var index = parentRecordDecl.AnonymousDecls.IndexOf(recordDecl) + 1; | ||
| remappedName += index.ToString(); | ||
| } | ||
|
|
||
| remappedName += $"_e__{(recordDecl.IsUnion ? "Union" : "Struct")}"; | ||
| } | ||
| remappedName += $"_e__{(recordDecl.IsUnion ? "Union" : "Struct")}"; | ||
| } | ||
|
|
||
| return remappedName; | ||
|
|
@@ -1087,6 +1176,10 @@ private string GetRemappedTypeName(Cursor cursor, Cursor context, Type type, out | |
|
|
||
| name += $"_e__{(recordDecl.IsUnion ? "Union" : "Struct")}"; | ||
| } | ||
| else if ((canonicalType is EnumType enumType) && name.StartsWith("__AnonymousEnum_")) | ||
| { | ||
| name = GetRemappedTypeName(enumType.Decl, context: null, enumType.Decl.IntegerType, out _); | ||
| } | ||
| else if (cursor is EnumDecl enumDecl) | ||
| { | ||
| var enumDeclName = GetRemappedCursorName(enumDecl); | ||
|
|
@@ -1906,6 +1999,14 @@ private bool IsExcluded(Cursor cursor, out bool isExcludedByConflictingDefinitio | |
| return false; | ||
| } | ||
|
|
||
| if (_config.ExcludeFunctionsWithBody && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| cursor is FunctionDecl functionDecl && | ||
| cursor.CursorKind == CXCursorKind.CXCursor_FunctionDecl && | ||
| functionDecl.HasBody) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return IsExcludedByFile(cursor) || IsExcludedByName(cursor, out isExcludedByConflictingDefinition); | ||
|
|
||
| bool IsAlwaysIncluded(Cursor cursor) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Simplify the nesting level here