Skip to content

Commit

Permalink
cpp: Add source region to choice and union
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Jul 8, 2024
1 parent a1d7e95 commit 5a6cbe1
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 40 deletions.
7 changes: 7 additions & 0 deletions compiler/extensions/cpp/freemarker/Choice.cpp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,14 @@ ${I}return {};

<#if withWriterCode>
<@choice_reflectable false/>
</#if>

<#if withSourceRegion>
<#lt>std::tuple<size_t, size_t> ${name}::getSourceRegion() const
<#lt>{
<#lt> return std::make_tuple<size_t, size_t>(
<#lt> m_sourcePosition, bitSizeOf(m_sourcePosition));
<#lt>}
</#if>
</#if>
</#if>
Expand Down
15 changes: 15 additions & 0 deletions compiler/extensions/cpp/freemarker/Choice.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ public:
</#if>
</#if>

<#if withSourceRegion>
/**
* Get the source offset and size of the structure in bits.
* For objects not loaded from a blob, the offest is 0.
*
* \return Tuple of the objects offset and size in bits.
*/
std::tuple<size_t, size_t> getSourceRegion() const;
</#if>

private:
<@private_section_declarations name, fieldList/>
<#if fieldList?has_content>
Expand All @@ -339,6 +349,11 @@ private:
</#if>
${types.anyHolder.name} copyObject(const allocator_type& allocator) const;

</#if>
<#-- Data members -->
<#if withSourceRegion>
<#-- Source position must be the first member in order to get initialized first. -->
uint32_t m_sourcePosition = 0;
</#if>
<@compound_parameter_members compoundParametersData/>
<@compound_constructor_members compoundConstructorsData/>
Expand Down
15 changes: 6 additions & 9 deletions compiler/extensions/cpp/freemarker/CompoundConstructor.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ ${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundNam
has_field_with_initialization(compoundConstructorsData.fieldList) ||
memberInitializationMacroName != ""/>
<#local wantsBitStreamReader = compoundConstructorsData.fieldList?has_content || withSourceRegion>
<#---->
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#if packed>
${compoundConstructorsData.compoundName}::ZserioPackingContext& context, <#t>
Expand Down Expand Up @@ -326,7 +325,7 @@ ${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundNam

<#macro compound_move_constructor_no_init_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(::zserio::NoInitT,
${compoundConstructorsData.compoundName}&&<#if compoundConstructorsData.fieldList?has_content> other</#if>)<#rt>
<#lt>${compoundConstructorsData.compoundName}&& <#if compoundConstructorsData.fieldList?has_content>other</#if>)<#rt>
<@cpp_initializer_list>
<#if withSourceRegion>
m_sourcePosition(::std::move(other.m_sourcePosition))
Expand Down Expand Up @@ -399,7 +398,7 @@ ${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundNam

<#macro compound_move_assignment_no_init_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundName}::assign(::zserio::NoInitT,
${compoundConstructorsData.compoundName}&&<#if compoundConstructorsData.fieldList?has_content> other</#if>)
<#lt>${compoundConstructorsData.compoundName}&& <#if compoundConstructorsData.fieldList?has_content>other</#if>)
{
<#if withSourceRegion>
m_sourcePosition = other.m_sourcePosition;
Expand Down Expand Up @@ -449,9 +448,8 @@ ${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundNam
<#local initialization><@compound_copy_initialization compoundConstructorsData/></#local>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#lt>::zserio::PropagateAllocatorT,
const ${compoundConstructorsData.compoundName}&<#rt>
<#lt><#if compoundConstructorsData.fieldList?has_content || initialization?has_content> other</#if>,<#rt>
<#lt> const allocator_type&<#if compoundConstructorsData.fieldList?has_content> allocator</#if>)<#rt>
<#lt>const ${compoundConstructorsData.compoundName}& <#if compoundConstructorsData.fieldList?has_content || initialization?has_content>other</#if>,
<#lt>const allocator_type&<#if compoundConstructorsData.fieldList?has_content> allocator</#if>)<#rt>
<@cpp_initializer_list>
<#if withSourceRegion>
m_sourcePosition(other.m_sourcePosition)
Expand All @@ -478,9 +476,8 @@ ${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundNam
<#macro compound_allocator_propagating_copy_constructor_no_init_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#lt>::zserio::PropagateAllocatorT, ::zserio::NoInitT,
const ${compoundConstructorsData.compoundName}&<#rt>
<#lt><#if compoundConstructorsData.fieldList?has_content || initialization?has_content> other</#if>,<#rt>
<#lt> const allocator_type&<#if compoundConstructorsData.fieldList?has_content> allocator</#if>)<#rt>
<#lt>[[maybe_unused]] const ${compoundConstructorsData.compoundName}& <#if compoundConstructorsData.fieldList?has_content || initialization?has_content>other</#if>,
<#lt>const allocator_type&<#if compoundConstructorsData.fieldList?has_content> allocator</#if>)<#rt>
<@cpp_initializer_list>
<#if withSourceRegion>
m_sourcePosition(other.m_sourcePosition)
Expand Down
33 changes: 19 additions & 14 deletions compiler/extensions/cpp/freemarker/CppUtility.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@
-->
<#macro cpp_initializer_list>
<#local body><#nested></#local>
<#local members = body?split(r'\R', 'r')>
<#local indent = ' '>
<#list members><#lt> :
<#items as member>
<#local member = member?trim>
<#if member == ''>
<#continue>
</#if>
<#lt>${indent}${member?remove_ending(',')}<#rt>
<#sep><#lt>,
</#sep>
</#items>
<#t>${'\n'}
</#list>
<#if body?trim != ''>
<#local members = body?split(r'\R', 'r')>
<#local indent = ' '>
<#list members>
<#lt> :
<#items as member>
<#local member = member?trim>
<#if member == ''>
<#continue>
</#if>
<#lt>${indent}${member?remove_ending(',')}<#rt>
<#sep><#lt>,
</#sep>
</#items>
<#t>${'\n'}
</#list>
<#else>
${'\n'}<#t>
</#if>
</#macro>
16 changes: 15 additions & 1 deletion compiler/extensions/cpp/freemarker/Union.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@ public:
</#if>
</#if>

<#if withSourceRegion>
/**
* Get the source offset and size of the structure in bits.
* For objects not loaded from a blob, the offest is 0.
*
* \return Tuple of the objects offset and size in bits.
*/
std::tuple<size_t, size_t> getSourceRegion() const;
</#if>

private:
<@private_section_declarations name, fieldList/>
<#if fieldList?has_content>
Expand All @@ -339,7 +349,11 @@ private:
const allocator_type& allocator);
</#if>
${types.anyHolder.name} copyObject(const allocator_type& allocator) const;

<#-- The source-position must be the first member to get called first in
the constructors initializer list -->
<#if withSourceRegion>
uint32_t m_sourcePosition = 0;
</#if>
</#if>
<@compound_parameter_members compoundParametersData/>
<@compound_constructor_members compoundConstructorsData/>
Expand Down
11 changes: 11 additions & 0 deletions compiler/extensions/cpp/runtime/src/zserio/Reflectable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3399,6 +3399,11 @@ class ReflectableOwner : public IBasicReflectable<ALLOC>
return toString(ALLOC());
}

std::tuple<size_t, size_t> getSourceRegion() const override
{
return m_reflectable->getSourceRegion();
}

private:
T m_object;
IBasicReflectablePtr<ALLOC> m_reflectable;
Expand Down Expand Up @@ -4370,6 +4375,12 @@ string<ALLOC> ReflectableBase<ALLOC>::toString() const
return toString(ALLOC());
}

template <typename ALLOC>
std::tuple<size_t, size_t> ReflectableBase<ALLOC>::getSourceRegion() const
{
throw CppRuntimeException("Source region unavailable for type '") << getTypeInfo().getSchemaName() << "'!";
}

template <typename ALLOC>
void ReflectableConstAllocatorHolderBase<ALLOC>::initializeChildren()
{
Expand Down
28 changes: 14 additions & 14 deletions compiler/extensions/cpp/runtime/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ project(ZserioCppRuntimeTest)
include(gtest_utils)
gtest_add_library("${ZSERIO_PROJECT_ROOT}/3rdparty/cpp/googletest")

## add SQLite3 library
# add SQLite3 library
include(sqlite_utils)
sqlite_add_library(${ZSERIO_PROJECT_ROOT})
#

compiler_set_warnings()
compiler_set_warnings_as_errors()
if (SANITIZERS_ENABLED)
Expand Down Expand Up @@ -193,18 +193,18 @@ set(ZSERIO_CPP_RUNTIME_TEST_HEADERS
zserio/TrackingAllocator.h
)

#add_executable(${PROJECT_NAME}
# ${TEST_OBJECT_SRCS}
# ${ZSERIO_CPP_RUNTIME_TEST_SRCS}
# ${ZSERIO_CPP_RUNTIME_TEST_HEADERS})
add_executable(${PROJECT_NAME}
${TEST_OBJECT_SRCS}
${ZSERIO_CPP_RUNTIME_TEST_SRCS}
${ZSERIO_CPP_RUNTIME_TEST_HEADERS})

#target_include_directories(${PROJECT_NAME} PRIVATE .)
#target_include_directories(${PROJECT_NAME} PRIVATE ../src)
#target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${SQLITE_INCDIR})
#target_link_libraries(${PROJECT_NAME} PRIVATE ZserioCppRuntime gtest_main ${SQLITE_LIBRARY})
target_include_directories(${PROJECT_NAME} PRIVATE .)
target_include_directories(${PROJECT_NAME} PRIVATE ../src)
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${SQLITE_INCDIR})
target_link_libraries(${PROJECT_NAME} PRIVATE ZserioCppRuntime gtest_main ${SQLITE_LIBRARY})

#gtest_add_tests(${PROJECT_NAME} "--gtest_output=xml" ${ZSERIO_CPP_RUNTIME_TEST_SRCS})
gtest_add_tests(${PROJECT_NAME} "--gtest_output=xml" ${ZSERIO_CPP_RUNTIME_TEST_SRCS})

#add_subdirectory(zserio/deprecated_attribute)
#set_source_files_properties(zserio/deprecated_attribute/DeprecatedAttributeTest.cpp
# PROPERTIES COMPILE_DEFINITIONS ${DEPRECATED_ATTRIBUTE_COMPILE_DEFINITIONS})
add_subdirectory(zserio/deprecated_attribute)
set_source_files_properties(zserio/deprecated_attribute/DeprecatedAttributeTest.cpp
PROPERTIES COMPILE_DEFINITIONS ${DEPRECATED_ATTRIBUTE_COMPILE_DEFINITIONS})
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ static void registerOptions(Options options)
options.addOptionGroup(sourcesAmalgamationGroup);

final OptionGroup sourceRegionGroup = new OptionGroup();
sourceRegionGroup.addOption(new Option(OptionWithSourceRegion, false, "enable storage of source region allong compounds"));
sourceRegionGroup.addOption(new Option(OptionWithoutSourceRegion, false, "disable storage of source region allong compounds"));
sourceRegionGroup.addOption(
new Option(OptionWithSourceRegion, false, "enable storage of source region allong compounds"));
sourceRegionGroup.addOption(new Option(
OptionWithoutSourceRegion, false, "disable storage of source region allong compounds"));
sourceRegionGroup.setRequired(false);
options.addOptionGroup(sourceRegionGroup);
}
Expand Down

0 comments on commit 5a6cbe1

Please sign in to comment.