[[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 837b2d464ff16fe0d892dcf2827747c97dd5465e)
This commit is contained in:
parent
7e51c08c82
commit
4ca9a4bb35
@ -9417,9 +9417,9 @@ def NonStringDocs : Documentation {
|
|||||||
let Category = DocCatDecl;
|
let Category = DocCatDecl;
|
||||||
let Content = [{
|
let Content = [{
|
||||||
The ``nonstring`` attribute can be applied to the declaration of a variable or
|
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
|
a field whose type is a character pointer or character array to specify that
|
||||||
not intended to behave like a null-terminated string. This will silence
|
the buffer is not intended to behave like a null-terminated string. This will
|
||||||
diagnostics with code like:
|
silence diagnostics with code like:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
|
@ -5011,10 +5011,10 @@ void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI,
|
|||||||
|
|
||||||
static void handleNonStringAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
|
static void handleNonStringAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
|
||||||
// This only applies to fields and variable declarations which have an array
|
// 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();
|
QualType QT = cast<ValueDecl>(D)->getType();
|
||||||
if (!QT->isArrayType() ||
|
if ((!QT->isArrayType() && !QT->isPointerType()) ||
|
||||||
!QT->getBaseElementTypeUnsafe()->isAnyCharacterType()) {
|
!QT->getPointeeOrArrayElementType()->isAnyCharacterType()) {
|
||||||
S.Diag(D->getBeginLoc(), diag::warn_attribute_non_character_array)
|
S.Diag(D->getBeginLoc(), diag::warn_attribute_non_character_array)
|
||||||
<< AL << AL.isRegularKeywordAttribute() << QT << AL.getRange();
|
<< AL << AL.isRegularKeywordAttribute() << QT << AL.getRange();
|
||||||
return;
|
return;
|
||||||
|
@ -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;
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user