llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang-tools-extra

Author: Zeyi Xu (zeyi2)

<details>
<summary>Changes</summary>

The check matches `VarDecls` with `hasInitializer()`, which uses 
`getAnyInitializer()` and may therefore match a redeclaration whose initializer 
is attached to another declaration. So calling `IgnoreParenCasts()` on 
`VD-&gt;getInit()` directly would crash when that redeclaration had no 
initializer of its own.

This commit fixes the problem by adding a guard to the dependent initializer 
before ignoring parens.

Closes https://github.com/llvm/llvm-project/issues/199197

---
Full diff: https://github.com/llvm/llvm-project/pull/200178.diff


3 Files Affected:

- (modified) 
clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp (+6-1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp 
(+8) 


``````````diff
diff --git 
a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
index cc12479bcd86e..f3414aaa552f1 100644
--- a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
@@ -104,7 +104,12 @@ void NonConstParameterCheck::check(const 
MatchFinder::MatchResult &Result) {
   } else if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("Mark")) {
     const QualType T = VD->getType();
     if (T->isDependentType()) {
-      const Expr *Init = VD->getInit()->IgnoreParenCasts();
+      // Initializer matched by hasInitializer() may be attached to a different
+      // redeclaration.
+      const Expr *Init = VD->getInit();
+      if (!Init)
+        return;
+      Init = Init->IgnoreParenCasts();
       if (const auto *U = dyn_cast<UnaryOperator>(Init);
           U && U->getOpcode() == UO_Deref) {
         markCanNotBeConst(U->getSubExpr(), true);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 0b3bb091307e7..9c846377d797a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -715,6 +715,9 @@ Changes in existing checks
   - Fixed a false positive in array subscript expressions where the types are
     not yet resolved.
 
+  - Fixed a crash when analyzing a redeclaration whose initializer is attached
+    to another declaration.
+
 - Improved :doc:`readability-redundant-casting
   <clang-tidy/checks/readability/redundant-casting>` check by adding the
   `IgnoreImplicitCasts` option (default `false`) to flag casts as redundant
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
index d3749285df071..1a56db3a10263 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
@@ -433,3 +433,11 @@ void dependentInitInGenericLambdaMultiArg() {
     DependentCtor2<T> s(p, p);
   };
 }
+
+template <class T>
+struct StaticMemberWithDependentType {
+  static const T X = 0;
+};
+
+template <class T>
+const T StaticMemberWithDependentType<T>::X;

``````````

</details>


https://github.com/llvm/llvm-project/pull/200178
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to