2011-11-16 13 views
19

पर जीसीसी चेतावनी अक्षम करें मैं इस काम को (बिना जीसीसी 4.6 में) बनाने के लिए कोशिश कर रहा हूं।अस्थायी रूप से redefinition

#define FOO "" 
#define BAR "" 

#if .... 
    #define FOO "Foo, good sir" 
#endif 

#if ... 
    #define BAR "Bar, my lady" 
#endif 
.... 

#define EVERYTHING  FOO BAR ... 

मैं एक बहुत इनमें से जा रहा हूँ। तो इसे इस तरह से कर रहे हैं:

#if ... 
    #define FOO "Foo" 
#else 
    #define FOO "" 
#endif 

बहुत सारे कोड बचाता है, और इसे और अधिक पठनीय बनाता है। चेतावनी है कि मैं मिलता है:

चेतावनी: "foo"

[डिफ़ॉल्ट रूप से सक्षम] नए सिरे से परिभाषित वहाँ इस विशेष खंड के लिए कोड में इस चेतावनी को निष्क्रिय करने के लिए कोई तरीका है? मुझे कुछ चेतावनियों को अक्षम करने के लिए Diagnostic Pragmas मिला, लेकिन मुझे यह चेतावनी नहीं मिली है कि Options to Request or Suppress Warnings की इस सूची में) को यहां अक्षम करने की आवश्यकता है।

कोई भी यह कैसे जानता है? या #else #define उन सभी को खाली स्ट्रिंग पर जाने से बचने का एक अलग तरीका है?

+8

आप 'उपयोग कर सकते हैं -Wp, -w' निष्क्रिय करने के लिए [पूर्वप्रक्रमक चेतावनी] (http://gcc.gnu.org/onlinedocs/cpp/Invocation.html#Invocation) – osgx

+1

यह चेतावनी सोवियत रूस से आता है जीसीसी में "cccp.c" नाम की फ़ाइल (2.95 संस्करण के रूप में), और इसे बंद नहीं किया जा सकता है। इस चेतावनी को अलग-अलग अक्षम करने के लिए अभी भी कोई विकल्प नहीं है [यहां तक ​​कि गिट हेड, जीसीसी/libcpp/macro.c] में भी (http://gcc.gnu.org/git/?p=gcc.git;a=blob;f= libcpp/macro.c; h = f3139590d5045b128709296d1abbb81753284f10; hb = HEAD # l2527) (और उसी फ़ाइल के लाइन 2 9 4 9) – osgx

+0

@osgx अगर आप यह टिप्पणी एक उत्तर देते हैं, तो मैं इसे स्वीकार करना चाहता हूं। –

उत्तर

6

यह चेतावनी (2.95 संस्करण के रूप में, "सोवियत रूस" से इस फाइल है?) जीसीसी में "cccp.c" नाम की फ़ाइल से आता है, और यह नहीं कर सकते बंद कर दिया जाना चाहिए। गिट हेड, gcc/libcpp/macro.c file (लाइन 2527 और line 2994 उसी फ़ाइल के) में भी व्यक्तिगत रूप से इस चेतावनी को अक्षम करने का कोई विकल्प नहीं है)

मैं सूत्रों को थोड़ा सा उद्धृत करूंगा।

2525 /* Returns nonzero if a macro redefinition warning is required. */ 
2526 static bool 
2527 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node, 
2528      const cpp_macro *macro2) 
2529 { 
... 
2537 /* Suppress warnings for builtins that lack the NODE_WARN flag. */ 
.. 
2545 /* Redefinitions of conditional (context-sensitive) macros, on 
2546  the other hand, must be allowed silently. */ 
... 
2550 /* Redefinition of a macro is allowed if and only if the old and new 
2551  definitions are the same. (6.10.3 paragraph 2). */ 
... 
2561 /* Check parameter spellings. */ 
... 
2566 /* Check the replacement text or tokens. */ 
... 
2573 for (i = 0; i < macro1->count; i++) 
2574  if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i])) 
2575  return true; 

तो आपके मामले में warn_of_redefinition फ़ंक्शन सत्य वापस आ जाएगा। और यहां वास्तविक उपयोग है:

2989 if (node->type == NT_MACRO) 
2990  { 
2991  if (CPP_OPTION (pfile, warn_unused_macros)) 
2992   _cpp_warn_if_unused_macro (pfile, node, NULL); 
2993 
2994  if (warn_of_redefinition (pfile, node, macro)) 
2995   { 
2996   const int reason = (node->flags & NODE_BUILTIN) 
2997        ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; 
2998   bool warned; 
2999 
3000   warned = cpp_pedwarning_with_line (pfile, reason, 
3001            pfile->directive_line, 0, 
3002            "\"%s\" redefined", 
3003            NODE_NAME (node)); 
3004 
3005   if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 
3006    cpp_error_with_line (pfile, CPP_DL_NOTE, 
3007         node->value.macro->line, 0, 
3008       "this is the location of the previous definition"); 
3009   } 
3010  } 

तो, कोई विशिष्ट विकल्प नहीं है। और ग्रेग द्वारा उत्तर इस मामले के लिए अच्छा है, बस अपनी परिभाषा को फिर से परिभाषित करने से पहले परिभाषित करें।

+9

सोवियत रूस में, मैक्रो आपको फिर से परिभाषित करता है! – einpoklum

31

#undef उपयोग करके देखें:

#define FOO "" 

#if .... 
    #undef FOO 
    #define FOO "Foo, good sir" 
#endif 
0

या अन्यथा उपयोग करने का प्रयास करें।

#if ... 
# define FOO "Foo, doof sir" 
#else 
# define FOO "" 
#endif 
संबंधित मुद्दे