[PATCH] D132413: [NFC] Make format() more amenable to format attributes

FĂ©lix Cloutier via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 22 14:43:34 PDT 2022


fcloutier created this revision.
fcloutier added a reviewer: ahatanak.
fcloutier added a project: LLVM.
Herald added subscribers: hiraditya, mgorny.
Herald added a project: All.
fcloutier requested review of this revision.
Herald added a subscriber: llvm-commits.

llvm/Support/Format.h provides format(), a variadic template function that accepts a printf-style format string and the corresponding fixed format arguments. While this function does forward its arguments to `snprintf`, which is type-checked thanks to the gnu::format attribute on platforms where that attribute is supported, it cannot itself have that attribute because GCC does not allow the format attribute on non-variadic functions (variadic template functions not excepted).

Regardless, we can change the implementation of format() so that it would be more amenable to this attribute. Recently, clang did gain support for gnu::format on non-variadic functions. We still can't use it because GCC would reject the code. However, this does allow vendors who never build LLVM with GCC themselves to maintain a patch which has that attribute enabled, catching a few errors in the process. The changes to the format_object class allow us to attribute its constructor with gnu::format (which, in turns, allows us to attribute format() with gnu::format as well).

In addition, we create a new function, format_chk(Expected, Fmt, T...), for the case where Fmt isn't known at compile-time. At runtime, Fmt is verified to have specifiers which are compatible with Expected's.

There will be another PR fixing any eventual format specifier mistakes found. In warning flags parlance, this would allow LLVM to build with -Wformat -Wformat-nonliteral.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132413

Files:
  llvm/include/llvm/Support/Format.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/Format.cpp
  llvm/lib/TableGen/SetTheory.cpp
  llvm/unittests/Support/CMakeLists.txt
  llvm/unittests/Support/FormatChkTest.cpp
  llvm/utils/TableGen/GlobalISel/GIMatchDag.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132413.454615.patch
Type: text/x-patch
Size: 26243 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220822/cd36b6fb/attachment.bin>


More information about the llvm-commits mailing list