diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -1304,6 +1304,9 @@ def ProfileInstrUnprofiled : DiagGroup<"profile-instr-unprofiled">; def MisExpect : DiagGroup<"misexpect">; +// Array bounds remarks. +def ArrayBoundsRemarks : DiagGroup<"array-bounds">; + // AddressSanitizer frontend instrumentation remarks. def SanitizeAddressRemarks : DiagGroup<"sanitize-address">; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -9450,6 +9450,10 @@ def note_array_declared_here : Note< "array %0 declared here">; +def remark_array_access : Remark< + "accessing %select{fixed|dynamic}0 sized array %1 by %2">, + InGroup; + def warn_inconsistent_array_form : Warning< "argument %0 of type %1 with mismatched bound">, InGroup, DefaultIgnore; diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -16236,8 +16236,22 @@ return; Expr::EvalResult Result; - if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) + if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) { + if (!IsUnboundedArray) { + SmallString<128> sizeString; + llvm::raw_svector_ostream OS(sizeString); + + OS << "'"; + IndexExpr->printPretty(OS, nullptr, getPrintingPolicy()); + OS << "'"; + + Context.getDiagnostics().Report( + BaseExpr->getBeginLoc(), diag::remark_array_access) + << 0 << ArrayTy->desugar() << OS.str(); + } + return; + } llvm::APSInt index = Result.Val.getInt(); if (IndexNegated) { @@ -16352,6 +16366,10 @@ else if (size.getBitWidth() < index.getBitWidth()) size = size.zext(index.getBitWidth()); + Context.getDiagnostics().Report( + BaseExpr->getBeginLoc(), diag::remark_array_access) + << 0 << ArrayTy->desugar() << toString(index, 10, true); + // For array subscripting the index must be less than size, but for pointer // arithmetic also allow the index (offset) to be equal to size since // computing the next address after the end of the array is legal and