find dead code in code compiled in amtel studio

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

trying to clean up my code a bit.

anyone know if there is any easy way in atmel studio to find "dead code"?

what I mean with dead code is for example dead function declarations :

.

.

.

void something used();

void something used();

void somethingnotused();

void something used();

void something used();

.

.

.

 

 

or other other simular things. that is unnecessary code that makes the code harder to read and adds pointless complexity.

any one know if there is any easy way to find these or other simular things in atmel studio?

Atmel studio don't give a warning for such things I have noticed. 

Last Edited: Tue. May 14, 2019 - 10:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go to your main routine...check which functions are called...dig a bit in your code...you will find the unnecessary functions.

 

its a bit manual work...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Just switch on -ffunction-sections and -gc-sections (which are on by default in AS7 projects) and all dead code will be garbage collected during the link anyway (and the map file will list what was discarded)

Last Edited: Tue. May 14, 2019 - 01:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Just switch on -ffunction-sections and -gc-sections (which are on by default in AS7 projects) and all dead code will be garbage collected during the link anyway (and the map file will list what was discarded)

 

I didnt know such thing existed in AS7, Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Moe123 wrote:

Go to your main routine...check which functions are called...dig a bit in your code...you will find the unnecessary functions.

 

its a bit manual work...

yes, it just seems like something that a program can do faster and better than a person 

Last Edited: Tue. May 14, 2019 - 01:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Just switch on -ffunction-sections and -gc-sections (which are on by default in AS7 projects) and all dead code will be garbage collected during the link anyway (and the map file will list what was discarded)

 

ok, great. I will check it out. thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The way function sections / gc sections works is that every function is built into separated named flash sections. The linker then only includes in the link any section with a reference counter of 1 or more. Any with 0 reference are garbage collected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some dead code is dead because it can never be logically executed & can be tough to spot

 

somewhere in the program:   if Motor A is running faster then 500 RPM, call calibrate valveb

 

valveb cal:  If motor is running faster then 300 rpm skip cal

 

 This may not be such a solid example, but gives the flavor.

 

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
Some dead code is dead because it can never be logically executed & can be tough to spot
Some static analyzers can detect that by data flow analysis akin to detection of inadvertent infinite loops (control flow analysis)

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some static analyzers can detect that by data flow analysis 

I wonder how advanced it gets...needs to be like a good chess program to analyze all of the paths & probably include advanced logic minimization.

I gave students a non-trivial circuit full of gates to build...later showed them the equivalent circuit was two pieces of wire.

 

 

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some years back I was a place that used Klocwork and it can really analyze your code. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Cheers!, Thanks for this Appnote.