Patchwork [Ada] More complete style checks for specs

login
register
mail settings
Submitter Arnaud Charlet
Date April 12, 2013, 12:58 p.m.
Message ID <20130412125812.GA16301@adacore.com>
Download mbox | patch
Permalink /patch/236082/
State New
Headers show

Comments

Arnaud Charlet - April 12, 2013, 12:58 p.m.
This patch corrects some cases of missing style checks on specs when
compiling the body. If the spec of the main unit is explicitly with'ed
from an indirect chain of with's, then style checks for this spec
were missed. This patch corrects this, so will have the effect of
catching previously missed style errors.

Consider the following program:

     1. with SCaseP;
     2. package SCaseQ is
     3.    procedure Proc;
     4. end SCaseQ;

     1. package SCaseP is
     2.    Var : Integer;
     3.    type Value_Number is access all Integer;
     4.    procedure External_Value_Rec (Addr : Value_number);
     5.    Val : Integer := var;
     6. end SCaseP;

     1. with SCaseQ;
     2. package body SCaseP is
     3.    V : Integer := Var;
     4.    procedure External_Value_Rec (Addr : Value_number) is
     5.    begin
     6.       null;
     7.    end External_Value_Rec;
     8. end SCaseP;

With the patch in place, if we compile the body of SCaseP with
-gnatyr, we get:

Compiling: scasep.adb

     1. with SCaseQ;
     2. package body SCaseP is
     3.    V : Integer := Var;
     4.    procedure External_Value_Rec (Addr : Value_number) is
                                                      |
        >>> (style) bad casing of "Value_Number" declared at scasep.ads:3

     5.    begin
     6.       null;
     7.    end External_Value_Rec;
     8. end SCaseP;

Compiling: scasep.ads

     1. package SCaseP is
     2.    Var : Integer;
     3.    type Value_Number is access all Integer;
     4.    procedure External_Value_Rec (Addr : Value_number);
                                                      |
        >>> (style) bad casing of "Value_Number" declared at line 3

     5.    Val : Integer := var;
                            |
        >>> (style) bad casing of "Var" declared at line 2

     6. end SCaseP;

Previous to this patch, this compilation lost the two warnings on
lines 4 and 5 of the spec in file scalsep.ads, and these style
warnings are clearly correct!

Tested on x86_64-pc-linux-gnu, committed on trunk

2013-04-12  Robert Dewar  <dewar@adacore.com>

	* opt.ads (Style_Check_Main): New switch.
	* sem.adb (Semantics): Set Style_Check flag properly for new
	unit to be analyzed.
	* sem_ch10.adb (Analyze_With_Clause): Don't reset Style_Check,
	the proper setting of this flag is now part of the Semantics
	procedure.
	* switch-c.adb (Scan_Front_End_Switches): Set Style_Check_Main
	for -gnatg and -gnaty

Patch

Index: switch-c.adb
===================================================================
--- switch-c.adb	(revision 197899)
+++ switch-c.adb	(working copy)
@@ -751,6 +751,7 @@ 
                Identifier_Character_Set := 'n';
                System_Extend_Unit := Empty;
                Warning_Mode := Treat_As_Error;
+               Style_Check_Main := True;
 
                --  Set Ada 2012 mode explicitly. We don't want to rely on the
                --  implicit setting here, since for example, we want
@@ -1173,6 +1174,7 @@ 
 
             when 'y' =>
                Ptr := Ptr + 1;
+               Style_Check_Main := True;
 
                if Ptr > Max then
                   Set_Default_Style_Check_Options;
Index: sem_ch10.adb
===================================================================
--- sem_ch10.adb	(revision 197899)
+++ sem_ch10.adb	(working copy)
@@ -2457,14 +2457,6 @@ 
          return;
       end if;
 
-      --  We reset ordinary style checking during the analysis of a with'ed
-      --  unit, but we do NOT reset GNAT special analysis mode (the latter
-      --  definitely *does* apply to with'ed units).
-
-      if not GNAT_Mode then
-         Style_Check := False;
-      end if;
-
       --  If the library unit is a predefined unit, and we are in high
       --  integrity mode, then temporarily reset Configurable_Run_Time_Mode
       --  for the analysis of the with'ed unit. This mode does not prevent
Index: sem.adb
===================================================================
--- sem.adb	(revision 197899)
+++ sem.adb	(working copy)
@@ -1311,6 +1311,7 @@ 
       S_In_Spec_Expr      : constant Boolean          := In_Spec_Expression;
       S_Inside_A_Generic  : constant Boolean          := Inside_A_Generic;
       S_Outer_Gen_Scope   : constant Entity_Id        := Outer_Generic_Scope;
+      S_Style_Check       : constant Boolean          := Style_Check;
 
       Generic_Main : constant Boolean :=
                        Nkind (Unit (Cunit (Main_Unit)))
@@ -1318,6 +1319,10 @@ 
       --  If the main unit is generic, every compiled unit, including its
       --  context, is compiled with expansion disabled.
 
+      Ext_Main_Source_Unit : constant Boolean :=
+                               In_Extended_Main_Source_Unit (Comp_Unit);
+      --  Determine if unit is in extended main source unit
+
       Save_Config_Switches : Config_Switches_Type;
       --  Variable used to save values of config switches while we analyze the
       --  new unit, to be restored on exit for proper recursive behavior.
@@ -1386,9 +1391,6 @@ 
       --  Sequential_IO) as this would prevent pragma Extend_System from being
       --  taken into account, for example when Text_IO is renaming DEC.Text_IO.
 
-      --  Cleaner might be to do the kludge at the point of excluding the
-      --  pragma (do not exclude for renamings ???)
-
       if Is_Predefined_File_Name
            (Unit_File_Name (Current_Sem_Unit), Renamings_Included => False)
       then
@@ -1423,12 +1425,28 @@ 
       --  For unit in main extended unit, we reset the configuration values
       --  for the non-partition-wide restrictions. For other units reset them.
 
-      if In_Extended_Main_Source_Unit (Comp_Unit) then
+      if Ext_Main_Source_Unit then
          Restore_Config_Cunit_Boolean_Restrictions;
       else
          Reset_Cunit_Boolean_Restrictions;
       end if;
 
+      --  Turn off style checks for unit that is not in the extended main
+      --  source unit. This improves processing efficiency for such units
+      --  (for which we don't want style checks anyway, and where they will
+      --  get suppressed), and is definitely needed to stop some style checks
+      --  from invading the run-time units (e.g. overriding checks).
+
+      if not Ext_Main_Source_Unit then
+         Style_Check := False;
+
+      --  If this is part of the extended main source unit, set style check
+      --  mode to match the style check mode of the main source unit itself.
+
+      else
+         Style_Check := Style_Check_Main;
+      end if;
+
       --  Only do analysis of unit that has not already been analyzed
 
       if not Analyzed (Comp_Unit) then
@@ -1482,6 +1500,7 @@ 
       In_Spec_Expression   := S_In_Spec_Expr;
       Inside_A_Generic     := S_Inside_A_Generic;
       Outer_Generic_Scope  := S_Outer_Gen_Scope;
+      Style_Check          := S_Style_Check;
 
       Restore_Opt_Config_Switches (Save_Config_Switches);
 
Index: opt.ads
===================================================================
--- opt.ads	(revision 197901)
+++ opt.ads	(working copy)
@@ -1267,8 +1267,16 @@ 
    --  GNAT
    --  Set True to perform style checks. Activates checks carried out in
    --  package Style (see body of this package for details of checks). This
-   --  flag is set True by either the -gnatg or -gnaty switches.
+   --  flag is set True by use of either the -gnatg or -gnaty switches, or
+   --  by the Style_Check pragma.
 
+   Style_Check_Main : Boolean := False;
+   --  GNAT
+   --  Set True if Style_Check was set for the main unit. This is used to
+   --  renable style checks for units in the mail extended source that get
+   --  with'ed indirectly. It is set on by use of either the -gnatg or -gnaty
+   --  switches, but not by use of the Style_Checks pragma.
+
    Suppress_All_Inlining : Boolean := False;
    --  GNAT
    --  Set by -fno-inline. Suppresses all inlining, both front end and back end