This is an archive of the discontinued LLVM Phabricator instance.

cppcoreguidelines-pro-bounds-constant-array-index: crash for value dependent index in c++03 mode
ClosedPublic

Authored by mgehre on Jul 9 2016, 2:47 PM.

Details

Summary

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?

Diff Detail

Repository
rL LLVM

Event Timeline

mgehre updated this revision to Diff 63401.Jul 9 2016, 2:47 PM
mgehre retitled this revision from to cppcoreguidelines-pro-bounds-constant-array-index: crash for value dependent index in c++03 mode.
mgehre updated this object.
mgehre added reviewers: alexfh, aaron.ballman.
mgehre added a subscriber: cfe-commits.
aaron.ballman added a subscriber: rsmith.

Hmm, perhaps @rsmith has other opinions, but I think this should be fixed in isIntegerConstantExpr(). C++03 has the notion of value dependence, and [temp.dep.type]p1 and [expr.const]p1 both specify that a non-type template parameters of integral types are constant expressions.

Even if we change isIntegerConstantExpr() to return true instead of asserting, we still need this fix to the check. Because
we call isIntegerConstantExpr() to find out if we can possibly calculate the (constant) value of the index expression.
If it is value dependent, we cannot.

Even if we change isIntegerConstantExpr() to return true instead of asserting, we still need this fix to the check. Because
we call isIntegerConstantExpr() to find out if we can possibly calculate the (constant) value of the index expression.
If it is value dependent, we cannot.

You can on instantiation, can you not?

mgehre updated this revision to Diff 63795.Jul 13 2016, 5:08 AM
mgehre edited edge metadata.

Suppress diagnostics if index is value-dependent. Emit them in specializations.

aaron.ballman accepted this revision.Jul 13 2016, 1:10 PM
aaron.ballman edited edge metadata.

LGTM!

clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
70 ↗(On Diff #63795)

Comments should be complete sentences (capitalization and punctuation).

test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index-c++03.cpp
7 ↗(On Diff #63795)

Missing a full stop.

This revision is now accepted and ready to land.Jul 13 2016, 1:10 PM
This revision was automatically updated to reflect the committed changes.
rsmith edited edge metadata.Jul 14 2016, 3:42 PM

It's not meaningful to ask whether a value-dependent expression is an integral constant expression. The answer is not knowable, so the function asserting doesn't seem unreasonable. Any value we returned from that function would be a lie.