[all-commits] [llvm/llvm-project] 92edd7: Allow non-variadic functions to be attributed with...

apple-fcloutier via All-commits all-commits at lists.llvm.org
Tue Jul 5 17:27:05 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 92edd74b37c7a96b1d47dc67cda7f92b65066025
      https://github.com/llvm/llvm-project/commit/92edd74b37c7a96b1d47dc67cda7f92b65066025
  Author: Félix Cloutier <fcloutier at apple.com>
  Date:   2022-07-05 (Tue, 05 Jul 2022)

  Changed paths:
    M clang/docs/ReleaseNotes.rst
    M clang/include/clang/Basic/AttrDocs.td
    M clang/include/clang/Basic/DiagnosticSemaKinds.td
    M clang/include/clang/Sema/Sema.h
    M clang/lib/AST/FormatString.cpp
    M clang/lib/Sema/SemaChecking.cpp
    M clang/lib/Sema/SemaDeclAttr.cpp
    M clang/test/Sema/attr-format.c
    M clang/test/Sema/format-strings.c
    M clang/test/SemaCXX/attr-format.cpp

  Log Message:
  -----------
  Allow non-variadic functions to be attributed with `__attribute__((format))`

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

```c++
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:

```c++
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).

Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D112579
rdar://84629099




More information about the All-commits mailing list