| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef mozilla_StaticAnalysisFunctions_h |
| 8 |
#define mozilla_StaticAnalysisFunctions_h |
| 9 |
|
| 10 |
#ifndef __cplusplus |
| 11 |
# ifndef bool |
| 12 |
# include <stdbool.h> |
| 13 |
# endif |
| 14 |
# define MOZ_CONSTEXPR |
| 15 |
#else // __cplusplus |
| 16 |
# include "mozilla/Attributes.h" |
| 17 |
# define MOZ_CONSTEXPR constexpr |
| 18 |
#endif |
| 19 |
/* |
| 20 |
* Functions that are used as markers in Gecko code for static analysis. Their |
| 21 |
* purpose is to have different AST nodes generated during compile time and to |
| 22 |
* match them based on different checkers implemented in build/clang-plugin |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifdef MOZ_CLANG_PLUGIN |
| 26 |
|
| 27 |
# ifdef __cplusplus |
| 28 |
/** |
| 29 |
* MOZ_KnownLive - used to opt an argument out of the CanRunScript checker so |
| 30 |
* that we don't check it if is a strong ref. |
| 31 |
* |
| 32 |
* Example: |
| 33 |
* canRunScript(MOZ_KnownLive(rawPointer)); |
| 34 |
*/ |
| 35 |
template <typename T> |
| 36 |
static MOZ_ALWAYS_INLINE T* MOZ_KnownLive(T* ptr) { |
| 37 |
return ptr; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Ditto, but for references. |
| 42 |
*/ |
| 43 |
template <typename T> |
| 44 |
static MOZ_ALWAYS_INLINE T& MOZ_KnownLive(T& ref) { |
| 45 |
return ref; |
| 46 |
} |
| 47 |
|
| 48 |
# endif |
| 49 |
|
| 50 |
/** |
| 51 |
* MOZ_AssertAssignmentTest - used in MOZ_ASSERT in order to test the possible |
| 52 |
* presence of assignment instead of logical comparisons. |
| 53 |
* |
| 54 |
* Example: |
| 55 |
* MOZ_ASSERT(retVal = true); |
| 56 |
*/ |
| 57 |
static MOZ_ALWAYS_INLINE MOZ_CONSTEXPR bool MOZ_AssertAssignmentTest( |
| 58 |
bool exprResult) { |
| 59 |
return exprResult; |
| 60 |
} |
| 61 |
|
| 62 |
# define MOZ_CHECK_ASSERT_ASSIGNMENT(expr) MOZ_AssertAssignmentTest(!!(expr)) |
| 63 |
|
| 64 |
#else |
| 65 |
|
| 66 |
# define MOZ_CHECK_ASSERT_ASSIGNMENT(expr) (!!(expr)) |
| 67 |
# define MOZ_KnownLive(expr) (expr) |
| 68 |
|
| 69 |
#endif /* MOZ_CLANG_PLUGIN */ |
| 70 |
#endif /* StaticAnalysisFunctions_h */ |