Skip to content

Commit

Permalink
Improved c++ readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Feb 9, 2024
1 parent 63e9839 commit b25863c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ In the following, a list of all supported languages with their supported languag
| Language | Version | CLI Argument Name | [state](https://github.com/jplag/JPlag/wiki/2.-Supported-Languages) | parser |
|------------------------------------------------------------|---------------------------------------------------------------------------------------:|-------------------|:-------------------------------------------------------------------:|:---------:|
| [Java](https://www.java.com) | 21 | java | mature | JavaC |
| [C/C++](https://isocpp.org) | 11 | cpp | legacy | JavaCC |
| [C/C++](https://isocpp.org) | 14 | cpp2 | beta | ANTLR 4 |
| [C](https://isocpp.org) | 11 | cpp | legacy | JavaCC |
| [C++](https://isocpp.org) | 14 | cpp2 | beta | ANTLR 4 |
| [C#](https://docs.microsoft.com/en-us/dotnet/csharp/) | 6 | csharp | beta | ANTLR 4 |
| [Go](https://go.dev) | 1.17 | golang | beta | ANTLR 4 |
| [Kotlin](https://kotlinlang.org) | 1.3 | kotlin | beta | ANTLR 4 |
Expand Down
2 changes: 1 addition & 1 deletion docs/2.-Supported-Languages.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
JPlag currently supports Java, C/C++, C#, Go, Kotlin, Python, R, Rust, Scala, Swift, and Scheme. Additionally, it has primitive support for text and prototypical support for EMF metamodels. A detailed list, including the supported language versions can be found in the [project readme](https://github.com/jplag/JPlag/blob/main/README.md#supported-languages).
JPlag currently supports Java, C, C++, C#, Go, Kotlin, Python, R, Rust, Scala, Swift, and Scheme. Additionally, it has primitive support for text and prototypical support for EMF metamodels. A detailed list, including the supported language versions can be found in the [project readme](https://github.com/jplag/JPlag/blob/main/README.md#supported-languages).

The language modules differ in their maturity due to their age and different usage frequencies.
Thus, each frontend has a state label:
Expand Down
58 changes: 39 additions & 19 deletions languages/cpp2/src/main/java/de/jplag/cpp2/CPPListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,11 @@ class CPPListener extends AbstractAntlrListener {

visit(FunctionDefinitionContext.class).map(FUNCTION_BEGIN, FUNCTION_END).addLocalScope().withSemantics(CodeSemantics::createControl);

visit(IterationStatementContext.class, rule -> rule.Do() != null).map(DO_BEGIN, DO_END).addLocalScope().withLoopSemantics();
visit(IterationStatementContext.class, rule -> rule.For() != null).map(FOR_BEGIN, FOR_END).addLocalScope().withLoopSemantics();
visit(IterationStatementContext.class, rule -> rule.While() != null && rule.Do() == null).map(WHILE_BEGIN, WHILE_END).addLocalScope()
.withLoopSemantics();

visit(SelectionStatementContext.class, rule -> rule.Switch() != null).map(SWITCH_BEGIN, SWITCH_END).addLocalScope()
.withSemantics(CodeSemantics::createControl);
visit(SelectionStatementContext.class, rule -> rule.If() != null).map(IF_BEGIN, IF_END).addLocalScope()
.withSemantics(CodeSemantics::createControl);
// 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);
statementRules();

visit(TryBlockContext.class).map(TRY_BEGIN, TRY_END).addLocalScope().withSemantics(CodeSemantics::createControl);
visit(HandlerContext.class).map(CATCH_BEGIN, CATCH_END).addLocalScope().withSemantics(CodeSemantics::createControl);

visit(JumpStatementContext.class, rule -> rule.Break() != null).map(BREAK).withSemantics(CodeSemantics::createControl);
visit(JumpStatementContext.class, rule -> rule.Continue() != null).map(CONTINUE).withSemantics(CodeSemantics::createControl);
visit(JumpStatementContext.class, rule -> rule.Goto() != null).map(GOTO).withSemantics(CodeSemantics::createControl);
visit(JumpStatementContext.class, rule -> rule.Return() != null).map(RETURN).withSemantics(CodeSemantics::createControl);

visit(ThrowExpressionContext.class).map(THROW).withSemantics(CodeSemantics::createControl);

visit(NewExpressionContext.class, rule -> rule.newInitializer() != null).map(NEWCLASS).withSemantics(CodeSemantics::new);
Expand All @@ -73,6 +55,35 @@ class CPPListener extends AbstractAntlrListener {
.onEnter((rule, varReg) -> varReg.setNextVariableAccessType(VariableAccessType.WRITE));
visit(BracedInitListContext.class).map(BRACED_INIT_BEGIN, BRACED_INIT_END).withSemantics(CodeSemantics::new);

typeSpecifierRule();
declarationRules();
expressionRules();
idRules();
}

private void statementRules() {
visit(IterationStatementContext.class, rule -> rule.Do() != null).map(DO_BEGIN, DO_END).addLocalScope().withLoopSemantics();
visit(IterationStatementContext.class, rule -> rule.For() != null).map(FOR_BEGIN, FOR_END).addLocalScope().withLoopSemantics();
visit(IterationStatementContext.class, rule -> rule.While() != null && rule.Do() == null).map(WHILE_BEGIN, WHILE_END).addLocalScope()
.withLoopSemantics();

visit(SelectionStatementContext.class, rule -> rule.Switch() != null).map(SWITCH_BEGIN, SWITCH_END).addLocalScope()
.withSemantics(CodeSemantics::createControl);
visit(SelectionStatementContext.class, rule -> rule.If() != null).map(IF_BEGIN, IF_END).addLocalScope()
.withSemantics(CodeSemantics::createControl);
// 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);

visit(JumpStatementContext.class, rule -> rule.Break() != null).map(BREAK).withSemantics(CodeSemantics::createControl);
visit(JumpStatementContext.class, rule -> rule.Continue() != null).map(CONTINUE).withSemantics(CodeSemantics::createControl);
visit(JumpStatementContext.class, rule -> rule.Goto() != null).map(GOTO).withSemantics(CodeSemantics::createControl);
visit(JumpStatementContext.class, rule -> rule.Return() != null).map(RETURN).withSemantics(CodeSemantics::createControl);
}

private void typeSpecifierRule() {
visit(SimpleTypeSpecifierContext.class, rule -> {
if (hasAncestor(rule, MemberdeclarationContext.class, FunctionDefinitionContext.class)) {
return true;
Expand All @@ -99,7 +110,9 @@ class CPPListener extends AbstractAntlrListener {
variableRegistry.registerVariable(name, scope, true);
}
});
}

private void declarationRules() {
mapApply(visit(SimpleDeclarationContext.class, rule -> {
if (!hasAncestor(rule, FunctionBodyContext.class)) {
return false;
Expand All @@ -125,12 +138,17 @@ class CPPListener extends AbstractAntlrListener {
varReg.setNextVariableAccessType(VariableAccessType.WRITE);
}
});
}

private void expressionRules() {
visit(ConditionalExpressionContext.class, rule -> rule.Question() != null).map(QUESTIONMARK).withSemantics(CodeSemantics::new);

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));
}

private void idRules() {
visit(UnqualifiedIdContext.class).onEnter((ctx, varReg) -> {
ParserRuleContext parentCtx = ctx.getParent().getParent();
if (!parentCtx.getParent().getParent().getText().contains("(")) {
Expand All @@ -141,6 +159,8 @@ class CPPListener extends AbstractAntlrListener {
});
}



private void mapApply(ContextVisitor<?> visitor) {
visitor.onExit((ctx, varReg) -> varReg.setMutableWrite(false)).onEnter((ctx, varReg) -> {
varReg.addAllNonLocalVariablesAsReads();
Expand Down

0 comments on commit b25863c

Please sign in to comment.