From patchwork Wed Feb 28 01:13:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Oskari Pirhonen X-Patchwork-Id: 86497 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 991EC385840F for ; Wed, 28 Feb 2024 01:14:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id 4CDD33858D37 for ; Wed, 28 Feb 2024 01:13:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CDD33858D37 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 4CDD33858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::636 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709082820; cv=none; b=M6plMdWV4kBDfiI1N9jfZAyq1r8E/4AEz5Bwedz+/OcQvk0l2+CWa16awkaFryJbXu9kLDSz0Tyl/nPGEK2Ml1smW2y9y6z8OtkrfNtqxBmfqnLIUMdv7Dk1svCRjp+7GP+RzkO4myA4ns3FuC12qBkJmb5cTTHIJBqFQ2ee5zA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709082820; c=relaxed/simple; bh=NierrswsREVTrGbrwDBunQlTfuey1RaRkzn2ORKb6yI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=udpLg88cPA//o/WSaQp/FSbDRPGgwk4/mcGFrDocc7QFdC0Cgje0qtYRmnYRsef4wvTxw7M+lAuERmlW17dppeMWRExVWlroMMAlsqai73VPUwDGKMx3fJzjNMhIwQdEwLyq0XI6DEtfgMlNH92Ixgj/316iHL0bOPwhSUuQ3jA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a3ed9cae56fso64792366b.1 for ; Tue, 27 Feb 2024 17:13:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709082816; x=1709687616; 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=n617sPKS4lgIEq4JwGtPOIEEAsR02GmgjA3n9iRBPyI=; b=FVMjx7GeElzpUGgkTx6zzNc/DLzZsDezaZk2cBWA1EUqyzjTKgBHiSJVhOm6MZJ1Hn cVcGDfRzjplBSx9+5LI32l/zJJYg3PmKJ1iBOgKxeG6sbSKlb+F7npq97QtcwMdbNTOX h07DINzl2vKBuqtzxEF+qcXDWzfA4Hu9eRCcrBjYKUNiITqv4LM84XELxwhdtzJ3936k 189t5EKMGZ1VITUGOUfLIAR6PNXhLYpL1Z/pKgoMg4l2pqpakXxDAYFehHQONmecHFzp HYTXi5cp8mWBpiTS1O6aMsX33ZLp0CnY8OQpT6LpMG+MVE8SWnWIkLgUD9gLwDz1HPx6 hq0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709082816; x=1709687616; 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=n617sPKS4lgIEq4JwGtPOIEEAsR02GmgjA3n9iRBPyI=; b=v1PtGtPbZRGGAiQQTVqjwq3rMBxgGnjbB4LrViBhkdubh5pRGx+2r8sMjvXhOI3ftS 2hYYVCL+JtoI6sfJIwk6uzA2yVb4GWYdKfM1YNu/E1xRgpWbsiKMnDO9LdyL97/DTyCw iK9XqXt73FSR6+F9PoLKOC/fQp4eGQd51N4hF8dT+UEmjFr0L62EK2RqcVVQDooaKQdm ZZlL4sZ8jYmUO5v3l1g5vJmxrrLeA0ft4381Lb6j8DsEX8rzlIO4VdcEXtHdB8G2XEUe a/M/iY7zYyYslIqe0PTGy8YDrk8oNYRc/8wYG+ubonCvxjrQDkSvw82hAOLMxtnZ1Q9+ 6wCA== X-Gm-Message-State: AOJu0YyyuasJQPZ5O9mMkQXVz0KgfWucIrvu6a3posA9fCfqd3T+/aGE JNpNJmUzWv4tpNFbxCt5Y5CK91m/4Yt/SxUn0dKmBKjghEKCUwmNOkCBxOP4 X-Google-Smtp-Source: AGHT+IF+80TFEQjKKLinrieM11wcX1PmbcyWEypCceBPeAd+3npuWR8Ln9+WcEgFfXiglId0fLS/bQ== X-Received: by 2002:a17:906:c090:b0:a43:9d17:dfba with SMTP id f16-20020a170906c09000b00a439d17dfbamr909633ejz.31.1709082815870; Tue, 27 Feb 2024 17:13:35 -0800 (PST) Received: from dj3ntoo (30.sub-97-147-14.myvzw.com. [97.147.14.30]) by smtp.gmail.com with ESMTPSA id b16-20020a170906835000b00a4395e9debasm1283100ejy.201.2024.02.27.17.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 17:13:35 -0800 (PST) Date: Tue, 27 Feb 2024 19:13:30 -0600 From: Oskari Pirhonen To: gcc-patches@gcc.gnu.org Cc: dodji@redhat.com, dmalcolm@redhat.com Subject: [PATCH v2] 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 --- v2: - check for error instead of warning in gcc.dg/spellcheck-stdlib-2.c - from linaro ci notification email 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..4762e2ddbbd --- /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-error "implicit declaration of function 'strerror'" } */ + /* { dg-message "'strerror' is defined in header ''" "" { target *-*-* } .-1 } */ +}