Pages: [1]   Go Down
Author Topic: Preprocessor prototype bug  (Read 435 times)
0 Members and 1 Guest are viewing this topic.
Indiana
Offline Offline
Full Member
***
Karma: 1
Posts: 234
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

version: arduino-0012 (I think the bug is in 0011 as well)

There appears to be a bug with the preprocessing and the function prototyping.  Some functions that are defined with pass by reference parameters are skipped and do not get prototype definitions in the resulting .cpp file.

For example, take this simple sketch:

Code:
byte a;

void setup()
{
}

void loop()
{
  Function1(a);
  Function2(a);
}

Then add a second tab (or add this code after the loop() function:

Code:
void Function1(byte var) {
// This function gets a prototype
}

void Function2(byte &var) {
// This function doesn't get a prototype
  var=1;
}

Trying to compile this sketch will result in:

In function 'void loop()':
error: 'Function2' was not declared in this scope


This is because no prototype is generated for Function2, only for Function1.  Here is the resulting .cpp file:

Code:
#include "WProgram.h"
void setup();
void loop();
void Function1(byte var);
byte a;

void setup()
{
}

void loop()
{
  Function1(a);
  Function2(a);
}
void Function1(byte var) {
// This function gets a prototype
}

void Function2(byte &var) {
// This function doesn't get a prototype
  var=1;
}

int main(void)
{
      init();

      setup();
    
      for (;;)
            loop();
        
      return 0;
}

The work-around is to manually add the missing prototype declarations before the functions are referenced.  Overall it makes keeping your code organized in multiple tabs difficult since you don't (seem) to have any control of the order of the final assembled program.  If the prototyping was working correctly then the order wouldn't matter.
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 11
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep.

Most people who use Arduino don't use references, and those that do probably also know about function prototypes.  

Still, it would be nice to improve this.  If anyone is interested in trying, the code is in: http://svn.berlios.de/wsvn/arduino/trunk/app/preproc/PdePreprocessor.java
Logged

Pages: [1]   Go Up
Jump to: