diff --git a/compiler/extensions/cpp/src/zserio/extension/cpp/CppExtension.java b/compiler/extensions/cpp/src/zserio/extension/cpp/CppExtension.java index 5b9a782b4..a5991c055 100644 --- a/compiler/extensions/cpp/src/zserio/extension/cpp/CppExtension.java +++ b/compiler/extensions/cpp/src/zserio/extension/cpp/CppExtension.java @@ -44,6 +44,8 @@ public boolean isEnabled(ExtensionParameters parameters) @Override public void check(Root rootNode, ExtensionParameters parameters) throws ZserioExtensionException { + CppExtensionParameters.check(parameters); + final ReservedKeywordsClashChecker cppKeywordsClashChecker = new ReservedKeywordsClashChecker("C++", CPP_KEYWORDS); rootNode.walk(cppKeywordsClashChecker); diff --git a/compiler/extensions/cpp/src/zserio/extension/cpp/CppExtensionParameters.java b/compiler/extensions/cpp/src/zserio/extension/cpp/CppExtensionParameters.java index eebaa2274..516aaf417 100644 --- a/compiler/extensions/cpp/src/zserio/extension/cpp/CppExtensionParameters.java +++ b/compiler/extensions/cpp/src/zserio/extension/cpp/CppExtensionParameters.java @@ -3,9 +3,9 @@ import java.util.StringJoiner; import org.apache.commons.cli.Option; -import org.apache.commons.cli.OptionGroup; import org.apache.commons.cli.Options; +import zserio.extension.common.ZserioExtensionException; import zserio.tools.ExtensionParameters; /** @@ -26,9 +26,12 @@ public CppExtensionParameters(ExtensionParameters parameters) withValidationCode = parameters.getWithValidationCode(); withRangeCheckCode = parameters.getWithRangeCheckCode(); withSourcesAmalgamation = parameters.getWithSourcesAmalgamation(); - allocatorDefinition = parameters.argumentExists(OptionUsePolymorphicAllocator) - ? TypesContext.PROPAGATING_POLYMORPHIC_ALLOCATOR - : TypesContext.STD_ALLOCATOR; + + final String cppAllocator = parameters.getCommandLineArg(OptionSetCppAllocator); + if (cppAllocator == null || cppAllocator.equals(StdAllocator)) + allocatorDefinition = TypesContext.STD_ALLOCATOR; + else + allocatorDefinition = TypesContext.PROPAGATING_POLYMORPHIC_ALLOCATOR; final StringJoiner description = new StringJoiner(", "); if (withWriterCode) @@ -106,15 +109,11 @@ static void registerOptions(Options options) option.setRequired(false); options.addOption(option); - final OptionGroup allocatorGroup = new OptionGroup(); - option = new Option(OptionUseStdAllocator, false, - "use std::allocator in generated C++ code (default)"); - allocatorGroup.addOption(option); - option = new Option(OptionUsePolymorphicAllocator, false, - "use zserio::pmr::PropagatingPolymorphicAllocator<> in generated C++ code"); - allocatorGroup.addOption(option); - allocatorGroup.setRequired(false); - options.addOptionGroup(allocatorGroup); + option = new Option(OptionSetCppAllocator, true, + "set the C++ allocator to be used in generated code: std (default), polymorphic"); + option.setArgName("allocator"); + option.setRequired(false); + options.addOption(option); } static boolean hasOptionCpp(ExtensionParameters parameters) @@ -122,6 +121,17 @@ static boolean hasOptionCpp(ExtensionParameters parameters) return parameters.argumentExists(OptionCpp); } + static void check(ExtensionParameters parameters) throws ZserioExtensionException + { + final String cppAllocator = parameters.getCommandLineArg(OptionSetCppAllocator); + if (cppAllocator != null && !cppAllocator.equals(StdAllocator) && + !cppAllocator.equals(PolymorphicAllocator)) + { + throw new ZserioExtensionException("The specified option '" + OptionSetCppAllocator + "' has " + + "unknown allocator '" + cppAllocator + "'!"); + } + } + private void addAllocatorDescription(StringJoiner description) { if (allocatorDefinition == TypesContext.STD_ALLOCATOR) @@ -140,8 +150,10 @@ else if (allocatorDefinition == TypesContext.PROPAGATING_POLYMORPHIC_ALLOCATOR) } private final static String OptionCpp = "cpp"; - private final static String OptionUseStdAllocator = "useStdAllocator"; - private final static String OptionUsePolymorphicAllocator = "usePolymorphicAllocator"; + private final static String OptionSetCppAllocator = "setCppAllocator"; + + private final static String StdAllocator = "std"; + private final static String PolymorphicAllocator = "polymorphic"; private final String outputDir; private final boolean withWriterCode; diff --git a/doc/ZserioUserGuide.md b/doc/ZserioUserGuide.md index 72d213aaf..69ce0d8e9 100644 --- a/doc/ZserioUserGuide.md +++ b/doc/ZserioUserGuide.md @@ -29,18 +29,19 @@ java -jar zserio.jar [-ignoreTimestamps] [-java ] [-python ] + [-setCppAllocator ] [-setDotExecutable ] - [-setDotLinksPrefix ] [-setTopLevelPackage ] [-src ] [-v,--version] [-withCrossExtensionCheck|-withoutCrossExtensionCheck] - [-withRangeCheckCode|-withoutRangeCheckCode] [-withPubsubCode|-withoutPubsubCode] + [-withRangeCheckCode|-withoutRangeCheckCode] [-withServiceCode|-withoutServiceCode] [-withSourcesAmalgamation|-withoutSourcesAmalgamation] [-withSqlCode|-withoutSqlCode] [-withSvgDiagrams|-withoutSvgDiagrams] + [-withTypeInfoCode|-withoutTypeInfoCode] [-withUnusedWarnings|-withoutUnusedWarnings] [-withValidationCode|-withoutValidationCode] [-withWriterCode|-withoutWriterCode] @@ -78,6 +79,14 @@ Zserio will generate Java API into a given output directory. Zserio will generate Python API into a given output directory. +**`-setCppAllocator`** + +Sets the C++ allocator type to be used in generated code. Possible values: `std` (default), `polymorphic`. + +`std` stands for `std::allocator<>` class implemented in standard C++ library. +`polymorphic` stands for `zserio::pmr::PropagatingPolymorphicAllocator<>` class implemented in +Zserio C++ runtime library. + **`-setDotExecutable`** Sets path to the executable for conversion of dot files to svg format. If this option is omitted, the `dot` @@ -86,15 +95,6 @@ executable is used and it is supposed that this executable is on the system path > `-setDotExecutable /usr/bin/dot` causes to run executable `/usr/bin/dot` whenever the conversion > of dot file to svg file is needed. -**`-setDotLinksPrefix`** - -Sets the prefix to all URL links which are generated in graphviz file `overview.dot`. Setting the prefix -to "`.`" means setting the URL links to the locally generated HMTL documentation (which is the default behavior -if this option is omitted). - -> Parameter `-setDotLinksPrefix https://my.web-site.org/zserio/html_doc` sets the URL links -> to `my.web-site.org` web site. - **`-setTopLevelPackage`** Sets the top level package for generated Java sources and top level namespace for generated C++ sources. @@ -109,8 +109,8 @@ value is the current working directory. Currently, only one source directory can directories as in the Java `CLASSPATH` is not supported. > If the source path is `C:\zserio` and the input file is `com\acme\foo.zs`, Zserio will try parsing -> `C:\zserio\com\acme\foo.zs`. If `foo.zs` contains the declaration `import com.acme.bar.*`, Zserio will try -> parsing `C:\zserio\com\acme\bar.zs`. +> `C:\zserio\com\acme\foo.zs`. If `foo.zs` contains the declaration `import com.acme.bar.*`, Zserio will +> try parsing `C:\zserio\com\acme\bar.zs`. **`-v`, `--version`** @@ -121,16 +121,16 @@ Shows the version of the Zserio tool. Enabled/disables cross extension check, which causes that the checking phase is executed for all available extensions. By default is enabled to simplify to write portable schemas. -**`-withRangeCheckCode|-withoutRangeCheckCode`** - -Enables/disables code for range checking for fields and parameters (integer types only). By default is disabled. -Note that range checking can be enabled only when writer code is enabled (see `-withWriterCode` option). - **`-withPubsubCode|-withoutPubsubCode`** Enables/disables generation of code for Pubsub Types. By default is enabled, but note that pubsub types can be enabled only when writer code is enabled (see `-withWriterCode` option). +**`-withRangeCheckCode|-withoutRangeCheckCode`** + +Enables/disables code for range checking for fields and parameters (integer types only). By default is disabled. +Note that range checking can be enabled only when writer code is enabled (see `-withWriterCode` option). + **`-withServiceCode|-withoutServiceCode`** Enables/disables generation of code for Service Types. By default is enabled, but note that services can be @@ -156,6 +156,12 @@ available on system path or must be defined by `-setDotExecutable` option. By de executable is a part of the Graphviz package which can be downloaded from [Graphviz Web Page](http://www.graphviz.org/download). +**`-withTypeInfoCode|-withoutTypeInfoCode`** + +Enables/disables generation of type information code. By default is disabled. + +> This parameter is currently supported by Python generator only. + **`-withUnusedWarnings|-withoutUnusedWarnings`** Enables/disables warnings for unused types. By default is disabled. diff --git a/test/arguments/use_polymorphic_allocator/CMakeLists.txt b/test/arguments/set_cpp_allocator/CMakeLists.txt similarity index 94% rename from test/arguments/use_polymorphic_allocator/CMakeLists.txt rename to test/arguments/set_cpp_allocator/CMakeLists.txt index 18ae27b7f..c767641a5 100644 --- a/test/arguments/use_polymorphic_allocator/CMakeLists.txt +++ b/test/arguments/set_cpp_allocator/CMakeLists.txt @@ -59,7 +59,7 @@ zserio_add_library(TARGET complex_allocation_zs OUT_FILES ${ZSERIO_GENERATED_SOURCES_COMPLEX_ALLOCATION} ZSERIO_CORE_DIR ${ZSERIO_JAR_DIR} ZSERIO_CPP_DIR ${ZSERIO_CPP_DISTR_DIR} - ZSERIO_OPTIONS -usePolymorphicAllocator -withoutSourcesAmalgamation + ZSERIO_OPTIONS -setCppAllocator polymorphic -withoutSourcesAmalgamation ) zserio_add_library(TARGET pubsub_allocation_zs @@ -69,7 +69,7 @@ zserio_add_library(TARGET pubsub_allocation_zs OUT_FILES ${ZSERIO_GENERATED_SOURCES_PUBSUB_ALLOCATION} ZSERIO_CORE_DIR ${ZSERIO_JAR_DIR} ZSERIO_CPP_DIR ${ZSERIO_CPP_DISTR_DIR} - ZSERIO_OPTIONS -usePolymorphicAllocator -withoutSourcesAmalgamation + ZSERIO_OPTIONS -setCppAllocator polymorphic -withoutSourcesAmalgamation ) zserio_add_library(TARGET service_allocation_zs @@ -79,7 +79,7 @@ zserio_add_library(TARGET service_allocation_zs OUT_FILES ${ZSERIO_GENERATED_SOURCES_SERVICE_ALLOCATION} ZSERIO_CORE_DIR ${ZSERIO_JAR_DIR} ZSERIO_CPP_DIR ${ZSERIO_CPP_DISTR_DIR} - ZSERIO_OPTIONS -usePolymorphicAllocator -withoutSourcesAmalgamation + ZSERIO_OPTIONS -setCppAllocator polymorphic -withoutSourcesAmalgamation ) zserio_add_library(TARGET sql_allocation_zs @@ -89,7 +89,7 @@ zserio_add_library(TARGET sql_allocation_zs OUT_FILES ${ZSERIO_GENERATED_SOURCES_SQL_ALLOCATION} ZSERIO_CORE_DIR ${ZSERIO_JAR_DIR} ZSERIO_CPP_DIR ${ZSERIO_CPP_DISTR_DIR} - ZSERIO_OPTIONS -usePolymorphicAllocator -withoutSourcesAmalgamation -withValidationCode + ZSERIO_OPTIONS -setCppAllocator polymorphic -withoutSourcesAmalgamation -withValidationCode ) target_include_directories(sql_allocation_zs SYSTEM PRIVATE ${SQLITE_INCDIR}) diff --git a/test/arguments/use_polymorphic_allocator/cpp/ComplexAllocationTest.cpp b/test/arguments/set_cpp_allocator/cpp/ComplexAllocationTest.cpp similarity index 100% rename from test/arguments/use_polymorphic_allocator/cpp/ComplexAllocationTest.cpp rename to test/arguments/set_cpp_allocator/cpp/ComplexAllocationTest.cpp diff --git a/test/arguments/use_polymorphic_allocator/cpp/CppcheckSuppressions.txt b/test/arguments/set_cpp_allocator/cpp/CppcheckSuppressions.txt similarity index 100% rename from test/arguments/use_polymorphic_allocator/cpp/CppcheckSuppressions.txt rename to test/arguments/set_cpp_allocator/cpp/CppcheckSuppressions.txt diff --git a/test/arguments/use_polymorphic_allocator/cpp/PubsubAllocationTest.cpp b/test/arguments/set_cpp_allocator/cpp/PubsubAllocationTest.cpp similarity index 100% rename from test/arguments/use_polymorphic_allocator/cpp/PubsubAllocationTest.cpp rename to test/arguments/set_cpp_allocator/cpp/PubsubAllocationTest.cpp diff --git a/test/arguments/use_polymorphic_allocator/cpp/ServiceAllocationTest.cpp b/test/arguments/set_cpp_allocator/cpp/ServiceAllocationTest.cpp similarity index 100% rename from test/arguments/use_polymorphic_allocator/cpp/ServiceAllocationTest.cpp rename to test/arguments/set_cpp_allocator/cpp/ServiceAllocationTest.cpp diff --git a/test/arguments/use_polymorphic_allocator/cpp/SqlAllocationTest.cpp b/test/arguments/set_cpp_allocator/cpp/SqlAllocationTest.cpp similarity index 100% rename from test/arguments/use_polymorphic_allocator/cpp/SqlAllocationTest.cpp rename to test/arguments/set_cpp_allocator/cpp/SqlAllocationTest.cpp diff --git a/test/arguments/use_polymorphic_allocator/zs/complex_allocation.zs b/test/arguments/set_cpp_allocator/zs/complex_allocation.zs similarity index 100% rename from test/arguments/use_polymorphic_allocator/zs/complex_allocation.zs rename to test/arguments/set_cpp_allocator/zs/complex_allocation.zs diff --git a/test/arguments/use_polymorphic_allocator/zs/complex_allocation/allocation_choice.zs b/test/arguments/set_cpp_allocator/zs/complex_allocation/allocation_choice.zs similarity index 100% rename from test/arguments/use_polymorphic_allocator/zs/complex_allocation/allocation_choice.zs rename to test/arguments/set_cpp_allocator/zs/complex_allocation/allocation_choice.zs diff --git a/test/arguments/use_polymorphic_allocator/zs/complex_allocation/allocation_struct.zs b/test/arguments/set_cpp_allocator/zs/complex_allocation/allocation_struct.zs similarity index 100% rename from test/arguments/use_polymorphic_allocator/zs/complex_allocation/allocation_struct.zs rename to test/arguments/set_cpp_allocator/zs/complex_allocation/allocation_struct.zs diff --git a/test/arguments/use_polymorphic_allocator/zs/complex_allocation/allocation_struct_optional.zs b/test/arguments/set_cpp_allocator/zs/complex_allocation/allocation_struct_optional.zs similarity index 100% rename from test/arguments/use_polymorphic_allocator/zs/complex_allocation/allocation_struct_optional.zs rename to test/arguments/set_cpp_allocator/zs/complex_allocation/allocation_struct_optional.zs diff --git a/test/arguments/use_polymorphic_allocator/zs/complex_allocation/allocation_union.zs b/test/arguments/set_cpp_allocator/zs/complex_allocation/allocation_union.zs similarity index 100% rename from test/arguments/use_polymorphic_allocator/zs/complex_allocation/allocation_union.zs rename to test/arguments/set_cpp_allocator/zs/complex_allocation/allocation_union.zs diff --git a/test/arguments/use_polymorphic_allocator/zs/pubsub_allocation.zs b/test/arguments/set_cpp_allocator/zs/pubsub_allocation.zs similarity index 100% rename from test/arguments/use_polymorphic_allocator/zs/pubsub_allocation.zs rename to test/arguments/set_cpp_allocator/zs/pubsub_allocation.zs diff --git a/test/arguments/use_polymorphic_allocator/zs/service_allocation.zs b/test/arguments/set_cpp_allocator/zs/service_allocation.zs similarity index 100% rename from test/arguments/use_polymorphic_allocator/zs/service_allocation.zs rename to test/arguments/set_cpp_allocator/zs/service_allocation.zs diff --git a/test/arguments/use_polymorphic_allocator/zs/sql_allocation.zs b/test/arguments/set_cpp_allocator/zs/sql_allocation.zs similarity index 100% rename from test/arguments/use_polymorphic_allocator/zs/sql_allocation.zs rename to test/arguments/set_cpp_allocator/zs/sql_allocation.zs diff --git a/test/errors/cpp_error/build.xml b/test/errors/cpp_error/build.xml new file mode 100644 index 000000000..9254c21d6 --- /dev/null +++ b/test/errors/cpp_error/build.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/errors/cpp_error/java/cpp_error/CppErrorTest.java b/test/errors/cpp_error/java/cpp_error/CppErrorTest.java new file mode 100644 index 000000000..94c172118 --- /dev/null +++ b/test/errors/cpp_error/java/cpp_error/CppErrorTest.java @@ -0,0 +1,29 @@ +package cpp_error; + +import static org.junit.Assert.*; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; + +import test_utils.ZserioErrors; + +public class CppErrorTest +{ + @BeforeClass + public static void readZserioErrors() throws IOException + { + zserioErrors = new ZserioErrors(); + } + + @Test + public void wrongSetCppAllocator() + { + final String error = "[ERROR] C++11 Generator: The specified option 'setCppAllocator' has unknown " + + "allocator 'wrong'!"; + assertTrue(zserioErrors.isPresent(error)); + } + + private static ZserioErrors zserioErrors; +} diff --git a/test/errors/cpp_error/zs/wrong_set_cpp_allocator_error.zs b/test/errors/cpp_error/zs/wrong_set_cpp_allocator_error.zs new file mode 100644 index 000000000..0f62133e0 --- /dev/null +++ b/test/errors/cpp_error/zs/wrong_set_cpp_allocator_error.zs @@ -0,0 +1 @@ +package wrong_set_cpp_allocator_error;