• AVR Freaks

Hot!Is there a way to issue a warning for unused functions?

Author
JPortici
Super Member
  • Total Posts : 1017
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
2020/05/21 01:28:52 (permalink)
0

Is there a way to issue a warning for unused functions?

So, i had an issue with a firmware.
I have written two very simillar functions with very simillar signatures that do very simillar things.
Actually, there was the first function and i had to write a slightly different one for specific situations.
 
However i found out that the second one was never called because i forgot to replace the call where needed.
 
XC8 would have warned me at every compilation that the function wasn't being used, so i would have found out.
 
It is my understanding that there is no GCC option that warn me for unused functions, so is there a way to do it?
 
#1

18 Replies Related Threads

    RISC
    Super Member
    • Total Posts : 5607
    • Reward points : 0
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 02:29:37 (permalink)
    0
    Hi,
    I am not sure about GCC but I believe the Code Coverage software package is doing that at run time.
    Regards
     
    #2
    JPortici
    Super Member
    • Total Posts : 1017
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 02:40:10 (permalink)
    +1 (1)
    i know that microchip has now an expensive yet useful option.
    However, this is something that can and should happen at compile time becasue the compiler CAN know about functions that will DEFINETLY be called because there is an explicit call.
     
    I understand that this is of course a classic use case for a static code analysis tool, but i never managed the effort to set one up, i should probably try harder, again
     
    really wish there was a compiler option, though. Just for giggles i checked the compiler option "isolate each function into a section" and then the linker option "remove unused sections", which were actually removed. I thought the linker would then inform me of the sections that were removed, but it didn't -unless there is another option i missed-.
     
    #3
    RISC
    Super Member
    • Total Posts : 5607
    • Reward points : 0
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 02:52:06 (permalink)
    +1 (1)
    Hi,
    Did you try the "call graph" option in the MPLAB X editor ? I am not sure if it displays functions which are not called
    I guess the special "Omnicient" technology of the XC8 compiler enables unused code analysis.
    This technology builds a complete function graph and therefore can assess which functions are never called...
    I don't think GCC is able to do something similar...
    Regards
    #4
    andersm
    Super Member
    • Total Posts : 2798
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 03:30:20 (permalink)
    0
    Newer binutils have a "--print-gc-sections" option, but XC16's linker is unfortunately too old.
    #5
    LdB_ECM
    Super Member
    • Total Posts : 359
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 04:19:51 (permalink)
    +1 (1)
    On the original problem there is probably a more elegant solution tag the old function
     __attribute__((deprecated))

    It should throw a warning anytime it gets used as opposed to your new shiny function
     
    Doesn't help with the larger problem of unused functions. 
    post edited by LdB_ECM - 2020/05/21 04:21:43
    #6
    mpgmike
    Super Member
    • Total Posts : 420
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 06:07:27 (permalink)
    0
    I don't know what setting I have set, but if I have a Function that I either haven't called yet because I'm still developing, or if I commented out the Call command for testing, I get a warning that the Function isn't called when I Build.

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #7
    LdB_ECM
    Super Member
    • Total Posts : 359
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 06:11:33 (permalink)
    +1 (1)
    That happens for "static functions" but the moment you expose them on an interface they go extern and the warning goes because the compiler expects not to include them sometimes.
    #8
    du00000001
    Just Some Member
    • Total Posts : 3675
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 06:24:11 (permalink)
    0
    Does the missing arning only apply to the latest version of XC16 (which I haven't yet used) ?
    IIRC, my version happily complains about uncalled functions, at least at the maximum warning level I'm usually using. (Better safe(r) than sorry.)
     
    [edit]
    The compile stage can only catch this for module-local functions ("static"). But the link stage is capable to identify the remaining (global) unused functions.
    post edited by du00000001 - 2020/05/21 06:27:54

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #9
    JPortici
    Super Member
    • Total Posts : 1017
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 06:49:42 (permalink)
    0
    Well, at this moment i am using v1.50 but i don't remember the compiler/linker ever complaining about unused functions.
    I have the tendency to update, test and delete old versions, you should really use the latest version available if you program for the dsPIC33C
    However, i still have 1.41 installed and it doesn't report that there are unused functions.
     
    would you mind to share your compiler version / configuration?
     
    stupidest code i could come up with:
    #include <xc.h>
    #include <stdint.h>

    void func(void) {
      static uint16_t pattern;

      if (pattern == 0x5555) {
        pattern = 0xAAAA;
      }
      else {
        pattern = 0x5555;
      }

      LATD = 0x5555;
    }
    void unused(void) {
      int idx;
      int abc;

      for (idx=0;idx<256;idx++) {
        abc = idx * idx;
      }

      LATC = abc;
    }

    int main(void) {
      TRISD = 0;
      TRISC = 0;

      while(1) {
        ClrWdt();
        func();
      }
    }

    what i did:
    -create a new project
    -dsPIC33CK256MC506 (last device i was working with)
    -simulator
    -compiler v1.50 (or v1.41)
    -type code above
    -compile
    #10
    du00000001
    Just Some Member
    • Total Posts : 3675
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 06:58:32 (permalink)
    0
    IIRC I'm on v1.45. And the warning level is set to maximum sensitivity (-9?).
     
    As I'm routinely commenting out all unused functions (e.g. when starting from some starter kit software bundle, stripping it down to the minimum necessary/useful) to get rid of those warnings, I'm sure they are issued  :)
     
    Might come back later with more details as I'm currently only on my tablet, not on my development machine.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #11
    JPortici
    Super Member
    • Total Posts : 1017
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 07:03:58 (permalink)
    0
    Wait.. How do you set the "warning level" for gcc?
    also, since when there was a V1.45 of XC16? Are you sure you are not talking about XC8?
     
     
    #12
    du00000001
    Just Some Member
    • Total Posts : 3675
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 07:22:55 (permalink)
    -1 (1)
    Jack_M
    Wait.. How do you set the "warning level" for gcc?
    also, since when there was a V1.45 of XC16? Are you sure you are not talking about XC8?



    That much about "IIRC": I'm using the last version prior v1.50.
    After some troubles to locate the download archives: you're right - must be v1.41.
    Somewhere in the project properties | compiler you can select the warning level, which I'm using on the most sensitive level.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #13
    JPortici
    Super Member
    • Total Posts : 1017
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 08:05:16 (permalink)
    -1 (1)
    I actually can't find it anywhere in the project options (MPLABX 5.35) nor a reference for it in the compiler manual, but if you find the time to show me exatly where it is (or the generated compiler command) i would appreciate it
    #14
    NorthGuy
    Super Member
    • Total Posts : 6085
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: online
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 09:24:55 (permalink)
    +1 (1)
    -Wall should turn all the warnings on
    #15
    JPortici
    Super Member
    • Total Posts : 1017
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 09:34:10 (permalink)
    0
    AFAIU from the compiler manual -Wall turns on a bunch of warning, not all of them, including -Wunused-function which is useless in this case because it applies only to static functions
     
    And -Wall is added automatically anyway
     
    mpgmike
    I don't know what setting I have set, but if I have a Function that I either haven't called yet because I'm still developing, or if I commented out the Call command for testing, I get a warning that the Function isn't called when I Build.


     would you mind sharing the generated command line and XC16 version?
    post edited by JPortici - 2020/05/21 09:35:13
    #16
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11820
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/21 20:56:20 (permalink)
    0
    -Wall -Wextra turns on almost all useful warnings.  I usually also add
     -Wcast-align -Wpointer-arith -Wbad-function-cast -Wsign-compare -Wunused -Wundef
    #17
    JPortici
    Super Member
    • Total Posts : 1017
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Is there a way to issue a warning for unused functions? 2020/05/22 00:09:18 (permalink)
    0
    whoever is giving thumbs up / down does not understand the issue.
    If i say i can't find the option is because there is not the option, on my machine. Is it there on yours? please show me.
    If i say please show me where it is is because i have tried litterally everything. You have the solution? please show me.
    As if i didn't read the fucking manual back to back several times or looked for solutions going outside of "XC16" but looking for "gcc" instead
     
     
     
     
    #18
    andersm
    Super Member
    • Total Posts : 2798
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: Is there a way to issue a warning for unused functions? 2020/05/22 05:00:45 (permalink)
    0
    GCC doesn't have numeric warning levels. du00000001 is probably thinking of XC8.
    #19
    Jump to:
    © 2020 APG vNext Commercial Version 4.5