From c399a9201727e17bb1bb37c08385d030a5645798 Mon Sep 17 00:00:00 2001 From: Chris Wiebe Date: Wed, 25 Aug 2004 21:29:45 +0000 Subject: [PATCH] 2004-08-25 Chris Wiebe add namespace validation to CConventions * index/org/eclipse/cdt/internal/core/messages.properties * src/org/eclipse/cdt/core/CConventions.java --- core/org.eclipse.cdt.core/ChangeLog | 6 ++ .../cdt/internal/core/messages.properties | 11 ++- .../org/eclipse/cdt/core/CConventions.java | 71 ++++++++++++++++++- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index 8e3b842c942..f050fc9d36c 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -1,3 +1,9 @@ +2004-08-25 Chris Wiebe + + add namespace validation to CConventions + * index/org/eclipse/cdt/internal/core/messages.properties + * src/org/eclipse/cdt/core/CConventions.java + 2004-08-25 Alain Magloire Fix for PR 72078 diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/messages.properties b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/messages.properties index 859a77ab614..a781efd3a06 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/messages.properties +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/messages.properties @@ -21,14 +21,23 @@ indexNotificationJob=Updating C/C++ Indexer clients convention.illegalIdentifier= Illegal identifier convention.invalid= Invalid identifier +convention.scope.leadingUnderscore= Scope starts with underscore convention.scope.lowercaseName= Scope starts with lower case convention.scope.nullName= Scope name is null convention.scope.emptyName= Scope name is empty convention.scope.dotName= Scope name starts or ends with a . -convention.scope.nameWithBlanks= Scop name has blanks +convention.scope.nameWithBlanks= Scope name has blanks convention.class.nullName= Class name is null convention.class.nameWithBlanks= Class name has blanks convention.class.dollarName= Class name has $ +convention.class.leadingUnderscore= Class name starts with underscore convention.class.lowercaseName= Class name starts with lower case convention.class.invalidName= Class name is invalid + +convention.namespace.nullName= Namespace is null +convention.namespace.nameWithBlanks= Namespace has blanks +convention.namespace.dollarName= Namespace has $ +convention.namespace.leadingUnderscore= Namespace starts with underscore +convention.namespace.lowercaseName= Namespace starts with lower case +convention.namespace.invalidName= Namespace is invalid 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 7cf4d8b8cfd..d798984c270 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 @@ -114,7 +114,10 @@ public class CConventions { if (CharOperation.contains('$', scannedID)) { return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.dollarName"), null); //$NON-NLS-1$ } - if ((scannedID.length > 0 && Character.isLowerCase(scannedID[0]))) { + if (scannedID.length > 0 && scannedID[0] == '_') { + return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.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.class.lowercaseName"), null); //$NON-NLS-1$ } return CModelStatus.VERIFIED_OK; @@ -122,6 +125,69 @@ public class CConventions { return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.invalidName", name), null); //$NON-NLS-1$ } } + + /** + * Validate the given CPP namespace name, either simple or qualified. For + * example, "A::B::C", or "C". + *

+ * + * @param name the name of a namespace + * @return a status object with code IStatus.OK if + * the given name is valid as a CPP class 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 validateNamespaceName(String name) { + if (name == null) { + return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.namespace.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.namespace.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.namespace.dollarName"), null); //$NON-NLS-1$ + } + if (scannedID.length > 0 && scannedID[0] == '_') { + return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.namespace.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.namespace.lowercaseName"), null); //$NON-NLS-1$ +// } + return CModelStatus.VERIFIED_OK; + } else { + return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.invalidName", name), null); //$NON-NLS-1$ + } + } + /** * Validate the given scope name. *

@@ -154,6 +220,9 @@ public class CConventions { if (scannedID == null) { return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.illegalIdentifier", typeName), null); //$NON-NLS-1$ } + if (firstToken && scannedID.length > 0 && scannedID[0] == '_') { + return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.scope.leadingUnderscore"), null); //$NON-NLS-1$ + } if (firstToken && scannedID.length > 0 && Character.isLowerCase(scannedID[0])) { return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.scope.lowercaseName"), null); //$NON-NLS-1$ }