Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5011,10 +5011,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;
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Sema/attr-nonstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -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