Skip to content

Commit 4ca9a4b

Browse files
AaronBallmantru
authored andcommitted
[[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 (cherry picked from commit 837b2d4)
1 parent 7e51c08 commit 4ca9a4b

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
@@ -5011,10 +5011,10 @@ void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI,
50115011

50125012
static void handleNonStringAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
50135013
// This only applies to fields and variable declarations which have an array
5014-
// type.
5014+
// type or pointer type, with character elements.
50155015
QualType QT = cast<ValueDecl>(D)->getType();
5016-
if (!QT->isArrayType() ||
5017-
!QT->getBaseElementTypeUnsafe()->isAnyCharacterType()) {
5016+
if ((!QT->isArrayType() && !QT->isPointerType()) ||
5017+
!QT->getPointeeOrArrayElementType()->isAnyCharacterType()) {
50185018
S.Diag(D->getBeginLoc(), diag::warn_attribute_non_character_array)
50195019
<< AL << AL.isRegularKeywordAttribute() << QT << AL.getRange();
50205020
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)