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 @@ -1302,6 +1302,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,13 @@ 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 remark_unknown_array_access : Remark< + "accessing unknown sized array by %0">, + 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 @@ -16232,8 +16232,25 @@ return; Expr::EvalResult Result; - if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) + if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) { + SmallString<128> sizeString; + llvm::raw_svector_ostream OS(sizeString); + + OS << "'"; + IndexExpr->printPretty(OS, nullptr, getPrintingPolicy()); + OS << "'"; + + if (!IsUnboundedArray) + Context.getDiagnostics().Report( + BaseExpr->getBeginLoc(), diag::remark_array_access) + << 0 << ArrayTy->desugar() << OS.str(); + else + Context.getDiagnostics().Report( + BaseExpr->getBeginLoc(), diag::remark_unknown_array_access) + OS.str(); + return; + } llvm::APSInt index = Result.Val.getInt(); if (IndexNegated) { @@ -16348,6 +16365,10 @@ else if (size.getBitWidth() < index.getBitWidth()) size = size.zext(index.getBitWidth()); + Context.getDiagnostics().Report( + BaseExpr->getBeginLoc(), diag::remark_array_access) + << 1 << 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