Macro to cause build failure if parameter not defined?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skillzero@gmail.com

    Macro to cause build failure if parameter not defined?

    Is there a way to cause a compile/preprocessor error in a macro if a
    preprocessor symbol is not defined? I want to do the equivalent of
    this in a macro:

    #if( !defined( SOME_FLAG ) )
    #error not defined
    #endif

    What I want is a way to test if a feature flag is defined to 1 such
    that if it's not defined at all (as opposed to defined to 0), I'll get
    a compile error. This is to catch cases where people test for a
    feature flag without including the right header file, using the right
    compile flags, or they just typed it wrong. I want to use it like
    this:

    #if( HAS_FEATURE( FEATURE_X ) )
    ... do something if FEATURE_X is defined
    #endif

    I'll #define FEATURE_X to 1 if supported or #define FEATURE_X to 0 if
    not suported. If not defined at all, I want to generate a compile
    error.

    I know some compilers have options to warn about #if used with
    undefined symbols, but I wanted something more portable.
  • viza

    #2
    Re: Macro to cause build failure if parameter not defined?

    Hi

    On Wed, 25 Jun 2008 14:54:18 -0700, skillzero@gmail .com wrote:
    Is there a way to cause a compile/preprocessor error in a macro if a
    preprocessor symbol is not defined? I want to do the equivalent of this
    in a macro:
    >
    #if( !defined( SOME_FLAG ) )
    #error not defined
    #endif
    What's wrong with:

    #ifndef SOME_FLAG
    #error not defined
    #endif

    Not all preprocessors have #error, but it should be an error in any case.

    viza

    Comment

    • skillzero@gmail.com

      #3
      Re: Macro to cause build failure if parameter not defined?

      On Jun 25, 3:17 pm, viza <tom.v...@gmil. comwrote:
      What's wrong with:
      >
      #ifndef SOME_FLAG
      #error not defined
      #endif
      That would require a lot of extra code to test for features (#error if
      not defined then test for the feature). I'm trying to automate it so
      you just use the macro and it does it all in one shot.

      Comment

      • Keith Thompson

        #4
        Re: Macro to cause build failure if parameter not defined?

        "skillzero@gmai l.com" <skillzero@gmai l.comwrites:
        On Jun 25, 3:17 pm, viza <tom.v...@gmil. comwrote:
        What's wrong with:

        #ifndef SOME_FLAG
        #error not defined
        #endif
        >
        That would require a lot of extra code to test for features (#error if
        not defined then test for the feature). I'm trying to automate it so
        you just use the macro and it does it all in one shot.
        The expansion of a macro cannot contain preprocessor directives.

        But it's not all that much extra code. It's 3 lines per feature
        rather than 1. I'm not sure what you mean by "#error if not defined
        then test for the feature"; are you making this more complicated than
        it needs to be?

        You can even test multiple features in one line, at the expense of not
        being able to have separate error messages:

        #if ! ( defined FLAG1 && ! defined FLAG2 && ! defined FLAG3 )
        #error "Something is missing"
        #endif

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Peter Nilsson

          #5
          Re: Macro to cause build failure if parameter not defined?

          skillzero@gmail .com wrote:
          ...What I want is a way to test if a feature flag is defined to
          1 such that if it's not defined at all (as opposed to defined
          to 0), I'll get a compile error. This is to catch cases where
          people test for a feature flag without including the right
          header file, using the right compile flags, or they just typed
          it wrong. I want to use it like this:
          >
          #if( HAS_FEATURE( FEATURE_X ) )
          ... do something if FEATURE_X is defined
          #endif
          >
          I'll #define FEATURE_X to 1 if supported or #define
          FEATURE_X to 0 if not suported. If not defined at all, I
          want to generate a compile error.
          Nothing is guaranteed to produce an error (not even #error
          in C90,) but you can try (untested)...

          #define CAT(a,b) a ## b
          #define CAT2(a,b) CAT(a,b)
          #define HAS_FEATURE_CHE CK_0 1
          #define HAS_FEATURE_CHE CK_1 1
          #define HAS_FEATURE(X) (X / CAT2(HAS_FEATUR E_CHECK_,X))

          Will also fail (or at least diagnose division by zero) if the
          feature is not defined to be 0 or 1.

          --
          Peter

          Comment

          • Nick Keighley

            #6
            Re: Macro to cause build failure if parameter not defined?

            On 25 Jun, 23:17, viza <tom.v...@gmil. comwrote:
            Not all preprocessors have #error [...]
            all ISO-C compliant one do though

            --
            Nick Keighley

            Comment

            • Eric Sosman

              #7
              Re: Macro to cause build failure if parameter not defined?

              skillzero@gmail .com wrote:
              Is there a way to cause a compile/preprocessor error in a macro if a
              preprocessor symbol is not defined? I want to do the equivalent of
              this in a macro:
              >
              #if( !defined( SOME_FLAG ) )
              #error not defined
              #endif
              >
              What I want is a way to test if a feature flag is defined to 1 such
              that if it's not defined at all (as opposed to defined to 0), I'll get
              a compile error. This is to catch cases where people test for a
              feature flag without including the right header file, using the right
              compile flags, or they just typed it wrong. I want to use it like
              this:
              >
              #if( HAS_FEATURE( FEATURE_X ) )
              ... do something if FEATURE_X is defined
              #endif
              >
              I'll #define FEATURE_X to 1 if supported or #define FEATURE_X to 0 if
              not suported. If not defined at all, I want to generate a compile
              error.
              I think

              #define HAS_FEATURE(mac ro) ( (macro) / defined(macro) )

              .... might suit you, if you don't mind an uninformative or maybe
              even misleading diagnostic.

              --
              Eric Sosman
              esosman@ieee-dot-org.invalid

              Comment

              • Peter Nilsson

                #8
                Re: Macro to cause build failure if parameter not defined?

                Eric Sosman wrote:
                skillzero@gmail .com wrote:
                #if( HAS_FEATURE( FEATURE_X ) )
                ... do something if FEATURE_X is defined
                #endif

                I'll #define FEATURE_X to 1 if supported or #define FEATURE_X to 0 if
                not suported. If not defined at all, I want to generate a compile
                error.
                >
                I think
                >
                #define HAS_FEATURE(mac ro) ( (macro) / defined(macro) )
                >
                ... might suit you, if you don't mind an uninformative or maybe
                even misleading diagnostic.
                That runs afoul of 6.10.1p4 [n1256]: "If the token defined is
                generated as
                a result of [the] replacement process ... the behavior is undefined."

                --
                Peter

                Comment

                Working...