@Override public void visitStructNameType(StructNameType structNameType) { out.append(structNameType.getName()); }
@Override public void visitStructNameType(StructNameType structNameType) { structNameType.setName(namedStructRenaming.get(structNameType.getName())); } }.visit(tu);
private List<String> getStructNames(List<Declaration> toBeAdded) { return toBeAdded .stream() .map(StructUtils::getStructDefinitions) .reduce(new ArrayList<>(), ListConcat::concatenate) .stream() .filter(StructDefinitionType::hasStructNameType) .map(item -> item.getStructNameType().getName()) .collect(Collectors.toList()); }
public String getOuterStructName() { return outerStruct.getStructNameType().getName(); } }
private Set<String> getStructNamesFromShader(TranslationUnit tu) { return tu.getStructDefinitions() .stream() .filter(StructDefinitionType::hasStructNameType) .map(item -> item.getStructNameType()) .filter(item -> item instanceof StructNameType) .map(item -> ((StructNameType) item).getName()) .collect(Collectors.toSet()); }
@Override public void visitTypeConstructorExpr(TypeConstructorExpr typeConstructorExpr) { super.visitTypeConstructorExpr(typeConstructorExpr); if (typeConstructorExpr.getTypename().equals(toLookFor.getName())) { counter++; } }
@Override public void visitStructNameType(StructNameType structNameType) { super.visitStructNameType(structNameType); unusedStructs.remove(currentScope.lookupStructName(structNameType.getName())); }
public void addStructDefinition(StructDefinitionType sdt) { assert sdt.hasStructNameType(); structMapping.put(sdt.getStructNameType().getName(), sdt); }
@Override public void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr) { super.visitMemberLookupExpr(memberLookupExpr); Type type = typer.lookupType(memberLookupExpr.getStructure()); if (type == null) { return; } type = type.getWithoutQualifiers(); if (!(type instanceof StructNameType)) { return; } final StructNameType structType = (StructNameType) type; assert structFieldRenaming.containsKey(structType.getName()); final Map<String, String> fieldRenaming = structFieldRenaming.get(structType.getName()); assert fieldRenaming.containsKey(memberLookupExpr.getMember()); memberLookupExpr.setMember(fieldRenaming .get(memberLookupExpr.getMember())); }
@Override public void visitTypeConstructorExpr(TypeConstructorExpr typeConstructorExpr) { super.visitTypeConstructorExpr(typeConstructorExpr); if (!typeConstructorExpr.getTypename().equals( targetStruct.getStructNameType().getName())) { return; } // This is the target struct, so remove the appropriate constructor component typeConstructorExpr.removeArg(index); } }.visit(subtreeInWhichStructIsUsed);
@Override public void visitStructDefinitionType(StructDefinitionType structDefinitionType) { super.visitStructDefinitionType(structDefinitionType); // TODO: right now we do not obfuscate fields of anonymous structs. if (structDefinitionType.hasStructNameType()) { final String oldName = structDefinitionType.getStructNameType().getName(); final String newName = renameStruct(oldName); namedStructRenaming.put(oldName, newName); structDefinitionTypes.add(structDefinitionType); final Map<String, String> fieldRenaming = new HashMap<>(); for (String fieldName : structDefinitionType.getFieldNames()) { fieldRenaming.put(fieldName, renameStructField(fieldName)); } structFieldRenaming.put(structDefinitionType.getStructNameType(), fieldRenaming); } }
public void findInliningOpportunities(StructNameType structType) { assert structType.getName().startsWith(Constants.STRUCTIFICATION_STRUCT_PREFIX); final StructDefinitionType structDefinitionType = currentScope.lookupStructName(structType.getName()); for (String f : structDefinitionType.getFieldNames()) { if (!f.startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { continue; } if (structDefinitionType.getFieldType(f).getWithoutQualifiers() instanceof StructNameType) { final StructNameType innerStructType = (StructNameType) structDefinitionType.getFieldType(f).getWithoutQualifiers(); opportunities.add(new InlineStructifiedFieldReductionOpportunity( structDefinitionType, currentScope.lookupStructName(innerStructType.getName()), f, tu, getVistitationDepth())); findInliningOpportunities(innerStructType); } } }
private boolean reachesOriginalVariable(StructDefinitionType structDefinitionType, String field) { if (!(field.startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX))) { return true; } if (!(structDefinitionType.getFieldType(field).getWithoutQualifiers() instanceof StructNameType)) { return false; } final StructNameType fieldType = (StructNameType) structDefinitionType.getFieldType(field).getWithoutQualifiers(); final StructDefinitionType nestedStruct = currentScope.lookupStructName(fieldType.getName()); return nestedStruct.getFieldNames().stream() .anyMatch(item -> reachesOriginalVariable(nestedStruct, item)); }
private Expr makeInitializationExpr(StructDefinitionType structDefinitionType, Expr originalInitializer) { List<Expr> args = new ArrayList<>(); for (int i = 0; i < structDefinitionType.getNumFields(); i++) { if (structDefinitionType.getFieldName(i).startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { final Type fieldType = structDefinitionType.getFieldType(i); args.add(fieldType instanceof StructNameType ? makeInitializationExpr(tu.getStructDefinition((StructNameType) fieldType), originalInitializer) : fieldType.getCanonicalConstant()); } else { args.add(originalInitializer); } } return new TypeConstructorExpr(structDefinitionType.getStructNameType().getName(), args); }
final String structName = ((StructNameType) targetType).getName(); final StructDefinitionType sdt = fuzzingContext.getCurrentScope().lookupStructName(structName); throw new RuntimeException("Could not find a struct named " + structName + " in scope."); assert sdt.getStructNameType().getName().equals(structName); return new TypeConstructorExpr(structName, sdt.getFieldTypes() .stream().map(item -> makeExpr(item, false, constContext, depth + 1))
private void getOpportunitiesForStruct(StructNameType structType, VisitationDepth visitationDepth) { if (!(structType.getName().startsWith(Constants .STRUCTIFICATION_STRUCT_PREFIX))) { return; } final StructDefinitionType structDefinitionType = currentScope.lookupStructName(structType.getName()); for (String field : structDefinitionType.getFieldNames()) { if (!reachesOriginalVariable(structDefinitionType, field) && structDefinitionType.getNumFields() > 1) { final RemoveStructFieldReductionOpportunity op = new RemoveStructFieldReductionOpportunity( structDefinitionType, field, translationUnit, visitationDepth); if (op.preconditionHolds()) { opportunities.add(op); } } if (structDefinitionType.getFieldType(field).getWithoutQualifiers() instanceof StructNameType) { getOpportunitiesForStruct( (StructNameType) structDefinitionType.getFieldType(field).getWithoutQualifiers(), visitationDepth.deeper()); } } }