[PATCH] D81396: [clang-tidy] New util `Aliasing` factored out from `bugprone-infinite-loop`

2020-07-01 Thread Balogh , Ádám via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4da65c2920b6: [clang-tidy] New util `Aliasing` factored out 
from `bugprone-infinite-loop` (authored by baloghadamsoftware).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81396/new/

https://reviews.llvm.org/D81396

Files:
  clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.h
  clang-tools-extra/clang-tidy/utils/CMakeLists.txt

Index: clang-tools-extra/clang-tidy/utils/CMakeLists.txt
===
--- clang-tools-extra/clang-tidy/utils/CMakeLists.txt
+++ clang-tools-extra/clang-tidy/utils/CMakeLists.txt
@@ -4,6 +4,7 @@
   )
 
 add_clang_library(clangTidyUtils
+  Aliasing.cpp
   ASTUtils.cpp
   DeclRefExprUtils.cpp
   ExceptionAnalyzer.cpp
Index: clang-tools-extra/clang-tidy/utils/Aliasing.h
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/utils/Aliasing.h
@@ -0,0 +1,36 @@
+//===- Aliasing.h - clang-tidy ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
+
+#include "clang/AST/Decl.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+
+/// Returns whether \p Var has a pointer or reference in \p Func.
+///
+/// Example:
+/// void f() {
+///   int n;
+///   ...
+///   int *p = 
+/// }
+///
+/// For `f()` and `n` the function returns ``true`` because `p` is a
+/// pointer to `n` created in `f()`.
+
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var);
+
+} // namespace utils
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
Index: clang-tools-extra/clang-tidy/utils/Aliasing.cpp
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/utils/Aliasing.cpp
@@ -0,0 +1,65 @@
+//===- Aliasing.cpp - clang-tidy --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Aliasing.h"
+
+#include "clang/AST/Expr.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+
+/// Return whether \p S is a reference to the declaration of \p Var.
+static bool isAccessForVar(const Stmt *S, const VarDecl *Var) {
+  if (const auto *DRE = dyn_cast(S))
+return DRE->getDecl() == Var;
+
+  return false;
+}
+
+/// Return whether \p Var has a pointer or reference in \p S.
+static bool isPtrOrReferenceForVar(const Stmt *S, const VarDecl *Var) {
+  if (const auto *DS = dyn_cast(S)) {
+for (const Decl *D : DS->getDeclGroup()) {
+  if (const auto *LeftVar = dyn_cast(D)) {
+if (LeftVar->hasInit() && LeftVar->getType()->isReferenceType()) {
+  return isAccessForVar(LeftVar->getInit(), Var);
+}
+  }
+}
+  } else if (const auto *UnOp = dyn_cast(S)) {
+if (UnOp->getOpcode() == UO_AddrOf)
+  return isAccessForVar(UnOp->getSubExpr(), Var);
+  }
+
+  return false;
+}
+
+/// Return whether \p Var has a pointer or reference in \p S.
+static bool hasPtrOrReferenceInStmt(const Stmt *S, const VarDecl *Var) {
+  if (isPtrOrReferenceForVar(S, Var))
+return true;
+
+  for (const Stmt *Child : S->children()) {
+if (!Child)
+  continue;
+
+if (hasPtrOrReferenceInStmt(Child, Var))
+  return true;
+  }
+
+  return false;
+}
+
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var) {
+  return hasPtrOrReferenceInStmt(Func->getBody(), Var);
+}
+
+} // namespace utils
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -10,8 +10,10 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
+#include "../utils/Aliasing.h"
 
 using namespace clang::ast_matchers;
+using clang::tidy::utils::hasPtrOrReferenceInFunc;
 
 namespace clang {
 namespace tidy {
@@ -24,54 +26,6 @@
 callExpr(Internal, 

[PATCH] D81396: [clang-tidy] New util `Aliasing` factored out from `bugprone-infinite-loop`

2020-06-10 Thread Nathan James via Phabricator via cfe-commits
njames93 accepted this revision.
njames93 added a comment.

LGTM, but with one more nit




Comment at: clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp:16
 using namespace clang::ast_matchers;
+using clang::tidy::utils::hasPtrOrReferenceInFunc;
 

This function is only used once in the file, so may as well remove this line 
and just qualify its call further down.



Comment at: clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp:60
 
   return hasPtrOrReferenceInFunc(Func, Var) ||
  isChanged(LoopStmt, Var, Context);

here.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81396/new/

https://reviews.llvm.org/D81396



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D81396: [clang-tidy] New util `Aliasing` factored out from `bugprone-infinite-loop`

2020-06-10 Thread Balogh , Ádám via Phabricator via cfe-commits
baloghadamsoftware updated this revision to Diff 269779.
baloghadamsoftware added a comment.

Parameters in comments changed to `\p `.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81396/new/

https://reviews.llvm.org/D81396

Files:
  clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.h
  clang-tools-extra/clang-tidy/utils/CMakeLists.txt

Index: clang-tools-extra/clang-tidy/utils/CMakeLists.txt
===
--- clang-tools-extra/clang-tidy/utils/CMakeLists.txt
+++ clang-tools-extra/clang-tidy/utils/CMakeLists.txt
@@ -4,6 +4,7 @@
   )
 
 add_clang_library(clangTidyUtils
+  Aliasing.cpp
   ASTUtils.cpp
   DeclRefExprUtils.cpp
   ExceptionAnalyzer.cpp
Index: clang-tools-extra/clang-tidy/utils/Aliasing.h
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/utils/Aliasing.h
@@ -0,0 +1,36 @@
+//===- Aliasing.h - clang-tidy ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
+
+#include "clang/AST/Decl.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+
+/// Returns whether \p Var has a pointer or reference in \p Func.
+///
+/// Example:
+/// void f() {
+///   int n;
+///   ...
+///   int *p = 
+/// }
+///
+/// For `f()` and `n` the function returns ``true`` because `p` is a
+/// pointer to `n` created in `f()`.
+
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var);
+
+} // namespace utils
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
Index: clang-tools-extra/clang-tidy/utils/Aliasing.cpp
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/utils/Aliasing.cpp
@@ -0,0 +1,65 @@
+//===- Aliasing.cpp - clang-tidy --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Aliasing.h"
+
+#include "clang/AST/Expr.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+
+/// Return whether \p S is a reference to the declaration of \p Var.
+static bool isAccessForVar(const Stmt *S, const VarDecl *Var) {
+  if (const auto *DRE = dyn_cast(S))
+return DRE->getDecl() == Var;
+
+  return false;
+}
+
+/// Return whether \p Var has a pointer or reference in \p S.
+static bool isPtrOrReferenceForVar(const Stmt *S, const VarDecl *Var) {
+  if (const auto *DS = dyn_cast(S)) {
+for (const Decl *D : DS->getDeclGroup()) {
+  if (const auto *LeftVar = dyn_cast(D)) {
+if (LeftVar->hasInit() && LeftVar->getType()->isReferenceType()) {
+  return isAccessForVar(LeftVar->getInit(), Var);
+}
+  }
+}
+  } else if (const auto *UnOp = dyn_cast(S)) {
+if (UnOp->getOpcode() == UO_AddrOf)
+  return isAccessForVar(UnOp->getSubExpr(), Var);
+  }
+
+  return false;
+}
+
+/// Return whether \p Var has a pointer or reference in \p S.
+static bool hasPtrOrReferenceInStmt(const Stmt *S, const VarDecl *Var) {
+  if (isPtrOrReferenceForVar(S, Var))
+return true;
+
+  for (const Stmt *Child : S->children()) {
+if (!Child)
+  continue;
+
+if (hasPtrOrReferenceInStmt(Child, Var))
+  return true;
+  }
+
+  return false;
+}
+
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var) {
+  return hasPtrOrReferenceInStmt(Func->getBody(), Var);
+}
+
+} // namespace utils
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -10,8 +10,10 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
+#include "../utils/Aliasing.h"
 
 using namespace clang::ast_matchers;
+using clang::tidy::utils::hasPtrOrReferenceInFunc;
 
 namespace clang {
 namespace tidy {
@@ -24,54 +26,6 @@
 callExpr(Internal, callee(functionDecl(isNoReturn());
 }
 
-/// Return whether `S` is a reference to the declaration of `Var`.
-static bool isAccessForVar(const 

[PATCH] D81396: [clang-tidy] New util `Aliasing` factored out from `bugprone-infinite-loop`

2020-06-10 Thread Nathan James via Phabricator via cfe-commits
njames93 added inline comments.



Comment at: clang-tools-extra/clang-tidy/utils/Aliasing.cpp:17
+
+/// Return whether `S` is a reference to the declaration of `Var`.
+static bool isAccessForVar(const Stmt *S, const VarDecl *Var) {

Ditto `\p `.



Comment at: clang-tools-extra/clang-tidy/utils/Aliasing.cpp:25
+
+/// Return whether `Var` has a pointer or reference in `S`.
+static bool isPtrOrReferenceForVar(const Stmt *S, const VarDecl *Var) {

Ditto.



Comment at: clang-tools-extra/clang-tidy/utils/Aliasing.cpp:43
+
+/// Return whether `Var` has a pointer or reference in `S`.
+static bool hasPtrOrReferenceInStmt(const Stmt *S, const VarDecl *Var) {

Ditto.



Comment at: clang-tools-extra/clang-tidy/utils/Aliasing.h:18
+
+/// Returns whether ``Var`` has a pointer or reference in ``Func``.
+///

Don't use double ticks for param names, instead use `\p `.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81396/new/

https://reviews.llvm.org/D81396



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D81396: [clang-tidy] New util `Aliasing` factored out from `bugprone-infinite-loop`

2020-06-09 Thread Dmitri Gribenko via Phabricator via cfe-commits
gribozavr2 accepted this revision.
gribozavr2 added inline comments.
This revision is now accepted and ready to land.



Comment at: clang-tools-extra/clang-tidy/utils/Aliasing.h:29
+/// pointer to ``n`` created in ``f()``.
+
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var);

Extra blank line.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81396/new/

https://reviews.llvm.org/D81396



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D81396: [clang-tidy] New util `Aliasing` factored out from `bugprone-infinite-loop`

2020-06-09 Thread Balogh , Ádám via Phabricator via cfe-commits
baloghadamsoftware updated this revision to Diff 269533.
baloghadamsoftware added a comment.

Thank you for the comment, @gribozavr2! Patch updated according to them.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81396/new/

https://reviews.llvm.org/D81396

Files:
  clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.h
  clang-tools-extra/clang-tidy/utils/CMakeLists.txt

Index: clang-tools-extra/clang-tidy/utils/CMakeLists.txt
===
--- clang-tools-extra/clang-tidy/utils/CMakeLists.txt
+++ clang-tools-extra/clang-tidy/utils/CMakeLists.txt
@@ -4,6 +4,7 @@
   )
 
 add_clang_library(clangTidyUtils
+  Aliasing.cpp
   ASTUtils.cpp
   DeclRefExprUtils.cpp
   ExceptionAnalyzer.cpp
Index: clang-tools-extra/clang-tidy/utils/Aliasing.h
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/utils/Aliasing.h
@@ -0,0 +1,36 @@
+//===- Aliasing.h - clang-tidy ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
+
+#include "clang/AST/Decl.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+
+/// Returns whether ``Var`` has a pointer or reference in ``Func``.
+///
+/// Example:
+/// void f() {
+///   int n;
+///   ...
+///   int *p = 
+/// }
+///
+/// For ``f()`` and ``n`` the function returns ``true`` because ``p`` is a
+/// pointer to ``n`` created in ``f()``.
+
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var);
+
+} // namespace utils
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
Index: clang-tools-extra/clang-tidy/utils/Aliasing.cpp
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/utils/Aliasing.cpp
@@ -0,0 +1,65 @@
+//===- Aliasing.cpp - clang-tidy --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Aliasing.h"
+
+#include "clang/AST/Expr.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+
+/// Return whether `S` is a reference to the declaration of `Var`.
+static bool isAccessForVar(const Stmt *S, const VarDecl *Var) {
+  if (const auto *DRE = dyn_cast(S))
+return DRE->getDecl() == Var;
+
+  return false;
+}
+
+/// Return whether `Var` has a pointer or reference in `S`.
+static bool isPtrOrReferenceForVar(const Stmt *S, const VarDecl *Var) {
+  if (const auto *DS = dyn_cast(S)) {
+for (const Decl *D : DS->getDeclGroup()) {
+  if (const auto *LeftVar = dyn_cast(D)) {
+if (LeftVar->hasInit() && LeftVar->getType()->isReferenceType()) {
+  return isAccessForVar(LeftVar->getInit(), Var);
+}
+  }
+}
+  } else if (const auto *UnOp = dyn_cast(S)) {
+if (UnOp->getOpcode() == UO_AddrOf)
+  return isAccessForVar(UnOp->getSubExpr(), Var);
+  }
+
+  return false;
+}
+
+/// Return whether `Var` has a pointer or reference in `S`.
+static bool hasPtrOrReferenceInStmt(const Stmt *S, const VarDecl *Var) {
+  if (isPtrOrReferenceForVar(S, Var))
+return true;
+
+  for (const Stmt *Child : S->children()) {
+if (!Child)
+  continue;
+
+if (hasPtrOrReferenceInStmt(Child, Var))
+  return true;
+  }
+
+  return false;
+}
+
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var) {
+  return hasPtrOrReferenceInStmt(Func->getBody(), Var);
+}
+
+} // namespace utils
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -10,8 +10,10 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
+#include "../utils/Aliasing.h"
 
 using namespace clang::ast_matchers;
+using clang::tidy::utils::hasPtrOrReferenceInFunc;
 
 namespace clang {
 namespace tidy {
@@ -24,54 +26,6 @@
 callExpr(Internal, callee(functionDecl(isNoReturn());
 }
 
-/// Return whether `S` is a reference to the declaration of 

[PATCH] D81396: [clang-tidy] New util `Aliasing` factored out from `bugprone-infinite-loop`

2020-06-08 Thread Dmitri Gribenko via Phabricator via cfe-commits
gribozavr2 added inline comments.



Comment at: clang-tools-extra/clang-tidy/utils/Aliasing.cpp:59
+
+/// Return whether `Var` has a pointer or reference in `Func`.
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var) {

Please don't duplicate the comment from the header in the cpp file.



Comment at: clang-tools-extra/clang-tidy/utils/Aliasing.h:18
+
+/// Returns whether a ``Var`` has a pointer or reference in ``Func``
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var);

Could you expand the comment? Best if you could add an example.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81396/new/

https://reviews.llvm.org/D81396



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D81396: [clang-tidy] New util `Aliasing` factored out from `bugprone-infinite-loop`

2020-06-08 Thread Balogh , Ádám via Phabricator via cfe-commits
baloghadamsoftware created this revision.
baloghadamsoftware added reviewers: aaron.ballman, gribozavr2, JonasToth, 
alexfh, hokein.
baloghadamsoftware added a project: clang-tools-extra.
Herald added subscribers: martong, steakhal, gamesh411, Szelethus, dkrupp, 
rnkovacs, xazax.hun, whisperity, mgorny.
Herald added a project: clang.
baloghadamsoftware added a child revision: D81272: [clang-tidy] New check 
`misc-redundant-condition`.

Function `hasPtrOrReferenceInfFunc()` of `bugprone-infinite-loop` is a generic 
function which could be reused in another checks. This patch moves this 
function into a newly created utility module.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81396

Files:
  clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.h
  clang-tools-extra/clang-tidy/utils/CMakeLists.txt

Index: clang-tools-extra/clang-tidy/utils/CMakeLists.txt
===
--- clang-tools-extra/clang-tidy/utils/CMakeLists.txt
+++ clang-tools-extra/clang-tidy/utils/CMakeLists.txt
@@ -4,6 +4,7 @@
   )
 
 add_clang_library(clangTidyUtils
+  Aliasing.cpp
   ASTUtils.cpp
   DeclRefExprUtils.cpp
   ExceptionAnalyzer.cpp
Index: clang-tools-extra/clang-tidy/utils/Aliasing.h
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/utils/Aliasing.h
@@ -0,0 +1,25 @@
+//===- Aliasing.h - clang-tidy ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
+
+#include "clang/AST/Decl.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+
+/// Returns whether a ``Var`` has a pointer or reference in ``Func``
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var);
+
+} // namespace utils
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_ALIASING_H
Index: clang-tools-extra/clang-tidy/utils/Aliasing.cpp
===
--- /dev/null
+++ clang-tools-extra/clang-tidy/utils/Aliasing.cpp
@@ -0,0 +1,66 @@
+//===- Aliasing.cpp - clang-tidy --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Aliasing.h"
+
+#include "clang/AST/Expr.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+
+/// Return whether `S` is a reference to the declaration of `Var`.
+static bool isAccessForVar(const Stmt *S, const VarDecl *Var) {
+  if (const auto *DRE = dyn_cast(S))
+return DRE->getDecl() == Var;
+
+  return false;
+}
+
+/// Return whether `Var` has a pointer or reference in `S`.
+static bool isPtrOrReferenceForVar(const Stmt *S, const VarDecl *Var) {
+  if (const auto *DS = dyn_cast(S)) {
+for (const Decl *D : DS->getDeclGroup()) {
+  if (const auto *LeftVar = dyn_cast(D)) {
+if (LeftVar->hasInit() && LeftVar->getType()->isReferenceType()) {
+  return isAccessForVar(LeftVar->getInit(), Var);
+}
+  }
+}
+  } else if (const auto *UnOp = dyn_cast(S)) {
+if (UnOp->getOpcode() == UO_AddrOf)
+  return isAccessForVar(UnOp->getSubExpr(), Var);
+  }
+
+  return false;
+}
+
+/// Return whether `Var` has a pointer or reference in `S`.
+static bool hasPtrOrReferenceInStmt(const Stmt *S, const VarDecl *Var) {
+  if (isPtrOrReferenceForVar(S, Var))
+return true;
+
+  for (const Stmt *Child : S->children()) {
+if (!Child)
+  continue;
+
+if (hasPtrOrReferenceInStmt(Child, Var))
+  return true;
+  }
+
+  return false;
+}
+
+/// Return whether `Var` has a pointer or reference in `Func`.
+bool hasPtrOrReferenceInFunc(const FunctionDecl *Func, const VarDecl *Var) {
+  return hasPtrOrReferenceInStmt(Func->getBody(), Var);
+}
+
+} // namespace utils
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -10,8 +10,10 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
+#include "../utils/Aliasing.h"