From e9975286b217e12dc957ed4fc8e214ade8abf637 Mon Sep 17 00:00:00 2001 From: Alastair Robertson Date: Mon, 31 Jul 2023 11:17:13 -0700 Subject: [PATCH] NameGen: Fix naming of const pointers --- oi/type_graph/NameGen.cpp | 12 +++++++++--- test/test_codegen.cpp | 4 ++-- test/test_name_gen.cpp | 8 +++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/oi/type_graph/NameGen.cpp b/oi/type_graph/NameGen.cpp index eec1efc..7f7204b 100644 --- a/oi/type_graph/NameGen.cpp +++ b/oi/type_graph/NameGen.cpp @@ -113,10 +113,16 @@ void NameGen::visit(Container& c) { if (param.value) { name += *param.value; } else { - if (param.qualifiers[Qualifier::Const]) { - name += "const "; - } name += param.type()->name(); + // The "const" keyword must come after the type name so that pointers are + // handled correctly. + // + // When we're told that we have a const member with type "Foo*", we want + // "Foo* const", meaning const pointer to Foo, rather than "const Foo*", + // meaning pointer to const Foo. + if (param.qualifiers[Qualifier::Const]) { + name += " const"; + } } name += ", "; } diff --git a/test/test_codegen.cpp b/test/test_codegen.cpp index 75d3caa..22b29ac 100644 --- a/test/test_codegen.cpp +++ b/test/test_codegen.cpp @@ -148,14 +148,14 @@ TEST(CodeGenTest, TransformContainerAllocatorParamInParent) { Function: deallocate )", R"( -[0] Container: std::map, 0, 0>> (size: 24) +[0] Container: std::map, 0, 0>> (size: 24) Param Primitive: int32_t Param Primitive: int32_t Param DummyAllocator (size: 0) -[1] Container: std::pair (size: 8) +[1] Container: std::pair (size: 8) Param Primitive: int32_t Qualifiers: const diff --git a/test/test_name_gen.cpp b/test/test_name_gen.cpp index 35af699..e829a54 100644 --- a/test/test_name_gen.cpp +++ b/test/test_name_gen.cpp @@ -171,18 +171,24 @@ TEST(NameGenTest, ContainerParamsDuplicatesAcrossContainers) { TEST(NameGenTest, ContainerParamsConst) { auto myparam1 = Class{0, Class::Kind::Struct, "MyConstParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13}; + auto ptrParam = Class{2, Class::Kind::Struct, "PtrParam", 13}; + auto myparam3 = Pointer{3, ptrParam}; auto mycontainer = getVector(); mycontainer.templateParams.push_back( TemplateParam{myparam1, {Qualifier::Const}}); mycontainer.templateParams.push_back(TemplateParam{myparam2}); + mycontainer.templateParams.push_back( + TemplateParam{myparam3, {Qualifier::Const}}); NameGen nameGen; nameGen.generateNames({mycontainer}); EXPECT_EQ(myparam1.name(), "MyConstParam_0"); EXPECT_EQ(myparam2.name(), "MyParam_1"); - EXPECT_EQ(mycontainer.name(), "std::vector"); + EXPECT_EQ(myparam3.name(), "PtrParam_2*"); + EXPECT_EQ(mycontainer.name(), + "std::vector"); } TEST(NameGenTest, ContainerNoParams) {