Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 401ad50b6f7558265a3f7a656aeb3734e70daff9 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2010, 2013 Severin Gehwolf
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Severin Gehwolf  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers;

import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;

/**
 * Checker that finds assignment to itself cases, such a
 * a = a. It can produce some false positives such as
 * a[f()]=a[f()] - but who writes code like that?
 */
public class AssignmentToItselfChecker extends AbstractIndexAstChecker {
	private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.AssignmentToItselfProblem"; //$NON-NLS-1$

	@Override
	public void processAst(IASTTranslationUnit ast) {
		// Traverse the ast using the visitor pattern.
		ast.accept(new ASTVisitor() {
			{ // constructor
				shouldVisitExpressions = true;
			}

			// visit expressions
			@Override
			public int visit(IASTExpression expression) {
				if (isAssignmentToItself(expression)) {
					reportProblem(ER_ID, expression, expression.getRawSignature());
				}
				return PROCESS_CONTINUE;
			}

			private boolean isAssignmentToItself(IASTExpression expr) {
				if (expr instanceof IASTBinaryExpression) {
					IASTBinaryExpression binExpr = (IASTBinaryExpression) expr;
					if (binExpr.getOperator() == IASTBinaryExpression.op_assign) {
						IASTExpression operand1 = binExpr.getOperand1();
						IASTExpression operand2 = binExpr.getOperand2();
						if (operand1 != null && operand2 != null) {
							String op1 = operand1.getRawSignature();
							String op2 = operand2.getRawSignature();
							String exprImage = binExpr.getRawSignature();
							return op1.equals(op2)
									// When macro is used, RawSignature returns macro name, see bug 321933
									&& !op1.equals(exprImage);
						}
					}
				}
				return false;
			}
		});
	}
}

Back to the top