Skip to content

Commit

Permalink
Bug 1331434 - Part 2: Add MOZ_MAY_CALL_AFTER_MUST_RETURN and MOZ_MUST…
Browse files Browse the repository at this point in the history
…_RETURN_FROM_CALLER annotations, r=ehsan

MozReview-Commit-ID: 1o2egvdhkqT
  • Loading branch information
mystor committed Mar 8, 2017
1 parent 4689eec commit 46ade67
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
41 changes: 39 additions & 2 deletions dom/bindings/ErrorResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,48 @@ class TErrorResult {
operator ErrorResult&();
operator OOMReporter&();

void Throw(nsresult rv) {
void MOZ_MUST_RETURN_FROM_CALLER Throw(nsresult rv) {
MOZ_ASSERT(NS_FAILED(rv), "Please don't try throwing success");
AssignErrorCode(rv);
}

// This method acts identically to the `Throw` method, however, it does not
// have the MOZ_MUST_RETURN_FROM_CALLER static analysis annotation. It is
// intended to be used in situations when additional work needs to be
// performed in the calling function after the Throw method is called.
//
// In general you should prefer using `Throw`, and returning after an error,
// for example:
//
// if (condition) {
// aRv.Throw(NS_ERROR_FAILURE);
// return;
// }
//
// or
//
// if (condition) {
// aRv.Throw(NS_ERROR_FAILURE);
// }
// return;
//
// However, if you need to do some other work after throwing, such as:
//
// if (condition) {
// aRv.ThrowWithCustomCleanup(NS_ERROR_FAILURE);
// }
// // Do some important clean-up work which couldn't happen earlier.
// // We want to do this clean-up work in both the success and failure cases.
// CleanUpImportantState();
// return;
//
// Then you'll need to use ThrowWithCustomCleanup to get around the static
// analysis, which would complain that you are doing work after the call to
// `Throw()`.
void ThrowWithCustomCleanup(nsresult rv) {
Throw(rv);
}

// Duplicate our current state on the given TErrorResult object. Any
// existing errors or messages on the target will be suppressed before
// cloning. Our own error state remains unchanged.
Expand Down Expand Up @@ -307,7 +344,7 @@ class TErrorResult {
}

// Support for uncatchable exceptions.
void ThrowUncatchableException() {
void MOZ_MUST_RETURN_FROM_CALLER ThrowUncatchableException() {
Throw(NS_ERROR_UNCATCHABLE_EXCEPTION);
}
bool IsUncatchableException() const {
Expand Down
2 changes: 1 addition & 1 deletion js/public/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ ExposeValueToActiveJS(const Value& v)

/************************************************************************/

static inline Value
static inline MOZ_MAY_CALL_AFTER_MUST_RETURN Value
NullValue()
{
Value v;
Expand Down
1 change: 1 addition & 0 deletions mfbt/RefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class RefPtr
}

already_AddRefed<T>
MOZ_MAY_CALL_AFTER_MUST_RETURN
forget()
// return the value of mRawPtr and null out mRawPtr. Useful for
// already_AddRefed return values.
Expand Down
2 changes: 1 addition & 1 deletion xpcom/base/nsCOMPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ class nsCOMPtr final

// Return the value of mRawPtr and null out mRawPtr. Useful for
// already_AddRefed return values.
already_AddRefed<T> forget()
already_AddRefed<T> MOZ_MAY_CALL_AFTER_MUST_RETURN forget()
{
T* temp = nullptr;
swap(temp);
Expand Down

0 comments on commit 46ade67

Please sign in to comment.