void updated this revision to Diff 499321.
void added a comment.

Report when there's a non-constant access:

array_access_report.c:32:17: remark: accessing fixed sized array 'int[16]' by 
'index' [-Rarray-bounds]

  report_size(p->array, index);
              ^


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144136/new/

https://reviews.llvm.org/D144136

Files:
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaChecking.cpp


Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ 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
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ 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<ArrayBoundsRemarks>;
+
 def warn_inconsistent_array_form : Warning<
   "argument %0 of type %1 with mismatched bound">,
   InGroup<ArrayParameter>, DefaultIgnore;
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ 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">;
 


Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ 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
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ 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<ArrayBoundsRemarks>;
+
 def warn_inconsistent_array_form : Warning<
   "argument %0 of type %1 with mismatched bound">,
   InGroup<ArrayParameter>, DefaultIgnore;
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ 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">;
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to