Skip to content

Commit

Permalink
Add clang-format to the C++ extension project
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jan 5, 2024
1 parent 4bdaaf3 commit f347b38
Show file tree
Hide file tree
Showing 41 changed files with 491 additions and 392 deletions.
35 changes: 33 additions & 2 deletions compiler/extensions/cpp/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ zserio_cpp.install_dir - Directory where to install Zserio C++ Extension
zserio_core.jar_file - Zserio core jar file used for classpath.
Default is "${zserio_extensions.build_dir}/../core/${ant.java.version}/jar/
zserio_core.jar".
clang_format.exe_file - Clang-format executable file. If not set, clang-format is not run.
spotbugs.home_dir - Location of the spotbugs tool. If not set, spotbugs is not run.
-->
Expand Down Expand Up @@ -161,7 +162,22 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spotb
</copy>
</target>

<target name="spotbugs" depends="install" if="spotbugs.home_dir">
<target name="clang_format" depends="install" if="clang_format.exe_file">
<echo>
Command hints to reformat source using clang-format tool:
git clang-format
clang-format --style=file -i [SOURCE]
</echo>
<apply executable="${clang_format.exe_file}" failonerror="true">
<arg value="--style=file"/>
<arg value="--dry-run"/>
<arg value="--Werror"/>
<srcfile/>
<fileset dir="${zserio_cpp.src_dir}" includes="**/*.java"/>
</apply>
</target>

<target name="spotbugs" depends="clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_cpp.spotbugs.out_dir}"/>
<taskdef classpath="${spotbugs.home_dir}/lib/spotbugs-ant.jar"
resource="edu/umd/cs/findbugs/anttask/tasks.properties"/>
Expand Down Expand Up @@ -203,7 +219,22 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spotb
</javac>
</target>

<target name="test.spotbugs" depends="spotbugs, test.compile" if="spotbugs.home_dir">
<target name="test.clang_format" depends="spotbugs, test.compile" if="clang_format.exe_file">
<echo>
Command hints to reformat source using clang-format tool:
git clang-format
clang-format --style=file -i [SOURCE]
</echo>
<apply executable="${clang_format.exe_file}" failonerror="true">
<arg value="--style=file"/>
<arg value="--dry-run"/>
<arg value="--Werror"/>
<srcfile/>
<fileset dir="${zserio_cpp.test.src_dir}" includes="**/*.java"/>
</apply>
</target>

<target name="test.spotbugs" depends="test.clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_cpp.test.spotbugs.out_dir}"/>
<taskdef classpath="${spotbugs.home_dir}/lib/spotbugs-ant.jar"
resource="edu/umd/cs/findbugs/anttask/tasks.properties"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ private static String getAccessorName(String accessorNamePrefix, String memberNa
return getAccessorName(accessorNamePrefix, memberName, "");
}

private static String getAccessorName(String accessorNamePrefix, String memberName,
String accessorNameSuffix)
private static String getAccessorName(
String accessorNamePrefix, String memberName, String accessorNameSuffix)
{
StringBuilder accessorName = new StringBuilder(accessorNamePrefix);
if (!memberName.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ else if (typeInstantiation instanceof DynamicBitFieldInstantiation)
(DynamicBitFieldInstantiation)typeInstantiation;

final ExpressionFormatter cppExpressionFormatter = context.getExpressionFormatter(includeCollector);
final String value = cppExpressionFormatter.formatGetter(
dynamicBitFieldInstantiation.getLengthExpression());
final String value =
cppExpressionFormatter.formatGetter(dynamicBitFieldInstantiation.getLengthExpression());

final ExpressionFormatter cppOwnerIndirectExpressionFormatter =
context.getIndirectExpressionFormatter(includeCollector, "owner");
Expand All @@ -95,10 +95,11 @@ else if (typeInstantiation instanceof DynamicBitFieldInstantiation)
final String rowIndirectValue = cppRowIndirectExpressionFormatter.formatGetter(
dynamicBitFieldInstantiation.getLengthExpression());

final boolean needsOwner = dynamicBitFieldInstantiation.getLengthExpression().requiresOwnerContext();
final boolean needsOwner =
dynamicBitFieldInstantiation.getLengthExpression().requiresOwnerContext();

return new BitSizeTemplateData(value, ownerIndirectValue, objectIndirectValue, rowIndirectValue,
needsOwner);
return new BitSizeTemplateData(
value, ownerIndirectValue, objectIndirectValue, rowIndirectValue, needsOwner);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public BitmaskEmitterTemplateData(TemplateDataContext context, BitmaskType bitma
runtimeFunction = RuntimeFunctionDataCreator.createData(context, bitmaskTypeInstantiation, this);

final BigInteger upperBound = getUpperBound(bitmaskTypeInstantiation);
this.upperBound = upperBound.equals(nativeBaseType.getUpperBound()) ? null :
nativeBaseType.formatLiteral(upperBound);
this.upperBound = upperBound.equals(nativeBaseType.getUpperBound())
? null
: nativeBaseType.formatLiteral(upperBound);

final List<BitmaskValue> bitmaskValues = bitmaskType.getValues();
values = new ArrayList<BitmaskValueData>(bitmaskValues.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
*/
public final class ChoiceEmitterTemplateData extends CompoundTypeTemplateData
{
public ChoiceEmitterTemplateData(TemplateDataContext context, ChoiceType choiceType) throws ZserioExtensionException
public ChoiceEmitterTemplateData(TemplateDataContext context, ChoiceType choiceType)
throws ZserioExtensionException
{
super(context, choiceType);

Expand Down Expand Up @@ -86,8 +87,8 @@ public boolean getIsDefaultUnreachable()

public static final class CaseMember
{
public CaseMember(TemplateDataContext context, ChoiceType choiceType,
ChoiceCase choiceCaseType, IncludeCollector includeCollector) throws ZserioExtensionException
public CaseMember(TemplateDataContext context, ChoiceType choiceType, ChoiceCase choiceCaseType,
IncludeCollector includeCollector) throws ZserioExtensionException
{
final ExpressionFormatter cppExpressionFormatter = context.getExpressionFormatter(includeCollector);

Expand All @@ -97,8 +98,9 @@ public CaseMember(TemplateDataContext context, ChoiceType choiceType,
expressionList.add(cppExpressionFormatter.formatGetter(caseExpression.getExpression()));

final Field fieldType = choiceCaseType.getField();
compoundField = (fieldType != null) ?
new CompoundFieldTemplateData(context, choiceType, fieldType, includeCollector) : null;
compoundField = (fieldType != null)
? new CompoundFieldTemplateData(context, choiceType, fieldType, includeCollector)
: null;
}

public List<String> getExpressionList()
Expand All @@ -117,9 +119,9 @@ public CompoundFieldTemplateData getCompoundField()

public static final class DefaultMember
{
public DefaultMember(TemplateDataContext context,
ChoiceType choiceType, ChoiceDefault choiceDefaultType,
IncludeCollector includeCollector) throws ZserioExtensionException
public DefaultMember(TemplateDataContext context, ChoiceType choiceType,
ChoiceDefault choiceDefaultType, IncludeCollector includeCollector)
throws ZserioExtensionException
{
final Field fieldType = choiceDefaultType.getField();
compoundField = (fieldType != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import java.util.ArrayList;

import zserio.ast.ArrayInstantiation;
import zserio.ast.DynamicBitFieldInstantiation;
import zserio.ast.ParameterizedTypeInstantiation;
import zserio.ast.UnionType;
import zserio.ast.ChoiceType;
import zserio.ast.CompoundType;
import zserio.ast.ZserioType;
import zserio.ast.DynamicBitFieldInstantiation;
import zserio.ast.Expression;
import zserio.ast.Field;
import zserio.ast.IntegerType;
import zserio.ast.ParameterizedTypeInstantiation;
import zserio.ast.ParameterizedTypeInstantiation.InstantiatedParameter;
import zserio.ast.TypeInstantiation;
import zserio.ast.TypeReference;
import zserio.ast.ParameterizedTypeInstantiation.InstantiatedParameter;
import zserio.ast.UnionType;
import zserio.ast.ZserioType;
import zserio.extension.common.ExpressionFormatter;
import zserio.extension.common.ZserioExtensionException;
import zserio.extension.cpp.types.CppNativeType;
Expand All @@ -27,8 +27,8 @@
*/
public final class CompoundFieldTemplateData
{
public CompoundFieldTemplateData(TemplateDataContext context, CompoundType parentType,
Field field, IncludeCollector includeCollector) throws ZserioExtensionException
public CompoundFieldTemplateData(TemplateDataContext context, CompoundType parentType, Field field,
IncludeCollector includeCollector) throws ZserioExtensionException
{
final TypeInstantiation fieldTypeInstantiation = field.getTypeInstantiation();
final ZserioType fieldBaseType = fieldTypeInstantiation.getBaseType();
Expand All @@ -37,8 +37,9 @@ public CompoundFieldTemplateData(TemplateDataContext context, CompoundType paren
final CppNativeType fieldNativeType = cppNativeMapper.getCppType(fieldTypeInstantiation);
includeCollector.addHeaderIncludesForType(fieldNativeType);

optional = (field.isOptional()) ?
createOptional(context, field, fieldBaseType, parentType, includeCollector) : null;
optional = (field.isOptional())
? createOptional(context, field, fieldBaseType, parentType, includeCollector)
: null;
compound = createCompound(context, fieldTypeInstantiation, includeCollector);

name = field.getName();
Expand Down Expand Up @@ -66,8 +67,8 @@ public CompoundFieldTemplateData(TemplateDataContext context, CompoundType paren
constraint = createConstraint(context, field, includeCollector);
offset = createOffset(context, field, includeCollector);
array = createArray(context, fieldNativeType, fieldTypeInstantiation, parentType, includeCollector);
runtimeFunction = RuntimeFunctionDataCreator.createData(context, fieldTypeInstantiation,
includeCollector);
runtimeFunction =
RuntimeFunctionDataCreator.createData(context, fieldTypeInstantiation, includeCollector);
bitSize = BitSizeTemplateData.create(context, fieldTypeInstantiation, includeCollector);
docComments = DocCommentsDataCreator.createData(context, field);
}
Expand Down Expand Up @@ -189,8 +190,9 @@ public Optional(TemplateDataContext context, Field field, boolean isRecursive,
{
final Expression optionalClauseExpression = field.getOptionalClauseExpr();
final ExpressionFormatter cppExpressionFormatter = context.getExpressionFormatter(includeCollector);
clause = (optionalClauseExpression == null) ? null :
cppExpressionFormatter.formatGetter(optionalClauseExpression);
clause = (optionalClauseExpression == null)
? null
: cppExpressionFormatter.formatGetter(optionalClauseExpression);
isUsedIndicatorName = AccessorNameFormatter.getIsUsedIndicatorName(field);
isSetIndicatorName = AccessorNameFormatter.getIsSetIndicatorName(field);
resetterName = AccessorNameFormatter.getResetterName(field);
Expand Down Expand Up @@ -268,11 +270,13 @@ public boolean getNeedsChildrenInitialization()

public static final class InstantiatedParameterData
{
public InstantiatedParameterData(TemplateDataContext context, InstantiatedParameter instantiatedParameter,
IncludeCollector includeCollector) throws ZserioExtensionException
public InstantiatedParameterData(TemplateDataContext context,
InstantiatedParameter instantiatedParameter, IncludeCollector includeCollector)
throws ZserioExtensionException
{
final Expression argumentExpression = instantiatedParameter.getArgumentExpression();
final ExpressionFormatter cppExpressionFormatter = context.getExpressionFormatter(includeCollector);
final ExpressionFormatter cppExpressionFormatter =
context.getExpressionFormatter(includeCollector);
expression = cppExpressionFormatter.formatGetter(argumentExpression);
final ExpressionFormatter cppOwnerIndirectExpressionFormatter =
context.getIndirectExpressionFormatter(includeCollector, "owner");
Expand Down Expand Up @@ -407,7 +411,7 @@ public static final class IntegerRange
{
public IntegerRange(boolean checkLowerBound, String lowerBound, String upperBound,
NativeIntegralTypeInfoTemplateData typeInfo, String bitFieldLength)
throws ZserioExtensionException
throws ZserioExtensionException
{
this.checkLowerBound = checkLowerBound;
this.lowerBound = lowerBound;
Expand Down Expand Up @@ -553,9 +557,9 @@ private static Optional createOptional(TemplateDataContext context, Field field,
return new Optional(context, field, isRecursive, includeCollector);
}

private static IntegerRange createIntegerRange(TemplateDataContext context,
TypeInstantiation typeInstantiation, IncludeCollector includeCollector)
throws ZserioExtensionException
private static IntegerRange createIntegerRange(
TemplateDataContext context, TypeInstantiation typeInstantiation, IncludeCollector includeCollector)
throws ZserioExtensionException
{
if (!(typeInstantiation.getBaseType() instanceof IntegerType))
return null;
Expand Down Expand Up @@ -653,7 +657,7 @@ private static Offset createOffset(TemplateDataContext context, Field field,

private static Array createArray(TemplateDataContext context, CppNativeType cppNativeType,
TypeInstantiation typeInstantiation, CompoundType parentType, IncludeCollector includeCollector)
throws ZserioExtensionException
throws ZserioExtensionException
{
if (!(typeInstantiation instanceof ArrayInstantiation))
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.util.List;

import zserio.ast.CompoundType;
import zserio.ast.TypeReference;
import zserio.ast.ZserioType;
import zserio.ast.Function;
import zserio.ast.StringType;
import zserio.ast.TypeReference;
import zserio.ast.ZserioType;
import zserio.extension.common.ExpressionFormatter;
import zserio.extension.common.ZserioExtensionException;
import zserio.extension.cpp.types.CppNativeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public CompoundParameterTemplateData(TemplateDataContext context, CompoundType c
final List<DocComment> compoundDocComments = compoundType.getDocComments();
for (Parameter compoundParameterType : compoundParameterTypeList)
{
final CompoundParameter parameter = new CompoundParameter(context, compoundParameterType,
includeCollector, compoundDocComments);
final CompoundParameter parameter = new CompoundParameter(
context, compoundParameterType, includeCollector, compoundDocComments);
compoundParameterList.add(parameter);
}
}
Expand All @@ -38,7 +38,7 @@ public static final class CompoundParameter
{
public CompoundParameter(TemplateDataContext context, Parameter parameter,
IncludeCollector includeCollector, List<DocComment> compoundDocComments)
throws ZserioExtensionException
throws ZserioExtensionException
{
final TypeReference parameterTypeReference = parameter.getTypeReference();
final CppNativeMapper cppNativeMapper = context.getCppNativeMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public CompoundTypeTemplateData(TemplateDataContext context, CompoundType compou
fieldList = new ArrayList<CompoundFieldTemplateData>(fieldTypeList.size());
for (Field fieldType : fieldTypeList)
{
final CompoundFieldTemplateData data = new CompoundFieldTemplateData(context,
compoundType, fieldType, this);
final CompoundFieldTemplateData data =
new CompoundFieldTemplateData(context, compoundType, fieldType, this);

fieldList.add(data);
}

compoundParametersData = new CompoundParameterTemplateData(context, compoundType, this);
compoundFunctionsData = new CompoundFunctionTemplateData(context, compoundType, this);
compoundConstructorsData = new CompoundConstructorTemplateData(compoundType, compoundParametersData,
fieldList);
compoundConstructorsData =
new CompoundConstructorTemplateData(compoundType, compoundParametersData, fieldList);

isPackable = compoundType.isPackable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public ConstEmitter(OutputFileManager outputFileManager, CppExtensionParameters
@Override
public void beginConst(Constant constant) throws ZserioExtensionException
{
final ConstEmitterTemplateData templateData = new ConstEmitterTemplateData(
getTemplateDataContext(), constant);
processHeaderTemplate(TEMPLATE_HEADER_NAME, templateData, constant.getPackage().getPackageName(),
constant.getName());
final ConstEmitterTemplateData templateData =
new ConstEmitterTemplateData(getTemplateDataContext(), constant);
processHeaderTemplate(
TEMPLATE_HEADER_NAME, templateData, constant.getPackage().getPackageName(), constant.getName());
}

private static final String TEMPLATE_HEADER_NAME = "Constant.h.ftl";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public ConstEmitterTemplateData(TemplateDataContext context, Constant constant)
super(context);

final CppNativeMapper cppNativeMapper = context.getCppNativeMapper();
final ExpressionFormatter cppExpressionFormatter = context.getExpressionFormatter(
new HeaderIncludeCollectorAdapter(this));
final ExpressionFormatter cppExpressionFormatter =
context.getExpressionFormatter(new HeaderIncludeCollectorAdapter(this));

final CppNativeSymbol constantNativeSymbol = cppNativeMapper.getCppSymbol(constant);
packageData = new PackageTemplateData(constantNativeSymbol.getPackageName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
public final class CppConstraintExpressionFormattingPolicy extends CppExpressionFormattingPolicy
{
public CppConstraintExpressionFormattingPolicy(CppNativeMapper cppNativeMapper,
IncludeCollector includeCollector, Field constraintField)
public CppConstraintExpressionFormattingPolicy(
CppNativeMapper cppNativeMapper, IncludeCollector includeCollector, Field constraintField)
{
super(cppNativeMapper, includeCollector);

Expand Down
Loading

0 comments on commit f347b38

Please sign in to comment.