Skip to content

Commit 837b2d4

Browse files
authored
[[gnu::nonstring]] should work on pointers too (#150974)
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
1 parent 6a45697 commit 837b2d4

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

clang/include/clang/Basic/AttrDocs.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9417,9 +9417,9 @@ def NonStringDocs : Documentation {
94179417
let Category = DocCatDecl;
94189418
let Content = [{
94199419
The ``nonstring`` attribute can be applied to the declaration of a variable or
9420-
a field whose type is a character array to specify that the character array is
9421-
not intended to behave like a null-terminated string. This will silence
9422-
diagnostics with code like:
9420+
a field whose type is a character pointer or character array to specify that
9421+
the buffer is not intended to behave like a null-terminated string. This will
9422+
silence diagnostics with code like:
94239423

94249424
.. code-block:: c
94259425

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4805,10 +4805,10 @@ void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI,
48054805

48064806
static void handleNonStringAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
48074807
// This only applies to fields and variable declarations which have an array
4808-
// type.
4808+
// type or pointer type, with character elements.
48094809
QualType QT = cast<ValueDecl>(D)->getType();
4810-
if (!QT->isArrayType() ||
4811-
!QT->getBaseElementTypeUnsafe()->isAnyCharacterType()) {
4810+
if ((!QT->isArrayType() && !QT->isPointerType()) ||
4811+
!QT->getPointeeOrArrayElementType()->isAnyCharacterType()) {
48124812
S.Diag(D->getBeginLoc(), diag::warn_attribute_non_character_array)
48134813
<< AL << AL.isRegularKeywordAttribute() << QT << AL.getRange();
48144814
return;

clang/test/Sema/attr-nonstring.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,11 @@ struct Outer o2[] = {
229229
}
230230
}
231231
};
232+
233+
// The attribute also works with a pointer type, not just an array type.
234+
__attribute__((nonstring)) char *ptr1;
235+
__attribute__((nonstring)) const unsigned char *ptr2;
236+
struct GH150951 {
237+
__attribute__((nonstring)) char *ptr1;
238+
__attribute__((nonstring)) const unsigned char *ptr2;
239+
};

0 commit comments

Comments
 (0)