• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===------------- 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 
13 namespace clang {
14 namespace tidy {
15 namespace utils {
16 
17 /// Return whether \p S is a reference to the declaration of \p Var.
isAccessForVar(const Stmt * S,const VarDecl * Var)18 static 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.
isPtrOrReferenceForVar(const Stmt * S,const VarDecl * Var)26 static 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.
hasPtrOrReferenceInStmt(const Stmt * S,const VarDecl * Var)44 static 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 
hasPtrOrReferenceInFunc(const FunctionDecl * Func,const VarDecl * Var)59 bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var) {
60   return hasPtrOrReferenceInStmt(Func->getBody(), Var);
61 }
62 
63 } // namespace utils
64 } // namespace tidy
65 } // namespace clang
66