Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Go optimizations #1160

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
*/
package de.fraunhofer.aisec.cpg.passes

import de.fraunhofer.aisec.cpg.ScopeManager
import de.fraunhofer.aisec.cpg.TranslationConfiguration
import de.fraunhofer.aisec.cpg.TranslationContext
import de.fraunhofer.aisec.cpg.analysis.ValueEvaluator
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
Expand All @@ -42,10 +41,7 @@ import de.fraunhofer.aisec.cpg.processing.strategy.Strategy
* by setting the [Properties.UNREACHABLE] property of an eog-edge to true.
*/
@DependsOn(ControlFlowSensitiveDFGPass::class)
class UnreachableEOGPass(
config: TranslationConfiguration,
scopeManager: ScopeManager,
) : TranslationUnitPass(config, scopeManager) {
class UnreachableEOGPass(ctx: TranslationContext) : TranslationUnitPass(ctx) {
override fun accept(tu: TranslationUnitDeclaration) {
tu.accept(
Strategy::AST_FORWARD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.analysis

import de.fraunhofer.aisec.cpg.TestUtils
import de.fraunhofer.aisec.cpg.frontends.TestHandler
import de.fraunhofer.aisec.cpg.frontends.TestLanguageFrontend
import de.fraunhofer.aisec.cpg.frontends.java.JavaLanguage
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration
Expand Down Expand Up @@ -181,7 +182,7 @@ class ValueEvaluatorTest {

@Test
fun testHandlePlus() {
with(TestHandler()) {
with(TestHandler(TestLanguageFrontend())) {
val binOp = newBinaryOperator("+")
binOp.lhs = newLiteral(3, parseType("int"))
binOp.rhs = newLiteral(2, parseType("int"))
Expand Down Expand Up @@ -242,7 +243,7 @@ class ValueEvaluatorTest {

@Test
fun testHandleMinus() {
with(TestHandler()) {
with(TestHandler(TestLanguageFrontend())) {
val binOp = newBinaryOperator("-")
binOp.lhs = newLiteral(3, parseType("int"))
binOp.rhs = newLiteral(2, parseType("int"))
Expand Down Expand Up @@ -300,7 +301,7 @@ class ValueEvaluatorTest {

@Test
fun testHandleTimes() {
with(TestHandler()) {
with(TestHandler(TestLanguageFrontend())) {
val binOp = newBinaryOperator("*")
binOp.lhs = newLiteral(3, parseType("int"))
binOp.rhs = newLiteral(2, parseType("int"))
Expand Down Expand Up @@ -358,7 +359,7 @@ class ValueEvaluatorTest {

@Test
fun testHandleDiv() {
with(TestHandler()) {
with(TestHandler(TestLanguageFrontend())) {
// For two integer values, we keep the result as a long.
val binOp = newBinaryOperator("/")
binOp.lhs = newLiteral(3, parseType("int"))
Expand Down Expand Up @@ -421,7 +422,7 @@ class ValueEvaluatorTest {

@Test
fun testHandleUnary() {
with(TestHandler()) {
with(TestHandler(TestLanguageFrontend())) {
val neg = newUnaryOperator("-", false, true)
neg.input = newLiteral(3, parseType("int"))
assertEquals(-3, ValueEvaluator().evaluate(neg))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static de.fraunhofer.aisec.cpg.graph.DeclarationBuilderKt.newTypedefDeclaration;

import de.fraunhofer.aisec.cpg.ScopeManager;
import de.fraunhofer.aisec.cpg.TranslationContext;
import de.fraunhofer.aisec.cpg.frontends.Language;
import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend;
import de.fraunhofer.aisec.cpg.frontends.cpp.CLanguage;
Expand Down Expand Up @@ -56,7 +57,7 @@ public class TypeManager {
// TODO: document/remove this regexp, merge with other pattern
private static final Pattern funPointerPattern =
Pattern.compile("\\(?\\*(?<alias>[^()]+)\\)?\\(.*\\)");
@NotNull private static TypeManager instance = new TypeManager();

private static boolean typeSystemActive = true;

@NotNull
Expand Down Expand Up @@ -90,10 +91,6 @@ public class TypeManager {
private final Set<Type> firstOrderTypes = Collections.synchronizedSet(new HashSet<>());
private final Set<Type> secondOrderTypes = Collections.synchronizedSet(new HashSet<>());

public static void reset() {
instance = new TypeManager();
}

/**
* @param recordDeclaration that is instantiated by a template containing parameterizedtypes
* @param name of the ParameterizedType we want to get
Expand Down Expand Up @@ -121,7 +118,6 @@ public ParameterizedType getTypeParameter(RecordDeclaration recordDeclaration, S
* @param typeParameters List containing all ParameterizedTypes used by the recordDeclaration and
* will be stored as value in the map
*/
@Deprecated
public void addTypeParameter(
RecordDeclaration recordDeclaration, List<ParameterizedType> typeParameters) {
this.recordToTypeParameters.put(recordDeclaration, typeParameters);
Expand All @@ -136,7 +132,6 @@ public void addTypeParameter(
* @return
*/
@Nullable
@Deprecated
public ParameterizedType getTypeParameter(TemplateDeclaration templateDeclaration, String name) {
if (this.templateToTypeParameters.containsKey(templateDeclaration)) {
for (ParameterizedType parameterizedType :
Expand Down Expand Up @@ -259,11 +254,7 @@ public boolean typeExists(String name) {
.anyMatch(type -> type.getRoot().getName().toString().equals(name));
}

private TypeManager() {}

public static @NotNull TypeManager getInstance() {
return instance;
}
public TypeManager() {}

public static boolean isTypeSystemActive() {
return typeSystemActive;
Expand Down Expand Up @@ -413,11 +404,13 @@ private Set<Type> unwrapTypes(Collection<Type> types, WrapState wrapState) {
* information and their record declarations. We want to get rid of that in the future.
*
* @param types the types to compare
* @param provider a {@link ScopeProvider}.
* @param ctx a {@link TranslationContext}.
* @return the common type
*/
@NotNull
public Optional<Type> getCommonType(@NotNull Collection<Type> types, ScopeProvider provider) {
public Optional<Type> getCommonType(@NotNull Collection<Type> types, TranslationContext ctx) {
var provider = ctx.getScopeManager();

// TODO: Documentation needed.
boolean sameType =
types.stream().map(t -> t.getClass().getCanonicalName()).collect(Collectors.toSet()).size()
Expand Down Expand Up @@ -513,7 +506,10 @@ public Optional<Type> getCommonType(@NotNull Collection<Type> types, ScopeProvid
Optional<Ancestor> lca =
commonAncestors.stream().max(Comparator.comparingInt(Ancestor::getDepth));
Optional<Type> commonType =
lca.map(a -> TypeParser.createFrom(a.getRecord().getName(), a.getRecord().getLanguage()));
lca.map(
a ->
TypeParser.createFrom(
a.getRecord().getName().toString(), a.getRecord().getLanguage(), false, ctx));

Type finalType;
if (commonType.isPresent()) {
Expand Down Expand Up @@ -549,6 +545,7 @@ private Set<Ancestor> getAncestors(RecordDeclaration recordDeclaration, int dept

public boolean isSupertypeOf(Type superType, Type subType, MetadataProvider provider) {
Language<?> language = null;
TranslationContext ctx;

if (superType instanceof UnknownType && subType instanceof UnknownType) return true;

Expand All @@ -560,6 +557,13 @@ public boolean isSupertypeOf(Type superType, Type subType, MetadataProvider prov
language = languageProvider.getLanguage();
}

if (provider instanceof ContextProvider contextProvider) {
ctx = contextProvider.getCtx();
} else {
log.error("Missing context provider");
return false;
}

// arrays and pointers match in C/C++
// TODO: Make this independent from the specific language
if (language instanceof CLanguage && checkArrayAndPointer(superType, subType)) {
Expand All @@ -576,8 +580,7 @@ public boolean isSupertypeOf(Type superType, Type subType, MetadataProvider prov
return false;
}

Optional<Type> commonType =
getCommonType(new HashSet<>(List.of(superType, subType)), scopeProvider);
Optional<Type> commonType = getCommonType(new HashSet<>(List.of(superType, subType)), ctx);
if (commonType.isPresent()) {
return commonType.get().equals(superType);
} else {
Expand Down Expand Up @@ -608,10 +611,11 @@ public void cleanup() {
this.typeToRecord.clear();
}

private Type getTargetType(Type currTarget, String alias) {
private Type getTargetType(Type currTarget, String alias, TranslationContext ctx) {
if (alias.contains("(") && alias.contains("*")) {
// function pointer
return TypeParser.createFrom(currTarget.getName() + " " + alias, currTarget.getLanguage());
return TypeParser.createFrom(
currTarget.getName() + " " + alias, currTarget.getLanguage(), false, ctx);
} else if (alias.endsWith("]")) {
// array type
return currTarget.reference(PointerType.PointerOrigin.ARRAY);
Expand All @@ -627,20 +631,23 @@ private Type getTargetType(Type currTarget, String alias) {
}
}

private Type getAlias(String alias, @NotNull Language<? extends LanguageFrontend> language) {
private Type getAlias(
String alias,
@NotNull Language<? extends LanguageFrontend> language,
TranslationContext ctx) {
if (alias.contains("(") && alias.contains("*")) {
// function pointer
Matcher matcher = funPointerPattern.matcher(alias);
if (matcher.find()) {
return TypeParser.createIgnoringAlias(matcher.group("alias"), language);
return TypeParser.createIgnoringAlias(matcher.group("alias"), language, ctx);
} else {
log.error("Could not find alias name in function pointer typedef: {}", alias);
return TypeParser.createIgnoringAlias(alias, language);
return TypeParser.createIgnoringAlias(alias, language, ctx);
}
} else {
alias = alias.split("\\[")[0];
alias = alias.replace("*", "");
return TypeParser.createIgnoringAlias(alias, language);
return TypeParser.createIgnoringAlias(alias, language, ctx);
}
}

Expand All @@ -658,9 +665,9 @@ private Type getAlias(String alias, @NotNull Language<? extends LanguageFrontend
public Declaration createTypeAlias(
@NotNull LanguageFrontend frontend, String rawCode, Type target, String aliasString) {
String cleanedPart = Util.removeRedundantParentheses(aliasString);
Type currTarget = getTargetType(target, cleanedPart);
Type currTarget = getTargetType(target, cleanedPart, frontend.getCtx());
Type alias;
alias = getAlias(cleanedPart, frontend.getLanguage());
alias = getAlias(cleanedPart, frontend.getLanguage(), frontend.getCtx());

if (alias instanceof SecondOrderType) {
Type chain = alias.duplicate();
Expand Down
Loading