Skip to content

Commit c5e5d93

Browse files
committed
Restored the removal of some useless members generated by the Q_OBJECT macro.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent f5ede4a commit c5e5d93

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

QtSharp/QtSharp.cs

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ public void SetupPasses(Driver driver)
295295
driver.TranslationUnitPasses.AddPass(new CompileInlinesPass(this.qtInfo.QMake, this.qtInfo.Make));
296296
driver.TranslationUnitPasses.AddPass(new GenerateSignalEventsPass());
297297
driver.TranslationUnitPasses.AddPass(new GenerateEventEventsPass());
298+
driver.TranslationUnitPasses.AddPass(new RemoveQObjectMembersPass());
298299
}
299300

300301
private readonly QtInfo qtInfo;

QtSharp/QtSharp.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="DocGeneration\MemberDocumentationNode.cs" />
7777
<Compile Include="Properties\AssemblyInfo.cs" />
7878
<Compile Include="ProcessHelper.cs" />
79+
<Compile Include="RemoveQObjectMembersPass.cs" />
7980
<None Include="packages.config" />
8081
<None Include="_iobuf.cs">
8182
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

QtSharp/RemoveQObjectMembersPass.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using CppSharp.AST;
4+
using CppSharp.Passes;
5+
6+
namespace QtSharp
7+
{
8+
public class RemoveQObjectMembersPass : TranslationUnitPass
9+
{
10+
public override bool VisitClassDecl(Class @class)
11+
{
12+
if (AlreadyVisited(@class) || @class.Name == "QObject")
13+
{
14+
return false;
15+
}
16+
17+
IEnumerable<MacroExpansion> expansions = @class.PreprocessedEntities.OfType<MacroExpansion>();
18+
19+
bool isQObject = expansions.Any(e => e.Text == "Q_OBJECT");
20+
if (isQObject)
21+
{
22+
RemoveQObjectMembers(@class);
23+
}
24+
return true;
25+
}
26+
27+
private static void RemoveQObjectMembers(Class @class)
28+
{
29+
// Every Qt object "inherits" a lot of members via the Q_OBJECT macro.
30+
// See the define of Q_OBJECT in qobjectdefs.h for a list of the members.
31+
// We cannot use the Qt defines for disabling the expansion of these
32+
// because it would mess up with the object layout size.
33+
34+
RemoveMethodOverloads(@class, "tr");
35+
RemoveMethodOverloads(@class, "trUtf8");
36+
RemoveMethodOverloads(@class, "qt_static_metacall");
37+
RemoveVariables(@class, "staticMetaObject");
38+
}
39+
40+
private static void RemoveMethodOverloads(Class @class, string originalName)
41+
{
42+
var overloads = @class.FindMethodByOriginalName(originalName).ToList();
43+
foreach (var method in overloads)
44+
@class.Methods.Remove(method);
45+
}
46+
47+
private static void RemoveVariables(Class @class, string originalName)
48+
{
49+
var variables = @class.FindVariableByOriginalName(originalName).ToList();
50+
foreach (var variable in variables)
51+
variable.ExplicitlyIgnore();
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)