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

(cherry picked from commit de4f724924)
This commit is contained in:
Juha 2021-09-28 13:12:46 +03:00 committed by Maxim Ganetsky
parent 5cd595f179
commit e2a24fadb6
4 changed files with 48 additions and 46 deletions

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -11770,15 +11770,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.