PDA

View Full Version : Debugging code to remove #ifdef statements, and I have some questions.



PatrickDickey
10-04-2011, 05:29 PM
Hi everyone,

I'm debugging some code for the PCTV 80e TV Tuner (hoping to get it included in the v4l project), and I'm trying to figure out whether or not some #ifdef statements need to be removed or moved or just altered.

I have things like


#ifdef DJH_DEBUG
int x;
#endif


and some where they are in the middle of a select/case statement



case option1
case option2
#ifdef only for certain conditions
case option3
case option4
#endif
case option5


Finally, I have one or two that are entire methods (which I think will have to be moved to the header, but I'm not entirely sure).

In some of the cases, I think the original author just used the ifdef statements as comments, but I'm not positive--which is why I'm here.

What I want to do is comment out the #ifdef statements, so they aren't compiled. In the first example, I want to comment out the entire code segment. In the second example, I just want to comment out the lines #ifdef and #endif, but leave the code intact. Same with the third example (where they use it for entire methods).

Can I simply surround the #ifdef statements with comment tags, or do I have to remove them completely from the program? In other words, will this work for the two given examples?



/*#ifdef DJH_DEBUG
int x;
#endif*/


and some where they are in the middle of a select/case statement



case option1
case option2
/*#ifdef only for certain conditions*/
case option3
case option4
/*#endif*/
case option5


Thank you for any information, and have a great day:)
Patrick.

P.S. I wasn't sure if this belonged in the C Programming or this forum, as the question is somewhat generic, but my application is for a Linux-based program. So, please move the thread, if necessary.

gardhr
10-04-2011, 08:19 PM
Hi everyone,

I'm debugging some code for the PCTV 80e TV Tuner (hoping to get it included in the v4l project), and I'm trying to figure out whether or not some #ifdef statements need to be removed or moved or just altered.

I have things like


#ifdef DJH_DEBUG
int x;
#endif


and some where they are in the middle of a select/case statement



case option1
case option2
#ifdef only for certain conditions
case option3
case option4
#endif
case option5


Finally, I have one or two that are entire methods (which I think will have to be moved to the header, but I'm not entirely sure).

In some of the cases, I think the original author just used the ifdef statements as comments, but I'm not positive--which is why I'm here.

What I want to do is comment out the #ifdef statements, so they aren't compiled. In the first example, I want to comment out the entire code segment. In the second example, I just want to comment out the lines #ifdef and #endif, but leave the code intact. Same with the third example (where they use it for entire methods).

Can I simply surround the #ifdef statements with comment tags, or do I have to remove them completely from the program? In other words, will this work for the two given examples?



/*#ifdef DJH_DEBUG
int x;
#endif*/


and some where they are in the middle of a select/case statement



case option1
case option2
/*#ifdef only for certain conditions*/
case option3
case option4
/*#endif*/
case option5


Thank you for any information, and have a great day:)
Patrick.

P.S. I wasn't sure if this belonged in the C Programming or this forum, as the question is somewhat generic, but my application is for a Linux-based program. So, please move the thread, if necessary.

Deleting or commenting out the code is not the answer! You can control which statements are enabled during compilation (your compiler's documentation will list which flag to use) or, at the very least, by directly defining/undefining things at "the top of the source file". The latter is generally frowned upon though because it's more tedious, less flexible, etc...

brewbuck
10-04-2011, 08:36 PM
Sounds like your question really should be, are any of these ifdef blocks potentially obsolete, and could be removed, and are there any ifdef blocks where anything non-obvious is going on?

The first step is to identify which of the ifdef blocks are actually being compiled in. The way to do this is to create a syntax error within the block, and try to compile. If the code is being compiled, you'll get a compiler error.

After you've identified the ifdef blocks of interest, I'm not sure what your next step is. Are you just trying to generally clean this stuff up, restructure it differently?

PatrickDickey
10-05-2011, 05:13 AM
Thank you for both of your replies. I realized this morning that I needed to clarify things a bit. The code is something that Devin Heitmueller started working on last year. He got it working (so that you can use the Pinnacle PCTV 80e USB Tuner in Linux), however it won't be accepted into the linuxtv project (or the kernel) because of "Coding Style issues". One of the issues are #ifdef statements in .c files. Essentially he said he's not going to waste his time cleaning the code up, so anyone that wants to is welcome to it. So, I'm trying to do that.

As for the #ifdef statements, he more or less said "You have a working driver, so try removing them and see what breaks." Rather than remove them, and then have to try and figure out what they were, I wanted to comment them out. If I find that I need one, then I'll try moving it to a header file, or removing the #ifdef statements around the code. One of the files which has the majority of these is about 15,400 lines of code. So I'm not sure how much of it I would need to quote here to give you the right context.

I guess my question (in reference to brewbuck's reply) is this: Which of these are just unnecessary debugging, which ones could be there without the #ifdef statements around them, and which ones could be moved to the header files?

To give you a bit more context, the first code snippet that I posted (the #ifdef int i one) uses i in a for loop. So, does C automatically assign a data type to the variable in a for loop, or do you have to explicitly declare it as an int (or whatever type you're using)? Along that line, does C++ handle this differently? (the answer to this may explain the reasoning behind the #ifdef statement).

If you're interested in the code, I have it up at a github site http://github.com/patrickdickey52761/PCTV80e should get you to the code. You'll probably want the second and then the topmost commit (as the second is all of the files, but the topmost fixed a stupid bug that I introduced). The files that I'm worried about are in the linux/drivers/media/dvb/frontends/drx39xyj directory.

I will try introducing some errors, and see which ones it triggers on. I'm worried though that it may not hit on my computers, but it might on someone else's.

Thanks for the answers, and have a great day. :)
Patrick.