Revert checking for Pascal keywords in LazIsValidIdent which is used also for unit names etc. Prevent illegal name by other means.

This commit is contained in:
Juha 2021-09-28 13:12:46 +03:00
parent 7cb38d7bc6
commit de4f724924
4 changed files with 48 additions and 46 deletions

View File

@ -16,7 +16,7 @@ unit LazStringUtils;
interface interface
uses uses
Classes, SysUtils, PascodeGen, Classes, SysUtils,
// LazUtils // LazUtils
LazUTF8, LazLoggerBase, LazTracer; LazUTF8, LazLoggerBase, LazTracer;
@ -1415,23 +1415,19 @@ end;
function LazIsValidIdent(const Ident: string; AllowDots: Boolean = False; function LazIsValidIdent(const Ident: string; AllowDots: Boolean = False;
StrictDots: Boolean = False): Boolean; StrictDots: Boolean = False): Boolean;
// This is a copy of IsValidIdent from FPC 3.1. // 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 const
Alpha = ['A'..'Z', 'a'..'z', '_']; Alpha = ['A'..'Z', 'a'..'z', '_'];
AlphaNum = Alpha + ['0'..'9']; AlphaNum = Alpha + ['0'..'9'];
var var
First: Boolean; First: Boolean;
I, Len, Poz: Integer; I, Len: Integer;
begin begin
Len := Length(Ident); Len := Length(Ident);
if (Len < 1) or (TPascalCodeGenerator(nil).IsKeyWord(Ident)) then if Len < 1 then
Exit(False); Exit(False);
First := True; First := True;
if Ident[1] = '&' then for I := 1 to Len do
Poz := 2
else
Poz := 1;
for I := Poz to Len do
begin begin
if First then if First then
begin begin

View File

@ -34,7 +34,7 @@ uses
// IdeIntf // IdeIntf
PropEdits, PropEdits,
// IDE // IDE
LazarusIDEStrConsts; IDEProcs, LazarusIDEStrConsts;
type type
@ -282,35 +282,37 @@ begin
ErrorMsg:=lisEmpty; ErrorMsg:=lisEmpty;
exit; exit;
end; end;
if not LazIsValidIdent(AName) then begin try
ErrorMsg:=lisNotAValidPascalIdentifier; CheckCompNameValidity(AName); // Will throw an exception on error.
exit; if (FLookupRoot<>nil) then begin
end; ConflictComponent:=FLookupRoot.FindComponent(AName);
if (FLookupRoot<>nil) then begin if (ConflictComponent<>nil)
ConflictComponent:=FLookupRoot.FindComponent(AName); and (ConflictComponent<>NewComponent) then begin
if (ConflictComponent<>nil) ErrorMsg:=lisThereIsAlreadyAComponentWithThisName;
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;
exit; exit;
end; end;
if SysUtils.CompareText(AName,FLookupRoot.ClassName)=0 then begin if FLookupRoot<>FNewComponent then
ErrorMsg:=lisTheOwnerClassHasThisName; 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; exit;
end; end;
end; end;
if SysUtils.CompareText(AName,GetClassUnitName(FLookupRoot.ClassType))=0 then begin ErrorMsg:='';
ErrorMsg:=lisTheUnitHasThisName; Result:=true;
exit; except
end; on E: Exception do
ErrorMsg:=E.Message;
end; end;
ErrorMsg:='';
Result:=true;
end; end;
end. end.

View File

@ -38,11 +38,11 @@ uses
// LCL // LCL
StdCtrls, ExtCtrls, StdCtrls, ExtCtrls,
// CodeTools // CodeTools
BasicCodeTools, FileProcs, CodeToolManager, CodeToolsConfig, CodeCache, BasicCodeTools, CodeToolManager, CodeToolsConfig, CodeCache, KeywordFuncLists,
// BuildIntf
PackageIntf, PackageIntf,
// IDE // IDE
TransferMacros, TransferMacros, LazConf, LazarusIDEStrConsts;
LazConf;
const const
SBuildMethod: array[TBuildMethod] of string = ( SBuildMethod: array[TBuildMethod] of string = (
@ -174,8 +174,8 @@ procedure ReverseList(List: TFPList);
procedure FreeListObjects(List: TList; FreeList: boolean); procedure FreeListObjects(List: TList; FreeList: boolean);
procedure FreeListObjects(List: TFPList; FreeList: boolean); procedure FreeListObjects(List: TFPList; FreeList: boolean);
function CompareMemStreamText(s1, s2: TMemoryStream): Boolean; function CompareMemStreamText(s1, s2: TMemoryStream): Boolean;
function CheckGroupItemChecked(CheckGroup: TCheckGroup; const Caption: string): Boolean; function CheckGroupItemChecked(CheckGroup: TCheckGroup; const Caption: string): Boolean;
procedure CheckCompNameValidity(const AName: string);
implementation implementation
@ -1410,5 +1410,17 @@ begin
Result := CheckGroup.Checked[CheckGroup.Items.IndexOf(Caption)]; Result := CheckGroup.Checked[CheckGroup.Items.IndexOf(Caption)];
end; 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. end.

View File

@ -11771,15 +11771,7 @@ var
OldOpenEditorsOnCodeToolChange: Boolean; OldOpenEditorsOnCodeToolChange: Boolean;
begin begin
DebugLn('Hint: (lazarus) TMainIDE.DesignerRenameComponent Old=',AComponent.Name,':',AComponent.ClassName,' New=',NewName,' Owner=',dbgsName(AComponent.Owner)); DebugLn('Hint: (lazarus) TMainIDE.DesignerRenameComponent Old=',AComponent.Name,':',AComponent.ClassName,' New=',NewName,' Owner=',dbgsName(AComponent.Owner));
if not IsValidIdent(NewName) then CheckCompNameValidity(NewName); // Will throw an exception on error.
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;
if AComponent.Name='' then if AComponent.Name='' then
exit; // this component was never added to the source. It is a new component. exit; // this component was never added to the source. It is a new component.