Clang warning: ... does not match previous declaration [-Wsection] (warn_mismatched_section)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error:
codeseg
section
does not match previous declaration

(since 7.0)
error: section does not match previous declaration (until 6.0)

Type Warning
Category Semantic Issue
Internal Id warn_mismatched_section
Active by Default Yes
Flags -Wno-section (3 elements)
Internal Message %select{codeseg|section}0 does not match previous declaration (since 7.0)
section does not match previous declaration (until 6.0)
Regular Expression (?:warning|error|fatal error)\: (?:codeseg|section) does not match previous declaration \[(?:\-Werror,)?\-Wsection[^\]]*\]
First Commit 2012-05-13 9869c3a10fdb Produce a warning for mismatched section attributes. Completest pr9356.

Description

The warning is issued by the Clang compiler when there is a discrepancy between the section attributes of declarations. This occurs when two declarations of the same function or variable specify different section attributes. The section attribute directs the linker to place the specified functions or variables in particular sections of the output binary. Discrepancies may lead to undefined behavior, as the compiler expects all declarations of an entity to agree on the section attribute. The Clang compiler identifies such mismatches and issues a warning to alert the developer. To prevent potential issues during linking, it is crucial to ensure consistency by using the same section name in all declarations of the same entity that specify a section attribute.  
AI Generated

Example

In the following example, two declarations of the function f are provided, each with a different section attribute. The first declaration specifies that f should be placed in the .text section of the output binary, commonly used for executable code. The second declaration, however, specifies the .data section, typically used for initialized data. This disparity between section attributes for the same function triggers the warning. The compiler output included demonstrates the generated warning message, indicating that the section specified in the second declaration does not match the section specified in the previous declaration. The warning is meant to alert about the inconsistency, as such discrepancies could potentially lead to issues during the linking stage.  
AI Generated


Flags -xc

[Try out in Compiler Explorer]

Source
__attribute__((section(".text"))) void f(void) {}
__attribute__((section(".data"))) void f(void);
Compiler Output
<source>:2:16: warning: section does not match previous declaration [-Wsection]
<source>:1:16: note: previous attribute is here


Clang Internals (17.0.6)

Git Commit Message

Produce a warning for mismatched section attributes. Completest pr9356.

llvm-svn: 156727

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/SemaDecl.cpp (line 3281)

/// mergeDeclAttributes - Copy attributes from the Old decl to the New one.
void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, AvailabilityMergeKind AMK) {
  // ...
  if (NewCSA && !Old->hasAttr<CodeSegAttr>() && !NewCSA->isImplicit() && isa<CXXMethodDecl>(New)) {
    Diag(New->getLocation(), diag::warn_mismatched_section) << 0 /*codeseg*/;

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

SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI, StringRef Name) {
  // ...
  if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
    // ...
    Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) << 1 /*section*/;

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

CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI, StringRef Name) {
  // ...
  if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
    // ...
    Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) << 0 /*codeseg*/;

Triggered in Clang Tests

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

clang/test/SemaCXX/attr-section.cpp

  • clang/test/SemaCXX/attr-section.cpp:15:21: warning: section does not match previous declaration [-Wsection]
  • clang/test/SemaCXX/attr-section.cpp:42:18: warning: section does not match previous declaration [-Wsection]