public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: FX Coudert <fxcoudert@gmail.com>
To: GNU Fortran <fortran@gcc.gnu.org>,
	 gcc-patches list <gcc-patches@gcc.gnu.org>
Subject: [gfortran,patch] Add -fmodule-private option (PR 31629)
Date: Thu, 09 Aug 2007 22:19:00 -0000	[thread overview]
Message-ID: <A9B43069-A61F-42D2-B809-964B858A0B4A@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 531 bytes --]

The attached simple patch adds a -fmodule-private option to make  
module entities private as default, i.e. when you USE a module and  
compile with this option, you cannot refer to any variable that  
wasn't explicitly declared as PUBLIC. This is a tool and help for  
some people's coding style.

It's very simple as the code was already factored nicely, so it's in  
fact a one-liner (plus option-handling stuff, documentation and  
testcase). Bootstrapped and regtested on x86_64-linux, OK to commit?

FX



:ADDPATCH fortran:

[-- Attachment #2: module_private.ChangeLog --]
[-- Type: application/octet-stream, Size: 588 bytes --]

2007-08-10  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

	PR fortran/31629
	* lang.opt (-fmodule-private): New option.
	* gfortran.h (gfc_option_t): Add flag_module_private member.
	* invoke.texi (-fmodule-private): Document the new option.
	* module.c (gfc_check_access): Allow the -fmodule-private option
	to modify the default behaviour.
	* options.c (gfc_init_options): Initialize flag_module_private.
	(gfc_handle_option): Handle -fmodule-private.



2007-08-10  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

	PR fortran/31629
	* gfortran.dg/module_private_1.f90: New test.

[-- Attachment #3: module_private.diff --]
[-- Type: application/octet-stream, Size: 3791 bytes --]

Index: lang.opt
===================================================================
--- lang.opt	(revision 127224)
+++ lang.opt	(working copy)
@@ -212,6 +212,10 @@ fmax-stack-var-size=
 Fortran RejectNegative Joined UInteger
 -fmax-stack-var-size=<n>	Size in bytes of the largest array that will be put on the stack
 
+fmodule-private
+Fortran
+Set default accessibility of module entities to PRIVATE.
+
 fopenmp
 Fortran
 Enable OpenMP
Index: gfortran.h
===================================================================
--- gfortran.h	(revision 127224)
+++ gfortran.h	(working copy)
@@ -1865,6 +1865,7 @@ typedef struct
   int flag_d_lines;
   int flag_openmp;
   int flag_sign_zero;
+  int flag_module_private;
 
   int fpe;
 
Index: invoke.texi
===================================================================
--- invoke.texi	(revision 127228)
+++ invoke.texi	(working copy)
@@ -121,7 +121,7 @@ by type.  Explanations are in the follow
 -ffixed-line-length-@var{n}  -ffixed-line-length-none @gol
 -ffree-line-length-@var{n}  -ffree-line-length-none @gol
 -fdefault-double-8  -fdefault-integer-8  -fdefault-real-8 @gol
--fcray-pointer  -fopenmp  -frange-check -fno-backslash }
+-fcray-pointer  -fopenmp  -frange-check -fno-backslash -fmodule-private}
 
 @item Error and Warning Options
 @xref{Error and Warning Options,,Options to request or suppress errors
@@ -238,6 +238,14 @@ Allow @samp{$} as a valid character in a
 Change the interpretation of backslashes in string literals from
 ``C-style'' escape characters to a single backslash character.
 
+@item -fmodule-private
+@opindex @code{fmodule-private}
+@cindex module entities
+@cindex private
+Set the default accessibility of module entities to @code{PRIVATE}.
+Use-associated entities will not be accessible unless they are explicitly
+declared as @code{PUBLIC}.
+
 @item -ffixed-line-length-@var{n}
 @opindex @code{ffixed-line-length-}@var{n}
 @cindex file format, fixed
Index: module.c
===================================================================
--- module.c	(revision 127224)
+++ module.c	(working copy)
@@ -3714,7 +3714,10 @@ gfc_check_access (gfc_access specific_ac
   if (specific_access == ACCESS_PRIVATE)
     return FALSE;
 
-  return default_access != ACCESS_PRIVATE;
+  if (gfc_option.flag_module_private)
+    return default_access == ACCESS_PUBLIC;
+  else
+    return default_access != ACCESS_PRIVATE;
 }
 
 
Index: options.c
===================================================================
--- options.c	(revision 127224)
+++ options.c	(working copy)
@@ -93,6 +93,7 @@ gfc_init_options (unsigned int argc ATTR
   gfc_option.flag_preprocessed = 0;
   gfc_option.flag_automatic = 1;
   gfc_option.flag_backslash = 1;
+  gfc_option.flag_module_private = 0;
   gfc_option.flag_backtrace = 0;
   gfc_option.flag_allow_leading_underscore = 0;
   gfc_option.flag_dump_core = 0;
@@ -575,6 +576,10 @@ gfc_handle_option (size_t scode, const c
       gfc_option.flag_max_stack_var_size = value;
       break;
 
+    case OPT_fmodule_private:
+      gfc_option.flag_module_private = value;
+      break;
+      
     case OPT_frange_check:
       gfc_option.flag_range_check = value;
       break;
Index: gcc/testsuite/gfortran.dg/module_private_1.f90
===================================================================
--- gcc/testsuite/gfortran.dg/module_private_1.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/module_private_1.f90	(revision 0)
@@ -0,0 +1,20 @@
+! { dg-do compile }
+! { dg-options "-fmodule-private" }
+module bar
+  implicit none
+  public :: i
+  integer :: i
+end module bar
+
+module foo
+  implicit none
+  integer :: j
+end module foo
+
+program main
+  use bar, only : i
+  use foo, only : j ! { dg-error "not found in module" }
+  i = 1
+  j = 1
+  print *, i, j
+end program main

             reply	other threads:[~2007-08-09 22:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-09 22:19 FX Coudert [this message]
2007-08-10  5:46 ` Paul Thomas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A9B43069-A61F-42D2-B809-964B858A0B4A@gmail.com \
    --to=fxcoudert@gmail.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).