Skip to content

Commit

Permalink
Restored the removal of some useless members generated by the Q_OBJEC…
Browse files Browse the repository at this point in the history
…T macro.

Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Jun 9, 2016
1 parent f5ede4a commit c5e5d93
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions QtSharp/QtSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public void SetupPasses(Driver driver)
driver.TranslationUnitPasses.AddPass(new CompileInlinesPass(this.qtInfo.QMake, this.qtInfo.Make));
driver.TranslationUnitPasses.AddPass(new GenerateSignalEventsPass());
driver.TranslationUnitPasses.AddPass(new GenerateEventEventsPass());
driver.TranslationUnitPasses.AddPass(new RemoveQObjectMembersPass());
}

private readonly QtInfo qtInfo;
Expand Down
1 change: 1 addition & 0 deletions QtSharp/QtSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="DocGeneration\MemberDocumentationNode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProcessHelper.cs" />
<Compile Include="RemoveQObjectMembersPass.cs" />
<None Include="packages.config" />
<None Include="_iobuf.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
54 changes: 54 additions & 0 deletions QtSharp/RemoveQObjectMembersPass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;
using CppSharp.Passes;

namespace QtSharp
{
public class RemoveQObjectMembersPass : TranslationUnitPass
{
public override bool VisitClassDecl(Class @class)
{
if (AlreadyVisited(@class) || @class.Name == "QObject")
{
return false;
}

IEnumerable<MacroExpansion> expansions = @class.PreprocessedEntities.OfType<MacroExpansion>();

bool isQObject = expansions.Any(e => e.Text == "Q_OBJECT");
if (isQObject)
{
RemoveQObjectMembers(@class);
}
return true;
}

private static void RemoveQObjectMembers(Class @class)
{
// Every Qt object "inherits" a lot of members via the Q_OBJECT macro.
// See the define of Q_OBJECT in qobjectdefs.h for a list of the members.
// We cannot use the Qt defines for disabling the expansion of these
// because it would mess up with the object layout size.

RemoveMethodOverloads(@class, "tr");
RemoveMethodOverloads(@class, "trUtf8");
RemoveMethodOverloads(@class, "qt_static_metacall");
RemoveVariables(@class, "staticMetaObject");
}

private static void RemoveMethodOverloads(Class @class, string originalName)
{
var overloads = @class.FindMethodByOriginalName(originalName).ToList();
foreach (var method in overloads)
@class.Methods.Remove(method);
}

private static void RemoveVariables(Class @class, string originalName)
{
var variables = @class.FindVariableByOriginalName(originalName).ToList();
foreach (var variable in variables)
variable.ExplicitlyIgnore();
}
}
}

0 comments on commit c5e5d93

Please sign in to comment.