gcc: -Wmissing-format-attribute WTF



  • moin,

    mögt ihr mir erklären, was das obengenannte gcc-flag genau macht? hier der manpage-auszug, aus dem ich nicht schlau werde:

    -Wmissing-format-attribute
    Warn about function pointers which might be candidates for "format" attributes. Note these are only possible candidates, not absolute ones. GCC will guess that function pointers with "format"
    attributes that are used in assignment, initialization, parameter passing or return statements should have a corresponding "format" attribute in the resulting type. I.e. the left-hand side of
    the assignment or initialization, the type of the parameter variable, or the return type of the containing function respectively should also have a "format" attribute to avoid the warning.

    GCC will also warn about function definitions which might be candidates for "format" attributes. Again, these are only possible candidates. GCC will guess that "format" attributes might be
    appropriate for any function that calls a function like "vprintf" or "vscanf", but this might not always be the case, and some functions for which "format" attributes are appropriate may not be
    detected.



  • Die Fehlermeldung ist übrigens

    warning: function might be possible candidate for `printf' format attribute
    


  • GccFormatter schrieb:

    mögt ihr mir erklären, was das obengenannte gcc-flag genau macht?

    Das Flag aktiviert Warnungen für Funktionen, die nach Meinung des Compilers ein format-Attribut brauchen, aber keins haben.

    Über dieses Attribut ermöglichst du es dem Compiler, zur Compilezeit die Parametertypen und Formatstrings bei Funktionen zu prüfen, die eine printf- oder scanf-ähnlich Parametersyntax haben.



  • MFK schrieb:

    GccFormatter schrieb:

    mögt ihr mir erklären, was das obengenannte gcc-flag genau macht?

    Das Flag aktiviert Warnungen für Funktionen, die nach Meinung des Compilers ein format-Attribut brauchen, aber keins haben.

    Über dieses Attribut ermöglichst du es dem Compiler, zur Compilezeit die Parametertypen und Formatstrings bei Funktionen zu prüfen, die eine printf- oder scanf-ähnlich Parametersyntax haben.

    Ok, danke.

    Wie bekomme ich die warnung bei folgendem Beispiel weg:

    #include <stdio.h>
    #include <stdarg.h>
    
    void myprintv(const char * format, va_list valist) 
    {
        vprintf(format, valist);
    }
    
    void myprint(const char * format, ...)
    {
        va_list valist;
        va_start(valist, format);
        myprintv(format, valist);
        va_end(valist);
    }
    
    int main()
    {
        myprint("%s", "hallo welt\n");
        return 0;
    }
    
    gcc -Wall -Wmissing-format-attribute   test.c
    test.c: In function ‘myprintv’:
    test.c:6: warning: function might be possible candidate for ‘printf’ format attribute
    

    Wenn ich

    __attribute__ ((format (printf, 2, 3)))
    

    in zeile 4 anhänge, beschwert er sich:

    test.c:5: error: args to be formatted is not ‘...’
    test.c:5: error: expected ‘,’ or ‘;’ before ‘{’ token
    

    (die unicode-quotes sind übrigens kaputt)



  • Hier irrt der Compiler. myprintv kann kein format-Attribut gebrauchen. Aber myprint könnte.



  • MFK schrieb:

    Hier irrt der Compiler. myprintv kann kein format-Attribut gebrauchen. Aber myprint könnte.

    Das habe ich vermutet. Aber kann ich die Meldung irgendwie unterdrücken, ohne sie gleich komplett auszuschalten?


Anmelden zum Antworten