Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 87adf7bf554f679a16bdd41177d6f3ac05b52808 (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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*******************************************************************************
 * Copyright (c) 2010, 2011 Marc-Andre Laperle and others.
 * 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:
 * Marc-Andre Laperle - Initial API and implementation
 * Tomasz Wesolowski  - Extension for fixes
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers;

import org.eclipse.cdt.codan.checkers.CodanCheckersActivator;
import org.eclipse.cdt.codan.core.cxx.CxxAstUtils;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.codan.core.model.CheckerLaunchMode;
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;

public class ProblemBindingChecker extends AbstractIndexAstChecker {
	public static String ERR_ID_OverloadProblem = "org.eclipse.cdt.codan.internal.checkers.OverloadProblem"; //$NON-NLS-1$
	public static String ERR_ID_AmbiguousProblem = "org.eclipse.cdt.codan.internal.checkers.AmbiguousProblem"; //$NON-NLS-1$
	public static String ERR_ID_CircularReferenceProblem = "org.eclipse.cdt.codan.internal.checkers.CircularReferenceProblem"; //$NON-NLS-1$
	public static String ERR_ID_RedeclarationProblem = "org.eclipse.cdt.codan.internal.checkers.RedeclarationProblem"; //$NON-NLS-1$
	public static String ERR_ID_RedefinitionProblem = "org.eclipse.cdt.codan.internal.checkers.RedefinitionProblem"; //$NON-NLS-1$
	public static String ERR_ID_MemberDeclarationNotFoundProblem = "org.eclipse.cdt.codan.internal.checkers.MemberDeclarationNotFoundProblem"; //$NON-NLS-1$
	public static String ERR_ID_LabelStatementNotFoundProblem = "org.eclipse.cdt.codan.internal.checkers.LabelStatementNotFoundProblem"; //$NON-NLS-1$
	public static String ERR_ID_InvalidTemplateArgumentsProblem = "org.eclipse.cdt.codan.internal.checkers.InvalidTemplateArgumentsProblem"; //$NON-NLS-1$
	public static String ERR_ID_TypeResolutionProblem = "org.eclipse.cdt.codan.internal.checkers.TypeResolutionProblem"; //$NON-NLS-1$
	public static String ERR_ID_FunctionResolutionProblem = "org.eclipse.cdt.codan.internal.checkers.FunctionResolutionProblem"; //$NON-NLS-1$
	public static String ERR_ID_InvalidArguments = "org.eclipse.cdt.codan.internal.checkers.InvalidArguments"; //$NON-NLS-1$
	public static String ERR_ID_MethodResolutionProblem = "org.eclipse.cdt.codan.internal.checkers.MethodResolutionProblem"; //$NON-NLS-1$
	public static String ERR_ID_FieldResolutionProblem = "org.eclipse.cdt.codan.internal.checkers.FieldResolutionProblem"; //$NON-NLS-1$
	public static String ERR_ID_VariableResolutionProblem = "org.eclipse.cdt.codan.internal.checkers.VariableResolutionProblem"; //$NON-NLS-1$
	public static String ERR_ID_Candidates = "org.eclipse.cdt.codan.internal.checkers.Candidates"; //$NON-NLS-1$

	@Override
	public boolean runInEditor() {
		return true;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences#initPreferences(org.eclipse.cdt.codan.core.model.IProblemWorkingCopy)
	 */
	@Override
	public void initPreferences(IProblemWorkingCopy problem) {
		super.initPreferences(problem);
		// these checkers should not run on full or incremental build
		getLaunchModePreference(problem).enableInLaunchModes(CheckerLaunchMode.RUN_AS_YOU_TYPE, CheckerLaunchMode.RUN_ON_DEMAND);
	}

	public void processAst(IASTTranslationUnit ast) {
		try {
			ast.accept(new ASTVisitor() {
				{
					shouldVisitNames = true;
				}

				@Override
				public int visit(IASTName name) {
					try {
						IBinding binding = name.resolveBinding();
						if (binding instanceof IProblemBinding) {
							IASTNode parentNode = name.getParent();
							// Don't report multiple problems with qualified names
							if (parentNode instanceof ICPPASTQualifiedName) {
								if (((ICPPASTQualifiedName) parentNode).resolveBinding() instanceof IProblemBinding)
									return PROCESS_CONTINUE;
							}
							String contextFlagsString = createContextFlagsString(name);
							IProblemBinding problemBinding = (IProblemBinding) binding;
							int id = problemBinding.getID();
							if (id == IProblemBinding.SEMANTIC_INVALID_OVERLOAD) {
								reportProblem(ERR_ID_OverloadProblem, name, name.getRawSignature(), contextFlagsString);
								return PROCESS_CONTINUE;
							}
							if (id == IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP) {
								String candidatesString = getCandidatesString(problemBinding);
								reportProblem(ERR_ID_AmbiguousProblem, name, name.getRawSignature(), candidatesString, contextFlagsString);
								return PROCESS_CONTINUE;
							}
							if (id == IProblemBinding.SEMANTIC_CIRCULAR_INHERITANCE) {
								String typeString;
								IASTNode problemNode;
								if (parentNode instanceof IASTFieldReference) {
									IASTExpression ownerExpression = ((IASTFieldReference) parentNode).getFieldOwner();
									typeString = ASTTypeUtil.getType(ownerExpression.getExpressionType());
									problemNode = ownerExpression;
								} else {
									problemNode = name;
									typeString = name.getRawSignature();
								}
								reportProblem(ERR_ID_CircularReferenceProblem, problemNode, typeString, contextFlagsString);
								return PROCESS_CONTINUE;
							}
							if (id == IProblemBinding.SEMANTIC_INVALID_REDECLARATION) {
								reportProblem(ERR_ID_RedeclarationProblem, name, name.getRawSignature(), contextFlagsString);
								return PROCESS_CONTINUE;
							}
							if (id == IProblemBinding.SEMANTIC_INVALID_REDEFINITION) {
								reportProblem(ERR_ID_RedefinitionProblem, name, name.getRawSignature(), contextFlagsString);
								return PROCESS_CONTINUE;
							}
							if (id == IProblemBinding.SEMANTIC_MEMBER_DECLARATION_NOT_FOUND) {
								reportProblem(ERR_ID_MemberDeclarationNotFoundProblem, name, contextFlagsString);
								return PROCESS_CONTINUE;
							}
							if (id == IProblemBinding.SEMANTIC_LABEL_STATEMENT_NOT_FOUND) {
								reportProblem(ERR_ID_LabelStatementNotFoundProblem, name, name.getRawSignature(), contextFlagsString);
								return PROCESS_CONTINUE;
							}
							if (id == IProblemBinding.SEMANTIC_INVALID_TEMPLATE_ARGUMENTS) {
								// We use the templateName since we don't want the whole
								// argument list to be underlined. That way we can see which argument is invalid.
								IASTNode templateName = getTemplateName(name);
								reportProblem(ERR_ID_InvalidTemplateArgumentsProblem, templateName, contextFlagsString);
								return PROCESS_CONTINUE;
							}
							// From this point, we'll deal only with NAME_NOT_FOUND problems. 
							// If it's something else continue because we don't want to give bad messages
							if (id != IProblemBinding.SEMANTIC_NAME_NOT_FOUND) {
								return PROCESS_CONTINUE;
							}
							if (isFunctionCall(parentNode)) {
								handleFunctionProblem(name, problemBinding, contextFlagsString);
							} else if (parentNode instanceof IASTFieldReference) {
								handleMemberProblem(name, parentNode, problemBinding, contextFlagsString);
							} else if (parentNode instanceof IASTNamedTypeSpecifier) {
								reportProblem(ERR_ID_TypeResolutionProblem, name, name.getRawSignature(), contextFlagsString);
							}
							// Probably a variable
							else {
								handleVariableProblem(name, contextFlagsString);
							}
						}
					} catch (DOMException e) {
						CodanCheckersActivator.log(e);
					}
					return PROCESS_CONTINUE;
				}
			});
		} catch (Exception e) {
			CodanCheckersActivator.log(e);
		}
	}

	protected String createContextFlagsString(IASTName name) {
		StringBuilder buf = new StringBuilder();
		if (isInClassContext(name)) {
			buf.append(":class"); //$NON-NLS-1$
		}
		if (isInFunctionContext(name)) {
			buf.append(":func"); //$NON-NLS-1$
		}
		return buf.toString();
	}

	private boolean isInClassContext(IASTName name) {
		CxxAstUtils utils = CxxAstUtils.getInstance();
		if (utils.getEnclosingCompositeTypeSpecifier(name) != null) {
			return true;
		}
		IASTFunctionDefinition function = utils.getEnclosingFunction(name);
		if (function == null) {
			return false;
		}
		@SuppressWarnings("restriction")
		IASTDeclarator innermostDeclarator = ASTQueries.findInnermostDeclarator(function.getDeclarator());
		IBinding binding = innermostDeclarator.getName().resolveBinding();
		return (binding instanceof ICPPMethod);
	}

	private boolean isInFunctionContext(IASTName name) {
		CxxAstUtils utils = CxxAstUtils.getInstance();
		IASTFunctionDefinition function = utils.getEnclosingFunction(name);
		return (function != null);
	}

	private void handleFunctionProblem(IASTName name, IProblemBinding problemBinding, String contextFlagsString) throws DOMException {
		if (problemBinding.getCandidateBindings().length == 0) {
			reportProblem(ERR_ID_FunctionResolutionProblem, name.getLastName(), new String(name.getSimpleID()), contextFlagsString);
		} else {
			String candidatesString = getCandidatesString(problemBinding);
			reportProblem(ERR_ID_InvalidArguments, name.getLastName(), candidatesString, contextFlagsString);
		}
	}

	private void handleMemberProblem(IASTName name, IASTNode parentNode, IProblemBinding problemBinding, String contextFlagsString)
			throws DOMException {
		IASTNode parentParentNode = parentNode.getParent();
		
		// An IASTFieldReference corresponds to a method if it's the first child in the parent function call expression
		// For example, 
		//   func(x.y()); the field reference is first in the x.y() function call expression -> y is a method
		//   func(x.y); the field reference is second in the func(x.y) function call expression, func is first as a Id Expression -> y is a field
		boolean isMethod = parentParentNode instanceof IASTFunctionCallExpression && parentParentNode.getChildren()[0] == parentNode;
		if (isMethod) {
			if (problemBinding.getCandidateBindings().length == 0) {
				reportProblem(ERR_ID_MethodResolutionProblem, name.getLastName(), new String(name.getSimpleID()), contextFlagsString);
			} else {
				String candidatesString = getCandidatesString(problemBinding);
				reportProblem(ERR_ID_InvalidArguments, name.getLastName(), candidatesString, contextFlagsString);
			}
		} else {
			reportProblem(ERR_ID_FieldResolutionProblem, name.getLastName(), name.getRawSignature(), contextFlagsString);
		}
	}

	private void handleVariableProblem(IASTName name, String contextFlagsString) {
		reportProblem(ERR_ID_VariableResolutionProblem, name, name.getBinding().getName(), contextFlagsString, name.getRawSignature());
	}

	private boolean isFunctionCall(IASTNode parentNode) {
		if (parentNode instanceof IASTIdExpression) {
			IASTIdExpression expression = (IASTIdExpression) parentNode;
			IASTNode parentParentNode = expression.getParent();
			if (parentParentNode instanceof IASTFunctionCallExpression
					&& expression.getPropertyInParent().getName().equals(IASTFunctionCallExpression.FUNCTION_NAME.getName())) {
				return true;
			}
		}
		return false;
	}

	protected IASTNode getTemplateName(IASTName name) {
		IASTName nameToGetTempate = name.getLastName();
		if (nameToGetTempate instanceof ICPPASTTemplateId) {
			return ((ICPPASTTemplateId) nameToGetTempate).getTemplateName();
		}
		return nameToGetTempate;
	}

	/**
	 * Returns a string of the candidates for the binding
	 * 
	 * @param problemBinding
	 * @return A string of the candidates, one per line
	 * @throws DOMException
	 */
	private String getCandidatesString(IProblemBinding problemBinding) throws DOMException {
		String candidatesString = "\n" + CheckersMessages.ProblemBindingChecker_Candidates + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
		String lastSignature = ""; //$NON-NLS-1$
		for (IBinding candidateBinding : problemBinding.getCandidateBindings()) {
			if (candidateBinding instanceof ICPPFunction) {
				ICPPFunction functionBinding = (ICPPFunction) candidateBinding;
				String signature = getFunctionSignature(functionBinding);
				if (!signature.equals(lastSignature)) {
					candidatesString += signature + "\n"; //$NON-NLS-1$
					lastSignature = signature;
				}
			} else if (candidateBinding instanceof ICPPClassType) {
				ICPPClassType classType = (ICPPClassType) candidateBinding;
				for (ICPPFunction constructor : classType.getConstructors()) {
					String signature = getFunctionSignature(constructor);
					if (!signature.equals(lastSignature)) {
						candidatesString += signature + "\n"; //$NON-NLS-1$
						lastSignature = signature;
					}
				}
			}
		}
		return candidatesString;
	}

	/**
	 * Returns a string of the function signature : returntype + function +
	 * parameters
	 * 
	 * @param functionBinding The function to get the signature
	 * @return A string of the function signature
	 * @throws DOMException
	 */
	private String getFunctionSignature(ICPPFunction functionBinding) throws DOMException {
		IFunctionType functionType = functionBinding.getType();
		String returnTypeString = ASTTypeUtil.getType(functionBinding.getType().getReturnType()) + " "; //$NON-NLS-1$
		String functionName = functionBinding.getName();
		String parameterTypeString = ASTTypeUtil.getParameterTypeString(functionType);
		return returnTypeString + functionName + parameterTypeString;
	}
}

Back to the top