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 ?
LVL 1
naseeamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
The compiler tells you that you have set the value to the variable pd0DeltaMaxArry[DebugParamDataIndex].pd0DeltaMax but you have never used it anywhere in your code. I understand that it is your test code and you probably checked the delta in the debugger.

To overcome this problem you can use the trick the MS MFC uses. There is a macro defined in the afx.h like this:
#define UNUSED_ALWAYS(x) x

Open in new window

Then all you need to do is to use the macro:
UNUSED_ALWAYS(pd0DeltaMaxArry[DebugParamDataIndex].pd0DeltaMax);

Open in new window

Which is equivalent to:
pd0DeltaMaxArry[DebugParamDataIndex].pd0DeltaMax;

Open in new window

0
phoffricCommented:
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"?
0
phoffricCommented:
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.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

sarabandeCommented:
static DebugParamData pd0DeltaMaxArry[No_Of_Pd0DeltaMaxValues];
you may move the variables into function DebugParamTrace and init the array:

static void DebugParamTrace(SpeedDelta  parameter)  {
       static int DebugParamDataIndex = 0;    // don't omit the 'int' even if the compiler tolerates
       static PosValue PinchStart = (PosValue)600;
       static PosValue PinchEnd = (PosValue)472;
       static DebugParamData pd0DeltaMaxArry[No_Of_Pd0DeltaMaxValues] = { 0 };
       PosInfo_t  PosInfo = GetPosInfo( );
       ...

Open in new window


where are the PosValue and PosInfo_t and GetPosInfo defined?

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
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
0
naseeamAuthor Commented:
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 ?
0
phoffricCommented:
According to MULTI: C and C++ Compiler Error Messages, the message you get is an error (as opposed to a warning).
0550 set_but_not_used — entity-kind "entity" was set but never used

Open in new window

(I found an Green Hill Ada document which indicates that you can suppress errors. You may be able to do something like that as well.

I advise that if you set a variable, then you should use it to avoid this error. I am guessing that you set it with the intention that you will look at the value in the debugger, but the compiler does not know your intention (unless you can suppress this error). So, just start using it as you have already done in your work-around.
0
naseeamAuthor Commented:
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.
0
naseeamAuthor Commented:
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 ?
0
naseeamAuthor Commented:
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.
0
naseeamAuthor Commented:
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 ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.