diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java index df78ee5bda9..a8d8460f51e 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java @@ -455,4 +455,65 @@ public class CConventions { return val; } + + /** + * Validate the given CPP enum name, either simple or qualified. For + * example, "A::B::C", or "C". + *

+ * + * @param name the name of a enum + * @return a status object with code IStatus.OK if + * the given name is valid as a CPP enum name, + * a status with code IStatus.WARNING + * indicating why the given name is discouraged, + * otherwise a status object indicating what is wrong with + * the name + */ + public static IStatus validateEnumName(String name) { + if (name == null) { + return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.enum.nullName"), null); //$NON-NLS-1$ + } + String trimmed = name.trim(); + if ((!name.equals(trimmed)) || (name.indexOf(" ") != -1) ){ //$NON-NLS-1$ + return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.enum.nameWithBlanks"), null); //$NON-NLS-1$ + } + int index = name.lastIndexOf(scopeResolutionOperator); + char[] scannedID; + if (index == -1) { + // simple name + IStatus status = validateIdentifier(name); + if (!status.isOK()){ + return status; + } + + scannedID = name.toCharArray(); + } else { + // qualified name + String pkg = name.substring(0, index).trim(); + IStatus status = validateScopeName(pkg); + if (!status.isOK()) { + return status; + } + String type = name.substring(index + scopeResolutionOperator.length()).trim(); + status = validateIdentifier(type); + if (!status.isOK()){ + return status; + } + scannedID = type.toCharArray(); + } + + if (scannedID != null) { + if (CharOperation.contains('$', scannedID)) { + return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.enum.dollarName"), null); //$NON-NLS-1$ + } + if (scannedID.length > 0 && scannedID[0] == '_') { + return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.enum.leadingUnderscore"), null); //$NON-NLS-1$ + } + if (scannedID.length > 0 && Character.isLowerCase(scannedID[0])) { + return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.enum.lowercaseName"), null); //$NON-NLS-1$ + } + return CModelStatus.VERIFIED_OK; + } + return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.enum.invalidName"), null); //$NON-NLS-1$ + } } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/internal/core/messages.properties b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/internal/core/messages.properties index 6fbef7672f2..8f4ed1eced0 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/internal/core/messages.properties +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/internal/core/messages.properties @@ -48,3 +48,10 @@ convention.filename.possiblyInvalid=File name contains non-standard or illegal c convention.filename.nameWithBlanks=File name contains spaces convention.headerFilename.filetype=File extension does not correspond to known header file types convention.sourceFilename.filetype=File extension does not correspond to known source file types + +convention.enum.nullName= Enum name is null +convention.enum.nameWithBlanks= Enum name has blanks +convention.enum.dollarName= Enum name has $ +convention.enum.leadingUnderscore= Enum name starts with underscore +convention.enum.lowercaseName= Enum name starts with lower case +convention.enum.invalidName= Enum name is invalid