Skip to content

Commit 84bcbd6

Browse files
authored
Merge pull request #10 from CognexVisionSoftware/bugfix/visit_unique_constructors
Duplicate constructors when using 'using'
2 parents 9395218 + 414a4b8 commit 84bcbd6

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

src/CppAst.Tests/TestMisc.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,30 @@ class Bar : public Foo
2323
public:
2424
using Foo::Foo;
2525
};
26+
27+
class Baz : public Bar
28+
{
29+
public:
30+
using Bar::Bar;
31+
Baz(double y) : y_{y} {}
32+
private:
33+
double y_{0};
34+
};
2635
",
2736
compilation =>
2837
{
2938
Assert.False(compilation.HasErrors);
30-
Assert.AreEqual(2, compilation.Classes.Count);
39+
Assert.AreEqual(3, compilation.Classes.Count);
3140
Assert.AreEqual(1, compilation.Classes[0].Constructors.Count);
32-
// Bar will get 3 constructors
33-
Assert.AreEqual(3, compilation.Classes[1].Constructors.Count);
41+
Assert.AreEqual(2, compilation.Classes[1].Constructors.Count);
3442
Assert.AreEqual(CppVisibility.Public, compilation.Classes[1].Constructors[0].Visibility);
3543
Assert.AreEqual(CppVisibility.Public, compilation.Classes[1].Constructors[1].Visibility);
36-
Assert.AreEqual(CppVisibility.Public, compilation.Classes[1].Constructors[2].Visibility);
44+
45+
Assert.AreEqual(3, compilation.Classes[2].Constructors.Count);
46+
Assert.AreEqual(CppVisibility.Public, compilation.Classes[2].Constructors[0].Visibility);
47+
Assert.AreEqual(CppVisibility.Public, compilation.Classes[2].Constructors[1].Visibility);
48+
Assert.AreEqual(CppVisibility.Public, compilation.Classes[2].Constructors[2].Visibility);
49+
3750
}
3851
);
3952
}

src/CppAst/CppFunction.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Linq;
78
using System.Text;
89

910
namespace CppAst
@@ -86,6 +87,69 @@ public int DefaultParamCount
8687
}
8788
}
8889

90+
/// <summary>
91+
/// Checks whether the two functions are the same or not
92+
/// </summary>
93+
public bool Equals(CppFunction other)
94+
{
95+
return Visibility.Equals(other.Visibility) &&
96+
Attributes.SequenceEqual(other.Attributes) &&
97+
TokenAttributes.SequenceEqual(other.TokenAttributes) &&
98+
StorageQualifier.Equals(other.StorageQualifier) &&
99+
LinkageKind.Equals(other.LinkageKind) &&
100+
IsConstructor.Equals(other.IsConstructor) &&
101+
Name.Equals(other.Name) &&
102+
Parameters.SequenceEqual(other.Parameters) &&
103+
TemplateParameters.SequenceEqual(other.TemplateParameters) &&
104+
CallingConvention.Equals(other.CallingConvention) &&
105+
Visibility.Equals(other.Visibility) &&
106+
Flags.Equals(other.Flags) &&
107+
ReturnType.Equals(other.ReturnType);
108+
}
109+
110+
public override bool Equals(object obj)
111+
{
112+
return ReferenceEquals(this, obj) || obj is CppFunction other && Equals(other);
113+
}
114+
115+
/// <inheritdoc />
116+
public override int GetHashCode()
117+
{
118+
unchecked
119+
{
120+
int hashCode = base.GetHashCode();
121+
hashCode = (hashCode * 397) ^ Visibility.GetHashCode();
122+
hashCode = (hashCode * 397) ^ StorageQualifier.GetHashCode();
123+
hashCode = (hashCode * 397) ^ LinkageKind.GetHashCode();
124+
hashCode = (hashCode * 397) ^ IsConstructor.GetHashCode();
125+
hashCode = (hashCode * 397) ^ Name.GetHashCode();
126+
hashCode = (hashCode * 397) ^ CallingConvention.GetHashCode();
127+
hashCode = (hashCode * 397) ^ Visibility.GetHashCode();
128+
hashCode = (hashCode * 397) ^ Flags.GetHashCode();
129+
hashCode = (hashCode * 397) ^ ReturnType.GetHashCode();
130+
131+
foreach (var templateParameter in TemplateParameters)
132+
{
133+
hashCode = (hashCode * 397) ^ templateParameter.GetHashCode();
134+
}
135+
foreach (var parameter in Parameters)
136+
{
137+
hashCode = (hashCode * 397) ^ parameter.GetHashCode();
138+
}
139+
foreach (var attribute in Attributes)
140+
{
141+
hashCode = (hashCode * 397) ^ attribute.GetHashCode();
142+
}
143+
foreach (var attribute in TokenAttributes)
144+
{
145+
hashCode = (hashCode * 397) ^ attribute.GetHashCode();
146+
}
147+
148+
return hashCode;
149+
}
150+
}
151+
152+
89153
/// <summary>
90154
/// Gets or sets the flags of this function.
91155
/// </summary>

src/CppAst/CppModelBuilder.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ private static CppStorageQualifier GetStorageQualifier(CXCursor cursor)
12601260
return CppStorageQualifier.None;
12611261
}
12621262

1263-
private CppFunction VisitFunctionDecl(CXCursor destinationCursor, CXCursor cursor, CXCursor parent, void* data)
1263+
private CppFunction VisitFunctionDecl(CXCursor destinationCursor, CXCursor cursor, CXCursor parent, void* data)
12641264
{
12651265
var destinationContextContainer = GetOrCreateDeclarationContainer(destinationCursor.SemanticParent, data);
12661266
var destinationContainer = destinationContextContainer.DeclarationContainer;
@@ -1294,13 +1294,7 @@ private CppFunction VisitFunctionDecl(CXCursor destinationCursor, CXCursor curso
12941294

12951295
if (cursor.Kind == CXCursorKind.CXCursor_Constructor)
12961296
{
1297-
var cppClass = (CppClass)destinationContainer;
12981297
cppFunction.IsConstructor = true;
1299-
cppClass.Constructors.Add(cppFunction);
1300-
}
1301-
else
1302-
{
1303-
destinationContainer.Functions.Add(cppFunction);
13041298
}
13051299

13061300
if (cursor.kind == CXCursorKind.CXCursor_FunctionTemplate)
@@ -1401,6 +1395,19 @@ private CppFunction VisitFunctionDecl(CXCursor destinationCursor, CXCursor curso
14011395

14021396
}, new CXClientData((IntPtr)data));
14031397

1398+
if (cursor.Kind == CXCursorKind.CXCursor_Constructor)
1399+
{
1400+
var cppClass = (CppClass)destinationContainer;
1401+
if (!cppClass.Constructors.Contains(cppFunction))
1402+
{
1403+
cppClass.Constructors.Add(cppFunction);
1404+
}
1405+
}
1406+
else if (!destinationContainer.Functions.Contains(cppFunction))
1407+
{
1408+
destinationContainer.Functions.Add(cppFunction);
1409+
}
1410+
14041411
return cppFunction;
14051412
}
14061413

0 commit comments

Comments
 (0)