]> granicus.if.org Git - clang/commitdiff
Moving the documentation for the format attribute into AttrDocs.
authorAaron Ballman <aaron@aaronballman.com>
Fri, 21 Feb 2014 14:36:13 +0000 (14:36 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Fri, 21 Feb 2014 14:36:13 +0000 (14:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@201857 91177308-0d34-0410-b5e6-96231b3b80d8

docs/AttributeReference.rst
docs/LanguageExtensions.rst
include/clang/Basic/Attr.td
include/clang/Basic/AttrDocs.td

index fd84adc5daa718829c675236d8b1722de4d0c22c..1d41cb32cf6df40777721a4368327281eaa56631 100644 (file)
@@ -291,6 +291,71 @@ not ODR-equivalent.
 Query for this feature with ``__has_attribute(enable_if)``.\r
 \r
 \r
+format (gnu::format)\r
+--------------------\r
+.. csv-table:: Supported Syntaxes\r
+   :header: "GNU", "C++11", "__declspec", "Keyword"\r
+\r
+   "X","X","",""\r
+\r
+Clang supports the ``format`` attribute, which indicates that the function\r
+accepts a ``printf`` or ``scanf``-like format string and corresponding\r
+arguments or a ``va_list`` that contains these arguments.\r
+\r
+Please see `GCC documentation about format attribute\r
+<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details\r
+about attribute syntax.\r
+\r
+Clang implements two kinds of checks with this attribute.\r
+\r
+#. Clang checks that the function with the ``format`` attribute is called with\r
+   a format string that uses format specifiers that are allowed, and that\r
+   arguments match the format string.  This is the ``-Wformat`` warning, it is\r
+   on by default.\r
+\r
+#. Clang checks that the format string argument is a literal string.  This is\r
+   the ``-Wformat-nonliteral`` warning, it is off by default.\r
+\r
+   Clang implements this mostly the same way as GCC, but there is a difference\r
+   for functions that accept a ``va_list`` argument (for example, ``vprintf``).\r
+   GCC does not emit ``-Wformat-nonliteral`` warning for calls to such\r
+   fuctions.  Clang does not warn if the format string comes from a function\r
+   parameter, where the function is annotated with a compatible attribute,\r
+   otherwise it warns.  For example:\r
+\r
+   .. code-block:: c\r
+\r
+     __attribute__((__format__ (__scanf__, 1, 3)))\r
+     void foo(const char* s, char *buf, ...) {\r
+       va_list ap;\r
+       va_start(ap, buf);\r
+\r
+       vprintf(s, ap); // warning: format string is not a string literal\r
+     }\r
+\r
+   In this case we warn because ``s`` contains a format string for a\r
+   ``scanf``-like function, but it is passed to a ``printf``-like function.\r
+\r
+   If the attribute is removed, clang still warns, because the format string is\r
+   not a string literal.\r
+\r
+   Another example:\r
+\r
+   .. code-block:: c\r
+\r
+     __attribute__((__format__ (__printf__, 1, 3)))\r
+     void foo(const char* s, char *buf, ...) {\r
+       va_list ap;\r
+       va_start(ap, buf);\r
+\r
+       vprintf(s, ap); // warning\r
+     }\r
+\r
+   In this case Clang does not warn because the format string ``s`` and\r
+   the corresponding arguments are annotated.  If the arguments are\r
+   incorrect, the caller of ``foo`` will receive a warning.\r
+\r
+\r
 no_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address)\r
 -----------------------------------------------------------------------------------------------------------\r
 .. csv-table:: Supported Syntaxes\r
index 13aae3443953b4ab81142c102b22975f0224610a..ce4656e508022b68a412b768c9e1a624dab74f7d 100644 (file)
@@ -1647,63 +1647,3 @@ with :doc:`ThreadSanitizer`.
 
 Use ``__has_feature(memory_sanitizer)`` to check if the code is being built
 with :doc:`MemorySanitizer`.
-
-Format String Checking
-======================
-
-Clang supports the ``format`` attribute, which indicates that the function
-accepts a ``printf`` or ``scanf``-like format string and corresponding
-arguments or a ``va_list`` that contains these arguments.
-
-Please see `GCC documentation about format attribute
-<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details
-about attribute syntax.
-
-Clang implements two kinds of checks with this attribute.
-
-#. Clang checks that the function with the ``format`` attribute is called with
-   a format string that uses format specifiers that are allowed, and that
-   arguments match the format string.  This is the ``-Wformat`` warning, it is
-   on by default.
-
-#. Clang checks that the format string argument is a literal string.  This is
-   the ``-Wformat-nonliteral`` warning, it is off by default.
-
-   Clang implements this mostly the same way as GCC, but there is a difference
-   for functions that accept a ``va_list`` argument (for example, ``vprintf``).
-   GCC does not emit ``-Wformat-nonliteral`` warning for calls to such
-   fuctions.  Clang does not warn if the format string comes from a function
-   parameter, where the function is annotated with a compatible attribute,
-   otherwise it warns.  For example:
-
-   .. code-block:: c
-
-     __attribute__((__format__ (__scanf__, 1, 3)))
-     void foo(const char* s, char *buf, ...) {
-       va_list ap;
-       va_start(ap, buf);
-
-       vprintf(s, ap); // warning: format string is not a string literal
-     }
-
-   In this case we warn because ``s`` contains a format string for a
-   ``scanf``-like function, but it is passed to a ``printf``-like function.
-
-   If the attribute is removed, clang still warns, because the format string is
-   not a string literal.
-
-   Another example:
-
-   .. code-block:: c
-
-     __attribute__((__format__ (__printf__, 1, 3)))
-     void foo(const char* s, char *buf, ...) {
-       va_list ap;
-       va_start(ap, buf);
-
-       vprintf(s, ap); // warning
-     }
-
-   In this case Clang does not warn because the format string ``s`` and
-   the corresponding arguments are annotated.  If the arguments are
-   incorrect, the caller of ``foo`` will receive a warning.
index a23254164d3613f155b49631acc8939c0b11729d..3b70c4cc3e58d0a56a6620720da5005bfdcb1d41 100644 (file)
@@ -672,7 +672,7 @@ def Format : InheritableAttr {
               IntArgument<"FirstArg">];
   let Subjects = SubjectList<[ObjCMethod, Block, HasFunctionProto], WarnDiag,
                              "ExpectedFunction">;
-  let Documentation = [Undocumented];
+  let Documentation = [FormatDocs];
 }
 
 def FormatArg : InheritableAttr {
index bf7bd4d3e99a162881870fad40ea18a040b3afc2..bb18944c05c95ff8298a7ceb6f6aa9a51beed681 100644 (file)
@@ -749,3 +749,66 @@ expression is compared to the type tag.  There are two supported flags:
                                                         // is not a null pointer
   }];
 }
+
+def FormatDocs : Documentation {
+  let Category = DocCatFunction;
+  let Content = [{
+
+Clang supports the ``format`` attribute, which indicates that the function
+accepts a ``printf`` or ``scanf``-like format string and corresponding
+arguments or a ``va_list`` that contains these arguments.
+
+Please see `GCC documentation about format attribute
+<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details
+about attribute syntax.
+
+Clang implements two kinds of checks with this attribute.
+
+#. Clang checks that the function with the ``format`` attribute is called with
+   a format string that uses format specifiers that are allowed, and that
+   arguments match the format string.  This is the ``-Wformat`` warning, it is
+   on by default.
+
+#. Clang checks that the format string argument is a literal string.  This is
+   the ``-Wformat-nonliteral`` warning, it is off by default.
+
+   Clang implements this mostly the same way as GCC, but there is a difference
+   for functions that accept a ``va_list`` argument (for example, ``vprintf``).
+   GCC does not emit ``-Wformat-nonliteral`` warning for calls to such
+   fuctions.  Clang does not warn if the format string comes from a function
+   parameter, where the function is annotated with a compatible attribute,
+   otherwise it warns.  For example:
+
+   .. code-block:: c
+
+     __attribute__((__format__ (__scanf__, 1, 3)))
+     void foo(const char* s, char *buf, ...) {
+       va_list ap;
+       va_start(ap, buf);
+
+       vprintf(s, ap); // warning: format string is not a string literal
+     }
+
+   In this case we warn because ``s`` contains a format string for a
+   ``scanf``-like function, but it is passed to a ``printf``-like function.
+
+   If the attribute is removed, clang still warns, because the format string is
+   not a string literal.
+
+   Another example:
+
+   .. code-block:: c
+
+     __attribute__((__format__ (__printf__, 1, 3)))
+     void foo(const char* s, char *buf, ...) {
+       va_list ap;
+       va_start(ap, buf);
+
+       vprintf(s, ap); // warning
+     }
+
+   In this case Clang does not warn because the format string ``s`` and
+   the corresponding arguments are annotated.  If the arguments are
+   incorrect, the caller of ``foo`` will receive a warning.
+  }];
+}
\ No newline at end of file