From 78e45126142ab1d2f114a86b4e57e34400dc3405 Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Thu, 18 Mar 2010 15:03:30 +0000 Subject: [PATCH] - added real NamingConvention checker --- .../org.eclipse.cdt.codan.checkers/plugin.xml | 14 +++ .../NamingConventionFunctionChecker.java | 101 ++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NamingConventionFunctionChecker.java diff --git a/codan/org.eclipse.cdt.codan.checkers/plugin.xml b/codan/org.eclipse.cdt.codan.checkers/plugin.xml index 475282f23c5..f2a7a7f1129 100644 --- a/codan/org.eclipse.cdt.codan.checkers/plugin.xml +++ b/codan/org.eclipse.cdt.codan.checkers/plugin.xml @@ -62,6 +62,20 @@ id="org.eclipse.cdt.codan.internal.checkers.SuggestedParenthesisProblem" name="Suggested parenthesis around expression"> + + + + + diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NamingConventionFunctionChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NamingConventionFunctionChecker.java new file mode 100644 index 00000000000..8dcb67db460 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NamingConventionFunctionChecker.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * Copyright (c) 2009 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 + *******************************************************************************/ +package org.eclipse.cdt.codan.internal.checkers; + +import java.util.regex.Pattern; + +import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker; +import org.eclipse.cdt.codan.core.model.ICheckerWithParameters; +import org.eclipse.cdt.codan.core.model.IProblem; +import org.eclipse.cdt.codan.core.model.IProblemParameterInfo; +import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; +import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; + +/** + * @author Alena + * + */ +public class NamingConventionFunctionChecker extends AbstractIndexAstChecker + implements ICheckerWithParameters { + private static final String DEFAULT_PATTERN = "^[a-z]"; // name starts with english lowercase letter //$NON-NLS-1$ + public static final String PARAM_KEY = "pattern"; //$NON-NLS-1$ + private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.NamingConventionFunctionChecker"; //$NON-NLS-1$ + + public void processAst(IASTTranslationUnit ast) { + final IProblem pt = getProblemById(ER_ID, getFile()); + try { + ast.accept(new ASTVisitor() { + { + shouldVisitDeclarations = true; + } + + public int visit(IASTDeclaration element) { + if (element instanceof IASTFunctionDefinition) { + String parameter = (String) pt.getParameter(PARAM_KEY); + Pattern pattern = Pattern.compile(parameter); + String name = ((IASTFunctionDefinition) element) + .getDeclarator().getName().toString(); + if (!pattern.matcher(name).find()) { + reportProblem(ER_ID, getFile(), 1, // TODO: line + // number + name, parameter); + } + } + return PROCESS_SKIP; + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.cdt.codan.core.model.ICheckerWithParameters#initParameters + * (org.eclipse.cdt.codan.core.model.IProblemWorkingCopy) + */ + public void initParameters(IProblemWorkingCopy problem) { + IProblemParameterInfo info = new IProblemParameterInfo() { + public String getUiInfo() { + return null; + } + + public String getType() { + return IProblemParameterInfo.TYPE_STRING; + } + + public String getLabel() { + return "Name Pattern"; + } + + public String getKey() { + return PARAM_KEY; + } + + public IProblemParameterInfo getElement(String key) { + return null; + } + }; + problem.setParameterInfo(info); + problem.setParameter(PARAM_KEY, DEFAULT_PATTERN); + } + + @Override + public boolean runInEditor() { + return true; + } + +}