[cfe-dev] Incorrect warning with -Wformat=2

Csaba Raduly rcsaba at gmail.com
Mon Nov 15 06:33:21 PST 2010


Hi all,
The following code:

enum error_type_t {
  none, warn, err, fatal
};

void error(error_type_t p_et, const char *fmt, ...)
    __attribute__ ((__format__ (__printf__, 2, 3)))
     ;

class ErrorContext {
public:

  ErrorContext();

  ErrorContext(const char *fmt, ...)
    __attribute__ ((__format__ (__printf__, 2, 3))); // 'this' is at 1

  ~ErrorContext();

  static void error(error_type_t p_et, const char *fmt, ...)
    __attribute__ ((__format__ (__printf__, 2, 3)))
      ;

  static void error_internal(const char *fmt, ...)
    __attribute__ ((__format__ (__printf__, 1, 2), __noreturn__));
};

int main(int argc, char **argv)
{
  if (argc < 2 ) {
    ErrorContext ec("While checking arguments");
    if (argc < 1) {
      ec.error_internal("argc=%d is not possible", argc);
    }
    ec.error(fatal, "Not enough arguments for %s", argv[0]); // line 34
    error   (fatal, "Not enough arguments for %s", argv[0]);
  }
  return 0;
}

and the following commandline

clang++ -c -Wformat=2 example.cc

generates a spurious warning:

example.cc:34:14: warning: format string is not a string literal
[-Wformat-nonliteral]
    ec.error(fatal, "Not enough arguments for %s", argv[0]);
             ^~~~~

The handling of the static method "error" seems to be inconsistent
with the free function "error" (there is no warning for line 35).

$ clang++ --version
clang version 2.9 (trunk 119134)
Target: x86_64-unknown-linux-gnu
Thread model: posix

-- 
GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds



More information about the cfe-dev mailing list