Skip to content

Commit

Permalink
Fix code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
brodmo committed Nov 24, 2023
1 parent f1f25c0 commit 3f7f6d9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
6 changes: 4 additions & 2 deletions core/src/main/java/de/jplag/Submission.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,10 @@ private static File createErrorDirectory(String... subdirectoryNames) {

try {
tokenList = language.parse(new HashSet<>(files));
for (Token token : tokenList) {
logger.debug(String.join(" | ", token.getType().toString(), Integer.toString(token.getLine()), token.getSemantics().toString()));
if (logger.isDebugEnabled()) {
for (Token token : tokenList) {
logger.debug(String.join(" | ", token.getType().toString(), Integer.toString(token.getLine()), token.getSemantics().toString()));
}
}
} catch (ParsingException e) {
logger.warn("Failed to parse submission {} with error {}", this, e.getMessage(), e);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/de/jplag/SubmissionSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void parseSubmissions(List<Submission> submissions) {

int tooShort = 0;
for (Submission submission : submissions) {
logger.info("Parsing submission " + submission.getName());
logger.info("Parsing submission {}", submission.getName());
boolean ok;

logger.trace("------ Parsing submission: " + submission.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @param <T> The type of the visited entity.
*/
public abstract class AbstractVisitor<T> {
private static final Logger logger = LoggerFactory.getLogger(TokenCollector.class);
private static final Logger logger = LoggerFactory.getLogger(AbstractVisitor.class);

private final Predicate<T> condition;
private final List<Consumer<HandlerData<T>>> entryHandlers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ <T> void addToken(TokenType jplagType, Function<T, CodeSemantics> semanticsSuppl
variableRegistry.updateSemantics(semantics);
} else {
if (semanticsSupplier != null) {
logger.warn(String.format("Received semantics for token %s despite not expecting any", jplagType.getDescription()));
logger.warn("Received semantics for token {} despite not expecting any", jplagType.getDescription());
}
token = new Token(jplagType, this.file, line, column, length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void updateSemantics(CodeSemantics semantics) {
* @param mutable Whether the variable is mutable.
*/
public void registerVariable(String variableName, VariableScope scope, boolean mutable) {
logger.debug("Register variable " + variableName);
logger.debug("Register variable {}", variableName);
Variable variable = new Variable(variableName, scope, mutable);
switch (scope) {
case FILE -> fileVariables.put(variableName, variable);
Expand All @@ -139,7 +139,7 @@ public void registerVariable(String variableName, VariableScope scope, boolean m
* "this" keyword in Java, for example.
*/
public void registerVariableAccess(String variableName, boolean isClassVariable) {
logger.debug(variableName + " " + nextVariableAccessType);
logger.debug("{} {}", variableName, nextVariableAccessType);
if (ignoreNextVariableAccess) {
ignoreNextVariableAccess = false;
return;
Expand Down
26 changes: 10 additions & 16 deletions languages/cpp2/src/main/java/de/jplag/cpp2/CPPListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class CPPListener extends AbstractAntlrListener {
.withSemantics(CodeSemantics::createControl);
visit(SelectionStatementContext.class, rule -> rule.If() != null).map(IF_BEGIN, IF_END).addLocalScope()
.withSemantics(CodeSemantics::createControl);
visit(CPP14Parser.Else).map(ELSE).withSemantics(CodeSemantics::createControl); // todo check interaction with if, variable not visible in
// else!
// possible problem: variable from if visible in else, but in reality is not -- doesn't really matter
visit(CPP14Parser.Else).map(ELSE).withSemantics(CodeSemantics::createControl);

visit(LabeledStatementContext.class, rule -> rule.Case() != null).map(CASE).withSemantics(CodeSemantics::createControl);
visit(LabeledStatementContext.class, rule -> rule.Default() != null).map(DEFAULT).withSemantics(CodeSemantics::createControl);
Expand All @@ -63,19 +63,14 @@ class CPPListener extends AbstractAntlrListener {

visit(TemplateDeclarationContext.class).map(GENERIC).withSemantics(CodeSemantics::new);

visit(AssignmentOperatorContext.class).map(ASSIGN).withSemantics(CodeSemantics::new).onEnter((rule, varReg) -> {
varReg.setNextVariableAccessType(VariableAccessType.WRITE);
});
visit(AssignmentOperatorContext.class).map(ASSIGN).withSemantics(CodeSemantics::new)
.onEnter((rule, varReg) -> varReg.setNextVariableAccessType(VariableAccessType.WRITE));
visit(UnaryExpressionContext.class, rule -> rule.PlusPlus() != null || rule.MinusMinus() != null).map(ASSIGN)
.withSemantics(CodeSemantics::new).onEnter((rule, varReg) -> {
varReg.setNextVariableAccessType(VariableAccessType.READ_WRITE);
});
;
.withSemantics(CodeSemantics::new).onEnter((rule, varReg) -> varReg.setNextVariableAccessType(VariableAccessType.READ_WRITE));

visit(StaticAssertDeclarationContext.class).map(STATIC_ASSERT).withSemantics(CodeSemantics::createControl);
visit(EnumeratorDefinitionContext.class).map(VARDEF).withSemantics(CodeSemantics::new).onEnter((rule, varReg) -> {
varReg.setNextVariableAccessType(VariableAccessType.WRITE);
});
visit(EnumeratorDefinitionContext.class).map(VARDEF).withSemantics(CodeSemantics::new)
.onEnter((rule, varReg) -> varReg.setNextVariableAccessType(VariableAccessType.WRITE));
visit(BracedInitListContext.class).map(BRACED_INIT_BEGIN, BRACED_INIT_END).withSemantics(CodeSemantics::new);

visit(SimpleTypeSpecifierContext.class, rule -> {
Expand All @@ -93,7 +88,8 @@ class CPPListener extends AbstractAntlrListener {
if (parent == null) // at this point we know parent exists
throw new IllegalStateException();
// boolean typeMutable = context.theTypeName() != null; // block is duplicate to member variable register
variableRegistry.setNextVariableAccessType(VariableAccessType.WRITE); // todo multiple??
// possible issue: what if multiple variables are declared in the same line?
variableRegistry.setNextVariableAccessType(VariableAccessType.WRITE);
if (parent.initDeclaratorList() == null) {
return;
}
Expand Down Expand Up @@ -133,9 +129,7 @@ class CPPListener extends AbstractAntlrListener {

mapApply(visit(PostfixExpressionContext.class, rule -> rule.LeftParen() != null));
visit(PostfixExpressionContext.class, rule -> rule.PlusPlus() != null || rule.MinusMinus() != null).map(ASSIGN)
.withSemantics(CodeSemantics::new).onEnter((rule, varReg) -> {
varReg.setNextVariableAccessType(VariableAccessType.READ_WRITE);
});
.withSemantics(CodeSemantics::new).onEnter((rule, varReg) -> varReg.setNextVariableAccessType(VariableAccessType.READ_WRITE));

visit(UnqualifiedIdContext.class).onEnter((ctx, varReg) -> {
ParserRuleContext parentCtx = ctx.getParent().getParent();
Expand Down

0 comments on commit 3f7f6d9

Please sign in to comment.