1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fixed misleading names and made few other readability improvements.

This commit is contained in:
Sergey Prigogin 2011-05-20 18:18:41 +00:00
parent 367325b8b3
commit be065cee02

View file

@ -36,14 +36,14 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.NonVirtualDestructorProblem"; //$NON-NLS-1$ private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.NonVirtualDestructorProblem"; //$NON-NLS-1$
public void processAst(IASTTranslationUnit ast) { public void processAst(IASTTranslationUnit ast) {
// traverse the ast using the visitor pattern. // Traverse the ast using the visitor pattern.
ast.accept(new OnEachClass()); ast.accept(new OnEachClass());
} }
class OnEachClass extends ASTVisitor { class OnEachClass extends ASTVisitor {
private IASTName className; private IASTName className;
private IBinding virMethodName; private IBinding virtualMethod;
private IBinding destructorName; private IBinding destructor;
OnEachClass() { OnEachClass() {
// shouldVisitDeclarations = true; // shouldVisitDeclarations = true;
@ -56,20 +56,19 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
boolean err = hasErrorCondition(decl); boolean err = hasErrorCondition(decl);
if (err) { if (err) {
String clazz = className.toString(); String clazz = className.toString();
String method = virMethodName.getName(); String method = virtualMethod.getName();
IASTNode ast = decl; IASTNode ast = decl;
if (destructorName != null) { if (destructor != null) {
if (destructorName instanceof ICPPInternalBinding) { if (destructor instanceof ICPPInternalBinding) {
ICPPInternalBinding bin = (ICPPInternalBinding) destructorName; IASTNode[] decls = ((ICPPInternalBinding) destructor).getDeclarations();
IASTNode[] decls = bin.getDeclarations();
if (decls != null && decls.length > 0) if (decls != null && decls.length > 0)
ast = decls[0]; ast = decls[0];
} }
reportProblem(ER_ID, ast, clazz, method, destructorName.getName()); reportProblem(ER_ID, ast, clazz, method, destructor.getName());
} }
} }
} catch (DOMException e) { } catch (DOMException e) {
// ignore, no error // Ignore, not an error.
} catch (Exception e) { } catch (Exception e) {
CodanCheckersActivator.log(e); CodanCheckersActivator.log(e);
} }
@ -91,8 +90,8 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
} }
if (binding instanceof ICPPClassType) { if (binding instanceof ICPPClassType) {
ICPPClassType classType = (ICPPClassType) binding; ICPPClassType classType = (ICPPClassType) binding;
virMethodName = null; virtualMethod = null;
destructorName = null; destructor = null;
// check for the following conditions: // check for the following conditions:
// class has own virtual method and own non-virtual destructor // class has own virtual method and own non-virtual destructor
// class has own virtual method and base non-virtual destructor // class has own virtual method and base non-virtual destructor
@ -102,26 +101,25 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
boolean hasOwnNonVirDestructor = false; boolean hasOwnNonVirDestructor = false;
boolean hasDestructor = false; boolean hasDestructor = false;
boolean hasVirtualMethod = false; boolean hasVirtualMethod = false;
for (int i = 0; i < declaredMethods.length; i++) { for (ICPPMethod method : declaredMethods) {
ICPPMethod icppMethod = declaredMethods[i]; if (method.isVirtual() && !method.isDestructor()) {
if (icppMethod.isVirtual() && !icppMethod.isDestructor()) {
hasOwnVirtualMethod = true; hasOwnVirtualMethod = true;
virMethodName = icppMethod; virtualMethod = method;
} }
if (icppMethod.isDestructor()) { if (method.isDestructor()) {
hasDestructor = true; hasDestructor = true;
if (!icppMethod.isVirtual()) { if (!method.isVirtual()) {
hasOwnNonVirDestructor = true; hasOwnNonVirDestructor = true;
destructorName = icppMethod; destructor = method;
} }
} }
} }
boolean hasVirtualDestructor = false; boolean hasVirtualDestructor = false;
// Class has own virtual method and own non-virtual destructor. // Class has own virtual method and own non-virtual destructor.
if (hasOwnVirtualMethod && hasOwnNonVirDestructor) { if (hasOwnVirtualMethod && hasOwnNonVirDestructor) {
if (destructorName instanceof ICPPMethod) { if (destructor instanceof ICPPMethod) {
// Check if dtor is public or is accessible by friends. // Check if dtor is public or is accessible by friends.
if (((ICPPMethod) destructorName).getVisibility() != ICPPASTVisibilityLabel.v_public && if (((ICPPMethod) destructor).getVisibility() != ICPPASTVisibilityLabel.v_public &&
classType.getFriends().length == 0) { classType.getFriends().length == 0) {
return false; return false;
} }
@ -135,20 +133,19 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
return false; return false;
} }
ICPPMethod[] allDeclaredMethods = classType.getAllDeclaredMethods(); ICPPMethod[] allDeclaredMethods = classType.getAllDeclaredMethods();
for (int i = 0; i < allDeclaredMethods.length; i++) { for (ICPPMethod method : allDeclaredMethods) {
ICPPMethod icppMethod = allDeclaredMethods[i]; if (method.isVirtual() && !method.isDestructor()) {
if (icppMethod.isVirtual() && !icppMethod.isDestructor()) {
hasVirtualMethod = true; hasVirtualMethod = true;
if (virMethodName == null) if (virtualMethod == null)
virMethodName = icppMethod; virtualMethod = method;
} }
if (icppMethod.isDestructor()) { if (method.isDestructor()) {
hasDestructor = true; hasDestructor = true;
if (icppMethod.isVirtual()) { if (method.isVirtual()) {
hasVirtualDestructor = true; hasVirtualDestructor = true;
} else { } else {
if (destructorName == null) if (destructor == null)
destructorName = icppMethod; destructor = method;
} }
} }
} }