From patchwork Mon Feb 26 06:27:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Oskari Pirhonen X-Patchwork-Id: 86361 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 6EA1B3858C39 for ; Mon, 26 Feb 2024 06:28:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by sourceware.org (Postfix) with ESMTPS id 30BA03858D3C for ; Mon, 26 Feb 2024 06:27:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 30BA03858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 30BA03858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::530 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708928857; cv=none; b=O67msh86KGfbVy6qmB7TlBKlwvQTd7CWEbuV8T1TV2fKh7mk8W2LznslXq7urzgX+UFaC15OcRZl5wlWsU/yeCNh5MNThlCvKycavzDaREhD7xklLsRbogGqF+Hj2QJ7d17bOACd+AwAMCZ4Ae0qey4z7qn4lDtr2yL2doevHMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708928857; c=relaxed/simple; bh=8OinRbtzo9kSHNzpp2724pzEHZOikj/sCDFPXvdFPXs=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=kiyr4iYk9/yZPBokORmpDhjcFnL+ztKpz/muPgVe54dr1/3DcxhilrkqA6JY5DDWnk8sAWMxIdm+VRw+4W5FZaf73RhcdTz0fhNPxJ8328lHlXrHCNLPzAdxziHK30IJ4EjFisNw3Zc7jBMs31sC8J9Jjs0OhD405CmBHamHMJA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-565d1656c12so1549452a12.1 for ; Sun, 25 Feb 2024 22:27:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708928853; x=1709533653; darn=gcc.gnu.org; h=content-transfer-encoding:content-disposition:mime-version :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=vRXgy4hGTuAzpYV8OXIgX2I89wfp3/7oVfcyCYxc/AY=; b=jPfR9403ioq3GJbn2e8msQnkxhFOMR7k7o87nt+tLScH/gKMi6hCL5QpU0lblXZaGX iC7IdYZIyCCBNBpkeJ3Q/lrVu7HwerYBOdkUHFclKGb0cnu5GzBb1te89ogZvB0OncjX C6a7//JgrC9jnqxqgK44i6Noc/8O5dIQy5kSwPh3RdjAffzBj5dEDjUNLuNkTBudkocE lCL9cKg/mJWJlehSeQkgrYF+zwJbcIJbunP5dYxQ9Fko/6coBdIIwwY2MW3go8YNO0d3 g5c0F9MBFKc8KXa6/P2KurD604DpYOjdZE+HSOLGd4aLZolRvARuDJ1fDCoBI4fKPgha L9Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708928853; x=1709533653; h=content-transfer-encoding:content-disposition:mime-version :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vRXgy4hGTuAzpYV8OXIgX2I89wfp3/7oVfcyCYxc/AY=; b=nUTbtVssZnt7WqTPOvXkRoawTT1464+Tx1CyURuUR7JWapxOL0SOLgDhp7SP0ZvUqR XJuyjnpdVPzh8whokNRyaPAzHjSk6xg7c8vhinpt+C9m0l8HmXUNpgmrq1nmxdKHt8UQ jioYf+3CkPSm8X93wpt93BhMDOSYkd7B7I7xlCQnzXikazB01Kxnh8aRxHfrMUU0lT1F VwtZ3Luj26ehMNznKhPimUx2yS2+HVxvJXVvfmkxia27q6GH2qWuOWYZzfq3m6b6Ty6B gmF8BaX89QyzErTlp79lUHvl3JLjOz7dJzYTaZxxgxnJFEwIS9nZbbgVSWdMoI3p0JBN c6/A== X-Gm-Message-State: AOJu0YxvQ0cVHuDq8Cy9LV8GaAkREfTMjuRuiLXYyAYv+9Z9yBukoRTj JJlBlvPZLm/8OQF1jFTW30C7/3WBqDhV5n3X5WObcidxfJT9YxVug7h/IgZr X-Google-Smtp-Source: AGHT+IGnepSr38CgrljjPOIfXlXIDkPdc524eMTFPATEHxkuMmPETCbALYkRWTC7M7luqqC7KInK/Q== X-Received: by 2002:aa7:d4d4:0:b0:565:9f59:b3b6 with SMTP id t20-20020aa7d4d4000000b005659f59b3b6mr4783396edr.3.1708928853127; Sun, 25 Feb 2024 22:27:33 -0800 (PST) Received: from dj3ntoo (30.sub-97-147-14.myvzw.com. [97.147.14.30]) by smtp.gmail.com with ESMTPSA id h10-20020aa7c94a000000b00565dd45b53esm1011821edt.39.2024.02.25.22.27.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 22:27:32 -0800 (PST) Date: Mon, 26 Feb 2024 00:27:27 -0600 From: Oskari Pirhonen To: gcc-patches@gcc.gnu.org Cc: dodji@redhat.com, dmalcolm@redhat.com Subject: [RESEND PATCH] C/C++: add hints for strerror Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Add proper hints for implicit declaration of strerror. The results could be confusing depending on the other included headers. These example messages are from compiling a trivial program to print the string for an errno value. It only includes stdio.h (cstdio for C++). Before: $ /tmp/gcc-master/bin/gcc test.c -o test_c test.c: In function ‘main’: test.c:4:20: warning: implicit declaration of function ‘strerror’; did you mean ‘perror’? [-Wimplicit-function-declaration] 4 | printf("%s\n", strerror(0)); | ^~~~~~~~ | perror $ /tmp/gcc-master/bin/g++ test.cpp -o test_cpp test.cpp: In function ‘int main()’: test.cpp:4:20: error: ‘strerror’ was not declared in this scope; did you mean ‘stderr’? 4 | printf("%s\n", strerror(0)); | ^~~~~~~~ | stderr After: $ /tmp/gcc-known-headers/bin/gcc test.c -o test_c test.c: In function ‘main’: test.c:4:20: warning: implicit declaration of function ‘strerror’ [-Wimplicit-function-declaration] 4 | printf("%s\n", strerror(0)); | ^~~~~~~~ test.c:2:1: note: ‘strerror’ is defined in header ‘’; this is probably fixable by adding ‘#include ’ 1 | #include +++ |+#include 2 | $ /tmp/gcc-known-headers/bin/g++ test.cpp -o test_cpp test.cpp: In function ‘int main()’: test.cpp:4:20: error: ‘strerror’ was not declared in this scope 4 | printf("%s\n", strerror(0)); | ^~~~~~~~ test.cpp:2:1: note: ‘strerror’ is defined in header ‘’; this is probably fixable by adding ‘#include ’ 1 | #include +++ |+#include 2 | gcc/c-family/ChangeLog: * known-headers.cc (get_stdlib_header_for_name): Add strerror. gcc/testsuite/ChangeLog: * g++.dg/spellcheck-stdlib.C: Add check for strerror. * gcc.dg/spellcheck-stdlib-2.c: New test. Signed-off-by: Oskari Pirhonen --- gcc/c-family/known-headers.cc | 1 + gcc/testsuite/g++.dg/spellcheck-stdlib.C | 2 ++ gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c | 8 ++++++++ 3 files changed, 11 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc index dbc42eacde1..871fd714eb5 100644 --- a/gcc/c-family/known-headers.cc +++ b/gcc/c-family/known-headers.cc @@ -182,6 +182,7 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib) {"strchr", {"", ""} }, {"strcmp", {"", ""} }, {"strcpy", {"", ""} }, + {"strerror", {"", ""} }, {"strlen", {"", ""} }, {"strncat", {"", ""} }, {"strncmp", {"", ""} }, diff --git a/gcc/testsuite/g++.dg/spellcheck-stdlib.C b/gcc/testsuite/g++.dg/spellcheck-stdlib.C index fd0f3a9b8c9..33718b8034e 100644 --- a/gcc/testsuite/g++.dg/spellcheck-stdlib.C +++ b/gcc/testsuite/g++.dg/spellcheck-stdlib.C @@ -104,6 +104,8 @@ void test_cstring (char *dest, char *src) // { dg-message "'#include '" "" { target *-*-* } .-1 } strcpy(dest, "test"); // { dg-error "was not declared" } // { dg-message "'#include '" "" { target *-*-* } .-1 } + strerror(0); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } strlen("test"); // { dg-error "was not declared" } // { dg-message "'#include '" "" { target *-*-* } .-1 } strncat(dest, "test", 3); // { dg-error "was not declared" } diff --git a/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c b/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c new file mode 100644 index 00000000000..61c17f350cb --- /dev/null +++ b/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c @@ -0,0 +1,8 @@ +/* { dg-options "-Wimplicit-function-declaration" } */ + +/* Missing . */ +void test_string_h (void) +{ + strerror (0); /* { dg-warning "implicit declaration of function 'strerror'" } */ + /* { dg-message "'strerror' is defined in header ''" "" { target *-*-* } .-1 } */ +}