Browse Source

Fix printing of const pointer to typedefs.

pull/1506/head
Joao Matos 5 years ago
parent
commit
da7df2954a
  1. 12
      src/Generator.Tests/AST/TestAST.cs
  2. 28
      src/Generator/Generators/C/CppTypePrinter.cs
  3. 7
      tests/Native/AST.h

12
src/Generator.Tests/AST/TestAST.cs

@ -503,6 +503,18 @@ namespace CppSharp.Generator.Tests.AST @@ -503,6 +503,18 @@ namespace CppSharp.Generator.Tests.AST
Assert.That(type, Is.EqualTo("const char* const"));
}
[Test]
public void TestPrintingConstPointerTypedef()
{
var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified };
cppTypePrinter.PushContext(TypePrinterContextKind.Native);
var method = Context.ASTContext.FindCompleteClass("TestTypePrinting").FindMethod("ConstPointerTypedef");
var result = method.Parameters[0].Visit(cppTypePrinter);
var type = result.ToString();
Assert.That(type, Is.EqualTo("const MyChar*"));
}
[Test]
public void TestPrintingSpecializationWithConstValue()
{

28
src/Generator/Generators/C/CppTypePrinter.cs

@ -105,7 +105,7 @@ namespace CppSharp.Generators.C @@ -105,7 +105,7 @@ namespace CppSharp.Generators.C
var result = new TypePrinterResult
{
Type = array.Type.Visit(this),
Type = array.QualifiedType.Visit(this),
NameSuffix = new System.Text.StringBuilder(arraySuffix)
};
@ -232,18 +232,26 @@ namespace CppSharp.Generators.C @@ -232,18 +232,26 @@ namespace CppSharp.Generators.C
public override TypePrinterResult VisitTypedefType(TypedefType typedef,
TypeQualifiers quals)
{
TypePrinterResult result;
FunctionType func;
var qual = GetStringQuals(quals);
if (ResolveTypedefs && !typedef.Declaration.Type.IsPointerTo(out func))
{
TypePrinterResult type = typedef.Declaration.QualifiedType.Visit(this);
return new TypePrinterResult { Type = $"{qual}{type.Type}",
NamePrefix = type.NamePrefix, NameSuffix = type.NameSuffix };
}
result = typedef.Declaration.QualifiedType.Visit(this);
else
result = typedef.Declaration.Visit(this);
var qual = GetStringQuals(quals);
var result = typedef.Declaration.Visit(this);
if (result.NamePrefix.Length > 0)
result.NamePrefix.Append($"{qual}");
// In the case of const references to const typedefs, we could end up printing
// a double const.
//
// As an example, consider the following code:
//
// typedef const T const_t;
// foo(const const_t&p) { }
//
if (!result.Type.StartsWith("const "))
result.Type = $"{qual}{result.Type}";
return result;
}

7
tests/Native/AST.h

@ -253,3 +253,10 @@ int non_deprecated_func(int num); @@ -253,3 +253,10 @@ int non_deprecated_func(int num);
TestTemplateClass<double> returnIncompleteTemplateSpecialization();
#define MACRO(x, y, z) x##y##z
typedef char MyChar;
class TestTypePrinting
{
void ConstPointerTypedef(const MyChar*);
};
Loading…
Cancel
Save