public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>
Subject: [committed] analyzer: only look for named functions in root ns [PR107788]
Date: Tue, 22 Nov 2022 17:37:11 -0500	[thread overview]
Message-ID: <20221122223711.3308884-1-dmalcolm@redhat.com> (raw)

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r13-4249-gec7c796de020cb.

gcc/analyzer/ChangeLog:
	PR analyzer/107788
	* known-function-manager.cc (known_function_manager::get_match):
	Don't look up fndecls by name when they're not in the root
	namespace.

gcc/testsuite/ChangeLog:
	PR analyzer/107788
	* g++.dg/analyzer/named-functions.C: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
 gcc/analyzer/known-function-manager.cc          | 15 ++++++++++++---
 gcc/testsuite/g++.dg/analyzer/named-functions.C | 12 ++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/analyzer/named-functions.C

diff --git a/gcc/analyzer/known-function-manager.cc b/gcc/analyzer/known-function-manager.cc
index e17350da5ec..c1074bcb6e5 100644
--- a/gcc/analyzer/known-function-manager.cc
+++ b/gcc/analyzer/known-function-manager.cc
@@ -91,6 +91,7 @@ known_function_manager::add (enum internal_fn ifn,
 const known_function *
 known_function_manager::get_match (tree fndecl, const call_details &cd) const
 {
+  /* Look for a matching built-in.  */
   if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
     {
       if (const known_function *candidate
@@ -99,10 +100,18 @@ known_function_manager::get_match (tree fndecl, const call_details &cd) const
 						    fndecl))
 	  return candidate;
     }
+
+  /* Look for a match by name.  */
+
+  /* Reject fndecls that aren't in the root namespace.  */
+  if (DECL_CONTEXT (fndecl)
+      && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
+    return NULL;
   if (tree identifier = DECL_NAME (fndecl))
-      if (const known_function *candidate = get_by_identifier (identifier))
-	if (candidate->matches_call_types_p (cd))
-	  return candidate;
+    if (const known_function *candidate = get_by_identifier (identifier))
+      if (candidate->matches_call_types_p (cd))
+	return candidate;
+
   return NULL;
 }
 
diff --git a/gcc/testsuite/g++.dg/analyzer/named-functions.C b/gcc/testsuite/g++.dg/analyzer/named-functions.C
new file mode 100644
index 00000000000..661a9307b81
--- /dev/null
+++ b/gcc/testsuite/g++.dg/analyzer/named-functions.C
@@ -0,0 +1,12 @@
+#define NULL ((void *)0)
+
+namespace my
+{
+  int socket (int, int, int);
+};
+
+void test_my_socket ()
+{
+  /* This shouldn't match the known function "::socket".  */
+  my::socket (0, 0, 0); /* { dg-bogus "leak" } */
+}
-- 
2.26.3


                 reply	other threads:[~2022-11-22 22:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221122223711.3308884-1-dmalcolm@redhat.com \
    --to=dmalcolm@redhat.com \
    --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).