Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

Why adding test code changes the build ?

I'm using Green Hills Cross Compiler.  My embedded target board is based on Renesas RH850F1L Microcontroller.  My computer language is C.  

We have large code base.  It always builds with no problem.  Today, I added some test code at the top of one of the .c file.


/* TEST CODE */
#define No_Of_Pd0DeltaMaxValues  128
static DebugParamDataIndex = 0;
static PosValue PinchStart = (PosValue)600;
static PosValue PinchEnd = (PosValue)472;

typedef struct {
      SpeedDelta pd0DeltaMax;
} DebugParamData;

static DebugParamData pd0DeltaMaxArry[No_Of_Pd0DeltaMaxValues];
static void DebugParamTrace(SpeedDelta  parameter)  {
         PosInfo_t  PosInfo = GetPosInfo( );
         if ( ( (PosInfo.Position) >= PinchStart) ||
              ( (PosInfo.Position) < PinchEnd) )
         {
               pd0DeltaMaxArry[DebugParamDataIndex].pd0DeltaMax = Parameter;
               DebugParamDataIndex++;
        }

}
/* TEST CODE */

One of the functions at the bottom of this file call the function defined in the test code.  

The code doesn't build because it says pd0DeltaMaxArry  is set but not used.  When I do something to get around this error.  Then, it throws same error on other variables that are set but not used.  When I remove the test code, everything builds fine.

Why am I getting variable set but not used error on parts of code that compiles without the test code ?  Why addition of this test ocde is breaking my build ?
SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

Is it safe to say that are using pd0DeltaMaxArry somewhere that you are not showing us?

What did you do to get around this particular problem, and what is the variable for the case where you get the "same error on other variables that are set but not used"?
If you are using pd0DeltaMaxArry array in a different file, then perhaps your compiler is complaining because it doesn't see the other file when it compiles the case where you are setting the array.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
note, static variables always need an initialization. some compilers may do that implicitly, but i assume this is not the case with yours.

also, you can't determine the order of static initialization made in different source files. so if you have static variables in mystring.c and myarray.c it could be that the myarray.c was initialized prior to mystring.c even if your code requires a different order. you can come out of this by putting all static initialization to one .c file. in c++, there is a much better solution by using a get function like the following:

std::vector<DebugParamData> & GetDebugParamData()
{
      static std::vector<DebugParamData> theDebugParamDataArray(No_Of_Pd0DeltaMaxValues);
      return theDebugParamDataArray;
}

Open in new window


here the first call of GetDebugParamData() would trigger the static initialization and provide the variable to anyone.

in c you could do similar only for pointers or arrays since c can't return values by reference. but for your case the following would work:

DebugParamData * GetDebugParamData()
{
      static DebugParamData theDebugParamDataArray[No_Of_Pd0DeltaMaxValues] = { 0 };
      return theDebugParamDataArray;
}

...
void DebugParamTrace(SpeedDelta  parameter)  {
    ...
    GetDebugParamData()[DebugParamDataIndex].pd0DeltaMax = Parameter;
    DebugParamDataIndex++;
    ....
}

Open in new window


Sara
Avatar of naseeam

ASKER

I added the macro as follows but still get the same error:

#define UNUSED_ALWAYS(x) x
..
..
UNUSED_ALWAYS(pd0DeltaMaxArry[DebugParamDataIndex].pd0DeltaMax) = Parameter;

The error I get is:   variable  "pd0DeltaMaxArry" was set but never used


pd0DeltaMaxArry is only used in my test code that I show in my question above.
To get around this problem, I increment the variable inside DebugParamTrace( ):
pd0DeltaMaxArry[DebugParamDataIndex].pd0DeltaMax++

Then, I get error same error in following:
void SetEvent(HMEvent Event) {

      HMEvent temp;
      temp = Event;

}

Error is something like temp is set but never used.  When I remove my test code, I don't get this error.  Does someone know why does my test code break the existing build ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of naseeam

ASKER

datatype PosValue  is signed short

datatype PosInfo_t  is as follows:

typedef struct {
  signed short Position;
  TUwl_MSM_HMBool     Normed;
} HMPosInfo;

Above types are defined in a header file.

I place the static variables inside the function.  Now I don't get set but never used  error in my test code but I set but never used error for temp variable that is in another function.  Removing the test code doesn't give this error and code builds and runs fine.

 GetPosInfo( ) is defined in a library.  We don't have the source code for this library.
Avatar of naseeam

ASKER

My question is that in our existing code as follows we set a variable but don't use it.  And it builds fine.

void SetEvent(HMEvent Event) {
       HMEvent temp;
       temp = Event;
 }

Why adding the test code won't compile the existing code ?
Avatar of naseeam

ASKER

When I do work-around to use the variable that is set in my test code, my test code compiles but it breaks the existing code.  I don't think I should be doing a work around on all unused variables in the existing code and  then undoing them when I'm done testing.
Avatar of naseeam

ASKER

I moved the test code near the bottom of the file.  Right before the function call
DebugParamTrace( ).

Now, the build works.

It probably works by there are no set but unused variables between my test code and end of file.

Maybe there is a bug in the compiler ?