0017 code-generation bug?

Hi, forgive my really long post here ... I think I've found a bug in the Arduino environment related to code generation, and it sounds like this forum is the place to report such things. I'm running arduino-0017 under Fedora 11 Linux - the environment startup message looks like:

** Experimental: JNI_OnLoad called.** ** Stable Library** ** =========================================** ** Native lib Version = RXTX-2.1-7** ** Java lib Version = RXTX-2.1-7**

"java -version" yields the following:

** java version "1.6.0_0"** ** OpenJDK Runtime Environment (IcedTea6 1.6) (fedora-30.b16.fc11-i386)** ** OpenJDK Server VM (build 14.0-b16, mixed mode)**

Anyways, here's the test-case sketch code:

** // Arduino 0017 environment bug test:** ** //#define SOMENAME** ** #ifdef SOMENAME** ** const int SomeNameConstant = 32;** ** #endif** ** const int AnotherConstant = 42;** ** void setup()** ** {** ** Serial.begin(9600);** ** }** ** void loop()** ** {** ** Serial.println("OK");** ** }** ** // EOF**

If I try to compile the above, I receive the following error message:

** In function 'void setup()':** ** error: 'Serial' was not declared in this scope In function 'void loop()':** ** In function 'int main()':**

Note that the following target code was generated (I'm just including the beginning, where it appears that the problem manifests itself):

** // Arduino 0017 environment bug test:** ** //#define SOMENAME** ** #ifdef SOMENAME** ** #include "WProgram.h"** ** void setup();** ** void loop();** ** const int SomeNameConstant = 32;** ** #endif** ** const int AnotherConstant = 42;** ** ...**

Note where '#include "WProgram.h"' was included. If I define SOMENAME, then this target code is generated, and compiles correctly (again, just part of the actual code):

** // Arduino 0017 environment bug test:** ** #define SOMENAME** ** #ifdef SOMENAME** ** #include "WProgram.h"** ** void setup();** ** void loop();** ** const int SomeNameConstant = 32;** ** #endif** ** const int AnotherConstant = 42;** ** ...**

This change to the Arduino code appears to solve the problem (note "ADD THIS LINE"):

** // Arduino 0017 environment bug test:** ** //#define SOMENAME** ** const int DONT_USE_THIS = 0; // ADD THIS LINE** ** #ifdef SOMENAME** ** const int SomeNameConstant = 32;** ** #endif** ** const int AnotherConstant = 42;**

and generates the following target code:

** // Arduino 0017 environment bug test:** ** //#define SOMENAME** ** #include "WProgram.h"** ** void setup();** ** void loop();** ** const int DONT_USE_THIS = 0;** ** #ifdef SOMENAME** ** const int SomeNameConstant = 32;** ** #endif** ** const int AnotherConstant = 42;** ** ...**

Thoughts? I'm not familiar with the development environment internals, but it appears that there's a pre-compilation pass which is ignoring preprocessor directives when it fills in the environment-specific code.

-Len

I've reproduced the problem with Windows Vista, Arduino 0017. Moving AnotherConstant earlier in the Sketch works-around the problem...

// Arduino 0017 environment bug test:

// #define SOMENAME

const int AnotherConstant = 42;

#ifdef SOMENAME
  const int SomeNameConstant = 32;
#else
#endif

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("OK");
}

// EOF

The Arduino IDE does a small amount of pre-processing with some imperfect regular expressions. In particular, it inserts an #include before the first non-comment, non-preprocessor directive, which in this case is within the false #ifdef.

I opened an issue for this: http://code.google.com/p/arduino/issues/detail?id=156

is the weird behavior im getting related to this bug?

#define byte uint8_t
#include <sd_raw_config.h>
#include <sd-reader_config.h>
#include <sd_raw.h>

int print_disk_info();
int sample();
int readDisk();

uint8_t incomingByte;
void printWelcome();
long int address;
uint8_t tempBytes[2];


void setup()
{

  Serial.begin(9600);
  delay(1000);
printWelcome();
  if(!sd_raw_init())
  {
     Serial.println("MMC/SD initialization failed");
  }
  print_disk_info();
}



void loop()
{
    int i;

    if(Serial.available()>0)
    {
         incomingByte=Serial.read();

       switch(incomingByte)
       {
           case 114:
                     readDisk();
                     break;
           case 115:
                     sample();
                     break;
           default:
                     break;
       }
    }
}

int sample()
{
    int i,j;
    int temp;
    byte low;
    byte high;
    byte inByte;

    Serial.println();
    Serial.println();
    Serial.println("Sampling..");
    for(i=0;i<500;i=i+2)
    {
        if(Serial.available()>0)
        {
            inByte=Serial.read();
            if(inByte==113) return 0;
        }
        temp=analogRead(0);
        Serial.print(temp,DEC);
        Serial.print(" ");

        //Convert int to 2 bytes
        low=temp&0xFF;
        high=temp>>8;
       // Serial.print(temp,DEC);
        //Serial.print(low,DEC);
        //Serial.print(high,DEC);

        tempBytes[0]=low;
        tempBytes[1]=high;

        if(!sd_raw_write(i,tempBytes,2))
        {
          Serial.print("Write error");
        }
        //sd_raw_sync();
        delay(5000);

  Serial.println();

    }

    return 1;
}


int readDisk()
{
    byte low;
    byte high;
    byte info[2];
    int i;
    int  result;
    Serial.println();
    for(i=0;i<50;i=i+2)
    {
        sd_raw_read(i,info,2);

        //Serial.print(info[0],DEC);
        //Serial.print(" ");
        //Serial.print(info[1],DEC);
        low=info[0];
        high=info[1];
        result=high<<8;
        //result<<8;
        Serial.print(" ");
        Serial.print(result+low,DEC);
        Serial.print(" ");
    }

}

void printWelcome()
{
    Serial.println("------------------------");
    Serial.println("Data sampling system");
    Serial.println("send r to read disk");
    Serial.println("send s to start sampling");
    Serial.println("send q to stop sampling");
    Serial.println("Ready.....");
    Serial.println("-------------------------");
}


int print_disk_info()
{


    struct sd_raw_info disk_info;
    if(!sd_raw_get_info(&disk_info))
    {
        return 0;
    }


    Serial.println();
    Serial.print("rev:    ");
    Serial.print(disk_info.revision,HEX);
    Serial.println();
    Serial.print("serial: 0x");
    Serial.print(disk_info.serial,HEX);
    Serial.println();
    Serial.print("date:   ");
    Serial.print(disk_info.manufacturing_month,DEC);
    Serial.println();
    Serial.print(disk_info.manufacturing_year,DEC);
    Serial.println();
    Serial.print("size:   ");
    Serial.print(disk_info.capacity,DEC);
    Serial.println();
    Serial.print("copy:   ");
    Serial.print(disk_info.flag_copy,DEC);
    Serial.println();
    Serial.print("wr.pr.: ");
    Serial.print(disk_info.flag_write_protect_temp,DEC);
    Serial.print('/');
    Serial.print(disk_info.flag_write_protect,DEC);
    Serial.println();
    Serial.print("format: ");
    Serial.print(disk_info.format,DEC);
    Serial.println();
    Serial.print("free:   ");

    return 1;
}
/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp: In function 'void setup()':

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:23: error: 'Serial' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:24: error: 'delay' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp: In function 'void loop()':

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:40: error: 'Serial' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp: In function 'int sample()':

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:66: error: 'Serial' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:76: error: 'analogRead' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:77: error: 'DEC' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:95: error: 'delay' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp: In function 'int readDisk()':

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:112: error: 'Serial' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:125: error: 'DEC' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp: In function 'void printWelcome()':

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:133: error: 'Serial' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp: In function 'int print_disk_info()':

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:154: error: 'Serial' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:156: error: 'HEX' was not declared in this scope

/home/sirus/arduino/hardware/libraries/SDcard/arduino sd card example.cpp:162: error: 'DEC' was not declared in this scope

Doesn't appear to be. Your Sketch compiled fine for me (Arduino-0017, AF_SDLog v2.zip).