summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/VariableAssignmentVisitor.java
blob: 25302102646462aed67b158ed304c6d1411b2eae (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
91
92
93
94
95
96
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.IASTLiteralExpression;
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;

/*******************************************************************************
 * 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 ExecutionState es;

	public VariableAssignmentVisitor(ExecutionState es) {
		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 = getTruthValue(valueExpr);
			if (truth != null) {
				es.addTruthAssignment(var, truth);
			}
		}
	}

	private Boolean getTruthValue(IASTExpression valueExpr) {
		Boolean result = null;
		// Handle assignment from literals
		if (valueExpr instanceof IASTLiteralExpression) {
			int kind = ((IASTLiteralExpression) valueExpr).getKind();
			String value = String.valueOf(((IASTLiteralExpression) valueExpr).getValue());
			switch (kind) {
			case IASTLiteralExpression.lk_integer_constant:
				// 0 = False, > 0 = True
				result = !value.equals("0");
			}
		}
		// TODO other variable assignments
		return result;
	}

}