Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab023eeb55d793af0e74e6640353caa0bbc71c00 (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
/*******************************************************************************
 * Copyright (c) 2009, 2011 Alena Laskavaia 
 * 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:
 *     Alena Laskavaia  - initial API and implementation
 *     Patrick Hofer [bug 315528]
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers;

import org.eclipse.cdt.codan.checkers.CodanCheckersActivator;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTName;
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.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;

/**
 * Checker to find that class has virtual method and non virtual destructor
 * 
 * @author Alena Laskavaia
 */
public class NonVirtualDestructor extends AbstractIndexAstChecker {
	private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.NonVirtualDestructorProblem"; //$NON-NLS-1$

	public void processAst(IASTTranslationUnit ast) {
		// traverse the ast using the visitor pattern.
		ast.accept(new OnEachClass());
	}

	class OnEachClass extends ASTVisitor {
		private IASTName className;
		private IBinding virMethodName;
		private IBinding destructorName;

		OnEachClass() {
			// shouldVisitDeclarations = true;
			shouldVisitDeclSpecifiers = true;
		}

		public int visit(IASTDeclSpecifier decl) {
			if (isClassDecl(decl)) {
				try {
					boolean err = hasErrorCondition(decl);
					if (err) {
						String clazz = className.toString();
						String method = virMethodName.getName();
						IASTNode ast = decl;
						if (destructorName != null) {
							if (destructorName instanceof ICPPInternalBinding) {
								ICPPInternalBinding bin = (ICPPInternalBinding) destructorName;
								IASTNode[] decls = bin.getDeclarations();
								if (decls != null && decls.length > 0)
									ast = decls[0];
							}
							reportProblem(ER_ID, ast, clazz, method, destructorName.getName());
						}
					}
				} catch (DOMException e) {
					// ignore, no error
				} catch (Exception e) {
					CodanCheckersActivator.log(e);
				}
				return PROCESS_SKIP;
			}
			return PROCESS_CONTINUE;
		}

		/**
		 * @param decl
		 * @throws DOMException
		 */
		private boolean hasErrorCondition(IASTDeclSpecifier decl) throws DOMException {
			ICPPASTCompositeTypeSpecifier spec = (ICPPASTCompositeTypeSpecifier) decl;
			className = spec.getName();
			IBinding binding = className.getBinding();
			if (binding == null) {
				binding = className.resolveBinding();
			}
			if (binding instanceof ICPPClassType) {
				ICPPClassType type = (ICPPClassType) binding;
				virMethodName = null;
				destructorName = null;
				// check for the following conditions:
				// class has own virtual method and own non-virtual destructor
				// class has own virtual method and base non-virtual destructor
				// class has base virtual method and own non-virtual destructor
				ICPPMethod[] declaredMethods = type.getDeclaredMethods();
				boolean hasOwnVirtualMethod = false;
				boolean hasOwnNonVirDestructor = false;
				boolean hasDestructor = false;
				boolean hasVirtualMethod = false;
				for (int i = 0; i < declaredMethods.length; i++) {
					ICPPMethod icppMethod = declaredMethods[i];
					if (icppMethod.isVirtual() && !icppMethod.isDestructor()) {
						hasOwnVirtualMethod = true;
						virMethodName = icppMethod;
					}
					if (icppMethod.isDestructor()) {
						hasDestructor = true;
						if (!icppMethod.isVirtual()) {
							hasOwnNonVirDestructor = true;
							destructorName = icppMethod;
						}
					}
				}
				boolean hasVirDestructor = false;
				// Class has own virtual method and own non-virtual destructor.
				if (hasOwnVirtualMethod && hasOwnNonVirDestructor) {
					// Check if one of its base classes has a virtual destructor.
					return !hasVirtualDtorInBaseClass(type);
				}
				// Class does not have virtual methods but has virtual destructor
				// - not an error
				if (!hasOwnVirtualMethod && hasDestructor && !hasOwnNonVirDestructor) {
					return false;
				}
				ICPPMethod[] allDeclaredMethods = type.getAllDeclaredMethods();
				for (int i = 0; i < allDeclaredMethods.length; i++) {
					ICPPMethod icppMethod = allDeclaredMethods[i];
					if (icppMethod.isVirtual() && !icppMethod.isDestructor()) {
						hasVirtualMethod = true;
						if (virMethodName == null)
							virMethodName = icppMethod;
					}
					if (icppMethod.isDestructor()) {
						hasDestructor = true;
						if (icppMethod.isVirtual()) {
							hasVirDestructor = true;
						} else {
							if (destructorName == null)
								destructorName = icppMethod;
						}
					}
				}
				if (hasOwnVirtualMethod) {
					// Class has own virtual method and base non-virtual destructor.
					if (hasDestructor && !hasVirDestructor) {
						return true;
					}
				} else if (hasVirtualMethod) {
					// Class has base virtual method and own non-virtual destructor.
					if (hasOwnNonVirDestructor) {
						return true;
					}
				}
			}
			return false;
		}

		private boolean hasVirtualDtorInBaseClass(ICPPClassType classType) {
			ICPPBase[] bases = classType.getBases();   
			for (ICPPBase base : bases) {
				if (!(base.getBaseClass() instanceof ICPPClassType)) {
					continue;
				}
				ICPPClassType testedBaseClass = (ICPPClassType) base.getBaseClass();
				ICPPMethod[] declaredBaseMethods = testedBaseClass.getDeclaredMethods();
				for (ICPPMethod method : declaredBaseMethods) {
					if (method.isDestructor() && method.isVirtual()) {
						return true;
					}
				}
				if (hasVirtualDtorInBaseClass(testedBaseClass))
					return true;
			}
			return false;
		}

		/**
		 * @param decl
		 * @return
		 */
		private boolean isClassDecl(IASTDeclSpecifier decl) {
			if (decl instanceof ICPPASTCompositeTypeSpecifier) {
				return true;
			}
			return false;
		}
	}
}

Back to the top