Clang error: overriding virtual function must specify the same code segment as its overridden function (err_mismatched_code_seg_override)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: overriding virtual function must specify the same code segment as its overridden function (since 7.0)
Type Error
Category Semantic Issue (since 7.0)
Internal Id err_mismatched_code_seg_override (since 7.0)
Internal Message overriding virtual function must specify the same code segment as its overridden function (since 7.0)
Regular Expression (?:error|fatal error)\: overriding virtual function must specify the same code segment as its overridden function
First Commit 2018-05-16 64144eb194c8 Add support for __declspec(code_seg("segname"))

Description

The error is issued by the Clang compiler when an overriding virtual function in a derived class specifies a different code segment (using the __declspec(code_seg("...")) attribute) from the overridden virtual function in the base class. In C++, virtual functions enable a derived class to provide a specific implementation that will be called instead of a base class's version. For a successful override, various conditions must be met, including compatibility in function signature and attributes that affect the function's linkage and behavior, such as the code segment where the function is placed.

Code segments are a way to inform the linker about how different pieces of code should be grouped in the final executable, which can be crucial for certain system-level programming tasks, such as developing drivers or operating systems. The __declspec(code_seg("...")) attribute explicitly places the decorated entity into a named section (code segment) of the resulting binary. This error occurs because the overriding function alters the intended placement, leading to inconsistencies in how the virtual function table (vtable) might be organized or how the linker treats these functions, potentially affecting optimizations or runtime behavior.

To resolve this issue, ensure that both the base and derived class methods specify the same code segment using the __declspec(code_seg("...")) attribute, or entirely remove the attribute from both methods if the specific segment placement is not required. This consistency helps in preserving the intended layout of the executable's sections, optimizing memory usage, and possibly influencing the executable's loading time and performance.

 
AI Generated

Example

In the following example, a structure named A is defined with a virtual function f that has no implementation. This structure utilizes the __declspec(code_seg(".text")) attribute to indicate that the function f should be placed in the .text code segment, a common segment for executable code. A derived structure named B overrides the virtual function f from A, also providing no implementation. However, the override in B specifies a different code segment for f using __declspec(code_seg(".text_different")). This difference in specified code segments between the base and derived classes triggers the Clang compiler to issue an error. The error message indicates that the overriding virtual function in the derived class must specify the same code segment as the overridden function in the base class to maintain consistency in code placement, which is essential for expected behavior and optimizations related to the organization of the binary's code segments.  
AI Generated


Flags -xc++ -fdeclspec

[Try out in Compiler Explorer]

Source
__declspec(code_seg(".text")) struct A {
    virtual void f() {}
};

struct B : A {
    __declspec(code_seg(".text_different")) void f() override {}
    // Different code segment
};

int main() {
    return 0;
}
Compiler Output
<source>:1:12: warning: attribute 'code_seg' is ignored, place it after "struct" to apply attribute to type declaration [-Wignored-attributes]
<source>:6:50: error: overriding virtual function must specify the same code segment as its overridden function
<source>:2:18: note: previous declaration is here


Clang Internals (17.0.6)

Git Commit Message

Add support for __declspec(code_seg("segname"))

Add support for __declspec(code_seg("segname"))

This patch is built on the existing support for #pragma code_seg. The code_seg
declspec is allowed on functions and classes. The attribute enables the
placement of code into separate named segments, including compiler-generated
members and template instantiations.

For more information, please see the following:
https://msdn.microsoft.com/en-us/library/dn636922.aspx

A new CodeSeg attribute is used instead of adding a new spelling to the existing
Section attribute since they don’t apply to the same Subjects. Section
attributes are also added for the code_seg declspec since they are used for
#pragma code_seg. No CodeSeg attributes are added to the AST.

The patch is written to match with the Microsoft compiler’s behavior even where
that behavior is a little complicated (see https://reviews.llvm.org/D22931, the
Microsoft feedback page is no longer available since MS has removed the page).
That code is in getImplicitSectionAttrFromClass routine.

Diagnostics messages are added to match with the Microsoft compiler for code-seg
attribute mismatches on base and derived classes and virtual overrides.


Differential Revision: https://reviews.llvm.org/D43352

llvm-svn: 332470

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/SemaDeclCXX.cpp (line 17993)

bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, const CXXMethodDecl *Old) {
  // ...
  if ((NewCSA || OldCSA) && (!OldCSA || !NewCSA || NewCSA->getName() != OldCSA->getName())) {
    Diag(New->getLocation(), diag::err_mismatched_code_seg_override);

Triggered in Clang Tests

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

clang/test/SemaCXX/code-seg.cpp

  • clang/test/SemaCXX/code-seg.cpp:60:7: error: overriding virtual function must specify the same code segment as its overridden function
  • clang/test/SemaCXX/code-seg.cpp:70:15: error: overriding virtual function must specify the same code segment as its overridden function
  • clang/test/SemaCXX/code-seg.cpp:73:46: error: overriding virtual function must specify the same code segment as its overridden function