Dear visitors and members, with the staff having moved on to other life interests and as a result of changes in people's internet usage habits, the CodeWalrus community have migrated almost entirely to Discord, IRC and WalrusIRC and is now essentially a place where to hang out, chat about gaming, programming, music, anime, and chill. As a result, even though the CW forums will remain open for posting, for faster TI, Casio or HP calculator help and news we recommend using the bilingual TI-Planet forums instead.

### WalrusIRC

You need to have 5 posts and not be part of restricted usergroups in order to use the WalrusIRC embedded shoutbox. However, you can also access our IRC channel called #CodeWalrus via EFnet.

### Author Topic: Know what C functions aren't called from a certain function?  (Read 1530 times)

0 Members and 1 Guest are viewing this topic.

#### Hayleia

• Full User
• Join Date: Nov 2014
• Location:
• Posts: 324
• Post Rating Ratio: +12/-0
##### Know what C functions aren't called from a certain function?
« on: June 22, 2016, 04:17:20 pm »
No idea if that's the right place to post, especially since I'm mostly asking for 3DS dev but since it might apply to C in general, I posted here...

So, is there a way to detect what functions are never called from a certain starting function, recursively?
Example, if you didn't get what I said.
Code: [Select]
`int a() {    return 1;}int b() {    return 2;}int c() {    return a()+1;}int main() {  return c()+2;}`Well, then.
- what functions aren't called from a? b, c, main.
- what functions aren't called from b? a, c, main.
- what functions aren't called from c? b, main (a is called directly from c).
- what functions aren't called from main? b (c is called directly from main and a is called indirectly from c).

So is there something that can parse my files and tell me this?
Note, this has to be a tool that happens before execution since, as I said, this is going to be executed from the 3DS, so I can't really run the compiled executable through some IDE tool that sends stuff to GDB or whatever, that won't work.

#### Ivoah

• Super User
• Join Date: Dec 2014
• Location:
• Posts: 523
• Post Rating Ratio: +6/-0
• Gender:
• WalriiPoints: 100
##### Re: Know what C functions aren't called from a certain function?
« Reply #1 on: June 22, 2016, 05:05:39 pm »
No idea if that's the right place to post, especially since I'm mostly asking for 3DS dev but since it might apply to C in general, I posted here...

So, is there a way to detect what functions are never called from a certain starting function, recursively?
Example, if you didn't get what I said.
Code: [Select]
`int a() {    return 1;}int b() {    return 2;}int c() {    return a()+1;}int main() {  return c()+2;}`Well, then.
- what functions aren't called from a? b, c, main.
- what functions aren't called from b? a, c, main.
- what functions aren't called from c? b, main (a is called directly from c).
- what functions aren't called from main? b (c is called directly from main and a is called indirectly from c).

So is there something that can parse my files and tell me this?
Note, this has to be a tool that happens before execution since, as I said, this is going to be executed from the 3DS, so I can't really run the compiled executable through some IDE tool that sends stuff to GDB or whatever, that won't work.
Sounds like you want a static analyzer, here's one for C: http://clang-analyzer.llvm.org/
• Calculators owned: TI-86 (now broken), TI SR-56, TI-Nspire CX CAS, TI-84+ SE, TI-84+ SE, TI-85, TI-73 Explorer VS, ViewScreen, TI-84+ CSE, TI-83+ SE

#### Hayleia

• Full User
• Join Date: Nov 2014
• Location:
• Posts: 324
• Post Rating Ratio: +12/-0
##### Re: Know what C functions aren't called from a certain function?
« Reply #2 on: June 22, 2016, 06:08:30 pm »
Thanks
In the meantime, I also discovered cflow. Not sure which one suits my needs best, I'll see.

edit Seems like cflow -r *.c | grep -x -E "[^ ].*|[ ]*main.*" gives an output that fulfills my need correctly (if I didn't fail, it should list all the functions in the .c files, either with nothing under them or with mentions of "main" which mean they are called by main).
« Last Edit: June 22, 2016, 07:26:59 pm by Hayleia »

#### Juju

• aka Yuki Kagayaki aka J̵̭͕͇ù̞̭̝̯̦j̴̭̙̗͖͡ù͏͓̲̕
• CodeWalrus Staff
• Super User
• Server Maintenance
• Moderator
• Forum Maintenance
• Original 5
• CodeWalrus Supporter
• Join Date: Nov 2014
• Location: Inside a walrus
• Posts: 3171
• Post Rating Ratio: +36/-2
• Couch potato
• Gender:
• WalriiPoints: 99999
##### Re: Know what C functions aren't called from a certain function?
« Reply #3 on: June 23, 2016, 05:52:38 am »
Depending of the compiler, I think they do a good job at removing unused and unreachable code when you ask for it through compiler options (like -O in GCC). But better audit the code yourself and comment out/remove the unused code, I guess.
• Consoles, mobile devices and vintage computers owned: A lot
On semi-hiatus until who knows when. CODEWALRUS 2.0 COMING SOON
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader --Snektron

if you wanna throw money at me and/or CodeWalrus monthly it's here

#### Streetwalrus

• Super User
• Original 5
• Join Date: Nov 2014
• Location:
• Posts: 2903
• Post Rating Ratio: +20/-0
##### Re: Know what C functions aren't called from a certain function?
« Reply #4 on: June 26, 2016, 05:39:05 pm »
Depending of the compiler, I think they do a good job at removing unused and unreachable code when you ask for it through compiler options (like -O in GCC). But better audit the code yourself and comment out/remove the unused code, I guess.
Actually unless you enable LTO, you need to add specific linker flags for that to happen. Otherwise it can't  know what to remove since code in other objects may call it.

#### Hayleia

• Full User
• Join Date: Nov 2014
• Location:
• Posts: 324
• Post Rating Ratio: +12/-0
##### Re: Know what C functions aren't called from a certain function?
« Reply #5 on: June 26, 2016, 06:00:59 pm »
This probably works when compiling a regular PC program but adding -flto to my cflags didn't remove anything to my compiled armloaderhax payload for 3DS
Removing unused functions by hand did though. Went down from 90KB to 12

You can also use the following HTML or bulletin board code to share it on your page or forum signature!

Also do not forget to check our affiliates below.