Clang warning: A attribute only applies to... pointer arguments [-Wignored-attributes] (warn_attribute_pointers_only)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: A attribute only applies to
 
constant
pointer arguments

Type Warning
Category Semantic Issue
Internal Id warn_attribute_pointers_only
Active by Default Yes
Flags -Wno-attributes (85 elements)
-Wno-ignored-attributes (84 elements)
Internal Message %0 attribute only applies to%select{| constant}1 pointer arguments
Regular Expression (?:warning|error|fatal error)\: (.*?) attribute only applies to(?:| constant) pointer arguments \[(?:\-Werror,)?\-Wignored\-attributes[^\]]*\]
First Commit 2013-12-26 cedaaea69127 This diagnostic did not accept arguments, and did not have any test coverage. Parameterized the diag...

Description

The warning is issued by the Clang compiler when an attribute that is intended to be applied only to pointer arguments, or to constant pointer arguments, is mistakenly applied to an argument, function, or variable of an incompatible type. This diagnostic message ensures that attributes which expect pointer types are not erroneously assigned to non-pointer or inappropriate target types. Such an assignment usually indicates a misunderstanding of the attribute's requirements or a programming error that could lead to unintended behavior of the program. Attributes serve as additional metadata for the compiler, guiding optimizations, checks, or code generation. Misapplied attributes, therefore, could result in less effective optimization, incorrect code generation, or the oversight of significant checks. The Clang compiler's ability to flag these mismatch issues assists in maintaining the intended semantic meaning of the program and ensures that the attributes applied to code elements adhere strictly to their defined applicability criteria.  
AI Generated

Example

In the provided example, the function f is declared with the nonnull attribute, which is designed to be applied solely to arguments that are pointers. The nonnull attribute informs the compiler that certain function parameters should not be NULL. However, this function is defined to take an integer argument instead of a pointer. Consequently, this incorrect application of the nonnull attribute to a non-pointer argument triggers a warning from the Clang compiler, highlighting that the nonnull attribute is applicable only to pointer arguments. Additionally, a warning is issued to signal that the attribute has been applied to a function devoid of pointer arguments. These warnings serve to notify the developer of the potential misuse of the nonnull attribute, which could result in incorrect assumptions about the program's behavior and potentially lead to undefined behavior.  
AI Generated


Flags -xc -ferror-limit=1 -fsyntax-only

[Try out in Compiler Explorer]

Source
void f(int x) __attribute__((nonnull(1))); // 'nonnull' expects a pointer
Compiler Output
<source>:1:30: warning: 'nonnull' attribute only applies to pointer arguments [-Wignored-attributes]
<source>:1:30: warning: 'nonnull' attribute applied to function with no pointer arguments [-Wignored-attributes]


Clang Internals (17.0.6)

Git Commit Message

This diagnostic did not accept arguments, and did not have any test coverage. Parameterized the diagnostic, and made it more consistent with other attribute diagnostic wordings. Added test coverage.

Since this warning was generalized, it was also given a sensible warning group flag and the corresponding test was updated to reflect this.

llvm-svn: 198053

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDeclAttr.cpp (line 1576)

static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL, SourceRange AttrParmRange, SourceRange TypeRange, bool isReturnValue = false) {
  if (!S.isValidPointerAttrType(T)) {
    if (isReturnValue)
    // ...
    else
      S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) << AL << AttrParmRange << TypeRange << 0;

clang/lib/Sema/SemaDeclAttr.cpp (line 1663)

static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
  // ...
  if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
    S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) << AL << AL.getRange() << 0;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/SemaObjC/nonnull.m

  • clang/test/SemaObjC/nonnull.m:104:66: warning: 'nonnull' attribute only applies to pointer arguments [-Wignored-attributes]