| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#ifndef VariableUsageHelpers_h__ |
| 6 |
#define VariableUsageHelpers_h__ |
| 7 |
|
| 8 |
#include "plugin.h" |
| 9 |
|
| 10 |
/// Returns a list of the statements where the given declaration is used as an |
| 11 |
/// rvalue (within the provided function). |
| 12 |
/// |
| 13 |
/// WARNING: incomplete behaviour/implementation for general-purpose use outside |
| 14 |
/// of escapesFunction(). This only detects very basic usages (see |
| 15 |
/// implementation for more details). |
| 16 |
std::vector<const Stmt *> getUsageAsRvalue(const ValueDecl *ValueDeclaration, |
| 17 |
const FunctionDecl *FuncDecl); |
| 18 |
|
| 19 |
/// This is the error enumeration for escapesFunction(), describing all the |
| 20 |
/// possible error cases. |
| 21 |
enum class EscapesFunctionError { |
| 22 |
ConstructorDeclNotFound = 1, |
| 23 |
FunctionDeclNotFound, |
| 24 |
FunctionIsBuiltin, |
| 25 |
FunctionIsVariadic, |
| 26 |
ExprNotInCall, |
| 27 |
NoParamForArg, |
| 28 |
ArgAndParamNotPointers |
| 29 |
}; |
| 30 |
|
| 31 |
/// Required by the std::error_code system to convert our enum into a general |
| 32 |
/// error code. |
| 33 |
std::error_code make_error_code(EscapesFunctionError); |
| 34 |
|
| 35 |
/// Returns a (statement, decl) tuple if an argument from an argument list |
| 36 |
/// escapes the function scope through globals/statics/other things. The |
| 37 |
/// statement is where the value escapes the function, while the declaration |
| 38 |
/// points to what it escapes through. If the argument doesn't escape the |
| 39 |
/// function, the tuple will only contain nullptrs. |
| 40 |
/// If the analysis runs into an unexpected error or into an unimplemented |
| 41 |
/// configuration, it will return an error_code of type EscapesFunctionError |
| 42 |
/// representing the precise issue. |
| 43 |
/// |
| 44 |
/// WARNING: incomplete behaviour/implementation for general-purpose use outside |
| 45 |
/// of DanglingOnTemporaryChecker. This only covers a limited set of cases, |
| 46 |
/// mainly in terms of arguments and parameter types. |
| 47 |
ErrorOr<std::tuple<const Stmt *, const Decl *>> |
| 48 |
escapesFunction(const Expr *Arg, const FunctionDecl *FuncDecl, |
| 49 |
const Expr *const *Arguments, unsigned NumArgs); |
| 50 |
|
| 51 |
/// Helper function taking a call expression. |
| 52 |
ErrorOr<std::tuple<const Stmt *, const Decl *>> |
| 53 |
escapesFunction(const Expr *Arg, const CallExpr *Call); |
| 54 |
|
| 55 |
/// Helper function taking a construct expression. |
| 56 |
ErrorOr<std::tuple<const Stmt *, const Decl *>> |
| 57 |
escapesFunction(const Expr *Arg, const CXXConstructExpr *Construct); |
| 58 |
|
| 59 |
/// Helper function taking an operator call expression. |
| 60 |
ErrorOr<std::tuple<const Stmt *, const Decl *>> |
| 61 |
escapesFunction(const Expr *Arg, const CXXOperatorCallExpr *OpCall); |
| 62 |
|
| 63 |
#endif |