aboutsummaryrefslogtreecommitdiffstats
path: root/src/vhdl
diff options
context:
space:
mode:
authorTristan Gingold <tgingold@free.fr>2022-11-27 08:21:36 +0100
committerTristan Gingold <tgingold@free.fr>2022-11-27 12:30:40 +0100
commit55e669e57725017cb356907abcfe7a4953563296 (patch)
tree0ca13c7f01014cb4aed75c3921057d7616018447 /src/vhdl
parentd2a0fe9e9c097a5130a6d6a6f2c8c76bf4394ae0 (diff)
downloadghdl-55e669e57725017cb356907abcfe7a4953563296.tar.gz
ghdl-55e669e57725017cb356907abcfe7a4953563296.tar.bz2
ghdl-55e669e57725017cb356907abcfe7a4953563296.zip
vhdl: rework comment gathering to handle empty lines.
Diffstat (limited to 'src/vhdl')
-rw-r--r--src/vhdl/vhdl-comments.adb26
-rw-r--r--src/vhdl/vhdl-comments.ads16
-rw-r--r--src/vhdl/vhdl-formatters.adb4
-rw-r--r--src/vhdl/vhdl-parse.adb78
-rw-r--r--src/vhdl/vhdl-prints.adb2
-rw-r--r--src/vhdl/vhdl-scanner.adb20
-rw-r--r--src/vhdl/vhdl-sem_lib.adb7
7 files changed, 108 insertions, 45 deletions
diff --git a/src/vhdl/vhdl-comments.adb b/src/vhdl/vhdl-comments.adb
index a1cc2e7bb..d8c64610e 100644
--- a/src/vhdl/vhdl-comments.adb
+++ b/src/vhdl/vhdl-comments.adb
@@ -21,24 +21,28 @@
-- but not to be use'd.
with Files_Map;
-
-with Vhdl.Scanner; use Vhdl.Scanner;
+with Vhdl.Scanner;
package body Vhdl.Comments is
- procedure Save_Comments (Rng : out Comments_Range_Type) is
+ procedure Gather_Comments_Block (Rng : Comments_Range; N : Iir) is
begin
- Save_Comments (Get_Current_Source_File, Rng);
- end Save_Comments;
+ Gather_Comments_Block (Rng, Uns32 (N));
+ end Gather_Comments_Block;
- procedure Gather_Comments (Rng : Comments_Range_Type; N : Iir) is
+ procedure Gather_Comments_Block (N : Iir) is
begin
- Gather_Comments (Get_Current_Source_File, Rng, Uns32 (N));
- end Gather_Comments;
+ Gather_Comments (Uns32 (N));
+ end Gather_Comments_Block;
- procedure Gather_Comments (N : Iir) is
+ procedure Gather_Comments_Line (N : Iir)
+ is
+ Coord : Source_Coord_Type;
+ Rng : Comments_Range;
begin
- Gather_Comments (Get_Current_Source_File, Uns32 (N));
- end Gather_Comments;
+ Save_Comments (Rng);
+ Coord := Scanner.Get_Current_Coord;
+ Gather_Comments_Line (Rng, Coord.Line_Pos, Uns32 (N));
+ end Gather_Comments_Line;
function Find_First_Comment (File : Source_File_Entry; N : Node)
return Comment_Index
diff --git a/src/vhdl/vhdl-comments.ads b/src/vhdl/vhdl-comments.ads
index 82d469284..4c4106b37 100644
--- a/src/vhdl/vhdl-comments.ads
+++ b/src/vhdl/vhdl-comments.ads
@@ -27,11 +27,19 @@ with Vhdl.Nodes; use Vhdl.Nodes;
package Vhdl.Comments is
-- Save comments and attached them to a node.
- procedure Save_Comments (Rng : out Comments_Range_Type);
- procedure Gather_Comments (Rng : Comments_Range_Type; N : Iir);
+ procedure Gather_Comments_Block (Rng : Comments_Range; N : Iir);
- -- Attach previously scanned comments to node N.
- procedure Gather_Comments (N : Iir);
+ -- General rule:
+ -- Previous unattached comments are attached to node N.
+ -- Previous attached comments from the last empty line are attached to N.
+ --
+ -- For Gather_Comments_Block: the following comments until an empty line
+ -- will be attached to node N too.
+ -- For Gather_Comments_Line: if there is a comment on the same line, it
+ -- is attached to node N and so are the following comments until an
+ -- empty line.
+ procedure Gather_Comments_Block (N : Iir);
+ procedure Gather_Comments_Line (N : Iir);
-- Return the first comment attached to node N. FILE must be the file
-- of N.
diff --git a/src/vhdl/vhdl-formatters.adb b/src/vhdl/vhdl-formatters.adb
index b40615f99..03c72dbaa 100644
--- a/src/vhdl/vhdl-formatters.adb
+++ b/src/vhdl/vhdl-formatters.adb
@@ -846,9 +846,12 @@ package body Vhdl.Formatters is
is
use Format_Disp_Ctxt;
Sfe : constant Source_File_Entry := Get_Design_File_Source (F);
+ Prev_Flag_Gather_Comments : constant Boolean :=
+ Flags.Flag_Gather_Comments;
begin
Scanner.Flag_Comment := True;
Scanner.Flag_Newline := True;
+ Flags.Flag_Gather_Comments := False;
Set_File (Sfe);
Scan;
@@ -859,6 +862,7 @@ package body Vhdl.Formatters is
Close_File;
Scanner.Flag_Comment := False;
Scanner.Flag_Newline := False;
+ Flags.Flag_Gather_Comments := Prev_Flag_Gather_Comments;
Append_Eof (Ctxt);
end Format_Init;
diff --git a/src/vhdl/vhdl-parse.adb b/src/vhdl/vhdl-parse.adb
index 38063b45b..421a08edc 100644
--- a/src/vhdl/vhdl-parse.adb
+++ b/src/vhdl/vhdl-parse.adb
@@ -18,7 +18,7 @@ with Std_Names; use Std_Names;
with Flags; use Flags;
with Str_Table;
with Errorout; use Errorout;
-with File_Comments;
+with File_Comments; use File_Comments;
with Vhdl.Nodes_Utils; use Vhdl.Nodes_Utils;
with Vhdl.Tokens; use Vhdl.Tokens;
@@ -1718,7 +1718,7 @@ package body Vhdl.Parse is
-- Comments for the interface.
if Flag_Gather_Comments then
- Gather_Comments (First);
+ Gather_Comments_Line (First);
end if;
if Current_Token = Tok_Identifier then
@@ -2586,7 +2586,7 @@ package body Vhdl.Parse is
-- Comments for the enumeration literal.
if Flag_Gather_Comments then
- Gather_Comments (Enum_Lit);
+ Gather_Comments_Line (Enum_Lit);
end if;
-- LRM93 3.1.1
@@ -2928,7 +2928,7 @@ package body Vhdl.Parse is
-- Comments attached to the first element.
if Flag_Gather_Comments then
- Gather_Comments (First);
+ Gather_Comments_Line (First);
end if;
-- Scan ':'.
@@ -3159,7 +3159,7 @@ package body Vhdl.Parse is
-- Comments attached to the type.
if Flag_Gather_Comments then
- Gather_Comments (Decl);
+ Gather_Comments_Line (Decl);
end if;
Def := Parse_Enumeration_Type_Definition (Parent);
@@ -3207,7 +3207,7 @@ package body Vhdl.Parse is
-- Comments attached to the record.
if Flag_Gather_Comments then
- Gather_Comments (Decl);
+ Gather_Comments_Block (Decl);
end if;
Def := Parse_Record_Type_Definition;
@@ -3268,7 +3268,7 @@ package body Vhdl.Parse is
-- Comments attached to the type.
if Flag_Gather_Comments then
- Gather_Comments (Decl);
+ Gather_Comments_Line (Decl);
end if;
end if;
Set_Identifier (Decl, Ident);
@@ -4310,7 +4310,7 @@ package body Vhdl.Parse is
-- Comments attached to the object.
if Flag_Gather_Comments then
- Gather_Comments (Object);
+ Gather_Comments_Line (Object);
end if;
Scan_Identifier (Object);
@@ -5737,7 +5737,7 @@ package body Vhdl.Parse is
-- Comments after 'entity' but before the first generic or port are
-- attached to the entity.
if Flag_Gather_Comments then
- Gather_Comments (Res);
+ Gather_Comments_Block (Res);
end if;
Parse_Generic_Port_Clauses (Res);
@@ -8461,7 +8461,7 @@ package body Vhdl.Parse is
-- Comments for the subprogram.
if Flag_Gather_Comments then
- Gather_Comments (Subprg);
+ Gather_Comments_Line (Subprg);
end if;
case Current_Token is
@@ -8564,14 +8564,14 @@ package body Vhdl.Parse is
Res: Iir;
Sensitivity_List : Iir_List;
Start_Loc, Begin_Loc, End_Loc : Location_Type;
- Comments_Rng : File_Comments.Comments_Range_Type;
+ Comments : Comments_Range;
begin
Start_Loc := Get_Token_Location;
-- Attach comments now, as 'process' may appear alone, followed
-- by a comment for the next declaration.
if Flag_Gather_Comments then
- Save_Comments (Comments_Rng);
+ File_Comments.Save_Comments (Comments);
end if;
-- Skip 'process'
@@ -8582,7 +8582,7 @@ package body Vhdl.Parse is
-- Comments for the process.
if Flag_Gather_Comments then
- Gather_Comments (Comments_Rng, Res);
+ Gather_Comments_Block (Comments, Res);
end if;
-- Skip '('
@@ -8606,7 +8606,7 @@ package body Vhdl.Parse is
-- Comments for the process.
if Flag_Gather_Comments then
- Gather_Comments (Comments_Rng, Res);
+ Gather_Comments_Block (Comments, Res);
end if;
end if;
@@ -10596,6 +10596,12 @@ package body Vhdl.Parse is
-- Skip 'architecture'.
Scan;
+ -- Comments after 'architecture' but before the first declaration are
+ -- attached to the architecture.
+ if Flag_Gather_Comments then
+ Gather_Comments_Block (Res);
+ end if;
+
-- Identifier.
Scan_Identifier (Res);
@@ -10607,14 +10613,14 @@ package body Vhdl.Parse is
-- Skip 'is'.
Expect_Scan (Tok_Is);
- -- Comments after 'architecture' but before the first declaration are
- -- attached to the architecture.
+ Parse_Declarative_Part (Res, Res);
+
+ -- Comments just before the 'begin' are attached to the last declaration
+ -- or the architecture (if no declarations).
if Flag_Gather_Comments then
- Gather_Comments (Res);
+ Gather_Comments_End;
end if;
- Parse_Declarative_Part (Res, Res);
-
-- Skip 'begin'.
Begin_Loc := Get_Token_Location;
Expect_Scan (Tok_Begin);
@@ -11107,7 +11113,7 @@ package body Vhdl.Parse is
-- Comments after 'context' but before the first clause are attached
-- to the context.
if Flag_Gather_Comments then
- Gather_Comments (Res);
+ Gather_Comments_Block (Res);
end if;
Parse_Configuration_Declarative_Part (Res);
@@ -11199,8 +11205,10 @@ package body Vhdl.Parse is
-- package_header -- LRM08
-- package_declarative_part
-- END [ PACKAGE ] [ PACKAGE_simple_name ] ;
- function Parse_Package_Declaration
- (Parent : Iir; Id : Name_Id; Loc : Location_Type) return Iir
+ function Parse_Package_Declaration (Parent : Iir;
+ Id : Name_Id;
+ Loc : Location_Type;
+ Comments : Comments_Range) return Iir
is
Res: Iir_Package_Declaration;
End_Loc : Location_Type;
@@ -11213,7 +11221,7 @@ package body Vhdl.Parse is
-- Comments after 'package' but before the first declaration are
-- attached to the package.
if Flag_Gather_Comments then
- Gather_Comments (Res);
+ Gather_Comments_Block (Comments, Res);
end if;
if Current_Token = Tok_Generic then
@@ -11225,6 +11233,12 @@ package body Vhdl.Parse is
End_Loc := Get_Token_Location;
+ -- Comments just before the 'end' are attached to the last declaration
+ -- or the package (if no declarations).
+ if Flag_Gather_Comments then
+ Gather_Comments_End;
+ end if;
+
-- Skip 'end'
Expect_Scan (Tok_End);
@@ -11358,6 +11372,7 @@ package body Vhdl.Parse is
Id : Name_Id;
Res : Iir;
Start_Loc : Location_Type;
+ Comments : Comments_Range;
begin
-- Skip 'package'
Start_Loc := Get_Token_Location;
@@ -11380,6 +11395,10 @@ package body Vhdl.Parse is
Expect (Tok_Identifier);
end if;
+ if Flag_Gather_Comments then
+ File_Comments.Save_Comments (Comments);
+ end if;
+
-- Skip 'is'.
Expect_Scan (Tok_Is);
@@ -11387,7 +11406,7 @@ package body Vhdl.Parse is
Res := Parse_Package_Instantiation_Declaration (Parent, Id, Loc);
-- Note: there is no 'end' in instantiation.
else
- Res := Parse_Package_Declaration (Parent, Id, Loc);
+ Res := Parse_Package_Declaration (Parent, Id, Loc, Comments);
end if;
end if;
@@ -11679,7 +11698,7 @@ package body Vhdl.Parse is
-- Comments after 'context' but before the first clause are attached
-- to the context.
if Flag_Gather_Comments then
- Gather_Comments (Decl);
+ Gather_Comments_Block (Decl);
end if;
Parse_Context_Clause (Decl);
@@ -11802,7 +11821,7 @@ package body Vhdl.Parse is
-- Attach comments to the design unit.
if Flag_Gather_Comments then
- Gather_Comments (Res);
+ Gather_Comments_Block (Res);
end if;
Parse_Context_Clause (Res);
@@ -11849,6 +11868,10 @@ package body Vhdl.Parse is
Res : Iir_Design_File;
Design, Last_Design : Iir_Design_Unit;
begin
+ if Flag_Gather_Comments then
+ File_Comments.Comment_Init_Scan (Get_Current_Source_File);
+ end if;
+
-- The first token.
pragma Assert (Current_Token = Tok_Invalid);
Scan;
@@ -11872,7 +11895,8 @@ package body Vhdl.Parse is
end loop;
if Flag_Gather_Comments then
- File_Comments.Sort_Comments_By_Node (Get_Current_Source_File);
+ File_Comments.Sort_Comments_By_Node;
+ File_Comments.Comment_Close_Scan;
end if;
if Last_Design = Null_Iir then
diff --git a/src/vhdl/vhdl-prints.adb b/src/vhdl/vhdl-prints.adb
index 13115cc22..c77bef029 100644
--- a/src/vhdl/vhdl-prints.adb
+++ b/src/vhdl/vhdl-prints.adb
@@ -1615,6 +1615,8 @@ package body Vhdl.Prints is
is
Next_Decl : Iir;
begin
+ Start_Node (Ctxt, Decl);
+
Start_Hbox (Ctxt);
case Get_Kind (Decl) is
when Iir_Kind_Variable_Declaration =>
diff --git a/src/vhdl/vhdl-scanner.adb b/src/vhdl/vhdl-scanner.adb
index cee853792..5f7b735f2 100644
--- a/src/vhdl/vhdl-scanner.adb
+++ b/src/vhdl/vhdl-scanner.adb
@@ -2062,12 +2062,17 @@ package body Vhdl.Scanner is
end Scan_Block_Comment;
-- Get a new token.
- procedure Scan is
+ procedure Scan
+ is
+ -- If true, newlines must be reported for comments.
+ Comment_Report_Newline : Boolean;
begin
if Current_Token /= Tok_Invalid then
Current_Context.Prev_Token := Current_Token;
end if;
+ Comment_Report_Newline := False;
+
Current_Context.Prev_Pos := Pos;
<< Again >> null;
@@ -2095,6 +2100,9 @@ package body Vhdl.Scanner is
Pos := Pos + 1;
goto Again;
when LF =>
+ if Comment_Report_Newline then
+ Comment_Newline (Current_Context.Line_Pos);
+ end if;
Scan_LF_Newline;
if Flag_Newline then
Current_Token := Tok_Newline;
@@ -2102,6 +2110,9 @@ package body Vhdl.Scanner is
end if;
goto Again;
when CR =>
+ if Comment_Report_Newline then
+ Comment_Newline (Current_Context.Line_Pos);
+ end if;
Scan_CR_Newline;
if Flag_Newline then
Current_Token := Tok_Newline;
@@ -2165,8 +2176,11 @@ package body Vhdl.Scanner is
end loop;
if Flag_Gather_Comments then
- Add_Comment (Current_Context.Source_File,
- Current_Context.Token_Pos, Pos - 1);
+ Add_Comment (Current_Context.Token_Pos, Pos - 1,
+ Current_Context.Line_Pos);
+ -- Following newlines will be reported so that a blank
+ -- line is detected.
+ Comment_Report_Newline := True;
end if;
if Flag_Comment then
diff --git a/src/vhdl/vhdl-sem_lib.adb b/src/vhdl/vhdl-sem_lib.adb
index 56312701b..3eccac5e0 100644
--- a/src/vhdl/vhdl-sem_lib.adb
+++ b/src/vhdl/vhdl-sem_lib.adb
@@ -178,6 +178,8 @@ package body Vhdl.Sem_Lib is
is
use Vhdl.Scanner;
Design_File : constant Iir_Design_File := Get_Design_File (Design_Unit);
+ Prev_Flag_Gather_Comments : constant Boolean :=
+ Flags.Flag_Gather_Comments;
Fe : Source_File_Entry;
Line, Off: Natural;
Pos: Source_Ptr;
@@ -225,10 +227,15 @@ package body Vhdl.Sem_Lib is
Files_Map.File_Add_Line_Number (Get_Current_Source_File, Line, Pos);
Set_Current_Position (Pos + Source_Ptr (Off));
+ Flags.Flag_Gather_Comments := False;
+
-- Parse
Scan;
Res := Vhdl.Parse.Parse_Design_Unit;
Close_File;
+
+ Flags.Flag_Gather_Comments := Prev_Flag_Gather_Comments;
+
if Res = Null_Iir then
raise Compilation_Error;
end if;