From patchwork Tue Nov 22 22:37:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 60994 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 931BF384F4BF for ; Tue, 22 Nov 2022 22:37:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 931BF384F4BF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669156666; bh=zFExKOCzx0E9JzUH7svZhXxf24uktveZxyV4lfTHB0A=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ersm+ieuUehC6sUlsPKUHLCx14HzX0KWVKvr49XHf+juEUrmSD9lv00PlaKJK0PGM 3SR4Qea8ioA3GakSG0wA0rwd3XWr+xWDJP4QnTvPy9+C/weLGR/s2RSsVMZGFA/vT8 DhMrMyiy9J4fEaSSW3SsQi9x16fY6ooO0o3/tf5w= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 571A2385781A for ; Tue, 22 Nov 2022 22:37:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 571A2385781A Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-43-g1AhFmmpNnK9Ed1jwD5Fqg-1; Tue, 22 Nov 2022 17:37:13 -0500 X-MC-Unique: g1AhFmmpNnK9Ed1jwD5Fqg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6DC96858F17 for ; Tue, 22 Nov 2022 22:37:13 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.16.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4ADEF112132D; Tue, 22 Nov 2022 22:37:13 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed] analyzer: only look for named functions in root ns [PR107788] Date: Tue, 22 Nov 2022 17:37:11 -0500 Message-Id: <20221122223711.3308884-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 --- 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" } */ +}