https://gcc.gnu.org/g:56902d171735f085dac88417b9ae8c72b7ab78fa
commit r16-4052-g56902d171735f085dac88417b9ae8c72b7ab78fa Author: Gary Dismukes <dismu...@adacore.com> Date: Wed Sep 10 19:33:56 2025 +0000 ada: New preprocessing option for emitting empty comments for deleted lines When integrated preprocessing is done on a source file with lines that are filtered out, by default this can result in multiple blank lines being emitted, but this can clash with the style switch -gnatyu, which will flag cases of multiple blank lines. A new preprocessing option "-e" is added to allow outputting "empty" comment lines rather than blank lines (the comments consist of "--!" and no other text). This option is also available for the gnatprep tool. This behavior is the default when integrated preprocessing is done without generating a ".prep" output file with -gnateG, but when -gnateG is used, blank lines are still substituted by default for directives and removed source lines (for compatibility with long-existing behavior). The -gnateG switch is also extended to allow appending 'b', 'c', or 'e' at the end of the switch to force any of the replacement options ('b' => blank lines, 'c' => comments including the original source lintes, and 'e' => empty comment lines). gcc/ada/ChangeLog: * doc/gnat_ugn/building_executable_programs_with_gnat.rst: Add doc for addition of -gnateG options b, c, and e. * doc/gnat_ugn/the_gnat_compilation_model.rst: Add doc for "-e" optionn on gnatprep and integrated preprocessing. Add doc for addition of -gnateG options b, c, and e. * gprep.adb (Scan_Command_Line): Add 'e' to the list of allowed switch letters in the string passed to GNAT.Command_Line.Getopt. Set the flag Opt.Empty_Comment_Deleted_Lines to True when the "-e" switch is found. (Usage): Output a switch description for the "-e" switch. * opt.ads: Add new flag variable Empty_Comment_Deleted_Lines. Add "GNAT" to the "GNATPREP" comment line for Comment_Deleted_Lines. * prep.adb (Output_Line): Add handling for Empty_Comment_Deleted_Lines, outputting comment lines consisting of "--!" for lines that are removed in the preprocessed source file when Empty_Comment_Deleted_Lines is True. * prepcomp.adb (Preproc_Data): Add Empty_Comments component (defaulting to False). (No_Preproc_Data): Add association for Empty_Comments component. (Parse_Preprocessing_Data_File): Add handling for new switch -e. (Prepare_To_Preprocess): Add logic for setting the new option Empty_Comment_Deleted_Lines (and making it the default for integrated preprocessing in the absence of other switches). * switch-c.adb (Scan_Front_End_Switches): Add support adding a single character 'b', 'c', 'e' to the "-gnateG" switch, to select any of the three options for replacing deleted lines in preprocessing output file. * usage.adb: Update usage info for -gnateG, to reflect the option of appending b, c, or e to the switch. * gnat_ugn.texi: Regenerate. Diff: --- .../building_executable_programs_with_gnat.rst | 8 ++- .../doc/gnat_ugn/the_gnat_compilation_model.rst | 29 +++++++- gcc/ada/gnat_ugn.texi | 48 +++++++++++-- gcc/ada/gprep.adb | 6 +- gcc/ada/opt.ads | 7 +- gcc/ada/prep.adb | 7 ++ gcc/ada/prepcomp.adb | 78 +++++++++++++++------- gcc/ada/switch-c.adb | 40 +++++++++++ gcc/ada/usage.adb | 4 +- 9 files changed, 191 insertions(+), 36 deletions(-) diff --git a/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst b/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst index fbd3202c3d65..b46f69b7d4a0 100644 --- a/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst +++ b/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst @@ -1608,8 +1608,12 @@ Alphabetical List of All Switches .. index:: -gnateG (gcc) -:switch:`-gnateG` - Save result of preprocessing in a text file. +:switch:`-gnateG[bce]` + Save result of preprocessing in a text file. An optional character (b, c, + or e) can be appended to indicate that filtered lines are to be replaced by + blank lines, comment lines that include the filtered line, or empty comment + lines ("--!"), respectively. The default is to replace filtered lines with + blank lines. .. index:: -gnateH (gcc) diff --git a/gcc/ada/doc/gnat_ugn/the_gnat_compilation_model.rst b/gcc/ada/doc/gnat_ugn/the_gnat_compilation_model.rst index 891886b53601..94b41b52adf2 100644 --- a/gcc/ada/doc/gnat_ugn/the_gnat_compilation_model.rst +++ b/gcc/ada/doc/gnat_ugn/the_gnat_compilation_model.rst @@ -2931,6 +2931,18 @@ Switches for ``gnatprep`` don't specify a value, the symbol is defined to be ``True``. You can use this switch instead of providing a definition file. +.. index:: -e (gnatprep) + +:switch:`-e` + Causes both preprocessor lines and the lines deleted by + preprocessing to be replaced by empty comment lines marked + with ``"--!"`` (and no other text) in the output source file, + preserving line numbers in the output file. This option can + be useful as an alternative to :switch:`-b` and :switch:`-c` + when compilation style switches like :switch:`-gnatyu` or + :switch:`-gnatyM` are used (to avoid warnings about multiple + blank lines or lines too long). + .. index:: -r (gnatprep) :switch:`-r` @@ -3248,7 +3260,7 @@ that relate to integrated preprocessing. Causes both preprocessor lines and the lines deleted by preprocessing to be replaced by blank lines, preserving the line number. This switch is always implied; however, if specified after :switch:`-c` - it cancels the effect of :switch:`-c`. + or :switch:`-e` it cancels the effect of those switches. :switch:`-c` Causes both preprocessor lines and the lines deleted @@ -3266,6 +3278,12 @@ that relate to integrated preprocessing. name defined in a definition file. + :switch:`-e` + Causes both preprocessor lines and the lines deleted by + preprocessing to be replaced by empty comment lines marked + with '`--!`' (and no other text) in the output source file, + + :switch:`-s` Causes a sorted list of symbol names and values to be listed on the standard output file. @@ -3306,12 +3324,19 @@ that relate to integrated preprocessing. This switch is similar to switch :switch:`-D` of ``gnatprep``. -:switch:`-gnateG` +.. index:: -gnateG (gcc) + +:switch:`-gnateG[bce]` When integrated preprocessing is performed on source file :file:`filename.extension`, create or overwrite :file:`filename.extension.prep` to contain the result of the preprocessing. For example if the source file is :file:`foo.adb` then the output file is :file:`foo.adb.prep`. + An optional character (b, c, or e) can be appended to indicate that filtered + lines are to be replaced by blank lines, comments, or empty comments (see + documentation above about :switch:`-b`, :switch:`-c`, and :switch:`-e`). + If one of those switches is given in a preprocessor data file, then it + will override any option included with :switch:`-gnateG`. .. _Mixed_Language_Programming: diff --git a/gcc/ada/gnat_ugn.texi b/gcc/ada/gnat_ugn.texi index 79b58d341ac8..e0771eb16b37 100644 --- a/gcc/ada/gnat_ugn.texi +++ b/gcc/ada/gnat_ugn.texi @@ -4628,6 +4628,23 @@ don’t specify a value, the symbol is defined to be @code{True}. You can use this switch instead of providing a definition file. @end table +@geindex -e (gnatprep) + + +@table @asis + +@item @code{-e} + +Causes both preprocessor lines and the lines deleted by +preprocessing to be replaced by empty comment lines marked +with @code{"--!"} (and no other text) in the output source file, +preserving line numbers in the output file. This option can +be useful as an alternative to @code{-b} and @code{-c} +when compilation style switches like @code{-gnatyu} or +@code{-gnatyM} are used (to avoid warnings about multiple +blank lines or lines too long). +@end table + @geindex -r (gnatprep) @@ -5023,7 +5040,7 @@ Finally, switches similar to those of @code{gnatprep} may optionally appear: Causes both preprocessor lines and the lines deleted by preprocessing to be replaced by blank lines, preserving the line number. This switch is always implied; however, if specified after @code{-c} -it cancels the effect of @code{-c}. +or @code{-e} it cancels the effect of those switches. @item @code{-c} @@ -5041,6 +5058,12 @@ literal string, an Ada identifier or any Ada reserved word. A symbol declared with this switch replaces a symbol with the same name defined in a definition file. +@item @code{-e} + +Causes both preprocessor lines and the lines deleted by +preprocessing to be replaced by empty comment lines marked +with ‘@cite{–!}’ (and no other text) in the output source file, + @item @code{-s} Causes a sorted list of symbol names and values to be @@ -5088,14 +5111,25 @@ symbol with the same name either in a definition file or specified with a switch @code{-D} in the preprocessor data file. This switch is similar to switch @code{-D} of @code{gnatprep}. +@end table + +@geindex -gnateG (gcc) -@item @code{-gnateG} + +@table @asis + +@item @code{-gnateG[bce]} When integrated preprocessing is performed on source file @code{filename.extension}, create or overwrite @code{filename.extension.prep} to contain the result of the preprocessing. For example if the source file is @code{foo.adb} then the output file is @code{foo.adb.prep}. +An optional character (b, c, or e) can be appended to indicate that filtered +lines are to be replaced by blank lines, comments, or empty comments (see +documentation above about @code{-b}, @code{-c}, and @code{-e}). +If one of those switches is given in a preprocessor data file, then it +will override any option included with @code{-gnateG}. @end table @node Mixed Language Programming,GNAT and Other Compilation Models,Conditional Compilation,The GNAT Compilation Model @@ -9155,9 +9189,13 @@ information. @table @asis -@item @code{-gnateG} +@item @code{-gnateG[bce]} -Save result of preprocessing in a text file. +Save result of preprocessing in a text file. An optional character (b, c, +or e) can be appended to indicate that filtered lines are to be replaced by +blank lines, comment lines that include the filtered line, or empty comment +lines (”–!”), respectively. The default is to replace filtered lines with +blank lines. @end table @geindex -gnateH (gcc) @@ -30295,8 +30333,8 @@ to permit their use in free software. @printindex ge -@anchor{d2}@w{ } @anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{ } +@anchor{d2}@w{ } @c %**end of body @bye diff --git a/gcc/ada/gprep.adb b/gcc/ada/gprep.adb index 98e0072401e2..f40c38d8b145 100644 --- a/gcc/ada/gprep.adb +++ b/gcc/ada/gprep.adb @@ -735,7 +735,7 @@ package body GPrep is loop begin - Switch := GNAT.Command_Line.Getopt ("D: a b c C r s T u v"); + Switch := GNAT.Command_Line.Getopt ("D: a b c C e r s T u v"); case Switch is when ASCII.NUL => @@ -755,6 +755,9 @@ package body GPrep is when 'c' => Opt.Comment_Deleted_Lines := True; + when 'e' => + Opt.Empty_Comment_Deleted_Lines := True; + when 'C' => Opt.Replace_In_Comments := True; @@ -831,6 +834,7 @@ package body GPrep is Write_Line (" -c Keep preprocessor lines as comments"); Write_Line (" -C Do symbol replacements within comments"); Write_Line (" -D Associate symbol with value"); + Write_Line (" -e Replace preprocessor lines by empty comment lines"); Write_Line (" -r Generate Source_Reference pragma"); Write_Line (" -s Print a sorted list of symbol names and values"); Write_Line (" -T Use LF as line terminators"); diff --git a/gcc/ada/opt.ads b/gcc/ada/opt.ads index ea3390e2b482..0fd5475e1279 100644 --- a/gcc/ada/opt.ads +++ b/gcc/ada/opt.ads @@ -338,7 +338,7 @@ package Opt is -- True if echoed commands to be written to stdout instead of stderr Comment_Deleted_Lines : Boolean := False; - -- GNATPREP + -- GNAT, GNATPREP -- True if source lines removed by the preprocessor should be commented -- in the output file. @@ -509,6 +509,11 @@ package Opt is -- GNATBIND -- Set to True to output chosen elaboration order + Empty_Comment_Deleted_Lines : Boolean := False; + -- GNAT, GNATPREP + -- True if source lines removed by the preprocessor are to be replaced + -- by empty comment lines ("--!" and no other text) in the output file. + Enable_128bit_Types : Boolean := False; -- GNAT -- Set to True to enable the support for 128-bit types in the compiler. diff --git a/gcc/ada/prep.adb b/gcc/ada/prep.adb index 951719e2cdf8..7e58421b56f3 100644 --- a/gcc/ada/prep.adb +++ b/gcc/ada/prep.adb @@ -1210,6 +1210,13 @@ package body Prep is Output (From, To); end if; + New_EOL.all; + + elsif Empty_Comment_Deleted_Lines then + Put_Char ('-'); + Put_Char ('-'); + Put_Char ('!'); + New_EOL.all; end if; diff --git a/gcc/ada/prepcomp.adb b/gcc/ada/prepcomp.adb index 35dd4cbf53ae..f5ef40a0ff95 100644 --- a/gcc/ada/prepcomp.adb +++ b/gcc/ada/prepcomp.adb @@ -54,29 +54,31 @@ package body Prepcomp is pragma Warnings (On); type Preproc_Data is record - Mapping : Symbol_Table.Instance; - File_Name : File_Name_Type := No_File; - Deffile : String_Id := No_String; - Undef_False : Boolean := False; - Always_Blank : Boolean := False; - Comments : Boolean := False; - No_Deletion : Boolean := False; - List_Symbols : Boolean := False; - Processed : Boolean := False; + Mapping : Symbol_Table.Instance; + File_Name : File_Name_Type := No_File; + Deffile : String_Id := No_String; + Undef_False : Boolean := False; + Always_Blank : Boolean := False; + Comments : Boolean := False; + Empty_Comments : Boolean := False; + No_Deletion : Boolean := False; + List_Symbols : Boolean := False; + Processed : Boolean := False; end record; -- Structure to keep the preprocessing data for a file name or for the -- default (when Name_Id = No_Name). No_Preproc_Data : constant Preproc_Data := - (Mapping => No_Mapping, - File_Name => No_File, - Deffile => No_String, - Undef_False => False, - Always_Blank => False, - Comments => False, - No_Deletion => False, - List_Symbols => False, - Processed => False); + (Mapping => No_Mapping, + File_Name => No_File, + Deffile => No_String, + Undef_False => False, + Always_Blank => False, + Comments => False, + Empty_Comments => False, + No_Deletion => False, + List_Symbols => False, + Processed => False); Default_Data : Preproc_Data := No_Preproc_Data; -- The preprocessing data to be used when no specific preprocessing data @@ -170,6 +172,17 @@ package body Prepcomp is No_Preprocessing := False; Current_Data.Processed := True; + if Opt.Blank_Deleted_Lines then + Current_Data.Always_Blank := True; + + elsif Opt.Comment_Deleted_Lines then + Current_Data.Comments := True; + + elsif Opt.Empty_Comment_Deleted_Lines then + Current_Data.Empty_Comments := True; + + end if; + -- Start with an empty, initialized mapping table; use Prep.Mapping, -- because Prep.Index_Of uses Prep.Mapping. @@ -379,6 +392,15 @@ package body Prepcomp is OK := True; end if; + when 'e' => + + -- Replace removed lines with empty comment lines ("--!") + + if Name_Len = 1 then + Current_Data.Empty_Comments := True; + OK := True; + end if; + when 's' => -- List symbols @@ -601,12 +623,22 @@ package body Prepcomp is -- Set the preprocessing flags according to the preprocessing data - if Current_Data.Comments and not Current_Data.Always_Blank then - Comment_Deleted_Lines := True; - Blank_Deleted_Lines := False; + if Current_Data.Comments then + Comment_Deleted_Lines := True; + Empty_Comment_Deleted_Lines := False; + Blank_Deleted_Lines := False; + + elsif Current_Data.Always_Blank then + Comment_Deleted_Lines := False; + Empty_Comment_Deleted_Lines := False; + Blank_Deleted_Lines := True; + + -- By default, emit empty comment lines in place of deleted lines + else - Comment_Deleted_Lines := False; - Blank_Deleted_Lines := True; + Comment_Deleted_Lines := False; + Empty_Comment_Deleted_Lines := True; + Blank_Deleted_Lines := False; end if; No_Deletion := Current_Data.No_Deletion; diff --git a/gcc/ada/switch-c.adb b/gcc/ada/switch-c.adb index efad12c9c408..3b4375700c01 100644 --- a/gcc/ada/switch-c.adb +++ b/gcc/ada/switch-c.adb @@ -636,6 +636,46 @@ package body Switch.C is Generate_Processed_File := True; Ptr := Ptr + 1; + if Ptr <= Max + and then Switch_Chars (Ptr) in 'b' | 'c' | 'e' + then + case Switch_Chars (Ptr) is + when 'b' => + Opt.Blank_Deleted_Lines := True; + Opt.Comment_Deleted_Lines := False; + Opt.Empty_Comment_Deleted_Lines := False; + + when 'c' => + Opt.Blank_Deleted_Lines := False; + Opt.Comment_Deleted_Lines := True; + Opt.Empty_Comment_Deleted_Lines := False; + + when 'e' => + Opt.Blank_Deleted_Lines := False; + Opt.Comment_Deleted_Lines := False; + Opt.Empty_Comment_Deleted_Lines := True; + + when others => + raise Program_Error; + end case; + + Ptr := Ptr + 1; + + -- Default to emitting blank lines for deleted lines + -- when generating a preprocessor output file. This is + -- despite the fact that when the file isn't being + -- generated, we emit empty comment lines for the + -- internally generated output (to avoid conflicts + -- with style switches -gnatyu and -gnatyM), but is + -- done for compatibility with the behavior of -gnateG + -- prior to adding support for empty comment lines. + + else + Opt.Blank_Deleted_Lines := True; + Opt.Comment_Deleted_Lines := False; + Opt.Empty_Comment_Deleted_Lines := False; + end if; + -- -gnateH (set reverse Bit_Order threshold to 64) when 'H' => diff --git a/gcc/ada/usage.adb b/gcc/ada/usage.adb index 5b87bb54dcaa..e59f32448d17 100644 --- a/gcc/ada/usage.adb +++ b/gcc/ada/usage.adb @@ -196,8 +196,8 @@ begin -- Line for -gnateG switch - Write_Switch_Char ("eG"); - Write_Line ("Generate preprocessed source"); + Write_Switch_Char ("eG[?]"); + Write_Line ("Generate preprocessed source (?=b/c/e)"); -- Line for -gnateH switch