ignore code when compiling
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: ignore code when compiling

  1. #1
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,049

    ignore code when compiling

    In a rather large codebase, there is a set of recurring calls to a couple vararg functions (mainly for logging)

    Code:
       logthis(foo, bar(), somefunction(parameter1,parameter2), somethingelse);
    The number of parameters to this function is variable
    It has all types of variables, function calls, etc.
    The code has thousands of these lines



    There is now a need for a special version of the compile that will remove a lot of the code, among others is the logging.
    Can I somehow #define the "logthis" name into something that'll remove all actual calls to the code. including the actual parameters/expressions to the call.


    I can't afford to add #ifdef/#endif around each of the calls because there are too many of them, and because that'll cause some undisired side effects in our code conformance tests.

    Code:
    #define logthis __noop
    comes close, that removes the call, but it still causes the compiler to evaluate and validate all the parameters to the logthis function, which doesn't work because in this case, removing the logging headers also removes other members of the class that get used as part of the parameters to logthis()

    Ideal would be if I could define logthis into // resulting in all the rest of the line being comments, but that doesn't work. But it's basically something like that which I'm seeking.

    Preferably I'd like something to be portable, but I'll take a solution that only works on visual studio as well.
    Last edited by OReubens; September 7th, 2012 at 07:53 AM.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,162

    Re: ignore code when compiling

    if no template appears as argument, you can use a variadic macro, can't you ? ( I ask, because I'm sure you already thought about that ... )

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,049

    Re: ignore code when compiling

    actually... I hadn't, for the simple fact, I didn't even know this existed. And I'm not alone, turns out of the whole programming team, nobody did, except for one guy that "claims" he did, but in several discussions, it never occured to him to use it like that.

    So yeh, it really is as simple as:
    Code:
    #define logthis(...)
    Now I'll go crawl in my shell of shame for the weekend.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: ignore code when compiling

    It's just to accept, even though you're experienced sometimes the solution just refuses to come to mind. That's life..
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: ignore code when compiling

    It's rarely a good idea to define a method to something else. This is how I would do it:

    Code:
    inline static void logthis(...){
       #ifndef NDEBUG
          //the functionality of logthis
       #endif
    }
    Turn NDEBUG on, and logthis suddenly becomes an empty inline function, which the compiler will optimize out.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,347

    Re: ignore code when compiling

    Quote Originally Posted by ninja9578
    Turn NDEBUG on, and logthis suddenly becomes an empty inline function, which the compiler will optimize out.
    I tested with this:
    Code:
    #include <iostream>
    
    inline static void logthis(int n)
    {
    #ifndef NDEBUG
        std::cout << "We logged: " << n << std::endl;
    #endif
    }
    
    #ifndef NDEBUG
    int bar()
    {
        std::cout << "Hello world!" << std::endl;
        return 0;
    }
    #endif
    
    int main()
    {
        logthis(bar());
    }
    Compiling with g++ 4.6.2 with -O3 and -DNDEBUG, I get the error:
    Code:
    test.cpp: In function 'int main()':
    test.cpp:20:17: error: 'bar' was not declared in this scope
    Changing to:
    Code:
    #include <iostream>
    
    #ifndef NDEBUG
    inline static void logthis(int n)
    {
        std::cout << "We logged: " << n << std::endl;
    }
    #else
    #define logthis(...)
    #endif
    
    #ifndef NDEBUG
    int bar()
    {
        std::cout << "Hello world!" << std::endl;
        return 0;
    }
    #endif
    
    int main()
    {
        logthis(bar());
    }
    I don't get an error, with or without NDEBUG set.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,049

    Re: ignore code when compiling

    the function solution doesn't work, because of the variety of parameters used. In the case where I want to remove the call, all the types, classes, templates etc associated with the logging have (obviously) also been removed from the build, and a dummy function doesn't work in that case because the compiler can't evaluate the parameter expressions. So yes, it is exactly like the first case laserlight posted (and like I did in my OP), the function won't work because bar() is no longer defined in the "do not log" compilation.

    The define is the only usable way out of this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center