diff --git a/components/lazutils/lazstringutils.pas b/components/lazutils/lazstringutils.pas index 96de78eee3..63fbbcd34d 100644 --- a/components/lazutils/lazstringutils.pas +++ b/components/lazutils/lazstringutils.pas @@ -16,7 +16,7 @@ unit LazStringUtils; interface uses - Classes, SysUtils, PascodeGen, + Classes, SysUtils, // LazUtils LazUTF8, LazLoggerBase, LazTracer; @@ -1415,23 +1415,19 @@ end; function LazIsValidIdent(const Ident: string; AllowDots: Boolean = False; StrictDots: Boolean = False): Boolean; // This is a copy of IsValidIdent from FPC 3.1. -// Later modified to check for Pascal keywords. They must be prepended with '&'. +// ToDo: Switch to using IsValidIdent from FPC 3.2 when it is the minimum requirement. const Alpha = ['A'..'Z', 'a'..'z', '_']; AlphaNum = Alpha + ['0'..'9']; var First: Boolean; - I, Len, Poz: Integer; + I, Len: Integer; begin Len := Length(Ident); - if (Len < 1) or (TPascalCodeGenerator(nil).IsKeyWord(Ident)) then + if Len < 1 then Exit(False); First := True; - if Ident[1] = '&' then - Poz := 2 - else - Poz := 1; - for I := Poz to Len do + for I := 1 to Len do begin if First then begin diff --git a/designer/askcompnamedlg.pas b/designer/askcompnamedlg.pas index 25e0660731..1188f8412c 100644 --- a/designer/askcompnamedlg.pas +++ b/designer/askcompnamedlg.pas @@ -34,7 +34,7 @@ uses // IdeIntf PropEdits, // IDE - LazarusIDEStrConsts; + IDEProcs, LazarusIDEStrConsts; type @@ -282,35 +282,37 @@ begin ErrorMsg:=lisEmpty; exit; end; - if not LazIsValidIdent(AName) then begin - ErrorMsg:=lisNotAValidPascalIdentifier; - exit; - end; - if (FLookupRoot<>nil) then begin - ConflictComponent:=FLookupRoot.FindComponent(AName); - if (ConflictComponent<>nil) - and (ConflictComponent<>NewComponent) then begin - ErrorMsg:=lisThereIsAlreadyAComponentWithThisName; - exit; - end; - if FLookupRoot<>FNewComponent then - begin - if SysUtils.CompareText(AName,FLookupRoot.Name)=0 then begin - ErrorMsg:=lisTheOwnerHasThisName; + try + CheckCompNameValidity(AName); // Will throw an exception on error. + if (FLookupRoot<>nil) then begin + ConflictComponent:=FLookupRoot.FindComponent(AName); + if (ConflictComponent<>nil) + and (ConflictComponent<>NewComponent) then begin + ErrorMsg:=lisThereIsAlreadyAComponentWithThisName; exit; end; - if SysUtils.CompareText(AName,FLookupRoot.ClassName)=0 then begin - ErrorMsg:=lisTheOwnerClassHasThisName; + if FLookupRoot<>FNewComponent then + begin + if SysUtils.CompareText(AName,FLookupRoot.Name)=0 then begin + ErrorMsg:=lisTheOwnerHasThisName; + exit; + end; + if SysUtils.CompareText(AName,FLookupRoot.ClassName)=0 then begin + ErrorMsg:=lisTheOwnerClassHasThisName; + exit; + end; + end; + if SysUtils.CompareText(AName,GetClassUnitName(FLookupRoot.ClassType))=0 then begin + ErrorMsg:=lisTheUnitHasThisName; exit; end; end; - if SysUtils.CompareText(AName,GetClassUnitName(FLookupRoot.ClassType))=0 then begin - ErrorMsg:=lisTheUnitHasThisName; - exit; - end; + ErrorMsg:=''; + Result:=true; + except + on E: Exception do + ErrorMsg:=E.Message; end; - ErrorMsg:=''; - Result:=true; end; end. diff --git a/ide/ideprocs.pp b/ide/ideprocs.pp index 7c224b6807..080d07d9ee 100644 --- a/ide/ideprocs.pp +++ b/ide/ideprocs.pp @@ -38,11 +38,11 @@ uses // LCL StdCtrls, ExtCtrls, // CodeTools - BasicCodeTools, FileProcs, CodeToolManager, CodeToolsConfig, CodeCache, + BasicCodeTools, CodeToolManager, CodeToolsConfig, CodeCache, KeywordFuncLists, + // BuildIntf PackageIntf, // IDE - TransferMacros, - LazConf; + TransferMacros, LazConf, LazarusIDEStrConsts; const SBuildMethod: array[TBuildMethod] of string = ( @@ -174,8 +174,8 @@ procedure ReverseList(List: TFPList); procedure FreeListObjects(List: TList; FreeList: boolean); procedure FreeListObjects(List: TFPList; FreeList: boolean); function CompareMemStreamText(s1, s2: TMemoryStream): Boolean; - function CheckGroupItemChecked(CheckGroup: TCheckGroup; const Caption: string): Boolean; +procedure CheckCompNameValidity(const AName: string); implementation @@ -1410,5 +1410,17 @@ begin Result := CheckGroup.Checked[CheckGroup.Items.IndexOf(Caption)]; end; +procedure CheckCompNameValidity(const AName: string); +// Raises an exception if not valid. +begin + if not IsValidIdent(AName) then + raise Exception.Create(Format(lisComponentNameIsNotAValidIdentifier, [Aname])); + if WordIsKeyWord.DoItCaseInsensitive(PChar(AName)) + or WordIsDelphiKeyWord.DoItCaseInsensitive(PChar(AName)) + or WordIsPredefinedFPCIdentifier.DoItCaseInsensitive(PChar(AName)) + or WordIsPredefinedDelphiIdentifier.DoItCaseInsensitive(PChar(AName)) then + raise Exception.Create(Format(lisComponentNameIsAPascalKeyword, [AName])); +end; + end. diff --git a/ide/main.pp b/ide/main.pp index 9b5f92b65b..f8f154c389 100644 --- a/ide/main.pp +++ b/ide/main.pp @@ -11771,15 +11771,7 @@ var OldOpenEditorsOnCodeToolChange: Boolean; begin DebugLn('Hint: (lazarus) TMainIDE.DesignerRenameComponent Old=',AComponent.Name,':',AComponent.ClassName,' New=',NewName,' Owner=',dbgsName(AComponent.Owner)); - if not IsValidIdent(NewName) then - raise Exception.Create(Format(lisComponentNameIsNotAValidIdentifier, [Newname])); - if WordIsKeyWord.DoItCaseInsensitive(PChar(NewName)) - or WordIsDelphiKeyWord.DoItCaseInsensitive(PChar(NewName)) - or WordIsPredefinedFPCIdentifier.DoItCaseInsensitive(PChar(NewName)) - or WordIsPredefinedDelphiIdentifier.DoItCaseInsensitive(PChar(NewName)) - then begin - raise Exception.Create(Format(lisComponentNameIsAPascalKeyword, [NewName])); - end; + CheckCompNameValidity(NewName); // Will throw an exception on error. if AComponent.Name='' then exit; // this component was never added to the source. It is a new component.