annotations.rst 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. Annotations
  2. ===========
  3. Sparse extends C's type system with a number of extra type qualifiers
  4. which add restrictions on what you can do on objects annotated with them.
  5. These qualifiers are specified with GCC's ``__attribute__`` syntax.
  6. address_space(*name*)
  7. ---------------------
  8. This attribute is to be used on pointers to specify that its target is
  9. in address space *name* (an identifier or a constant integer).
  10. Sparse treats pointers with different address spaces as distinct types
  11. and will warn on casts (implicit or explicit) mixing the address spaces.
  12. An exception to this is when the destination type is ``uintptr_t`` or
  13. ``unsigned long`` since the resulting integer value is independent
  14. of the address space and can't be dereferenced without first casting
  15. it back to a pointer type.
  16. bitwise
  17. -------
  18. This attribute is to be used to define new, unique integer types that
  19. cannot be mixed with other types. In particular, you can't mix a
  20. "bitwise" integer with a normal integer expression, and you can't even
  21. mix it with another bitwise expression of a different type.
  22. The integer 0 is special, though, and can be mixed with any bitwise type
  23. since it's safe for all bitwise operations.
  24. Since this qualifier defines new types, it only makes sense to use
  25. it in typedefs, which effectively makes each of these typedefs
  26. a single "bitwise class", incompatible with any other types.
  27. context(*ctxt*, *entry*, *exit*)
  28. --------------------------------
  29. This attribute is to be used on function declarations to specify
  30. the function's entry and exit count for a given context. This
  31. context can be pretty much anything that can be counted.
  32. Sparse will check that the function's entry and exit contexts match, and
  33. that no path through a function is ever entered with conflicting contexts.
  34. In particular, it is designed for doing things like matching up a "lock"
  35. with the pairing "unlock". For example, a function doing a lock should be
  36. annotated with an entry value of 0 and an exit value of 1, the corresponding
  37. unlock function should use the values 1 and 0, and a function that should
  38. only be called on some locked data, release the lock but which doesn't exit
  39. without reacquiring the lock being, should use entry and exit values of 1.
  40. The first argument, *ctxt*, is an expression only used as documentation
  41. to identify the context. Usually, what is used is a pointer to the structure
  42. containing the context, for example, the structure protected by the lock.
  43. See also https://lwn.net/Articles/109066/.
  44. noderef
  45. -------
  46. This attribute is to be used on a r-value to specify it cannot be
  47. dereferenced. A pointer so annotated is in all other aspects exactly
  48. like a pointer but trying to actually access anything through it will
  49. cause a warning.
  50. nocast
  51. ------
  52. This attribute is similar to ``bitwise`` but in a much weaker form.
  53. It warns about explicit or implicit casting to different types.
  54. However, it doesn't warn about the mixing with other types and it easily
  55. gets lost: you can add plain integers to __nocast integer types and the
  56. result will be plain integers.
  57. So, it ends to be more useful for big integers that still need to act
  58. like integers, but you want to make it much less likely that they get
  59. truncated by mistake. For example, a 64-bit integer that you don't want
  60. to mistakenly/silently be returned as int.
  61. See also `Linus' e-mail about __nocast vs __bitwise
  62. <https://lore.kernel.org/linux-mm/CA+55aFzbhYvw7Am9EYgatpjTknBFm9eq+3jBWQHkSCUpnb3HRQ@mail.gmail.com/>`_.
  63. safe
  64. ----
  65. This attribute specifies that the object, which should be a pointer,
  66. is defined to never be NULL or nontrapping.
  67. It causes a warning if the object is tested in a conditional.
  68. force
  69. -----
  70. This attribute is to be used in casts to suppress warnings that would
  71. otherwise be caused by the presence of one of these extra qualifiers.