[PATCH] D101791: [clang-tidy] Aliasing: Add support for aggregates with references.

Artem Dergachev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 3 15:30:29 PDT 2021


NoQ created this revision.
NoQ added reviewers: alexfh, gribozavr2, aaron.ballman, vsavchenko.
Herald added subscribers: martong, mgehre, xazax.hun.
NoQ requested review of this revision.
Herald added a project: clang-tools-extra.

When a variable is used in an initializer of an aggregate for its reference-type field this counts as aliasing.

This covers a real problem inspired by @aaron.ballman's comment in D96215#2554178 <https://reviews.llvm.org/D96215#2554178>. Because in the two checkers that use alias analysis (bugprone-infinite-loop and bugprone-redundant-branch-condition) only variables of plain integral types are being analyzed, we don't seem to need to worry about arrays yet. Arrays of references (unlike structures of references) don't exist. And if an array element is used as a counter (to be decomposed into a reference to an element type), it's a non-starter for those checkers as well.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D101791

Files:
  clang-tools-extra/clang-tidy/utils/Aliasing.cpp
  clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
@@ -576,3 +576,29 @@
     // No warning. The loop is finite because 'y' is a reference to 'x'.
   }
 }
+
+struct AggregateWithReference {
+  int &y;
+};
+
+void test_structured_bindings_good() {
+  int x = 0;
+  AggregateWithReference ref { x };
+  auto &[y] = ref;
+  for (; x < 10; ++y) {
+    // No warning. The loop is finite because 'y' is a reference to 'x'.
+  }
+}
+
+struct AggregateWithValue {
+  int y;
+};
+
+void test_structured_bindings_bad() {
+  int x = 0;
+  AggregateWithValue val { x };
+  auto &[y] = val;
+  for (; x < 10; ++y) {
+      // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (x) are updated in the loop body [bugprone-infinite-loop]
+  }
+}
Index: clang-tools-extra/clang-tidy/utils/Aliasing.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/Aliasing.cpp
+++ clang-tools-extra/clang-tidy/utils/Aliasing.cpp
@@ -55,6 +55,13 @@
     return llvm::any_of(Call->arguments(), [Var](const Expr *ArgE) {
       return isAccessForVar(ArgE, Var);
     });
+  } else if (const auto *ILE = dyn_cast<InitListExpr>(S)) {
+    return llvm::any_of(ILE->inits(), [Var](const Expr *ChildE) {
+      // If the child expression is a reference to Var, this means that it's
+      // used as an initializer of a reference-typed field. Otherwise
+      // it would have been surrounded with an implicit lvalue-to-rvalue cast.
+      return isAccessForVar(ChildE, Var);
+    });
   }
 
   return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101791.342568.patch
Type: text/x-patch
Size: 1815 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210503/20d9d095/attachment.bin>


More information about the cfe-commits mailing list