Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ada : fixed cppcheck errors + added ada-label test #2946

Merged
merged 2 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Units/parser-ada.r/ada-label.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--sort=no
--kinds-Ada=""
--kinds-Ada=+{label}
58 changes: 58 additions & 0 deletions Units/parser-ada.r/ada-label.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Null_Before_Label input.adb /^ <<Null_Before_Label>> null; <<Null_After_Label>>$/;" b subprogram:Input file:
Null_After_Label input.adb /^ <<Null_Before_Label>> null; <<Null_After_Label>>$/;" b subprogram:Input file:
Assignment_Before_Label input.adb /^ <<Assignment_Before_Label>> X := 1; <<Assignment_After_Label>>$/;" b subprogram:Input file:
Assignment_After_Label input.adb /^ <<Assignment_Before_Label>> X := 1; <<Assignment_After_Label>>$/;" b subprogram:Input file:
Exit_Before_Label input.adb /^ <<Exit_Before_Label>> exit; <<Exit_After_Label>>$/;" b anon:loop file:
Exit_After_Label input.adb /^ <<Exit_Before_Label>> exit; <<Exit_After_Label>>$/;" b anon:loop file:
Goto_Before_Label input.adb /^ <<Goto_Before_Label>> goto Assignment_Before_Label; <<Goto_After_Label>>$/;" b anon:loop file:
Goto_After_Label input.adb /^ <<Goto_Before_Label>> goto Assignment_Before_Label; <<Goto_After_Label>>$/;" b anon:loop file:
Procedure_Call_Before_Label input.adb /^ <<Procedure_Call_Before_Label>> Ada.Text_IO.Put_Line ("Hello World"); <<Procedure_Call_After_L/;" b subprogram:Input file:
Procedure_Call_After_Label input.adb /^ <<Procedure_Call_Before_Label>> Ada.Text_IO.Put_Line ("Hello World"); <<Procedure_Call_After_L/;" b subprogram:Input file:
Return_Before_Label input.adb /^ <<Return_Before_Label>> return; <<Return_After_Label>>$/;" b subprogram:Input file:
Return_After_Label input.adb /^ <<Return_Before_Label>> return; <<Return_After_Label>>$/;" b subprogram:Input file:
Accept_Before_Label input.adb /^ <<Accept_Before_Label>> accept Start(Nr : in Natural) do$/;" b task:Server file:
Requeue_Before_Label input.adb /^ <<Requeue_Before_Label>> requeue Start; <<Requeue_After_Label>>$/;" b entry:Start file:
Requeue_After_Label input.adb /^ <<Requeue_Before_Label>> requeue Start; <<Requeue_After_Label>>$/;" b entry:Start file:
Accept_After_Label input.adb /^ end Start; <<Accept_After_Label>>$/;" b task:Server file:
Entry_Before_Label input.adb /^ <<Entry_Before_Label>> My_Task.Start (1); <<Entry_After_Label>>$/;" b anon:declare file:
Entry_After_Label input.adb /^ <<Entry_Before_Label>> My_Task.Start (1); <<Entry_After_Label>>$/;" b anon:declare file:
Delay_Before_Label input.adb /^ <<Delay_Before_Label>> delay 100.0; <<Delay_After_Label>>$/;" b anon:declare file:
Delay_After_Label input.adb /^ <<Delay_Before_Label>> delay 100.0; <<Delay_After_Label>>$/;" b anon:declare file:
Abort_Before_Label input.adb /^ <<Abort_Before_Label>> abort My_Task; <<Abort_After_Label>>$/;" b anon:declare file:
Abort_After_Label input.adb /^ <<Abort_Before_Label>> abort My_Task; <<Abort_After_Label>>$/;" b anon:declare file:
Select_Before_Statement input.adb /^ <<Select_Before_Statement>> select$/;" b anon:declare file:
Select_After_Statement input.adb /^ end select; <<Select_After_Statement>>$/;" b task:Server file:
Code_Before_Label input.adb /^ <<Code_Before_Label>>$/;" b task:Server file:
Code_After_Label input.adb /^ <<Code_After_Label>>$/;" b task:Server file:
Multiple_1_Before_Label input.adb /^ <<Multiple_1_Before_Label>> <<Multiple_2_Before_Label>> <<Multiple_3_Before_Label>> null; <<Mu/;" b task:Server file:
Multiple_2_Before_Label input.adb /^ <<Multiple_1_Before_Label>> <<Multiple_2_Before_Label>> <<Multiple_3_Before_Label>> null; <<Mu/;" b task:Server file:
Multiple_3_Before_Label input.adb /^ <<Multiple_1_Before_Label>> <<Multiple_2_Before_Label>> <<Multiple_3_Before_Label>> null; <<Mu/;" b task:Server file:
Multiple_1_After_Label input.adb /^ <<Multiple_1_Before_Label>> <<Multiple_2_Before_Label>> <<Multiple_3_Before_Label>> null; <<Mu/;" b task:Server file:
Multiple_2_After_Label input.adb /^ <<Multiple_1_Before_Label>> <<Multiple_2_Before_Label>> <<Multiple_3_Before_Label>> null; <<Mu/;" b task:Server file:
Multiple_3_After_Label input.adb /^ <<Multiple_1_Before_Label>> <<Multiple_2_Before_Label>> <<Multiple_3_Before_Label>> null; <<Mu/;" b task:Server file:
Raise_Before_Label input.adb /^ <<Raise_Before_Label>> raise constraint_error; <<Raise_After_Label>>$/;" b task:Server file:
Raise_After_Label input.adb /^ <<Raise_Before_Label>> raise constraint_error; <<Raise_After_Label>>$/;" b task:Server file:
If_Before_Label input.adb /^ <<If_Before_Label>> if X>1 then$/;" b task:Server file:
If_After_Label input.adb /^ end if; <<If_After_Label>> $/;" b task:Server file:
Case_Before_Label input.adb /^ <<Case_Before_Label>> case X is$/;" b task:Server file:
Case_Code_Before_Label input.adb /^ when 1 => <<Case_Code_Before_Label>> Ada.Text_IO.Put_Line ("1"); <<Case_Code_After_Label>> $/;" b task:Server file:
Case_Code_After_Label input.adb /^ when 1 => <<Case_Code_Before_Label>> Ada.Text_IO.Put_Line ("1"); <<Case_Code_After_Label>> $/;" b task:Server file:
Case_After_Label input.adb /^ end case; <<Case_After_Label>>$/;" b task:Server file:
Loop_Before_Label input.adb /^ <<Loop_Before_Label>> loop $/;" b task:Server file:
Loop_Code_Before_Label input.adb /^ <<Loop_Code_Before_Label>> Ada.Text_IO.Put_Line ("1"); <<Loop_Code_After_Label>>$/;" b anon:loop file:
Loop_Code_After_Label input.adb /^ <<Loop_Code_Before_Label>> Ada.Text_IO.Put_Line ("1"); <<Loop_Code_After_Label>>$/;" b anon:loop file:
Loop_After_Label input.adb /^ end loop; <<Loop_After_Label>>$/;" b task:Server file:
While_Before_Label input.adb /^ <<While_Before_Label>> while X > 1 loop$/;" b task:Server file:
While_Code_Before_Label input.adb /^ <<While_Code_Before_Label>> X := X + 1; <<While_Code_After_Label>>$/;" b anon:loop file:
While_Code_After_Label input.adb /^ <<While_Code_Before_Label>> X := X + 1; <<While_Code_After_Label>>$/;" b anon:loop file:
While_After_Label input.adb /^ end loop; <<While_After_Label>>$/;" b task:Server file:
For_Before_Label input.adb /^ <<For_Before_Label>> for I in 1 .. X loop$/;" b task:Server file:
For_Code_Before_Label input.adb /^ <<For_Code_Before_Label>> Ada.Text_IO.Put_Line (X'Image); <<For_Code_After_Label>> $/;" b anon:loop file:
For_Code_After_Label input.adb /^ <<For_Code_Before_Label>> Ada.Text_IO.Put_Line (X'Image); <<For_Code_After_Label>> $/;" b anon:loop file:
For_After_Label input.adb /^ end loop; <<For_After_Label>>$/;" b task:Server file:
Declare_Before_Label input.adb /^ <<Declare_Before_Label>> declare$/;" b task:Server file:
Declare_After_Label input.adb /^ end; <<Declare_After_Label>>$/;" b task:Server file:
Begin_Before_Label input.adb /^ <<Begin_Before_Label>> begin$/;" b task:Server file:
Begin_After_Label input.adb /^ end; <<Begin_After_Label>>$/;" b task:Server file:
Return_Before_Label input.adb /^ <<Return_Before_Label>> return Coord : Coordinate do$/;" b subprogram:Extended_Return file:
Return_After_Label input.adb /^ end return; <<Return_After_Label>>$/;" b subprogram:Extended_Return file:
133 changes: 133 additions & 0 deletions Units/parser-ada.r/ada-label.d/input.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
-- Test labels
-- See: ARM 5.1 : Simple and Compound Statements - Sequences of Statements
--
-- A label should come before a simple statement or a compound statment.
--
-- Check if file compiles: gnatmake -gnatc Units/parser-ada.r/ada-label.d/input.adb
--
with Ada.Text_IO;

procedure Input is
X : Integer;
begin
-- ARM 5.1(4/2) : Simple statements
-- null statement
<<Null_Before_Label>> null; <<Null_After_Label>>

-- assignment
<<Assignment_Before_Label>> X := 1; <<Assignment_After_Label>>

for I in 1 .. X loop
if I > 1 then
-- exit statement
<<Exit_Before_Label>> exit; <<Exit_After_Label>>
elsif I > 2 then
-- goto statement
<<Goto_Before_Label>> goto Assignment_Before_Label; <<Goto_After_Label>>
end if;
end loop;

-- procedure call statement
<<Procedure_Call_Before_Label>> Ada.Text_IO.Put_Line ("Hello World"); <<Procedure_Call_After_Label>>
if X > 1 then
-- simple return statement
<<Return_Before_Label>> return; <<Return_After_Label>>
end if;

declare
task type Server is
entry Start(Nr : in Natural);
end Server;

task body Server is
Iden : Natural;
begin
<<Accept_Before_Label>> accept Start(Nr : in Natural) do
Iden := Nr;
<<Requeue_Before_Label>> requeue Start; <<Requeue_After_Label>>
end Start; <<Accept_After_Label>>
Ada.Text_IO.Put_Line ("Working...");
end;

My_Task : Server;
begin
<<Entry_Before_Label>> My_Task.Start (1); <<Entry_After_Label>>
<<Delay_Before_Label>> delay 100.0; <<Delay_After_Label>>
<<Abort_Before_Label>> abort My_Task; <<Abort_After_Label>>
if X > 1 then
<<Select_Before_Statement>> select
My_Task.Start (1);
or
delay 10.0;
end select; <<Select_After_Statement>>
end if;
end;
<<Code_Before_Label>>
Ada.Text_IO.Put_Line ("Code 1");
Ada.Text_IO.Put_Line ("Code 2");
<<Code_After_Label>>
<<Multiple_1_Before_Label>> <<Multiple_2_Before_Label>> <<Multiple_3_Before_Label>> null; <<Multiple_1_After_Label>> <<Multiple_2_After_Label>> <<Multiple_3_After_Label>>
<<Raise_Before_Label>> raise constraint_error; <<Raise_After_Label>>

-- ARM 5.1(5/2) Compound statements
-- if statement
<<If_Before_Label>> if X>1 then
Ada.Text_IO.Put_Line ("X>1");
elsif X > 2 then
Ada.Text_IO.Put_Line ("X > 2");
end if; <<If_After_Label>>

-- case statement
<<Case_Before_Label>> case X is
when 1 => <<Case_Code_Before_Label>> Ada.Text_IO.Put_Line ("1"); <<Case_Code_After_Label>>
when 2 => Ada.Text_IO.Put_Line ("2");
when others => Ada.Text_IO.Put_Line ("2");
end case; <<Case_After_Label>>

-- loop statement
<<Loop_Before_Label>> loop
<<Loop_Code_Before_Label>> Ada.Text_IO.Put_Line ("1"); <<Loop_Code_After_Label>>
exit;
end loop; <<Loop_After_Label>>

<<While_Before_Label>> while X > 1 loop
<<While_Code_Before_Label>> X := X + 1; <<While_Code_After_Label>>
end loop; <<While_After_Label>>

<<For_Before_Label>> for I in 1 .. X loop
<<For_Code_Before_Label>> Ada.Text_IO.Put_Line (X'Image); <<For_Code_After_Label>>
end loop; <<For_After_Label>>

-- block statement
<<Declare_Before_Label>> declare
Y : Integer;
begin
Y := X + 1;
Ada.Text_IO.Put_Line (Y'Image);
end; <<Declare_After_Label>>

<<Begin_Before_Label>> begin
null;
end; <<Begin_After_Label>>

-- extended return statement
declare
type Coordinate is record
X : Integer;
Y : Integer;
end record;

function Extended_Return return Coordinate is
begin
<<Return_Before_Label>> return Coord : Coordinate do
Coord.X := 10;
Coord.Y := 20;
end return; <<Return_After_Label>>
end Extended_Return;
begin
null;
end;

-- accept statement See above <<Accept_Before_Label>> and <<Accept_After_Label>>
-- select statement See above <<Select_Before_Statement>> and <<Select_After_Statement>>
end Input;
11 changes: 7 additions & 4 deletions parsers/ada.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,10 @@ static void freeAdaToken(adaTokenList *list, adaTokenInfo *token)
{
token->prev->next = token->next;
}
else if(list != NULL && token->prev == NULL)
else if(list != NULL)
{
/* Token to remove is head in list as 'token->prev == NULL' */
Assert(token->prev == NULL);
list->head = token->next;
}

Expand All @@ -504,8 +506,10 @@ static void freeAdaToken(adaTokenList *list, adaTokenInfo *token)
{
token->next->prev = token->prev;
}
else if(list != NULL && token->next == NULL)
else if(list != NULL)
{
/* Token to remove is tail of list as 'token->next == NULL') */
Assert(token->next == NULL);
list->tail = token->prev;
}

Expand Down Expand Up @@ -2020,9 +2024,8 @@ static adaTokenInfo *adaParse(adaParseMode mode, adaTokenInfo *parent)
* found, if we didn't just fall through */
if((pos + i) < lineLen)
{
token = newAdaToken(&line[pos], i, ADA_KIND_LABEL, false, parent);
newAdaToken(&line[pos], i, ADA_KIND_LABEL, false, parent);
skipPast(">>");
token = NULL;
}
} /* else if(strncasecmp(line[pos], "<<", strlen("<<")) == 0) */
/* we need to check for a few special case keywords that might cause
Expand Down