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
Assert.That(type, Is.EqualTo("const char* const")); 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] [Test]
public void TestPrintingSpecializationWithConstValue() public void TestPrintingSpecializationWithConstValue()
{ {

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

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

7
tests/Native/AST.h

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