Skip to content

Conversation

@AaronBallman
Copy link
Collaborator

Clang's current implementation only works on array types, but GCC (which is where we got this attribute) supports it on pointers as well as arrays.

Fixes #150951

Clang's current implementation only works on array types, but GCC
(which is where we got this attribute) supports it on pointers as well
as arrays.

Fixes llvm#150951
@AaronBallman AaronBallman added this to the LLVM 21.x Release milestone Jul 28, 2025
@AaronBallman AaronBallman added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 28, 2025
@AaronBallman AaronBallman added the diverges-from:gcc Does the clang frontend diverge from gcc on this issue label Jul 28, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Jul 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 28, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

Clang's current implementation only works on array types, but GCC (which is where we got this attribute) supports it on pointers as well as arrays.

Fixes #150951


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/AttrDocs.td (+3-3)
  • (modified) clang/lib/Sema/SemaDeclAttr.cpp (+3-3)
  • (modified) clang/test/Sema/attr-nonstring.c (+8)
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index fefdaba7f8bf5..76747d2b11811 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -9417,9 +9417,9 @@ def NonStringDocs : Documentation {
   let Category = DocCatDecl;
   let Content = [{
 The ``nonstring`` attribute can be applied to the declaration of a variable or
-a field whose type is a character array to specify that the character array is
-not intended to behave like a null-terminated string. This will silence
-diagnostics with code like:
+a field whose type is a character pointer or character array to specify that
+the buffer is not intended to behave like a null-terminated string. This will
+silence diagnostics with code like:
 
 .. code-block:: c
 
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index a4e8de49a4229..16b18bcb6a2a0 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -4805,10 +4805,10 @@ void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI,
 
 static void handleNonStringAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   // This only applies to fields and variable declarations which have an array
-  // type.
+  // type or pointer type, with character elements.
   QualType QT = cast<ValueDecl>(D)->getType();
-  if (!QT->isArrayType() ||
-      !QT->getBaseElementTypeUnsafe()->isAnyCharacterType()) {
+  if ((!QT->isArrayType() && !QT->isPointerType()) ||
+      !QT->getPointeeOrArrayElementType()->isAnyCharacterType()) {
     S.Diag(D->getBeginLoc(), diag::warn_attribute_non_character_array)
         << AL << AL.isRegularKeywordAttribute() << QT << AL.getRange();
     return;
diff --git a/clang/test/Sema/attr-nonstring.c b/clang/test/Sema/attr-nonstring.c
index 3838aa3bbee15..fe7b6d259dd79 100644
--- a/clang/test/Sema/attr-nonstring.c
+++ b/clang/test/Sema/attr-nonstring.c
@@ -229,3 +229,11 @@ struct Outer o2[] = {
     }
   }
 };
+
+// The attribute also works with a pointer type, not just an array type.
+__attribute__((nonstring)) char *ptr1;
+__attribute__((nonstring)) const unsigned char *ptr2;
+struct GH150951 {
+  __attribute__((nonstring)) char *ptr1;
+  __attribute__((nonstring)) const unsigned char *ptr2;
+};

@AaronBallman
Copy link
Collaborator Author

No release note for this one because I plan to cherry-pick it to the 21.x branch which has a release note talking about adding initial support for the attribute.

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Jul 28, 2025
@AaronBallman AaronBallman merged commit 837b2d4 into llvm:main Jul 28, 2025
14 checks passed
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Jul 28, 2025
@AaronBallman AaronBallman deleted the aballman-gh150951 branch July 28, 2025 15:53
@AaronBallman
Copy link
Collaborator Author

/cherry-pick 837b2d4

@llvmbot
Copy link
Member

llvmbot commented Jul 28, 2025

/pull-request #150980

tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 1, 2025
Clang's current implementation only works on array types, but GCC (which
is where we got this attribute) supports it on pointers as well as
arrays.

Fixes llvm#150951

(cherry picked from commit 837b2d4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category diverges-from:gcc Does the clang frontend diverge from gcc on this issue

Projects

Development

Successfully merging this pull request may close these issues.

[Clang] Attribute gnu::nonstring should apply to char * variables

3 participants