A VSI C x86-64 V7.5-009 question about variable shadowing

OpenVMS x86 native compilers, cross compilers news and questions.
Post Reply

Topic author
mjvms27
Contributor
Posts: 23
Joined: Wed May 17, 2023 2:11 pm
Reputation: 0
Status: Offline

A VSI C x86-64 V7.5-009 question about variable shadowing

Post by mjvms27 » Sun Jan 14, 2024 12:55 pm

The earlier issue posted by mgdaniel
viewtopic.php?f=38&t=8943
got me wondering about reusing a name for a variable in a function that shadows a variable at global scope.

Code: Select all

#include <stdlib.h>
#include <stdio.h>

int test1;

void myfunc(void);

int main(void)
{
    myfunc();
}

void myfunc(void)
{
    static char test1[32] = { 0 };
}
That code is legal, but clang (and presumably gcc) with -Wshadow in effect, will offer the following complaint to the developer, which can be useful (or very annoying), depending on the situation:

Code: Select all

testshadow.c:15:17: warning: declaration shadows a variable in the global scope [-Wshadow]
   15 |     static char test1[32] = { 0 };
      |                 ^
testshadow.c:4:5: note: previous declaration is here                                                                                              
    4 | int test1;
      |     ^
1 warning generated.    
I'm just curious if there is a way to deliberately coax a similar warning from the VSI C compiler?

User avatar

arne_v
Master
Posts: 347
Joined: Fri Apr 17, 2020 7:31 pm
Reputation: 0
Location: Rhode Island, USA
Status: Offline
Contact:

Re: A VSI C x86-64 V7.5-009 question about variable shadowing

Post by arne_v » Sun Jan 14, 2024 7:10 pm

I don't think so.

$ cc/stand=c99/warn=ena:all/check=all something

does not give a warning.

But neither does:

$ clang "-Wshadow-all" something.c
Arne
arne@vajhoej.dk
VMS user since 1986


Topic author
mjvms27
Contributor
Posts: 23
Joined: Wed May 17, 2023 2:11 pm
Reputation: 0
Status: Offline

Re: A VSI C x86-64 V7.5-009 question about variable shadowing

Post by mjvms27 » Wed Jan 17, 2024 8:13 am

arne_v wrote:
Sun Jan 14, 2024 7:10 pm

But neither does:

$ clang "-Wshadow-all" something.c
For the record, what version of clang was used for your test?

User avatar

arne_v
Master
Posts: 347
Joined: Fri Apr 17, 2020 7:31 pm
Reputation: 0
Location: Rhode Island, USA
Status: Offline
Contact:

Re: A VSI C x86-64 V7.5-009 question about variable shadowing

Post by arne_v » Wed Jan 17, 2024 3:18 pm

Just retested with latest and greatest (on VMS):

$ type z.c
#include <stdlib.h>
#include <stdio.h>

int test1;

void myfunc(void);

int main(void)
{
myfunc();
}

void myfunc(void)
{
static char test1[32] = { 0 };
}
$ clang "-Wshadow-all" z.c
$ clang -v
VSI C++ V10.1-1
clang version 10.0.1 (git@bitbucket.org:vms_software/llvm-10.0.1.git f8c06aa3b946de6956aade907a641da216dbe1fd)
Build date: 01-10-2024
Target: x86_64-OpenVMS
Thread model: posix
InstalledDir: ARNE4$DKA0:[SYS0.SYSCOMMON.][SYSEXE]
CLANG: warning: argument unused during compilation: '-Wno-invalid-source-encoding' [-Wunused-command-line-argument]
Arne
arne@vajhoej.dk
VMS user since 1986


craigberry
Contributor
Posts: 23
Joined: Fri Nov 17, 2023 11:27 am
Reputation: 1
Status: Offline

Re: A VSI C x86-64 V7.5-009 question about variable shadowing

Post by craigberry » Wed Jan 17, 2024 5:48 pm

Also no warning with a newer clang:

Code: Select all

$ clang --version
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: x86_64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ clang -Wshadow-all testshadow.c
$ clang -Wshadow testshadow.c


Topic author
mjvms27
Contributor
Posts: 23
Joined: Wed May 17, 2023 2:11 pm
Reputation: 0
Status: Offline

Re: A VSI C x86-64 V7.5-009 question about variable shadowing

Post by mjvms27 » Wed Jan 17, 2024 7:18 pm

The one above warned on clang-17 for me.

This updated test really does shadow and make the test1 declared at file scope unusable inside myfunc.

Code: Select all

#include <stdlib.h>
#include <stdio.h>

int test1 = 0;

void myfunc(void);

int main(void)
{
    myfunc();
}

void myfunc(void)
{
    int test1 = 1;
}


jreagan
VSI Expert
Master
Posts: 139
Joined: Tue Dec 01, 2020 8:40 am
Reputation: 0
Status: Offline

Re: A VSI C x86-64 V7.5-009 question about variable shadowing

Post by jreagan » Thu Jan 18, 2024 11:40 am

I understand your request about such scoping issues (it can happen in MANY languages).

However, I've been unable to find a compiler that reports it

C on Itanium

$ cc/warn=enable=all x

static char test1[32] = { 0 };
................^
%CC-I-UNREFSDECL, A static variable is declared but never referenced in this module.
at line number 15 in file WORK20:[JREAGAN]X.C;1

C on x86

$ cc/warning=enable=all x.c

static char test1[32] = { 0 };
................^
%CC-I-UNREFSDECL, A static variable is declared but never referenced in this module.
at line number 15 in file DKA300:[jreagan]X.C;1
$

clang on x86 (based on clang 10)

$ clang "-Wall" x.c
x.c:15:17: warning: unused variable 'test1' [-Wunused-variable]
static char test1[32] = { 0 };
^
1 warning generated.

and clang 17 -Wall (top of trunk) from godbolt.org https://godbolt.org/z/a7sbvPcx6

<source>:15:17: warning: unused variable 'test1' [-Wunused-variable]
15 | static char test1[32] = { 0 };
| ^~~~~
1 warning generated.
Compiler returned: 0

and gcc -Wall (top of trunk) from godbolt.org https://godbolt.org/z/r5YYhh9da
<source>: In function 'myfunc':
<source>:15:17: warning: unused variable 'test1' [-Wunused-variable]
15 | static char test1[32] = { 0 };
| ^~~~~
Compiler returned: 0


Topic author
mjvms27
Contributor
Posts: 23
Joined: Wed May 17, 2023 2:11 pm
Reputation: 0
Status: Offline

Re: A VSI C x86-64 V7.5-009 question about variable shadowing

Post by mjvms27 » Thu Jan 18, 2024 1:14 pm

With the above update from Jan 17:
https://godbolt.org/z/jjEnfd35z

with the original code in the original post:
https://godbolt.org/z/xoGq1dx84
Last edited by mjvms27 on Thu Jan 18, 2024 1:19 pm, edited 1 time in total.


jreagan
VSI Expert
Master
Posts: 139
Joined: Tue Dec 01, 2020 8:40 am
Reputation: 0
Status: Offline

Re: A VSI C x86-64 V7.5-009 question about variable shadowing

Post by jreagan » Sat Jan 20, 2024 12:11 pm

So -Wall doesn't enable ALL of the messages?

Such a message is indeed useful but we don't have that in our legacy C frontend and I can't get the clang 10 that we currently use for C++ to emit it.

Post Reply