Bug 1180993 - Part 2: Add the MOZ_MUST_USE attribute to mfbt, and use it to verify usage of already_AddRefed. r=ehsan
authorMichael Layzell <michael@thelayzells.com>
Mon, 06 Jul 2015 21:42:00 -0400
changeset 284267 6f25cf87649092d52c7446369bc3dd6fb5ec5af9
parent 284266 8b5add472b89d0d60e38113e7aa48fc7c0a4b563
child 284268 7898bcce7c5a4b30b228436954828c852b38571d
push idunknown
push userunknown
push dateunknown
reviewersehsan
bugs1180993
milestone42.0a1
Bug 1180993 - Part 2: Add the MOZ_MUST_USE attribute to mfbt, and use it to verify usage of already_AddRefed. r=ehsan
mfbt/AlreadyAddRefed.h
mfbt/Attributes.h
--- a/mfbt/AlreadyAddRefed.h
+++ b/mfbt/AlreadyAddRefed.h
@@ -24,17 +24,17 @@ struct unused_t;
  * you to assign in a pointer _without_ |AddRef|ing it.  You might want to use
  * this as a return type from a function that returns an already |AddRef|ed
  * pointer.
  *
  * TODO Move already_AddRefed to namespace mozilla.  This has not yet been done
  * because of the sheer number of usages of already_AddRefed.
  */
 template<class T>
-struct already_AddRefed
+struct MOZ_MUST_USE already_AddRefed
 {
   /*
    * We want to allow returning nullptr from functions returning
    * already_AddRefed<T>, for simplicity.  But we also don't want to allow
    * returning raw T*, instead preferring creation of already_AddRefed<T> from
    * a reference counting smart pointer.
    *
    * We address the latter requirement by making the (T*) constructor explicit.
--- a/mfbt/Attributes.h
+++ b/mfbt/Attributes.h
@@ -446,16 +446,19 @@
  *
  *   Use of this annotation is discouraged when a strong reference or one of the above
  *   two annotations can be used instead.
  * MOZ_NO_ADDREF_RELEASE_ON_RETURN: Applies to function declarations.  Makes it
  *   a compile time error to call AddRef or Release on the return value of a
  *   function.  This is intended to be used with operator->() of our smart
  *   pointer classes to ensure that the refcount of an object wrapped in a
  *   smart pointer is not manipulated directly.
+ * MOZ_MUST_USE: Applies to type declarations.  Makes it a compile time error to not
+ *   use the return value of a function which has this type.  This is intended to be
+ *   used with types which it is an error to not use.
  */
 #ifdef MOZ_CLANG_PLUGIN
 #  define MOZ_MUST_OVERRIDE __attribute__((annotate("moz_must_override")))
 #  define MOZ_STACK_CLASS __attribute__((annotate("moz_stack_class")))
 #  define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class")))
 #  define MOZ_TRIVIAL_CTOR_DTOR __attribute__((annotate("moz_trivial_ctor_dtor")))
 #  ifdef DEBUG
      /* in debug builds, these classes do have non-trivial constructors. */
@@ -465,16 +468,17 @@
             MOZ_TRIVIAL_CTOR_DTOR
 #  endif
 #  define MOZ_IMPLICIT __attribute__((annotate("moz_implicit")))
 #  define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT __attribute__((annotate("moz_no_arith_expr_in_arg")))
 #  define MOZ_OWNING_REF __attribute__((annotate("moz_strong_ref")))
 #  define MOZ_NON_OWNING_REF __attribute__((annotate("moz_weak_ref")))
 #  define MOZ_UNSAFE_REF(reason) __attribute__((annotate("moz_weak_ref")))
 #  define MOZ_NO_ADDREF_RELEASE_ON_RETURN __attribute__((annotate("moz_no_addref_release_on_return")))
+#  define MOZ_MUST_USE __attribute__((annotate("moz_must_use")))
 /*
  * It turns out that clang doesn't like void func() __attribute__ {} without a
  * warning, so use pragmas to disable the warning. This code won't work on GCC
  * anyways, so the warning is safe to ignore.
  */
 #  define MOZ_HEAP_ALLOCATOR \
     _Pragma("clang diagnostic push") \
     _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
@@ -488,13 +492,14 @@
 #  define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS /* nothing */
 #  define MOZ_IMPLICIT /* nothing */
 #  define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT /* nothing */
 #  define MOZ_HEAP_ALLOCATOR /* nothing */
 #  define MOZ_OWNING_REF /* nothing */
 #  define MOZ_NON_OWNING_REF /* nothing */
 #  define MOZ_UNSAFE_REF(reason) /* nothing */
 #  define MOZ_NO_ADDREF_RELEASE_ON_RETURN /* nothing */
+#  define MOZ_MUST_USE /* nothing */
 #endif /* MOZ_CLANG_PLUGIN */
 
 #endif /* __cplusplus */
 
 #endif /* mozilla_Attributes_h */