Clang warning: A attribute ignored on inline function [-Wignored-attributes] (warn_attribute_ignored_on_inline)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: A attribute ignored on inline function
Type Warning
Category Semantic Issue
Internal Id warn_attribute_ignored_on_inline
Active by Default Yes
Flags -Wno-attributes (85 elements)
-Wno-ignored-attributes (84 elements)
Internal Message %0 attribute ignored on inline function
Regular Expression (?:warning|error|fatal error)\: (.*?) attribute ignored on inline function \[(?:\-Werror,)?\-Wignored\-attributes[^\]]*\]
First Commit 2014-11-03 606bd6dcc547 Don't dllimport inline functions when targeting MinGW (PR21366)

Description

The warning is issued by the Clang compiler when an attribute applied to an inline function is ignored because it is not applicable to inline functions. Attributes are annotations added to program elements such as functions, variables, and types, which can instruct the compiler to treat those elements in a certain way. However, some attributes may have no effect when applied to inline functions due to the nature of inlining. Inlining is the process where the compiler replaces a function call with the actual code of the function, effectively eliminating the call overhead but also making some attributes irrelevant since the function is not called in the traditional sense. This warning serves to inform the user that the specified attribute will not be honored for the inline function, potentially indicating a misunderstanding of how attributes or inline functions work, or an unintended coding error.  
AI Generated

Example

In the following example, a function is declared with both the inline specifier and the __attribute__((dllimport)) attribute. The dllimport attribute is intended for use with functions or variables that are to be imported from a DLL, generally implying that the compiler should generate external linkage for the symbol. However, when applied to an inline function, the attribute is not relevant because inline functions are expanded at the call site, meaning they do not require external linkage in the same way. The compiler recognizes this discrepancy and issues a warning to inform that the dllimport attribute will not have any effect on an inline function. This serves as an alert to potentially incorrect assumptions about how the code will be handled or a misunderstanding of attribute applicability in this context.  
AI Generated


Flags -xc++ -target i686-pc-windows-gnu

[Try out in Compiler Explorer]

Source
__attribute__((dllimport)) inline void f() {}

// dllimport on inline function
Compiler Output
<source>:1:16: warning: 'dllimport' attribute ignored on inline function [-Wignored-attributes]


Clang Internals (17.0.6)

Git Commit Message

Don't dllimport inline functions when targeting MinGW (PR21366)

It turns out that MinGW never dllimports of exports inline functions.
This means that code compiled with Clang would fail to link with
MinGW-compiled libraries since we might try to import functions that
are not imported.

To fix this, make Clang never dllimport inline functions when targeting
MinGW.

llvm-svn: 221154

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 8039)

static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
  // ...
  if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
    if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport && !(S.Context.getTargetInfo().shouldDLLImportComdatSymbols())) {
      // ...
      S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) << A;

Triggered in Clang Tests

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

clang/test/Sema/dllimport.c

  • clang/test/Sema/dllimport.c:172:12: warning: 'dllimport' attribute ignored on inline function [-Wignored-attributes]
  • clang/test/Sema/dllimport.c:173:28: warning: 'dllimport' attribute ignored on inline function [-Wignored-attributes]
  • clang/test/Sema/dllimport.c:217:12: warning: 'dllimport' attribute ignored on inline function [-Wignored-attributes]