summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/VariableAssignmentVisitor.java
blob: 0f6a2ad3a9eaa6e21cdd4caa9feaf4dd23bf8697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*******************************************************************************
 * Copyright (c) 2009 Elliott Baron
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Elliott Baron - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.extension;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IBlock;

/*******************************************************************************
 * Copyright (c) 2009 Elliott Baron
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Elliott Baron - initial API and implementation
 *******************************************************************************/
public class VariableAssignmentVisitor extends ASTVisitor {
	private IBlock blk;
	private ExecutionState es;

	public VariableAssignmentVisitor(IBlock blk, ExecutionState es) {
		this.blk = blk;
		this.es = es;
		shouldVisitInitializers = true;
		shouldVisitExpressions = true;		
	}
		
	@Override
	public int visit(IASTInitializer initializer) {
		IASTNode parent = initializer.getParent();
		if (parent instanceof IASTDeclarator) {
			if (initializer instanceof IASTInitializerExpression) {
				IASTDeclarator decl = (IASTDeclarator) parent;
				IASTExpression expr = ((IASTInitializerExpression) initializer).getExpression();
				parseAssignment(parent, decl.getName(), expr);
			}
			return PROCESS_SKIP;
		}
		return PROCESS_CONTINUE;
	}
	
	@Override
	public int visit(IASTExpression expression) {
		if (expression instanceof IASTBinaryExpression) {
			IASTBinaryExpression binExpr = (IASTBinaryExpression) expression;
			int op = binExpr.getOperator();

			// Check operator is an assignment operator
			if (op == IASTBinaryExpression.op_assign) { // FIXME other ops
				IASTExpression o1 = binExpr.getOperand1();
				if (o1 instanceof IASTIdExpression) {
					IASTName name = ((IASTIdExpression) o1).getName();
					parseAssignment(binExpr, name, binExpr.getOperand2());
				}
				return PROCESS_SKIP;
			}
		}		
		return PROCESS_CONTINUE;
	}

	private void parseAssignment(IASTNode parent, IASTName name, IASTExpression valueExpr) {
		IBinding binding = name.resolveBinding();
		if (binding instanceof IVariable) {
			IVariable var = (IVariable) binding;
			Boolean truth = ASTParserUtil.getTruthValue(valueExpr);
			if (truth != null) {
				es.addTruthAssignment(var, truth, blk);
			}
		}
	}
}