Re: [RFC PATCH 1/7] compiler_attributes.h: add __attribute__((format_arg)) shorthand

2019-01-09 Thread Miguel Ojeda
Hi Rasmus,

On Fri, Nov 2, 2018 at 11:43 AM Rasmus Villemoes
 wrote:
>
> On 2018-11-02 11:36, Miguel Ojeda wrote:
> > Hi Rasmus,
> >
> > On Sat, Oct 27, 2018 at 2:06 PM Miguel Ojeda
> >  wrote:
> >>
> >> I did quick check and gcc >= 4.1, clang >= 3.0, icc >= 13 compilers
> >> seem to support it (or at least recognize it, even if they just ignore
> >> it), so we do not need to make it optional, no? Did I miss some case?
> >
> > compiler-attributes landed -- do you want to do the v2 of this (i.e.
> > #defining it unconditionally) or you prefer I simply fix up the patch?
>
> I'll wait a few more days for comments on fmtcheck(), and allow you to
> do the 'optional'/'required' clarification in the meantime. I hope to
> get fmtcheck() picked up for -next around -rc3 or so.

Any news about fmtcheck()? The "Optional" clarification landed a few
weeks ago, in case you wanted to know. Hopefully it is more clear now.

Cheers,
Miguel


Re: [RFC PATCH 1/7] compiler_attributes.h: add __attribute__((format_arg)) shorthand

2018-11-02 Thread Rasmus Villemoes
On 2018-11-02 11:36, Miguel Ojeda wrote:
> Hi Rasmus,
> 
> On Sat, Oct 27, 2018 at 2:06 PM Miguel Ojeda
>  wrote:
>>
>> I did quick check and gcc >= 4.1, clang >= 3.0, icc >= 13 compilers
>> seem to support it (or at least recognize it, even if they just ignore
>> it), so we do not need to make it optional, no? Did I miss some case?
> 
> compiler-attributes landed -- do you want to do the v2 of this (i.e.
> #defining it unconditionally) or you prefer I simply fix up the patch?

I'll wait a few more days for comments on fmtcheck(), and allow you to
do the 'optional'/'required' clarification in the meantime. I hope to
get fmtcheck() picked up for -next around -rc3 or so.

Rasmus


Re: [RFC PATCH 1/7] compiler_attributes.h: add __attribute__((format_arg)) shorthand

2018-11-02 Thread Miguel Ojeda
Hi Rasmus,

On Sat, Oct 27, 2018 at 2:06 PM Miguel Ojeda
 wrote:
>
> I did quick check and gcc >= 4.1, clang >= 3.0, icc >= 13 compilers
> seem to support it (or at least recognize it, even if they just ignore
> it), so we do not need to make it optional, no? Did I miss some case?

compiler-attributes landed -- do you want to do the v2 of this (i.e.
#defining it unconditionally) or you prefer I simply fix up the patch?

(I will improve the docs as well in another patch about the "optional" naming).

Cheers,
Miguel


Re: [RFC PATCH 1/7] compiler_attributes.h: add __attribute__((format_arg)) shorthand

2018-10-29 Thread Miguel Ojeda
On Mon, Oct 29, 2018 at 11:20 AM Rasmus Villemoes
 wrote:
>
> No idea. I think I didn't really know what was meant by
> required/optional.

Yeah, no worries, "optional" here is intended to mean "we cannot
define it unconditionally (yet)". The word leads to confusion, I
agree. I will improve the wording of the comment in the top and/or
change the word after this lands.

Cheers,
Miguel


Re: [RFC PATCH 1/7] compiler_attributes.h: add __attribute__((format_arg)) shorthand

2018-10-29 Thread Rasmus Villemoes
On 2018-10-27 14:06, Miguel Ojeda wrote:
> Hi Rasmus,
> 
> On Sat, Oct 27, 2018 at 1:24 AM Rasmus Villemoes
>  wrote:
>>
>> +/*
>> + * Optional
> 
> I did quick check and gcc >= 4.1, clang >= 3.0, icc >= 13 compilers
> seem to support it (or at least recognize it, even if they just ignore
> it), so we do not need to make it optional, no? Did I miss some case?

No idea. I think I didn't really know what was meant by
required/optional. E.g. something like __aligned must be supported and
honoured for correctness, while format_arg is just about getting some
diagnostics, and assume_aligned just allows for certain optimizations,
so it doesn't really matter whether every single compiler understands
them. It does seem that both icc and clang understand format_arg and
issues warnings when the template doesn't match the varargs, so in that
sense we can make it "required", though it seems a little backwards to
define required in terms of what the current range of compilers support.

Rasmus


Re: [RFC PATCH 1/7] compiler_attributes.h: add __attribute__((format_arg)) shorthand

2018-10-27 Thread Miguel Ojeda
Hi Rasmus,

On Sat, Oct 27, 2018 at 1:24 AM Rasmus Villemoes
 wrote:
>
> diff --git a/include/linux/compiler_attributes.h 
> b/include/linux/compiler_attributes.h
> index 6b28c1b7310c..08264df52322 100644
> --- a/include/linux/compiler_attributes.h
> +++ b/include/linux/compiler_attributes.h
> @@ -32,6 +32,7 @@
>  # define __GCC4_has_attribute___assume_aligned__  (__GNUC_MINOR__ >= 9)
>  # define __GCC4_has_attribute___designated_init__ 0
>  # define __GCC4_has_attribute___externally_visible__  1
> +# define __GCC4_has_attribute___format_arg__  1

Thank you Rasmus for doing this already on top of this tree!

>  # define __GCC4_has_attribute___noclone__ 1
>  # define __GCC4_has_attribute___optimize__1
>  # define __GCC4_has_attribute___nonstring__   0
> @@ -140,6 +141,18 @@
>  #define __printf(a, b)  __attribute__((__format__(printf, a, 
> b)))
>  #define __scanf(a, b)   __attribute__((__format__(scanf, a, 
> b)))
>
> +/*
> + * Optional

I did quick check and gcc >= 4.1, clang >= 3.0, icc >= 13 compilers
seem to support it (or at least recognize it, even if they just ignore
it), so we do not need to make it optional, no? Did I miss some case?

Thanks!

Cheers,
Miguel


[RFC PATCH 1/7] compiler_attributes.h: add __attribute__((format_arg)) shorthand

2018-10-26 Thread Rasmus Villemoes
The __format_arg attribute tells gcc that it can use a specific
argument to the annotated function as the format string for the
purpose of type-checking a surrounding __printf function call. For
example, assuming one has a fmtcheck function declared as

  const char *fmtcheck(const char *, const char *, unsigned) __format_arg(2);

and this is used in

  sprintf(buf, fmtcheck(what->ever, "%d %lx", 0), i, m)

gcc checks that the varargs (i and m) matches the second argument to the
fmtcheck function, i.e. that they are (int, long). With

  sprintf(buf, what->ever, i, m)

the compiler cannot do any type checking.

Even a static inline fmtcheck() that just returns its first argument
would provide documentation for which specifiers what->ever is supposed
to contain, but we'll implement an actual run-time check later.

Signed-off-by: Rasmus Villemoes 
---
 include/linux/compiler_attributes.h | 13 +
 1 file changed, 13 insertions(+)

diff --git a/include/linux/compiler_attributes.h 
b/include/linux/compiler_attributes.h
index 6b28c1b7310c..08264df52322 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h
@@ -32,6 +32,7 @@
 # define __GCC4_has_attribute___assume_aligned__  (__GNUC_MINOR__ >= 9)
 # define __GCC4_has_attribute___designated_init__ 0
 # define __GCC4_has_attribute___externally_visible__  1
+# define __GCC4_has_attribute___format_arg__  1
 # define __GCC4_has_attribute___noclone__ 1
 # define __GCC4_has_attribute___optimize__1
 # define __GCC4_has_attribute___nonstring__   0
@@ -140,6 +141,18 @@
 #define __printf(a, b)  __attribute__((__format__(printf, a, 
b)))
 #define __scanf(a, b)   __attribute__((__format__(scanf, a, 
b)))
 
+/*
+ * Optional
+ *
+ *   gcc: 
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format_005farg-function-attribute
+ * clang: apparently supported, but undocumented
+ */
+#if __has_attribute(__format_arg__)
+# define __format_arg(n) __attribute__((__format_arg__(n)))
+#else
+# define __format_arg(n)
+#endif
+
 /*
  *   gcc: 
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute
  * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline
-- 
2.19.1.6.gbde171bbf5