invoke.texi (Option Summary): Mention new option -Wdisallowed-function-list=...
authorRaksit Ashok <raksit@google.com>
Wed, 9 Jul 2008 08:23:48 +0000 (08:23 +0000)
committerRaksit Ashok <raksit@gcc.gnu.org>
Wed, 9 Jul 2008 08:23:48 +0000 (08:23 +0000)
2008-07-09  Raksit Ashok <raksit@google.com>

* doc/invoke.texi (Option Summary): Mention new option
-Wdisallowed-function-list=...
(Warning Options): Document -Wdisallowed-function-list=...
* common.opt (Wdisallowed-function-list=): New flag.
* flags.h (warn_disallowed_functions): External definition of new
boolean warning flag.
(warn_if_disallowed_function_p): Declare new function.
* opts.c (warning_disallowed_functions): New static variable.
(warn_disallowed_functions): New boolean warning flag.
(warn_if_disallowed_function_p): New function.
(add_comma_separated_to_vector): Rename
add_instrument_functions_exclude_list to this.
(common_handle_option): Handle new option. Rename calls to
add_instrument_functions_exclude_list into calls to
add_comma_separated_to_vector.
* c-parser.c (c_parser_postfix_expression_after_primary): New warning
based on flag warn_disallowed_functions.

gcc/cp/ChangeLog

2008-07-09  Raksit Ashok <raksit@google.com>

* parser.c (cp_parser_postfix_expression): New warning based on flag
warn_disallowed_functions.

gcc/testsuite/ChangeLog

2008-07-09  Raksit Ashok <raksit@google.com>

* gcc.dg/wdisallowed-functions-1.c: New test.
* gcc.dg/wdisallowed-functions-2.c: New test.
* g++.dg/warn/Wdisallowed-functions-1.C: New test.
* g++.dg/warn/Wdisallowed-functions-2.C: New test.

From-SVN: r137655

13 files changed:
gcc/ChangeLog
gcc/c-parser.c
gcc/common.opt
gcc/cp/ChangeLog
gcc/cp/parser.c
gcc/doc/invoke.texi
gcc/flags.h
gcc/opts.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wdisallowed-functions-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wdisallowed-functions-2.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/wdisallowed-functions-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/wdisallowed-functions-2.c [new file with mode: 0644]

index 5fe59fe673a5e8a731ef97418e55b82fb1ee9b02..29576bbc7ad1b417cb352e6161d5f97066d2c623 100644 (file)
@@ -1,3 +1,23 @@
+2008-07-09  Raksit Ashok <raksit@google.com>
+
+       * doc/invoke.texi (Option Summary): Mention new option
+       -Wdisallowed-function-list=...
+       (Warning Options): Document -Wdisallowed-function-list=...
+       * common.opt (Wdisallowed-function-list=): New flag.
+       * flags.h (warn_disallowed_functions): External definition of new
+       boolean warning flag.
+       (warn_if_disallowed_function_p): Declare new function.
+       * opts.c (warning_disallowed_functions): New static variable.
+       (warn_disallowed_functions): New boolean warning flag.
+       (warn_if_disallowed_function_p): New function.
+       (add_comma_separated_to_vector): Rename
+       add_instrument_functions_exclude_list to this.
+       (common_handle_option): Handle new option. Rename calls to
+       add_instrument_functions_exclude_list into calls to
+       add_comma_separated_to_vector.
+       * c-parser.c (c_parser_postfix_expression_after_primary): New warning
+       based on flag warn_disallowed_functions.
+
 2008-07-09  Christian Bruel  <christian.bruel@st.com>
 
        * final.c (get_attr_length_1): Call get_attr_length_1 with fallback_fn
index 45aff80d6c04929bbe88d7b542e45ee2987ce156..4ca06fec414e22404e9133f05919c44ed66a60ac 100644 (file)
@@ -5630,6 +5630,8 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
                                     "expected %<)%>");
          expr.value = build_function_call (expr.value, exprlist);
          expr.original_code = ERROR_MARK;
+          if (warn_disallowed_functions)
+            warn_if_disallowed_function_p (expr.value);
          break;
        case CPP_DOT:
          /* Structure element reference.  */
index 200425d3128bdd0a1290735d7b759163276f6193..28a5af25dd984ccd5b824b41a3fb927d28f5753c 100644 (file)
@@ -94,6 +94,10 @@ Wdisabled-optimization
 Common Var(warn_disabled_optimization) Warning
 Warn when an optimization pass is disabled
 
+Wdisallowed-function-list=
+Common RejectNegative Joined Warning
+Warn on calls to these functions
+
 Werror
 Common Var(warnings_are_errors)
 Treat all warnings as errors
index 75435cffda6008f74aba48f8cdc6b2d5b5a956ea..fa4419e998a472e7722a133dd7e818cfe4a661cd 100644 (file)
@@ -1,3 +1,8 @@
+2008-07-09  Raksit Ashok <raksit@google.com>
+
+       * parser.c (cp_parser_postfix_expression): New warning based on flag
+       warn_disallowed_functions.
+
 2008-07-08  Simon Martin  <simartin@users.sourceforge.net>
 
        PR c++/34963
index 2323c6721665886ac4430ea8e18c9a4b9d1d9776..767d14d2a6199cb92f9b3d0d6a125da7f54ee077 100644 (file)
@@ -4725,6 +4725,9 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
                                    koenig_p,
                                    tf_warning_or_error);
 
+            if (warn_disallowed_functions)
+              warn_if_disallowed_function_p (postfix_expression);
+
            /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
            idk = CP_ID_KIND_NONE;
          }
index 689f8381e1b744bb1e15a3faac025ad0f3690c45..6122f791b680a0a2d986ebce42bc1bbdffd27fba 100644 (file)
@@ -231,8 +231,9 @@ Objective-C and Objective-C++ Dialects}.
 -Wno-attributes -Wc++-compat -Wc++0x-compat -Wcast-align  -Wcast-qual  @gol
 -Wchar-subscripts -Wclobbered  -Wcomment @gol
 -Wconversion  -Wcoverage-mismatch  -Wno-deprecated  @gol
--Wno-deprecated-declarations -Wdisabled-optimization  -Wno-div-by-zero  @gol
--Wempty-body  -Wenum-compare -Wno-endif-labels @gol
+-Wno-deprecated-declarations -Wdisabled-optimization  @gol
+-Wdisallowed-function-list=@var{sym},@var{sym},@dots{} @gol
+-Wno-div-by-zero -Wempty-body  -Wenum-compare -Wno-endif-labels @gol
 -Werror  -Werror=* @gol
 -Wfatal-errors  -Wfloat-equal  -Wformat  -Wformat=2 @gol
 -Wno-format-contains-nul -Wno-format-extra-args -Wformat-nonliteral @gol
@@ -4101,6 +4102,13 @@ minimum maximum, so we do not diagnose overlength strings in C++@.
 
 This option is implied by @option{-pedantic}, and can be disabled with
 @option{-Wno-overlength-strings}.
+
+@item -Wdisallowed-function-list=@var{sym},@var{sym},@dots{}
+@opindex Wdisallowed-function-list
+
+If any of @var{sym} is called, GCC will issue a warning. This can be useful
+in enforcing coding conventions that ban calls to certain functions, for
+example, @code{alloca}, @code{malloc}, etc.
 @end table
 
 @node Debugging Options
index 1818a42c5caa76e4deb22c9e79425da5eb0915c2..edc96e95920c789d31f648499929ec8b1b5dc6f3 100644 (file)
@@ -279,6 +279,13 @@ extern bool flag_speculative_prefetching_set;
    instrumentation.  */
 extern bool flag_instrument_functions_exclude_p (tree fndecl);
 
+/* Emit warning if the function call is disallowed under
+   -Wdisallowed-function-list=...  */
+extern void warn_if_disallowed_function_p (const_tree fncall);
+
+/* True, if the -Wdisallowed-function-list=... option has been specified.  */
+extern bool warn_disallowed_functions;
+
 /* True if the given mode has a NaN representation and the treatment of
    NaN operands is important.  Certain optimizations, such as folding
    x * 0 into 0, are not correct for NaN operands, and are normally
index c26260acdd762d2efdd0f81f53147e10107a058d..8431a406bd7ac2c19749114dca11c19ef2e09e50 100644 (file)
@@ -371,6 +371,12 @@ DEF_VEC_ALLOC_P(const_char_p,heap);
 
 static VEC(const_char_p,heap) *ignored_options;
 
+/* Function calls disallowed under -Wdisallowed-function-list=...  */
+static VEC(char_p,heap) *warning_disallowed_functions;
+
+/* If -Wdisallowed-function-list=...  */
+bool warn_disallowed_functions = false;
+
 /* Input file names.  */
 const char **in_fnames;
 unsigned num_in_fnames;
@@ -655,12 +661,10 @@ add_input_filename (const char *filename)
   in_fnames[num_in_fnames - 1] = filename;
 }
 
-/* Add functions or file names to a vector of names to exclude from
-   instrumentation.  */
+/* Add comma-separated strings to a char_p vector.  */
 
 static void
-add_instrument_functions_exclude_list (VEC(char_p,heap) **pvec,
-                                      const char* arg)
+add_comma_separated_to_vector (VEC(char_p,heap) **pvec, const char* arg)
 {
   char *tmp;
   char *r;
@@ -736,6 +740,31 @@ flag_instrument_functions_exclude_p (tree fndecl)
   return false;
 }
 
+
+/* Return whether this function call is disallowed.  */
+void
+warn_if_disallowed_function_p (const_tree exp)
+{
+  if (TREE_CODE(exp) == CALL_EXPR
+      && VEC_length (char_p, warning_disallowed_functions) > 0)
+    {
+      int i;
+      char *s;
+      const char *fnname =
+          IDENTIFIER_POINTER (DECL_NAME (get_callee_fndecl (exp)));
+      for (i = 0; VEC_iterate (char_p, warning_disallowed_functions, i, s);
+           ++i)
+        {
+          if (strcmp (fnname, s) == 0)
+            {
+              warning (OPT_Wdisallowed_function_list_,
+                       "disallowed call to %qs", fnname);
+              break;
+            }
+        }
+    }
+}
+
 /* Decode and handle the vector of command line options.  LANG_MASK
    contains has a single bit set representing the current
    language.  */
@@ -1541,6 +1570,12 @@ common_handle_option (size_t scode, const char *arg, int value,
       set_Wextra (value);
       break;
 
+    case OPT_Wdisallowed_function_list_:
+      warn_disallowed_functions = true;
+      add_comma_separated_to_vector
+       (&warning_disallowed_functions, arg);
+      break;
+
     case OPT_Werror_:
       enable_warning_as_error (arg, value, lang_mask);
       break;
@@ -1691,12 +1726,12 @@ common_handle_option (size_t scode, const char *arg, int value,
       break;
 
     case OPT_finstrument_functions_exclude_function_list_:
-      add_instrument_functions_exclude_list
+      add_comma_separated_to_vector
        (&flag_instrument_functions_exclude_functions, arg);
       break;
 
     case OPT_finstrument_functions_exclude_file_list_:
-      add_instrument_functions_exclude_list
+      add_comma_separated_to_vector
        (&flag_instrument_functions_exclude_files, arg);
       break;
 
index 07b3827c2e71fbbd3c38c31680eb2894bd28840a..755f8e4719c956aa60b9dfe918cd28474a94188f 100644 (file)
@@ -1,3 +1,10 @@
+2008-07-09  Raksit Ashok <raksit@google.com>
+
+       * gcc.dg/wdisallowed-functions-1.c: New test.
+       * gcc.dg/wdisallowed-functions-2.c: New test.
+       * g++.dg/warn/Wdisallowed-functions-1.C: New test.
+       * g++.dg/warn/Wdisallowed-functions-2.C: New test.
+
 2008-07-08  Simon Martin  <simartin@users.sourceforge.net>
 
        PR c++/34963
diff --git a/gcc/testsuite/g++.dg/warn/Wdisallowed-functions-1.C b/gcc/testsuite/g++.dg/warn/Wdisallowed-functions-1.C
new file mode 100644 (file)
index 0000000..3976095
--- /dev/null
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-Wdisallowed-function-list=foobar" } */
+
+int foobar (int i)
+{
+  return (i * 5);
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wdisallowed-functions-2.C b/gcc/testsuite/g++.dg/warn/Wdisallowed-functions-2.C
new file mode 100644 (file)
index 0000000..ca5aab0
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-Wdisallowed-function-list=foo,foobar,bar,foobar" } */
+
+int foobar (int i)
+{
+  return (i * 5);
+}
+
+int foobar1 (int i)
+{
+  return foobar (i);  /* { dg-warning "disallowed call to 'foobar'" } */
+}
diff --git a/gcc/testsuite/gcc.dg/wdisallowed-functions-1.c b/gcc/testsuite/gcc.dg/wdisallowed-functions-1.c
new file mode 100644 (file)
index 0000000..3976095
--- /dev/null
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-Wdisallowed-function-list=foobar" } */
+
+int foobar (int i)
+{
+  return (i * 5);
+}
diff --git a/gcc/testsuite/gcc.dg/wdisallowed-functions-2.c b/gcc/testsuite/gcc.dg/wdisallowed-functions-2.c
new file mode 100644 (file)
index 0000000..ca5aab0
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-Wdisallowed-function-list=foo,foobar,bar,foobar" } */
+
+int foobar (int i)
+{
+  return (i * 5);
+}
+
+int foobar1 (int i)
+{
+  return foobar (i);  /* { dg-warning "disallowed call to 'foobar'" } */
+}