clang-tools 20.0.0git
DeclRefExprUtils.h
Go to the documentation of this file.
1//===--- DeclRefExprUtils.h - clang-tidy-------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_DECLREFEXPRUTILS_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_DECLREFEXPRUTILS_H
11
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/Type.h"
14#include "llvm/ADT/SmallPtrSet.h"
15
17
18/// Returns set of all ``DeclRefExprs`` to ``VarDecl`` within ``Stmt``.
19llvm::SmallPtrSet<const DeclRefExpr *, 16>
20allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context);
21
22/// Returns set of all ``DeclRefExprs`` to ``VarDecl`` within ``Decl``.
23llvm::SmallPtrSet<const DeclRefExpr *, 16>
24allDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl, ASTContext &Context);
25
26/// Returns set of all ``DeclRefExprs`` to ``VarDecl`` within ``Stmt`` where
27/// ``VarDecl`` is guaranteed to be accessed in a const fashion.
28///
29/// If ``VarDecl`` is of pointer type, ``Indirections`` specifies the level
30/// of indirection of the object whose mutations we are tracking.
31///
32/// For example, given:
33/// ```
34/// int i;
35/// int* p;
36/// p = &i; // (A)
37/// *p = 3; // (B)
38/// ```
39///
40/// - `constReferenceDeclRefExprs(P, Stmt, Context, 0)` returns the reference
41// to `p` in (B): the pointee is modified, but the pointer is not;
42/// - `constReferenceDeclRefExprs(P, Stmt, Context, 1)` returns the reference
43// to `p` in (A): the pointee is modified, but the pointer is not;
44llvm::SmallPtrSet<const DeclRefExpr *, 16>
45constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt,
46 ASTContext &Context, int Indirections);
47
48/// Returns true if all ``DeclRefExpr`` to the variable within ``Stmt``
49/// do not modify it.
50/// See `constReferenceDeclRefExprs` for the meaning of ``Indirections``.
51bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
52 ASTContext &Context, int Indirections);
53
54/// Returns ``true`` if ``DeclRefExpr`` is the argument of a copy-constructor
55/// call expression within ``Decl``.
56bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
57 ASTContext &Context);
58
59/// Returns ``true`` if ``DeclRefExpr`` is the argument of a copy-assignment
60/// operator CallExpr within ``Decl``.
61bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
62 ASTContext &Context);
63
64} // namespace clang::tidy::utils::decl_ref_expr
65
66#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_DECLREFEXPRUTILS_H
const FunctionDecl * Decl
const DeclRefExpr * DeclRef
SmallPtrSet< const DeclRefExpr *, 16 > allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context)
Returns set of all DeclRefExprs to VarDecl within Stmt.
bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl, ASTContext &Context)
Returns true if DeclRefExpr is the argument of a copy-constructor call expression within Decl.
bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt, ASTContext &Context, int Indirections)
Returns true if all DeclRefExpr to the variable within Stmt do not modify it.
bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Decl &Decl, ASTContext &Context)
Returns true if DeclRefExpr is the argument of a copy-assignment operator CallExpr within Decl.
SmallPtrSet< const DeclRefExpr *, 16 > constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context, int Indirections)
Returns set of all DeclRefExprs to VarDecl within Stmt where VarDecl is guaranteed to be accessed in ...