2014-04-11 71 views
0

我試圖抑制-Wformat-非字面的警告。我用屬性((格式(printf的其他地方的成功,但是下面的例子逃避我。使用__attribute __((格式(printf的,

例外.HPP

class Exceptions { 
    ... 

    static void fthrow(Thread* thread, const char* file, int line, Symbol* name, 
        const char* format, ...); 
}; 

exceptions.cpp

__attribute__((format(printf, 5, 6))) 
void Exceptions::fthrow(Thread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) { 
    const int max_msg_size = 1024; 
    va_list ap; 
    va_start(ap, format); 
    char msg[max_msg_size]; 
    vsnprintf(msg, max_msg_size, format, ap); 
    msg[max_msg_size-1] = '\0'; 
    va_end(ap); 
    _throw_msg(thread, file, line, h_name, msg); 
} 

結果

exceptions.cpp:229:16: error: format argument not a string type 
__attribute__((format(printf, 5, 6))) 
      ^   ~ 
exceptions.cpp:235:32: error: format string is not a string literal [-Werror,-Wformat-nonliteral] 
    vsnprintf(msg, max_msg_size, format, ap); 
           ^~~~~~ 

由於它是一個靜態的,應該使用確切的指數,但我已經嘗試了4,5和6,7(如果出一個)類似的失敗。

回答

0

該屬性應該設置在函數的聲明中,而不是定義。

class Exceptions { 
    ... 

    static void fthrow(Thread* thread, const char* file, int line, Symbol* name, 
        const char* format, ...) __attribute__((format(printf, 5, 6))); 
};