Skip to content

Commit

Permalink
Add clang-format to the Zserio core project
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jan 9, 2024
1 parent 6d9ae66 commit 823ef80
Show file tree
Hide file tree
Showing 89 changed files with 1,744 additions and 1,709 deletions.
35 changes: 33 additions & 2 deletions compiler/core/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ zserio_core.build_dir - Directory where to put generated files.
zserio_core.install_dir - Directory where to install Zserio core jar file.
Default is "${zserio.install_dir}/zserio_libs".
zserio.version - Zserio version to be used for Zserio core. Default is "unknown".
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 @@ -205,7 +206,22 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spotbugs is
</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_core.src_dir}" includes="**/*.java"/>
</apply>
</target>

<target name="spotbugs" depends="clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_core.spotbugs.out_dir}"/>
<taskdef classpath="${spotbugs.home_dir}/lib/spotbugs-ant.jar"
resource="edu/umd/cs/findbugs/anttask/tasks.properties"/>
Expand Down Expand Up @@ -246,7 +262,22 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spotbugs is
</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_core.test.src_dir}" includes="**/*.java"/>
</apply>
</target>

<target name="test.spotbugs" depends="test.clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_core.test.spotbugs.out_dir}"/>
<taskdef classpath="${spotbugs.home_dir}/lib/spotbugs-ant.jar"
resource="edu/umd/cs/findbugs/anttask/tasks.properties"/>
Expand Down
23 changes: 11 additions & 12 deletions compiler/core/src/zserio/antlr/util/ParseErrorListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,31 @@
public final class ParseErrorListener extends BaseErrorListener
{
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg,
RecognitionException e) throws ParseCancellationException
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException
{
final AstLocation location = new AstLocation(recognizer.getInputStream().getSourceName(), line,
charPositionInLine);
final AstLocation location =
new AstLocation(recognizer.getInputStream().getSourceName(), line, charPositionInLine);
if (e instanceof InputMismatchException)
{
final Token offendingToken = e.getOffendingToken();
if (offendingToken != null)
{
if (isKeyword(offendingToken))
{
final ParserStackedException stackedException = new ParserStackedException(location,
"'" + offendingToken.getText() + "' is a reserved keyword!");
stackedException.pushMessage(location, msg);
final ParserStackedException stackedException = new ParserStackedException(
location, "'" + offendingToken.getText() + "' is a reserved keyword!");
stackedException.pushMessage(location, msg);
throw stackedException;
}

if (isInvalidStringLiteral(offendingToken))
throw new ParserException(location, "'" + offendingToken.getText() +
"' is an invalid string literal!");
throw new ParserException(
location, "'" + offendingToken.getText() + "' is an invalid string literal!");

if (isInvalidToken(offendingToken))
throw new ParserException(location, "'" + offendingToken.getText() +
"' is an invalid token!");
throw new ParserException(
location, "'" + offendingToken.getText() + "' is an invalid token!");
}
}

Expand Down
13 changes: 7 additions & 6 deletions compiler/core/src/zserio/antlr/util/TokenParseErrorListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public TokenParseErrorListener(Token token)
}

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg,
RecognitionException e) throws ParseCancellationException
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException
{
throw new ParserException(new AstLocation(this.sourceName,
this.line + line - 1, // lines are numbered from 1
(line == 1 ? this.charPositionInLine + charPositionInLine : charPositionInLine)), msg);
throw new ParserException(
new AstLocation(this.sourceName,
this.line + line - 1, // lines are numbered from 1
(line == 1 ? this.charPositionInLine + charPositionInLine : charPositionInLine)),
msg);
}

final private String sourceName;
Expand Down
41 changes: 22 additions & 19 deletions compiler/core/src/zserio/ast/ArrayInstantiation.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public final class ArrayInstantiation extends TypeInstantiation
* @param lengthExpression Array length expression.
*/
public ArrayInstantiation(AstLocation location, TypeReference typeReference,
TypeInstantiation elementTypeInstantiation, boolean isPacked,
boolean isImplicit, Expression lengthExpression)
TypeInstantiation elementTypeInstantiation, boolean isPacked, boolean isImplicit,
Expression lengthExpression)
{
super(location, typeReference);

Expand Down Expand Up @@ -114,8 +114,9 @@ ArrayInstantiation instantiateImpl(List<TemplateParameter> templateParameters,
{
final TypeInstantiation instantiatedElementTypeInstantiation =
elementTypeInstantiation.instantiate(templateParameters, templateArguments);
final Expression instantiatedLengthExpression = getLengthExpression() == null ? null :
getLengthExpression().instantiate(templateParameters, templateArguments);
final Expression instantiatedLengthExpression = getLengthExpression() == null
? null
: getLengthExpression().instantiate(templateParameters, templateArguments);

return new ArrayInstantiation(getLocation(), instantiatedTypeReference,
instantiatedElementTypeInstantiation, isPacked, isImplicit, instantiatedLengthExpression);
Expand All @@ -126,8 +127,9 @@ void resolve()
{
if (!(super.getBaseType() instanceof ArrayType))
{
throw new ParserException(getTypeReference(), "Referenced type '" +
ZserioTypeUtil.getReferencedFullName(getTypeReference()) + "' is not an array type!");
throw new ParserException(getTypeReference(),
"Referenced type '" + ZserioTypeUtil.getReferencedFullName(getTypeReference()) +
"' is not an array type!");
}
}

Expand Down Expand Up @@ -160,8 +162,8 @@ void check(WarningsConfig warningsConfig, ZserioTemplatableType currentTemplateI
{
if (lengthExpression.getExprType() != Expression.ExpressionType.INTEGER)
{
throw new ParserException(lengthExpression,
"Invalid length expression for array. Length must be integer!");
throw new ParserException(
lengthExpression, "Invalid length expression for array. Length must be integer!");
}
}
}
Expand All @@ -184,12 +186,12 @@ static boolean isTypePackable(ZserioType baseType)

private static boolean isSimpleTypePackable(ZserioType baseType)
{
return baseType instanceof IntegerType ||
baseType instanceof EnumType || baseType instanceof BitmaskType;
return baseType instanceof IntegerType || baseType instanceof EnumType ||
baseType instanceof BitmaskType;
}

private void checkPackedArrayElementType(WarningsConfig warningsConfig,
ZserioTemplatableType currentTemplateInstantiation)
private void checkPackedArrayElementType(
WarningsConfig warningsConfig, ZserioTemplatableType currentTemplateInstantiation)
{
if (isPacked)
{
Expand All @@ -205,8 +207,9 @@ private void checkPackedArrayElementType(WarningsConfig warningsConfig,
if (!elementCompoundType.getFields().isEmpty())
{
printUnpackableWarning(warningsConfig, currentTemplateInstantiation,
"Keyword 'packed' doesn't have any effect. " +
"'" + elementCompoundType.getName() + "' doesn't contain any packable field.",
"Keyword 'packed' doesn't have any effect. "
+ "'" + elementCompoundType.getName() +
"' doesn't contain any packable field.",
WarningsConfig.UNPACKABLE_ARRAY);
}
}
Expand All @@ -220,8 +223,8 @@ else if (elementCompoundType instanceof UnionType && !elementCompoundType.hasPac
else if (!(isSimpleTypePackable(elementBaseType)))
{
printUnpackableWarning(warningsConfig, currentTemplateInstantiation,
"Keyword 'packed' doesn't have any effect. " +
"'" + elementBaseType.getName() + "' is not packable element type.",
"Keyword 'packed' doesn't have any effect. "
+ "'" + elementBaseType.getName() + "' is not packable element type.",
WarningsConfig.UNPACKABLE_ARRAY);
}
}
Expand All @@ -237,11 +240,11 @@ private void printUnpackableWarning(WarningsConfig warningsConfig,
{
ZserioToolPrinter.printWarning(instantiationReference.getLocation(),
" In instantiation of '" + instantiationReference.getReferencedTypeName() +
"' required from here");
"' required from here");
}
}
ZserioToolPrinter.printWarning(getElementTypeInstantiation(), message,
warningsConfig, warningSpecifier);
ZserioToolPrinter.printWarning(
getElementTypeInstantiation(), message, warningsConfig, warningSpecifier);
}

private boolean checkImplicitArrayElementType()
Expand Down
6 changes: 4 additions & 2 deletions compiler/core/src/zserio/ast/AstLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ public int getColumn()
@Override
public String toString()
{
return fileName == null ? "" : StringJoinUtil.joinStrings(fileName, Integer.toString(line),
Integer.toString(column), LOCATION_SEPARATOR);
return fileName == null
? ""
: StringJoinUtil.joinStrings(
fileName, Integer.toString(line), Integer.toString(column), LOCATION_SEPARATOR);
}

private final String fileName;
Expand Down
19 changes: 11 additions & 8 deletions compiler/core/src/zserio/ast/BitmaskType.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ void evaluate()
final ZserioType baseType = typeInstantiation.getBaseType();
if (!(baseType instanceof IntegerType) || ((IntegerType)baseType).isSigned())
{
throw new ParserException(this, "Bitmask '" + this.getName() + "' cannot use " +
baseType.getName() + " type! Only unsigned integer types are allowed!");
throw new ParserException(this,
"Bitmask '" + this.getName() + "' cannot use " + baseType.getName() +
" type! Only unsigned integer types are allowed!");
}

// evaluate bitmask values
Expand Down Expand Up @@ -154,8 +155,8 @@ private void checkBitmaskValues()
{
// bitmask value is duplicated
final ParserStackedException stackedException = new ParserStackedException(
getValueLocation(bitmaskValue), "Bitmask '" + bitmaskValue.getName() +
"' has duplicated value (" + intValue + ")!");
getValueLocation(bitmaskValue),
"Bitmask '" + bitmaskValue.getName() + "' has duplicated value (" + intValue + ")!");
stackedException.pushMessage(getValueLocation(prevValue), " First defined here");
throw stackedException;
}
Expand All @@ -167,16 +168,18 @@ private void checkBitmaskValues()
final BigInteger upperBound = integerBaseType.getUpperBound(typeInstantiation);
if (intValue.compareTo(lowerBound) < 0 || intValue.compareTo(upperBound) > 0)
{
throw new ParserException(getValueLocation(bitmaskValue), "Bitmask '" + bitmaskValue.getName() +
"' has value (" + intValue + ") out of range <" + lowerBound + "," + upperBound + ">!");
throw new ParserException(getValueLocation(bitmaskValue),
"Bitmask '" + bitmaskValue.getName() + "' has value (" + intValue + ") out of range <" +
lowerBound + "," + upperBound + ">!");
}
}
}

private AstLocation getValueLocation(BitmaskValue bitmaskValue)
{
return bitmaskValue.getValueExpression() != null ?
bitmaskValue.getValueExpression().getLocation() : bitmaskValue.getLocation();
return bitmaskValue.getValueExpression() != null
? bitmaskValue.getValueExpression().getLocation()
: bitmaskValue.getLocation();
}

private final Scope scope = new Scope(this);
Expand Down
12 changes: 6 additions & 6 deletions compiler/core/src/zserio/ast/BitmaskValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public final class BitmaskValue extends DocumentableAstNode implements ScopeSymb
* @param valueExpression Expression value of the bitmask.
* @param docComments List of documentation comments belonging to this node.
*/
public BitmaskValue(AstLocation location, String name, Expression valueExpression,
List<DocComment> docComments)
public BitmaskValue(
AstLocation location, String name, Expression valueExpression, List<DocComment> docComments)
{
super(location, docComments);

Expand Down Expand Up @@ -90,13 +90,13 @@ void evaluate()
if (valueExpression != null)
{
if (valueExpression.getExprType() != Expression.ExpressionType.INTEGER)
throw new ParserException(valueExpression, "Bitmask value '" + getName() +
"' is not an integral type!");
throw new ParserException(
valueExpression, "Bitmask value '" + getName() + "' is not an integral type!");

value = valueExpression.getIntegerValue();
if (value.compareTo(BigInteger.ZERO) == -1)
throw new ParserException(valueExpression, "Bitmask value '" + getName() +
"(" + value + ") cannot be negative!");
throw new ParserException(valueExpression,
"Bitmask value '" + getName() + "(" + value + ") cannot be negative!");
}

isEvaluated = true;
Expand Down
4 changes: 2 additions & 2 deletions compiler/core/src/zserio/ast/ChoiceCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public Field getField()
*/
ChoiceCase instantiate(List<TemplateParameter> templateParameters, List<TemplateArgument> templateArguments)
{
final Field instantiatedCaseField = caseField == null ? null :
caseField.instantiate(templateParameters, templateArguments);
final Field instantiatedCaseField =
caseField == null ? null : caseField.instantiate(templateParameters, templateArguments);

final List<ChoiceCaseExpression> instantiatedCaseExpressions = new ArrayList<ChoiceCaseExpression>();
for (ChoiceCaseExpression choiceCaseExpression : caseExpressions)
Expand Down
4 changes: 2 additions & 2 deletions compiler/core/src/zserio/ast/ChoiceCaseExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public Expression getExpression()
*
* @return New choice case expression instantiated from this using the given template arguments.
*/
ChoiceCaseExpression instantiate(List<TemplateParameter> templateParameters,
List<TemplateArgument> templateArguments)
ChoiceCaseExpression instantiate(
List<TemplateParameter> templateParameters, List<TemplateArgument> templateArguments)
{
final Expression instantiatedExpression =
getExpression().instantiate(templateParameters, templateArguments);
Expand Down
8 changes: 4 additions & 4 deletions compiler/core/src/zserio/ast/ChoiceDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public Field getField()
*
* @return New choice default instantiated from this using the given template arguments.
*/
ChoiceDefault instantiate(List<TemplateParameter> templateParameters,
List<TemplateArgument> templateArguments)
ChoiceDefault instantiate(
List<TemplateParameter> templateParameters, List<TemplateArgument> templateArguments)
{
final Field instantiatedDefaultField = defaultField == null ? null :
defaultField.instantiate(templateParameters, templateArguments);
final Field instantiatedDefaultField =
defaultField == null ? null : defaultField.instantiate(templateParameters, templateArguments);

return new ChoiceDefault(getLocation(), instantiatedDefaultField, getDocComments());
}
Expand Down
Loading

0 comments on commit 823ef80

Please sign in to comment.