blob: 3a88126a9ee6a31824b0eee36c125ce5b92caffe [file] [log] [blame]
Adam Balogh4da65c22020-06-08 12:41:36 +02001//===------------- Aliasing.cpp - clang-tidy ------------------------------===//
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#include "Aliasing.h"
10
11#include "clang/AST/Expr.h"
12
13namespace clang {
14namespace tidy {
15namespace utils {
16
17/// Return whether \p S is a reference to the declaration of \p Var.
18static bool isAccessForVar(const Stmt *S, const VarDecl *Var) {
19 if (const auto *DRE = dyn_cast<DeclRefExpr>(S))
20 return DRE->getDecl() == Var;
21
22 return false;
23}
24
25/// Return whether \p Var has a pointer or reference in \p S.
26static bool isPtrOrReferenceForVar(const Stmt *S, const VarDecl *Var) {
27 if (const auto *DS = dyn_cast<DeclStmt>(S)) {
28 for (const Decl *D : DS->getDeclGroup()) {
29 if (const auto *LeftVar = dyn_cast<VarDecl>(D)) {
30 if (LeftVar->hasInit() && LeftVar->getType()->isReferenceType()) {
31 return isAccessForVar(LeftVar->getInit(), Var);
32 }
33 }
34 }
35 } else if (const auto *UnOp = dyn_cast<UnaryOperator>(S)) {
36 if (UnOp->getOpcode() == UO_AddrOf)
37 return isAccessForVar(UnOp->getSubExpr(), Var);
38 }
39
40 return false;
41}
42
43/// Return whether \p Var has a pointer or reference in \p S.
44static bool hasPtrOrReferenceInStmt(const Stmt *S, const VarDecl *Var) {
45 if (isPtrOrReferenceForVar(S, Var))
46 return true;
47
48 for (const Stmt *Child : S->children()) {
49 if (!Child)
50 continue;
51
52 if (hasPtrOrReferenceInStmt(Child, Var))
53 return true;
54 }
55
56 return false;
57}
58
59bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var) {
60 return hasPtrOrReferenceInStmt(Func->getBody(), Var);
61}
62
63} // namespace utils
64} // namespace tidy
65} // namespace clang