On 2026-09-24 20:08, David Malcolm wrote:
On Mon, 2026-08-31 at 06:26 +0000, Torbjörn Svensson via Sourceware
Forge wrote:
From: Torbjörn SVENSSON <[email protected]>
Newlib, picolibc, and probably other libc implementations, use
#define O_CREAT _FCREAT
#define O_TRUNC _FTRUNC
#define O_EXCL _FEXCL
In r17-177-g9a5f89005dc49d, support was added to identify redundant
flags, but the implementation requires that the value of the macros
O_CREAT, O_EXCL, O_RDWR to be a number. This patch extends the
support
to also allow macro aliases. It will follow up to 8 levels of macros
in
order to reach the number.
This change fixes
FAIL: gcc.dg/analyzer/mkostemp-1.c (test for warnings, line 109)
FAIL: gcc.dg/analyzer/mkostemp-1.c (test for warnings, line 115)
FAIL: gcc.dg/analyzer/mkostemps-1.c (test for warnings, line 115)
FAIL: gcc.dg/analyzer/mkostemps-1.c (test for warnings, line 121)
Regtested with arm-none-eabi (with newlib) on r17-3738-
gaaa0f6b1e87bc6.
gcc/c/ChangeLog:
PR analyzer/125265
* c-parser.cc: Follow, up to eight, macro alias to get the
value
for a named constant.
Signed-off-by: Torbjörn SVENSSON <[email protected]>
---
Ok for trunk?
Thanks for this patch; various notes inline below...
I'm unsure if this should also go into releases/gcc-16, WDYT?
--
This is a forge pull request published on the gcc-patches mailing
list mailing list
as requested by Torbjörn Svensson via Sourceware Forge
<[email protected]>.
Forge discussion: https://forge.sourceware.org/gcc/gcc/pulls/226
Get it locally using:
```
git fetch forge-upstream
"+refs/versioned_pull/226/*:refs/versioned_pull/226/*"
git switch -c "pr-226-v1" "refs/versioned_pull/226/1/head"
```
Or, download the patch at:
https://forge.sourceware.org/gcc/gcc/pulls/226.diff
Created on: 2026-08-31 06:26:23+00:00
Latest update: 2026-08-31 06:26:55+00:00
Changes: 1 changed files, 20 additions, 3 deletions
Head revision: azoff/gcc ref pr/macro-aliases commit
650c07cdb28058525967b479fb10934f44f12d1d
Base revision: gcc/gcc ref trunk commit
aaa0f6b1e87bc62ea15da6eab431af6dcebf6414 r17-3738-gaaa0f6b1e87bc6
Merge base: aaa0f6b1e87bc62ea15da6eab431af6dcebf6414
Requested Reviewers: dmalcolm
Changed files:
- M: gcc/c/c-parser.cc
gcc/c/c-parser.cc | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 9dc5ba1e3a13f..afa3b79d8a340 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -2019,9 +2019,13 @@ private:
#define NAME 42
and other bases but not negative numbers, parentheses or e.g.
#define NAME 1 << 7
- as doing so would require a parser. */
- tree consider_macro (cpp_macro *macro) const
+ as doing so would require a parser.
+ Also handle the case where MACRO's definition is a single
identifier
+ that is itself a macro name (e.g. "#define O_CREAT _FCREAT"),
by
+ following the chain of aliases up to MAX_ALIAS_DEPTH deep. */
+ tree consider_macro (cpp_macro *macro, unsigned int depth = 0)
const
{
+ const unsigned int MAX_ALIAS_DEPTH = 8;
I'm slightly nervous about introducing a magic constant here (we
normally have params for this); where did the value of 8 come from?
It's just a made up value.
For the newlib case, I think 1 would be sufficient, but to be safe, I used
a slightly higher value. I could also make this unbound, but it felt safer
to have a magic constant than unbound recursion.
Anyway, I can make this a param if you feel that it would make sense.
If so, what would you like me to call it?
Do you perhaps have a commit that I can look at on how the param machinery
works, so that I know what I need to implement?
Otherwise the patch looks good to me (with my "analyzer maintainer" hat
on, since this is ana::c_translation_unit within the C frontend).
I don't think there is need for any other hat than the analyzer hat
for this change.
Kind regards,
Torbjörn
Dave
if (macro->paramc > 0)
return NULL_TREE;
if (macro->kind != cmk_macro)
@@ -2029,7 +2033,20 @@ private:
if (macro->count != 1)
return NULL_TREE;
const cpp_token &tok = macro->exp.tokens[0];
- if (tok.type != CPP_NUMBER)
+ if (tok.type == CPP_NAME)
+ {
+ /* Limit recursion. */
+ if (depth >= MAX_ALIAS_DEPTH)
+ return NULL_TREE;
+
+ cpp_hashnode *alias = tok.val.node.node;
+ if (cpp_macro_p (alias))
+ return consider_macro (alias->value.macro, depth + 1);
+
+ /* Not a simple alias. */
+ return NULL_TREE;
+ }
+ else if (tok.type != CPP_NUMBER)
return NULL_TREE;
cpp_reader *old_parse_in = parse_in;