[PATCH] D22190: cppcoreguidelines-pro-bounds-constant-array-index: crash for value dependent index in c++03 mode

Matthias Gehre via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 9 14:47:53 PDT 2016


mgehre created this revision.
mgehre added reviewers: alexfh, aaron.ballman.
mgehre added a subscriber: cfe-commits.
Herald added a subscriber: nemanjai.

When the expression is value dependent,
isIntegerConstantExpr() crashes in C++03 mode with
 ../tools/clang/lib/AST/ExprConstant.cpp:9330: (anonymous namespace)::ICEDiag CheckICE(const clang::Expr *, const clang::ASTContext &):
  Assertion `!E->isValueDependent() && "Should not see value dependent exprs!"' failed.
In C++11 mode, that assert does not trigger.

This commit works around this in the check, but maybe this
should be fixed in isIntegerConstantExpr?

http://reviews.llvm.org/D22190

Files:
  clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
  test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index-c++03.cpp

Index: test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index-c++03.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index-c++03.cpp
@@ -0,0 +1,11 @@
+// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-constant-array-index %t -- -- --std=c++03
+struct A {
+  char x[3];
+};
+template <int index> class B {
+  void operator()(A p1) {
+    // The next line used to crash the check (in C++03 mode only)
+    p1.x[index];
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]
+  }
+};
Index: clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
===================================================================
--- clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
+++ clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
@@ -66,7 +66,8 @@
   const auto *Matched = Result.Nodes.getNodeAs<Expr>("expr");
   const auto *IndexExpr = Result.Nodes.getNodeAs<Expr>("index");
   llvm::APSInt Index;
-  if (!IndexExpr->isIntegerConstantExpr(Index, *Result.Context, nullptr,
+  if (IndexExpr->isValueDependent()
+        || !IndexExpr->isIntegerConstantExpr(Index, *Result.Context, nullptr,
                                         /*isEvaluated=*/true)) {
     SourceRange BaseRange;
     if (const auto *ArraySubscriptE = dyn_cast<ArraySubscriptExpr>(Matched))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22190.63401.patch
Type: text/x-patch
Size: 1563 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160709/d303602a/attachment.bin>


More information about the cfe-commits mailing list