write_file: add format attribute

This gives us compile-time checking of our format strings, which is a good thing. I had also hoped it would help with confusing write_file() and write_file_buf(), since the former's "..." can make it match the signature of the latter. But given that the buffer for write_file_buf() is generally not a string literal, the compiler won't complain unless -Wformat-nonliteral is on, and that creates a ton of false positives elsewhere in the code base. While we're there, let's also give the function a docstring, which it never had. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 8, 2016 at 05:12 UTC e04d08a4b3373972717c805ae8e788219b873b2a
1 file changed +8
cache.h
+8
@@ -1740,6 +1740,14 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
1740 */
1741 extern void write_file_buf(const char *path, const char *buf, size_t len);
1742
1743 +/**
1744 + * Like write_file_buf(), but format the contents into a buffer first.
1745 + * Additionally, write_file() will append a newline if one is not already
1746 + * present, making it convenient to write text files:
1747 + *
1748 + * write_file(path, "counter: %d", ctr);
1749 + */
1750 +__attribute__((format (printf, 2, 3)))
1751 extern void write_file(const char *path, const char *fmt, ...);
1752
1753 /* pager.c */