[PATCH] D112579: Allow non-variadic functions to be attributed with `__attribute__((format))`

Félix Cloutier via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 26 15:35:17 PDT 2021


fcloutier created this revision.
fcloutier added reviewers: dcoughlin, doug.gregor, rsmith.
fcloutier added a project: clang.
Herald added a reviewer: aaron.ballman.
fcloutier requested review of this revision.
Herald added a subscriber: cfe-commits.

Clang only allows you to use `__attribute__((format))` on variadic functions. There are legit use cases for `__attribute__((format))` on non-variadic functions, such as:

(1) variadic templates

  template<typename… Args>
  void print(const char *fmt, Args… &&args) __attribute__((format(1, 2))); // error: format attribute requires variadic function

(2) functions which take fixed arguments and a custom format:

  void print_number_string(const char *fmt, unsigned number, const char *string) __attribute__((format(1, 2)));
  // ^error: format attribute requires variadic function
  
  void foo(void) {
      print_number_string(“%08x %s\n”, 0xdeadbeef, “hello”);
      print_number_string(“%d %s”, 0xcafebabe, “bar”);
  }

This change allows Clang users to attach `__attribute__((format))` to non-variadic functions, including functions with C++ variadic templates. It replaces the error with a GCC compatibility warning and improves the type checker to ensure that received arrays are treated like pointers (this is a possibility in C++ since references to template types can bind to arrays).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112579

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/AST/FormatString.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Sema/attr-format.c
  clang/test/Sema/attr-format.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112579.382473.patch
Type: text/x-patch
Size: 6067 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211026/812d2f6e/attachment.bin>


More information about the cfe-commits mailing list