Skip to content

Commit

Permalink
[#330] Introduce new set_cpp_allocator command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Sep 17, 2021
1 parent 1f462a7 commit 5b0642d
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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)
Expand Down Expand Up @@ -106,22 +109,29 @@ static void registerOptions(Options options)
option.setRequired(false);
options.addOption(option);

final OptionGroup allocatorGroup = new OptionGroup();
option = new Option(OptionUseStdAllocator, false,
"use std::allocator<uint8_t> 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)
{
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)
Expand All @@ -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;
Expand Down
42 changes: 24 additions & 18 deletions doc/ZserioUserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ java -jar zserio.jar
[-ignoreTimestamps]
[-java <output directory>]
[-python <output directory>]
[-setCppAllocator <allocator>]
[-setDotExecutable <dotExec>]
[-setDotLinksPrefix <prefix>]
[-setTopLevelPackage <package>]
[-src <source directory>]
[-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]
Expand Down Expand Up @@ -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`
Expand All @@ -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.
Expand All @@ -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`**

Expand All @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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})
Expand Down
34 changes: 34 additions & 0 deletions test/errors/cpp_error/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<project name="cpp_error" basedir="." default="run">
<testProperties testPath="errors/cpp_error" testName="cpp_error"/>

<target name="prepare">
<testPrepare testName="cpp_error"/>
</target>

<target name="gen.check" depends="prepare">
<testGenCheck testName="cpp_error"/>
</target>

<target name="gen" depends="gen.check" unless="cpp_error.zs.gen_is_uptodate">
<testGenClean testName="cpp_error"/>
<testGen testName="cpp_error" zsFile="wrong_set_cpp_allocator_error.zs" ignoreErrors="true">
<arg name="setCppAllocator" value="wrong"/>
</testGen>
</target>

<target name="compile" depends="gen">
<testCompile testName="cpp_error" srcPath="${cpp_error.base_dir}/java"/>
</target>

<target name="spotbugs" depends="compile" if="spotbugs.home_dir">
<testSpotBugs testName="cpp_error" srcPath="${cpp_error.base_dir}/java"/>
</target>

<target name="run" depends="spotbugs">
<testRun testName="cpp_error"/>
</target>

<target name="clean">
<testClean testName="cpp_error"/>
</target>
</project>
29 changes: 29 additions & 0 deletions test/errors/cpp_error/java/cpp_error/CppErrorTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions test/errors/cpp_error/zs/wrong_set_cpp_allocator_error.zs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package wrong_set_cpp_allocator_error;

0 comments on commit 5b0642d

Please sign in to comment.