From patchwork Mon Oct 2 02:14:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Oskari Pirhonen X-Patchwork-Id: 147183 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1148570vqb; Sun, 1 Oct 2023 19:15:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF5RjbouYQTYH2siSooWQF8ck2vbG1usWLNnsJYS8QVmvGUgWcZhsbB9O2WfhRLzDUG1pL5 X-Received: by 2002:a17:906:1099:b0:9ae:62ec:f4a1 with SMTP id u25-20020a170906109900b009ae62ecf4a1mr8422329eju.33.1696212932623; Sun, 01 Oct 2023 19:15:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696212932; cv=none; d=google.com; s=arc-20160816; b=dg9JEpNPKnD6ZBgfV8RFIabGX15lfJjEjXq8+VL5jZ24uanDKq6So2SiMvyeK9H4w4 IJvI8WBtNFV5SiLLIep4JHRP8ykwrdhl7mXjIZ42HTE1xvtZqlWRXnWuV04Ju4HqpJRf 48r0+CaK+PNAcehw4QKI8MTjI+x95j7PHja2pPvyXKGKTYz32UVd7xyVL/2An/18ZxFf qIVTJcOyQ72iKPGTJmT3PWrqKrMPXptME7Q7BcZfezM3J/c9vHbgtfQGx+Cx4dfSUVan NMuTuDdAEk5/hTQyWgX8g54REhQ5jTkcNUNrva1ZDTKqtNQrWkaTSfZZj0i//Mfp0J4r 5vYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :content-disposition:mime-version:message-id:subject:to:from:date :dkim-signature:dmarc-filter:delivered-to; bh=pyc5vmJiZr+lTf59UvmaDnuUL+E5aICbAZ0JeLul15k=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=cXJA3nQilaBkSir2Y1CjxzO7P00cKmfysKfuQ5dOoSDYZrFZIr1+S41w9ZKw1/5fVg gnvZSgKnVNPzbISKqC6UBAqDFurSg/TGywTgdqsSyRW+QgYySqh8WKy1IM745g1suB4f NXydCV3Q1bwfx3E1KOyhTvz2W4IOp2xPZupLdj9Ybtf2mofgnFbKP+wCDd80V6QniyWe /gCfmOR41GDKm2oW3jLFu5+C3ZvMps4d0y66bM+xyRJIMWdT/0LVgxwC4nNkncoJ7DHb GUHc7XXlWSMLEBuOQ61wEOP1c1Csd51lgKHoQLeYHOhOHE8qjthBcK85YcJ4iG/q9eAF dQwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KRRoSw6R; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n7-20020a170906118700b0099cc98af80fsi19117793eja.552.2023.10.01.19.15.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 19:15:32 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KRRoSw6R; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E3486385DC30 for ; Mon, 2 Oct 2023 02:15:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 708A23858D35 for ; Mon, 2 Oct 2023 02:14:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 708A23858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-9ad8a822508so2247931866b.0 for ; Sun, 01 Oct 2023 19:14:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696212891; x=1696817691; darn=gcc.gnu.org; h=content-transfer-encoding:content-disposition:mime-version :message-id:subject:to:from:date:from:to:cc:subject:date:message-id :reply-to; bh=pyc5vmJiZr+lTf59UvmaDnuUL+E5aICbAZ0JeLul15k=; b=KRRoSw6R4OSCLQBe2kquzpcOAXWhYXVUbfJE9MLzLr96Q5d7ODG6RxBZ4KD/ZDNTTK Nc/jY9t0WcZMrAsoNrIzeKXawQ8xoubERC9qnyy+wBYgGvLxVaZ/zMmGgXN3toDSfVbO eExg5Q5avTmtIvR2efI12Pw+9zCyLsyVbZoWiR0B2EDiBUsmFjc4KKX6wKGzGIeLAWD5 s9EqaK61K6AcTleKklCePD5lQgDVWObyWBcFoYs37ipQ0Mc22F4YuC6rk+rBbWDyL+pl +bmowuBlFV4rp7ve1mMmGqihtENIeOrRKpEYc/KT4mIBVVHYAQFPoMQTe3jViDtNE1st dRlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696212891; x=1696817691; h=content-transfer-encoding:content-disposition:mime-version :message-id:subject:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pyc5vmJiZr+lTf59UvmaDnuUL+E5aICbAZ0JeLul15k=; b=CacBPpZ0Ts6FBeyr3vipVYXmtJFI/yEbL/UQHVd0brFlyHqOjZT2/BEBGHSCKseVTE 0oniDbJa9PC1+3FaD1UIgighB8vzSUAct+ndy5EAHTUsSnBQsakN1+aYD66Q55zWI+Np gLN6g6nDCxNteDS/SBzLDKk6QIQIy14U7ux1TM21d4T5+qDcFnqpezHqOcRDk7LQMLi+ un8PtUipaO4gDMvfXywe6zbLSmrTmPiO7t6mi88lAbpm3N1DU+tec3JRPJDxNeKYJBJs rIxr/VpnRx+lWvDtEqrf75rCwSEyZ0P6RiKh3lomXwokeppE+5rBNJlx4G4zE0L4/jGd kKCQ== X-Gm-Message-State: AOJu0Yzd51e2IZDS8m3FG8QZoIx3eGWwGLQpvoxKN7Kh8OBtotU/igI7 ifLqgcNBauG6yoImMLIioBboas2wuQU= X-Received: by 2002:a17:906:8a:b0:9a9:d5f4:1a0d with SMTP id 10-20020a170906008a00b009a9d5f41a0dmr8686321ejc.45.1696212890550; Sun, 01 Oct 2023 19:14:50 -0700 (PDT) Received: from dj3ntoo (161.sub-97-139-114.myvzw.com. [97.139.114.161]) by smtp.gmail.com with ESMTPSA id e8-20020a170906374800b0099df2ddfc37sm16214185ejc.165.2023.10.01.19.14.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 19:14:50 -0700 (PDT) Date: Sun, 1 Oct 2023 21:14:44 -0500 From: Oskari Pirhonen To: gcc-patches@gcc.gnu.org Subject: [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 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778608171576326126 X-GMAIL-MSGID: 1778608171576326126 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 3484c867ca0..14855bd75c7 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 } */ +}