Skip to content

Printf format checks are skipped for constexpr format strings #55805

Closed
@paradust7

Description

@paradust7

demo.cpp:

#include <cstdio>

constexpr const char* foo() {
  return "%s %d";
}

struct Bar {
    static constexpr char value[] = {'%', 's', '%', 'd', '\0'};
};
constexpr char Bar::value[];

int main() {
   printf(foo(), "abc", "def");
   printf(Bar::value, "abc", "def");
   return 0;
}

Clang does not produce any warning. gcc 11.2.0 produces both warnings:

demo.cpp: In function ‘int main()’:
demo.cpp:13:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘const char*’ [-Wformat=]
demo.cpp:14:16: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘const char*’ [-Wformat=]

Clang version is:

clang version 15.0.0 (https://github.com/llvm/llvm-project.git a72cc958a386e5fc97e8c30137fb56eb77ef571c)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Metadata

Metadata

Assignees

Labels

clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second party

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions