annotations.tc.TCPrintfAnnotation Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
*
* Copyright (c) 2018 Nick Battle.
*
* Author: Nick Battle
*
* This file is part of VDMJ.
*
* VDMJ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VDMJ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VDMJ. If not, see .
* SPDX-License-Identifier: GPL-3.0-or-later
*
******************************************************************************/
package annotations.tc;
import java.util.Arrays;
import com.fujitsu.vdmj.tc.annotations.TCAnnotation;
import com.fujitsu.vdmj.tc.definitions.TCClassDefinition;
import com.fujitsu.vdmj.tc.definitions.TCDefinition;
import com.fujitsu.vdmj.tc.expressions.TCExpression;
import com.fujitsu.vdmj.tc.expressions.TCExpressionList;
import com.fujitsu.vdmj.tc.expressions.TCStringLiteralExpression;
import com.fujitsu.vdmj.tc.lex.TCIdentifierToken;
import com.fujitsu.vdmj.tc.modules.TCModule;
import com.fujitsu.vdmj.tc.statements.TCStatement;
import com.fujitsu.vdmj.typechecker.Environment;
import com.fujitsu.vdmj.typechecker.NameScope;
import com.fujitsu.vdmj.values.SeqValue;
public class TCPrintfAnnotation extends TCAnnotation
{
private static final long serialVersionUID = 1L;
public TCPrintfAnnotation(TCIdentifierToken name, TCExpressionList args)
{
super(name, args);
}
@Override
public void tcBefore(TCDefinition def, Environment env, NameScope scope)
{
name.report(6009, "@Printf only applies to statements and expressions");
}
@Override
public void tcBefore(TCModule module)
{
name.report(6009, "@Printf only applies to statements and expressions");
}
@Override
public void tcBefore(TCClassDefinition clazz)
{
name.report(6009, "@Printf only applies to statements and expressions");
}
@Override
public void tcBefore(TCExpression exp, Environment env, NameScope scope)
{
checkArgs(env, scope);
}
@Override
public void tcBefore(TCStatement stmt, Environment env, NameScope scope)
{
checkArgs(env, scope);
}
private void checkArgs(Environment env, NameScope scope)
{
if (args.isEmpty())
{
name.report(6008, "@Printf must start with a string argument");
}
else
{
if (args.get(0) instanceof TCStringLiteralExpression)
{
for (TCExpression arg: args)
{
arg.typeCheck(env, null, scope, null); // Just checks scope
}
TCStringLiteralExpression str = (TCStringLiteralExpression)args.get(0);
String format = str.value.value;
try
{
// Try to format with string arguments to check they are all %s (up to 20)
Object[] args = new SeqValue[20];
Arrays.fill(args, new SeqValue("abc"));
String.format(format, args);
}
catch (IllegalArgumentException e)
{
name.report(6008, "@Printf must use %[arg$][#][width](s|S) conversions");
}
}
else
{
name.report(6008, "@Printf must start with a string argument");
}
}
}
}