Skip to content

Conversation

@malavikasamak
Copy link
Contributor

Add more tests, where the index of the const array access evaluates to a constant and depends on a template argument.

rdar://143759014

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Feb 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 3, 2025

@llvm/pr-subscribers-clang

Author: Malavika Samak (malavikasamak)

Changes

Add more tests, where the index of the const array access evaluates to a constant and depends on a template argument.

rdar://143759014


Full diff: https://github.com/llvm/llvm-project/pull/125483.diff

1 Files Affected:

  • (modified) clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp (+33)
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
index e80b54b7c69677..03a7ae5c7a8fb5 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
@@ -124,3 +124,36 @@ void array_indexed_const_expr(unsigned idx) {
   k = arr[get_const(5)]; // expected-note {{used in buffer access here}}
   k = arr[get_const(4)];
 }
+
+template<unsigned length>
+consteval bool isNullTerminated(const char (&literal)[length])
+{
+  return literal[length - 1] == '\0';
+}
+
+template <typename T, unsigned M, unsigned N>
+T access2DArray(const T (&arr)[M][N]) {
+  return arr[M-1][N-1];
+}
+
+template<unsigned idx>
+constexpr int access_elements() {
+  int arr[idx + 20];
+  return arr[idx + 1]; 
+}
+
+void test_template_methods()
+{
+  constexpr char arr[] = "Good Morning!"; // = {'a', 'b', 'c', 'd', 'e'};
+  isNullTerminated(arr);
+  isNullTerminated("");
+  auto _ = isNullTerminated("hello world\n");
+  access_elements<5>();
+
+  int arr1[3][4] = {
+        {1, 2, 3, 4},
+        {5, 6, 7, 8},
+        {9, 10, 11, 12}
+    };
+  access2DArray(arr1);
+}

Copy link
Contributor

@ziqingluo-90 ziqingluo-90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

…o const sized array are not warned

Add more tests, where the index of the const array access evaluates to a constant and depends on a template
argument.

rdar://143759014
@malavikasamak malavikasamak force-pushed the msamak-143759014-template-based-indexing branch from 12dc35b to efd4f32 Compare February 7, 2025 07:02
@malavikasamak malavikasamak merged commit ac640a2 into llvm:main Feb 7, 2025
8 checks passed
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…o const sized array are not warned (llvm#125483)

Add more tests, where the index of the const array access evaluates to a
constant and depends on a template argument.

rdar://143759014

Co-authored-by: MalavikaSamak <[email protected]>
malavikasamak added a commit to swiftlang/llvm-project that referenced this pull request Feb 17, 2025
…o const sized array are not warned (llvm#125483)

Add more tests, where the index of the const array access evaluates to a
constant and depends on a template argument.

rdar://143759014

Co-authored-by: MalavikaSamak <[email protected]>
(cherry picked from commit ac640a2)
malavikasamak added a commit to swiftlang/llvm-project that referenced this pull request Feb 19, 2025
[Cherry-pick][Wunsafe-buffer-usage] Add additional tests to ensure safe accesses to const sized array are not warned (llvm#125483)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants