diff --git a/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/frontends/cpp/CXXLanguageFrontend.java b/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/frontends/cpp/CXXLanguageFrontend.java index 55fbff3871..9d17254131 100644 --- a/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/frontends/cpp/CXXLanguageFrontend.java +++ b/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/frontends/cpp/CXXLanguageFrontend.java @@ -39,8 +39,6 @@ import de.fraunhofer.aisec.cpg.graph.TypeManager; import de.fraunhofer.aisec.cpg.graph.declarations.Declaration; import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration; -import de.fraunhofer.aisec.cpg.graph.declarations.ValueDeclaration; -import de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression; import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression; import de.fraunhofer.aisec.cpg.graph.types.TypeParser; import de.fraunhofer.aisec.cpg.graph.types.UnknownType; @@ -189,16 +187,16 @@ public InternalFileContent getContentForInclusion(IIndexFileLocation ifl, String return getContentUncached(astPath); } }; - private DeclarationHandler declarationHandler = new DeclarationHandler(this); - private DeclaratorHandler declaratorHandler = new DeclaratorHandler(this); - private ExpressionHandler expressionHandler = new ExpressionHandler(this); - private InitializerHandler initializerHandler = new InitializerHandler(this); - private ParameterDeclarationHandler parameterDeclarationHandler = + private final DeclarationHandler declarationHandler = new DeclarationHandler(this); + private final DeclaratorHandler declaratorHandler = new DeclaratorHandler(this); + private final ExpressionHandler expressionHandler = new ExpressionHandler(this); + private final InitializerHandler initializerHandler = new InitializerHandler(this); + private final ParameterDeclarationHandler parameterDeclarationHandler = new ParameterDeclarationHandler(this); - private StatementHandler statementHandler = new StatementHandler(this); - private HashMap cachedDeclarations = new HashMap<>(); - private HashMap> cachedExpressions = new HashMap<>(); - private HashMap comments = new HashMap<>(); + private final StatementHandler statementHandler = new StatementHandler(this); + private final HashMap cachedDeclarations = new HashMap<>(); + private final HashMap> cachedExpressions = new HashMap<>(); + private final HashMap comments = new HashMap<>(); public CXXLanguageFrontend(@NonNull TranslationConfiguration config, ScopeManager scopeManager) { super(config, scopeManager, "::"); @@ -298,7 +296,7 @@ public TranslationUnitDeclaration parse(File file) throws TranslationException { } TranslationUnitDeclaration translationUnitDeclaration = - declarationHandler.handleTranslationUnit((CPPASTTranslationUnit) translationUnit); + declarationHandler.handleTranslationUnit(translationUnit); bench.stop(); return translationUnitDeclaration; } catch (CoreException ex) { @@ -469,55 +467,8 @@ private Field getField(Class type, String fieldName) throws NoSuchFieldExcept } } - private void addCachedExpression(IBinding binding, Expression expression) { - if (cachedExpressions.containsKey(binding)) { - cachedExpressions.get(binding).add(expression); - } else { - List expressions = new ArrayList<>(); - expressions.add(expression); - cachedExpressions.put(binding, expressions); - } - } - - /** - * Updates Expressions Refers-To Edge and cachedDeclaration from an old Declaration to new - * Declaration by using cachedExpressions. E.g. VariableDeclaration is replaced by - * FieldDeclaration and the refersTo Edge from the Expression must point to the new - * FieldExpression - * - * @param newDeclaration replaces the old target of the refersTo Edge (e.g. FieldDeclaration) - * @param oldDeclaration current target of the refersTo Edge and will be replaced (e.g. - * VariableDeclaration) - */ - public void replaceDeclarationInExpression( - Declaration newDeclaration, Declaration oldDeclaration) { - IBinding binding = - cachedDeclarations.entrySet().stream() - .filter(d -> d.getValue().equals(oldDeclaration)) - .map(Map.Entry::getKey) - .findFirst() - .orElse(null); - if (binding != null && cachedExpressions.containsKey(binding)) { - List expressions = cachedExpressions.get(binding); - for (Expression expression : expressions) { - ((DeclaredReferenceExpression) expression).setRefersTo((ValueDeclaration) newDeclaration); - } - } - - if (binding != null) { - cachedDeclarations.remove(binding); - cachedDeclarations.put(binding, newDeclaration); - } - } - @Nullable public Declaration cacheDeclaration(IBinding binding, Declaration declaration) { - if (cachedExpressions.containsKey(binding)) { - List expressionList = cachedExpressions.get(binding); - for (Expression expression : expressionList) { - ((DeclaredReferenceExpression) expression).setRefersTo((ValueDeclaration) declaration); - } - } return cachedDeclarations.put(binding, declaration); } @@ -525,13 +476,6 @@ public Declaration getCachedDeclaration(IBinding binding) { return cachedDeclarations.get(binding); } - public List getCachedExpression(IBinding binding) { - if (cachedExpressions.containsKey(binding)) { - return cachedExpressions.get(binding); - } - return new ArrayList<>(); - } - public Map getCachedDeclarations() { return cachedDeclarations; } diff --git a/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/frontends/cpp/ExpressionHandler.java b/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/frontends/cpp/ExpressionHandler.java index 34f6469502..43a1ada966 100644 --- a/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/frontends/cpp/ExpressionHandler.java +++ b/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/frontends/cpp/ExpressionHandler.java @@ -54,7 +54,7 @@ class ExpressionHandler extends Handler { /* Note: CDT expresses hierarchies in Interfaces to allow to have multi-inheritance in java. Because some Expressions - have subelements of type IASTInitalizerClause and in the hierarchy IASTExpression extends IASTInitializerClause. + have sub elements of type IASTInitializerClause and in the hierarchy IASTExpression extends IASTInitializerClause. The later is the appropriate Interface type for the handler. */ @@ -391,15 +391,12 @@ private Expression handleFieldReference(CPPASTFieldReference ctx) { base.setLocation(location); } - MemberExpression memberExpression = - NodeBuilder.newMemberExpression( - base, - UnknownType.getUnknownType(), - ctx.getFieldName().toString(), - ctx.isPointerDereference() ? "->" : ".", - ctx.getRawSignature()); - - return memberExpression; + return NodeBuilder.newMemberExpression( + base, + UnknownType.getUnknownType(), + ctx.getFieldName().toString(), + ctx.isPointerDereference() ? "->" : ".", + ctx.getRawSignature()); } private Expression handleUnaryExpression(CPPASTUnaryExpression ctx) { diff --git a/cpg-library/src/test/java/de/fraunhofer/aisec/cpg/frontends/cpp/CXXBindingsTest.java b/cpg-library/src/test/java/de/fraunhofer/aisec/cpg/frontends/cpp/CXXBindingsTest.java deleted file mode 100644 index da4cda4d98..0000000000 --- a/cpg-library/src/test/java/de/fraunhofer/aisec/cpg/frontends/cpp/CXXBindingsTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2021, Fraunhofer AISEC. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * $$$$$$\ $$$$$$$\ $$$$$$\ - * $$ __$$\ $$ __$$\ $$ __$$\ - * $$ / \__|$$ | $$ |$$ / \__| - * $$ | $$$$$$$ |$$ |$$$$\ - * $$ | $$ ____/ $$ |\_$$ | - * $$ | $$\ $$ | $$ | $$ | - * \$$$$$ |$$ | \$$$$$ | - * \______/ \__| \______/ - * - */ -package de.fraunhofer.aisec.cpg.frontends.cpp; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import de.fraunhofer.aisec.cpg.BaseTest; -import de.fraunhofer.aisec.cpg.TranslationConfiguration; -import de.fraunhofer.aisec.cpg.graph.declarations.Declaration; -import de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression; -import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression; -import de.fraunhofer.aisec.cpg.passes.scopes.ScopeManager; -import java.io.File; -import org.eclipse.cdt.core.dom.ast.IBinding; -import org.junit.jupiter.api.Test; - -class CXXBindingsTest extends BaseTest { - - void checkBindings(CXXLanguageFrontend cxxLanguageFrontend) { - for (IBinding binding : cxxLanguageFrontend.getCachedDeclarations().keySet()) { - Declaration declaration = cxxLanguageFrontend.getCachedDeclaration(binding); - for (Expression expression : cxxLanguageFrontend.getCachedExpression(binding)) { - assertEquals(declaration, ((DeclaredReferenceExpression) expression).getRefersTo()); - } - } - } - - @Test - void testUseThenDeclaration() throws Exception { - CXXLanguageFrontend cxxLanguageFrontend = - new CXXLanguageFrontend(TranslationConfiguration.builder().build(), new ScopeManager()); - cxxLanguageFrontend.parse(new File("src/test/resources/bindings/use_then_declare.cpp")); - - checkBindings(cxxLanguageFrontend); - } - - @Test - void testDeclarationReplacement() throws Exception { - CXXLanguageFrontend cxxLanguageFrontend = - new CXXLanguageFrontend(TranslationConfiguration.builder().build(), new ScopeManager()); - cxxLanguageFrontend.parse(new File("src/test/resources/bindings/replace_declaration.cpp")); - - checkBindings(cxxLanguageFrontend); - } -}