Search in sources :

Example 1 with ASTNode

use of org.eclipse.cdt.internal.core.dom.parser.ASTNode in project cpg by Fraunhofer-AISEC.

the class Handler method handle.

/**
 * Searches for a handler matching the most specific superclass of {@link T}. The created map
 * should thus contain a handler for every semantically different AST node and can reuse handler
 * code as long as the handled AST nodes have a common ancestor.
 *
 * @param ctx The AST node, whose handler is matched with respect to the AST node class.
 * @return most specific handler.
 */
public S handle(T ctx) {
    S ret;
    if (ctx == null) {
        log.error("ctx is NULL. This can happen when ast children are optional in {}. Called by {}", this.getClass(), Thread.currentThread().getStackTrace()[2]);
        return null;
    }
    // If we do not want to load includes into the CPG and the current fileLocation was included
    if (!this.lang.config.loadIncludes && ctx instanceof ASTNode) {
        ASTNode astNode = (ASTNode) ctx;
        if (astNode.getFileLocation() != null && astNode.getFileLocation().getContextInclusionStatement() != null) {
            log.debug("Skip parsing include file" + astNode.getContainingFilename());
            return null;
        }
    }
    Class<?> toHandle = ctx.getClass();
    HandlerInterface<S, T> handler = map.get(toHandle);
    while (handler == null) {
        toHandle = toHandle.getSuperclass();
        handler = map.get(toHandle);
        if (handler != null && // always ok to handle as generic literal expr
        !ctx.getClass().getSimpleName().contains("LiteralExpr")) {
            errorWithFileLocation(lang, ctx, log, "No handler for type {}, resolving for its superclass {}.", ctx.getClass(), toHandle);
        }
        if (toHandle == typeOfT || (typeOfT != null && !typeOfT.isAssignableFrom(toHandle))) {
            break;
        }
    }
    if (handler != null) {
        S s = handler.handle(ctx);
        if (s != null) {
            // set the location here.
            if (((Node) s).getLocation() == null) {
                lang.setCodeAndRegion(s, ctx);
            }
            lang.setComment(s, ctx);
        }
        ret = s;
    } else {
        errorWithFileLocation(lang, ctx, log, "Parsing of type {} is not supported (yet)", ctx.getClass());
        ret = this.configConstructor.get();
        if (ret instanceof ProblemNode) {
            ProblemNode problem = (ProblemNode) ret;
            problem.setProblem(String.format("Parsing of type {} is not supported (yet)", ctx.getClass()));
        }
    }
    lang.process(ctx, ret);
    return ret;
}
Also used : ASTNode(org.eclipse.cdt.internal.core.dom.parser.ASTNode) ProblemNode(de.fraunhofer.aisec.cpg.graph.ProblemNode)

Aggregations

ProblemNode (de.fraunhofer.aisec.cpg.graph.ProblemNode)1 ASTNode (org.eclipse.cdt.internal.core.dom.parser.ASTNode)1