diff --git a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/java/StatementHandler.java b/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/java/StatementHandler.java index 60679c24a5..b386061b2d 100644 --- a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/java/StatementHandler.java +++ b/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/java/StatementHandler.java @@ -402,9 +402,8 @@ public de.fraunhofer.aisec.cpg.graph.statements.Statement handleCaseDefaultState PhysicalLocation parentLocation = lang.getLocationFromRawNode(sEntry); Optional optionalTokenRange = sEntry.getTokenRange(); - Pair caseTokens = null; + Pair caseTokens = new Pair<>(null, null); if (optionalTokenRange.isEmpty()) { - caseTokens = new Pair<>(null, null); log.error("Token for Region for Default case not available"); } @@ -416,12 +415,11 @@ public de.fraunhofer.aisec.cpg.graph.statements.Statement handleCaseDefaultState this is correct anyway */ // Compute region and code for self generated default statement to match the c++ versions + caseTokens = - getOuterTokensWithText( - "default", - ":", - optionalTokenRange.get().getBegin(), - optionalTokenRange.get().getEnd()); + new Pair<>( + getNextTokenWith("default", optionalTokenRange.get().getBegin()), + getNextTokenWith(":", optionalTokenRange.get().getBegin())); } DefaultStatement defaultStatement = NodeBuilder.newDefaultStatement(getCodeBetweenTokens(caseTokens.a, caseTokens.b)); @@ -430,11 +428,13 @@ public de.fraunhofer.aisec.cpg.graph.statements.Statement handleCaseDefaultState return defaultStatement; } - if (optionalTokenRange.isPresent()) { + Optional caseExprTokenRange = caseExpression.getTokenRange(); + if (optionalTokenRange.isPresent() && caseExprTokenRange.isPresent()) { // Compute region and code for self generated case statement to match the c++ versions caseTokens = - getOuterTokensWithText( - "case", ":", optionalTokenRange.get().getBegin(), optionalTokenRange.get().getEnd()); + new Pair<>( + getPreviousTokenWith("case", optionalTokenRange.get().getBegin()), + getNextTokenWith(":", caseExprTokenRange.get().getEnd())); } CaseStatement caseStatement = @@ -442,11 +442,6 @@ public de.fraunhofer.aisec.cpg.graph.statements.Statement handleCaseDefaultState caseStatement.setCaseExpression( (Expression) lang.getExpressionHandler().handle(caseExpression)); - /* - TODO: not sure if this is really necessary, it seems to be the same location as - parentLocation, except that column starts 1 character later and I am not sure if - this is correct anyway - */ caseStatement.setLocation(getLocationsFromTokens(parentLocation, caseTokens.a, caseTokens.b)); return caseStatement; @@ -454,7 +449,7 @@ public de.fraunhofer.aisec.cpg.graph.statements.Statement handleCaseDefaultState public JavaToken getPreviousTokenWith(String text, JavaToken token) { Optional optional = token.getPreviousToken(); - while (token.getText().equals(text) && optional.isPresent()) { + while (!token.getText().equals(text) && optional.isPresent()) { token = optional.get(); optional = token.getPreviousToken(); } @@ -463,18 +458,13 @@ public JavaToken getPreviousTokenWith(String text, JavaToken token) { public JavaToken getNextTokenWith(String text, JavaToken token) { Optional optional = token.getNextToken(); - while (token.getText().equals(text) && optional.isPresent()) { + while (!token.getText().equals(text) && optional.isPresent()) { token = optional.get(); optional = token.getNextToken(); } return token; } - public Pair getOuterTokensWithText( - String startDelim, String endDelim, JavaToken start, JavaToken end) { - return new Pair<>(getPreviousTokenWith(startDelim, start), getNextTokenWith(endDelim, end)); - } - @Nullable public PhysicalLocation getLocationsFromTokens( PhysicalLocation parentLocation, JavaToken startToken, JavaToken endToken) {