Skip to content

Commit

Permalink
Add clang-format to the Python extension project
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jan 5, 2024
1 parent bc7f40b commit 37bc91a
Show file tree
Hide file tree
Showing 37 changed files with 233 additions and 195 deletions.
35 changes: 33 additions & 2 deletions compiler/extensions/python/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ zserio_python.install_dir - Directory where to install Zserio Python Extensi
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 @@ -164,7 +165,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_python.src_dir}" includes="**/*.java"/>
</apply>
</target>

<target name="spotbugs" depends="clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_python.spotbugs.out_dir}"/>
<taskdef classpath="${spotbugs.home_dir}/lib/spotbugs-ant.jar"
resource="edu/umd/cs/findbugs/anttask/tasks.properties"/>
Expand Down Expand Up @@ -205,7 +221,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_python.test.src_dir}" includes="**/*.java"/>
</apply>
</target>

<target name="test.spotbugs" depends="test.clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_python.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
@@ -1,9 +1,9 @@
package zserio.extension.python;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.ArrayList;

import zserio.ast.PackageName;
import zserio.extension.python.symbols.PythonNativeSymbol;
Expand Down Expand Up @@ -51,7 +51,7 @@ public String getSymbol()
public boolean equals(Object otherObject)
{
if (!(otherObject instanceof ApiImport))
return false;
return false;

final ApiImport other = (ApiImport)otherObject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public BitmaskEmitterTemplateData(TemplateDataContext context, BitmaskType bitma
bitSize = createBitSize(bitmaskTypeInstantiation);

final ExpressionFormatter pythonExpressionFormatter = context.getPythonExpressionFormatter(this);
runtimeFunction = RuntimeFunctionDataCreator.createData(
bitmaskTypeInstantiation, pythonExpressionFormatter);
runtimeFunction =
RuntimeFunctionDataCreator.createData(bitmaskTypeInstantiation, pythonExpressionFormatter);

lowerBound = PythonLiteralFormatter.formatDecimalLiteral(getLowerBound(bitmaskTypeInstantiation));
upperBound = PythonLiteralFormatter.formatDecimalLiteral(getUpperBound(bitmaskTypeInstantiation));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public ChoiceEmitterTemplateData(TemplateDataContext context, ChoiceType choiceT
final Iterable<ChoiceCase> choiceCases = choiceType.getChoiceCases();
for (ChoiceCase choiceCase : choiceCases)
{
caseMemberList.add(new CaseMember(
context, choiceType, choiceCase, pythonExpressionFormatter, this));
caseMemberList.add(
new CaseMember(context, choiceType, choiceCase, pythonExpressionFormatter, this));
}

final ChoiceDefault choiceDefault = choiceType.getChoiceDefault();
defaultMember = (choiceDefault == null) ? null:
new DefaultMember(context, choiceType, choiceDefault, this);
defaultMember =
(choiceDefault == null) ? null : new DefaultMember(context, choiceType, choiceDefault, this);

isDefaultUnreachable = choiceType.isChoiceDefaultUnreachable();
}
Expand Down Expand Up @@ -65,16 +65,17 @@ public static final class CaseMember
{
public CaseMember(TemplateDataContext context, ChoiceType choiceType, ChoiceCase choiceCase,
ExpressionFormatter expressionFormatter, ImportCollector importCollector)
throws ZserioExtensionException
throws ZserioExtensionException
{
expressionList = new ArrayList<String>();
final Iterable<ChoiceCaseExpression> caseExpressions = choiceCase.getExpressions();
for (ChoiceCaseExpression caseExpression : caseExpressions)
expressionList.add(expressionFormatter.formatGetter(caseExpression.getExpression()));

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

public Iterable<String> getExpressionList()
Expand All @@ -97,8 +98,9 @@ public DefaultMember(TemplateDataContext context, ChoiceType choiceType, ChoiceD
ImportCollector importCollector) throws ZserioExtensionException
{
final Field fieldType = choiceDefault.getField();
compoundField = (fieldType != null) ?
new CompoundFieldTemplateData(context, choiceType, fieldType, importCollector) : null;
compoundField = (fieldType != null)
? new CompoundFieldTemplateData(context, choiceType, fieldType, importCollector)
: null;
}

public CompoundFieldTemplateData getCompoundField()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
public final class ChoiceTemplateDataContext extends TemplateDataContext
{
public ChoiceTemplateDataContext(PythonExtensionParameters pythonParameters,
PackedTypesCollector packedTypesCollector)
public ChoiceTemplateDataContext(
PythonExtensionParameters pythonParameters, PackedTypesCollector packedTypesCollector)
{
super(pythonParameters, packedTypesCollector);
}
Expand All @@ -27,8 +27,9 @@ public ExpressionFormatter getPythonExpressionFormatter(ImportCollector importCo
@Override
public ExpressionFormatter getPythonOwnerIndirectExpressionFormatter(ImportCollector importCollector)
{
final ExpressionFormattingPolicy expressionFormattingPolicy = new PythonChoiceExpressionFormattingPolicy(
this, importCollector, TemplateDataContext.PYTHON_OWNER_PREFIX);
final ExpressionFormattingPolicy expressionFormattingPolicy =
new PythonChoiceExpressionFormattingPolicy(
this, importCollector, TemplateDataContext.PYTHON_OWNER_PREFIX);

return new ExpressionFormatter(expressionFormattingPolicy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import zserio.ast.ChoiceType;
import zserio.ast.CompoundType;
import zserio.ast.DynamicBitFieldInstantiation;
import zserio.ast.Expression;
import zserio.ast.Field;
import zserio.ast.FixedSizeType;
import zserio.ast.IntegerType;
import zserio.ast.ParameterizedTypeInstantiation;
import zserio.ast.ParameterizedTypeInstantiation.InstantiatedParameter;
import zserio.ast.TypeInstantiation;
import zserio.ast.UnionType;
import zserio.ast.ZserioType;
import zserio.ast.Expression;
import zserio.ast.Field;
import zserio.ast.FixedSizeType;
import zserio.ast.TypeInstantiation;
import zserio.extension.common.ExpressionFormatter;
import zserio.extension.common.ZserioExtensionException;
import zserio.extension.python.types.PythonNativeType;
Expand Down Expand Up @@ -60,8 +60,8 @@ public CompoundFieldTemplateData(TemplateDataContext context, CompoundType paren
bitSize = new BitSize(fieldTypeInstantiation, pythonExpressionFormatter);
offset = createOffset(field, pythonExpressionFormatter);
array = createArray(context, fieldTypeInstantiation, parentType, importCollector);
runtimeFunction = RuntimeFunctionDataCreator.createData(fieldTypeInstantiation,
pythonExpressionFormatter);
runtimeFunction =
RuntimeFunctionDataCreator.createData(fieldTypeInstantiation, pythonExpressionFormatter);
compound = createCompound(context, fieldTypeInstantiation, importCollector);
docComments = DocCommentsDataCreator.createData(context, field);
}
Expand Down Expand Up @@ -195,8 +195,8 @@ public static final class BitFieldWithExpression
public BitFieldWithExpression(DynamicBitFieldInstantiation dynamicBitFieldInstantiation,
ExpressionFormatter pythonExpressionFormatter) throws ZserioExtensionException
{
lengthExpression = pythonExpressionFormatter.formatGetter(
dynamicBitFieldInstantiation.getLengthExpression());
lengthExpression =
pythonExpressionFormatter.formatGetter(dynamicBitFieldInstantiation.getLengthExpression());
isSigned = dynamicBitFieldInstantiation.getBaseType().isSigned();
}

Expand All @@ -220,8 +220,9 @@ public Optional(Field field, ExpressionFormatter pythonExpressionFormatter, bool
throws ZserioExtensionException
{
final Expression optionalClauseExpression = field.getOptionalClauseExpr();
clause = (optionalClauseExpression == null) ? null :
pythonExpressionFormatter.formatGetter(optionalClauseExpression);
clause = (optionalClauseExpression == null)
? null
: pythonExpressionFormatter.formatGetter(optionalClauseExpression);

isUsedIndicatorName = AccessorNameFormatter.getIsUsedIndicatorName(field);
isSetIndicatorName = AccessorNameFormatter.getIsSetIndicatorName(field);
Expand Down Expand Up @@ -267,8 +268,9 @@ public BitSize(TypeInstantiation typeInstantiation, ExpressionFormatter pythonEx
throws ZserioExtensionException
{
value = createValue(typeInstantiation, pythonExpressionFormatter);
runtimeFunction = (value != null) ? null :
RuntimeFunctionDataCreator.createData(typeInstantiation, pythonExpressionFormatter);
runtimeFunction = (value != null)
? null
: RuntimeFunctionDataCreator.createData(typeInstantiation, pythonExpressionFormatter);
}

public String getValue()
Expand Down Expand Up @@ -454,7 +456,7 @@ public static final class Compound
{
public Compound(TemplateDataContext context,
ParameterizedTypeInstantiation parameterizedTypeInstantiation, ImportCollector importCollector)
throws ZserioExtensionException
throws ZserioExtensionException
{
this(context, parameterizedTypeInstantiation.getBaseType(), importCollector);

Expand Down Expand Up @@ -483,8 +485,9 @@ public CompoundParameterTemplateData getParameters()

public static final class InstantiatedParameterData
{
public InstantiatedParameterData(TemplateDataContext context, InstantiatedParameter instantiatedParameter,
ImportCollector importCollector) throws ZserioExtensionException
public InstantiatedParameterData(TemplateDataContext context,
InstantiatedParameter instantiatedParameter, ImportCollector importCollector)
throws ZserioExtensionException
{
final Expression argumentExpression = instantiatedParameter.getArgumentExpression();
final ExpressionFormatter pythonExpressionFormatter =
Expand Down Expand Up @@ -530,16 +533,16 @@ private static RangeCheck createRangeCheck(TypeInstantiation typeInstantiation,
return null;

final IntegerType integerType = (IntegerType)baseType;
final BitFieldWithExpression bitFieldWithExpression = createBitFieldWithExpression(typeInstantiation,
pythonExpressionFormatter);
final BitFieldWithExpression bitFieldWithExpression =
createBitFieldWithExpression(typeInstantiation, pythonExpressionFormatter);

final BigInteger zserioLowerBound = integerType.getLowerBound(typeInstantiation);
final String lowerBound = zserioLowerBound != null ?
PythonLiteralFormatter.formatDecimalLiteral(zserioLowerBound) : null;
final String lowerBound =
zserioLowerBound != null ? PythonLiteralFormatter.formatDecimalLiteral(zserioLowerBound) : null;

final BigInteger zserioUpperBound = integerType.getUpperBound(typeInstantiation);
final String upperBound = zserioUpperBound != null ?
PythonLiteralFormatter.formatDecimalLiteral(zserioUpperBound) : null;
final String upperBound =
zserioUpperBound != null ? PythonLiteralFormatter.formatDecimalLiteral(zserioUpperBound) : null;

return new RangeCheck(bitFieldWithExpression, lowerBound, upperBound);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public static final class CompoundParameter
{
public CompoundParameter(TemplateDataContext context, Parameter parameter,
ImportCollector importCollector, List<DocComment> compoundDocComments)
throws ZserioExtensionException
throws ZserioExtensionException
{
name = parameter.getName();
snakeCaseName = PythonSymbolConverter.toLowerSnakeCase(name);
final TypeReference parameterTypeReference = parameter.getTypeReference();
final PythonNativeType nativeType = context.getPythonNativeMapper().getPythonType(
parameterTypeReference);
final PythonNativeType nativeType =
context.getPythonNativeMapper().getPythonType(parameterTypeReference);
importCollector.importType(nativeType);
typeInfo = new NativeTypeInfoTemplateData(nativeType, parameterTypeReference);
propertyName = AccessorNameFormatter.getPropertyName(parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
*/
final class DocCommentsDataCreator
{
public static DocCommentsTemplateData createData(TemplateDataContext context,
DocumentableAstNode documentableNode) throws ZserioExtensionException
public static DocCommentsTemplateData createData(
TemplateDataContext context, DocumentableAstNode documentableNode) throws ZserioExtensionException
{
return createData(context, documentableNode.getDocComments());
}

public static DocCommentsTemplateData createData(TemplateDataContext context,
List<DocComment> docComments) throws ZserioExtensionException
public static DocCommentsTemplateData createData(TemplateDataContext context, List<DocComment> docComments)
throws ZserioExtensionException
{
final int numComments = docComments.size();
int firstStickyCommentIndex = numComments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
import zserio.ast.EnumItem;
import zserio.ast.Field;
import zserio.ast.Function;
import zserio.ast.SymbolReference;
import zserio.extension.common.ZserioExtensionException;
import zserio.extension.python.symbols.PythonNativeSymbol;
import zserio.ast.Package;
import zserio.ast.PackageSymbol;
import zserio.ast.Parameter;
import zserio.ast.PubsubMessage;
import zserio.ast.ScopeSymbol;
import zserio.ast.ServiceMethod;
import zserio.ast.SqlTableType;
import zserio.ast.SymbolReference;
import zserio.extension.common.ZserioExtensionException;
import zserio.extension.python.symbols.PythonNativeSymbol;

/**
* FreeMarker template data for documentation comments.
Expand Down Expand Up @@ -185,8 +185,7 @@ public DocTagSeeData getSeeTag()

public static final class DocTagSeeData
{
public DocTagSeeData(TemplateDataContext context, DocTagSee docTagSee)
throws ZserioExtensionException
public DocTagSeeData(TemplateDataContext context, DocTagSee docTagSee) throws ZserioExtensionException
{
alias = docTagSee.getLinkAlias();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public EnumerationEmitterTemplateData(TemplateDataContext context, EnumType enum
bitSize = createBitSize(enumTypeInstantiation);

final ExpressionFormatter pythonExpressionFormatter = context.getPythonExpressionFormatter(this);
runtimeFunction = RuntimeFunctionDataCreator.createData(
enumTypeInstantiation, pythonExpressionFormatter);
runtimeFunction =
RuntimeFunctionDataCreator.createData(enumTypeInstantiation, pythonExpressionFormatter);

final List<EnumItem> enumItems = enumType.getItems();
items = new ArrayList<EnumItemData>(enumItems.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ private void processPackage(PackageName packageName) throws ZserioExtensionExcep

private static final String INIT_PY_TEMPLATE = "__init__.py.ftl";
private static final String INIT_PY_FILENAME_ROOT = "__init__";
private final Set<PackageName> processedPackages= new HashSet<PackageName>();
private final Set<PackageName> processedPackages = new HashSet<PackageName>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ private NativeTypeInfoTemplateData(PythonNativeType pythonNativeType, TypeInstan
TypeReference typeReference) throws ZserioExtensionException
{
typeFullName = PythonFullNameFormatter.getFullName(pythonNativeType);
final PythonNativeType pythonNativeBaseType = (pythonNativeType instanceof NativeSubtype) ?
((NativeSubtype)pythonNativeType).getNativeTargetBaseType() : pythonNativeType;
final PythonNativeType pythonNativeBaseType = (pythonNativeType instanceof NativeSubtype)
? ((NativeSubtype)pythonNativeType).getNativeTargetBaseType()
: pythonNativeType;
isBuiltin = pythonNativeBaseType instanceof NativeBuiltinType;
final ZserioType baseType = typeReference.getBaseTypeReference().getType();
isBytes = baseType instanceof BytesType;
Expand All @@ -104,9 +105,9 @@ private NativeTypeInfoTemplateData(PythonNativeType pythonNativeType, TypeInstan
}
else
{
hashCodeFunc = (typeInstantiation != null) ?
RuntimeFunctionDataCreator.createHashCodeData(typeInstantiation) :
RuntimeFunctionDataCreator.createHashCodeData(typeReference);
hashCodeFunc = (typeInstantiation != null)
? RuntimeFunctionDataCreator.createHashCodeData(typeInstantiation)
: RuntimeFunctionDataCreator.createHashCodeData(typeReference);
}
}

Expand Down
Loading

0 comments on commit 37bc91a

Please sign in to comment.