Bug 108224 - Suggest stdlib.h header for rand
Summary: Suggest stdlib.h header for rand
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 12.2.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2022-12-25 22:04 UTC by Jonny Grant
Modified: 2023-12-01 08:44 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-12-25 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonny Grant 2022-12-25 22:04:46 UTC
Would be great if the suggestions could suggest <stdlib.h> for rand()
Same in C and C++
Comment 1 Andrew Pinski 2022-12-25 22:11:56 UTC
Confirmed.
Simple testcase:
int f(void)
{
  return rand();
}
Comment 2 Andrew Pinski 2022-12-25 22:15:40 UTC
diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc
index 9c256173b82..dd4c23e5923 100644
--- a/gcc/c-family/known-headers.cc
+++ b/gcc/c-family/known-headers.cc
@@ -171,6 +171,8 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib)
     {"getenv", {"<stdlib.h>", "<cstdlib>"} },
     {"malloc", {"<stdlib.h>", "<cstdlib>"} },
     {"realloc", {"<stdlib.h>", "<cstdlib>"} },
+    {"rand", {"<stdlib.h>", "<cstdlib>"} },
+    {"srand", {"<stdlib.h>", "<cstdlib>"} },

     /* <string.h> and <cstring>.  */
     {"memchr", {"<string.h>", "<cstring>"} },


Should be enough for both C and C++.
Comment 3 Jonny Grant 2022-12-25 22:40:24 UTC
Great! I just saw it is the same for random(), srandom(), initstate(), setstate()

Is there an easy way to add them all based on the C API to save opening separate tickets?

I added those :

From 6ff344979af46dbcd739dd9068d6d595547e4c27 Mon Sep 17 00:00:00 2001
From: Jonathan Grant <jg@jguk.org>
Date: Sun, 25 Dec 2022 22:38:44 +0000
Subject: [PATCH] add srandom random initstate setstate

---
 gcc/c-family/known-headers.cc | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc
index 9c256173b82..ade9fa2dcc0 100644
--- a/gcc/c-family/known-headers.cc
+++ b/gcc/c-family/known-headers.cc
@@ -171,6 +171,10 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib)
     {"getenv", {"<stdlib.h>", "<cstdlib>"} },
     {"malloc", {"<stdlib.h>", "<cstdlib>"} },
     {"realloc", {"<stdlib.h>", "<cstdlib>"} },
+    {"random", {"<stdlib.h>", "<cstdlib>"} },
+    {"srandom", {"<stdlib.h>", "<cstdlib>"} },
+    {"initstate", {"<stdlib.h>", "<cstdlib>"} },
+    {"setstate", {"<stdlib.h>", "<cstdlib>"} },
 
     /* <string.h> and <cstring>.  */
     {"memchr", {"<string.h>", "<cstring>"} },
-- 
2.37.2
Comment 4 Sam James 2022-12-26 06:54:02 UTC
(In reply to Jonny Grant from comment #3)
> Great! I just saw it is the same for random(), srandom(), initstate(),
> setstate()
> 
> Is there an easy way to add them all based on the C API to save opening
> separate tickets?
> 
> I added those :

Could you send the patch to gcc-patches (the mailing list)? Thanks.
Comment 5 Jonny Grant 2022-12-26 08:59:36 UTC
(In reply to Sam James from comment #4)
> (In reply to Jonny Grant from comment #3)
> > Great! I just saw it is the same for random(), srandom(), initstate(),
> > setstate()
> > 
> > Is there an easy way to add them all based on the C API to save opening
> > separate tickets?
> > 
> > I added those :
> 
> Could you send the patch to gcc-patches (the mailing list)? Thanks.

Sure, just sent :-)