Fix -Wundefined-var-template warnings
This warning, which we treat as an error, occurs when a static member of
an implicitly instatiated template class is referenced, but there is no
definition available in the current translation unit.
Note that with a non-template class this isn't a problem, since there
can only be one definition, which is expected to be resolved at link
time. But with a template class each instantiation can have a different
static member variable definition.
This warning typically occurs when the definition is available in
another translation unit, so it may be tempting to move that definition
into the header file to make it available to any translation unit where
the static member may be referenced. However, this typically leads to
multiple-definition errors.
Like all multiple-definition errors, we can address that by declaring,
not defining, the variable in the header, and leaving the definition in
one translation unit. In the case of a non-template class the
declaration in the class definition suffices since it's fully
instantiated, but for a template class we need to re-declare the static
member for the explicit instantiation.
Concretely, in this case Subzero has a template class for x86
instructions, with a static member for the Opcode string. This string is
different for each instantiation of the instruction template, so we need
to forward-declare each instantiated class's static member in the
header. There's already a macro for the definitions, which is repurposed
for declaring the instantiated members in the header.
Note that the C++ reference states that "An explicit specialization of a
static data member of a template is a definition if the declaration
includes an initializer; otherwise, it is a declaration."
(https://en.cppreference.com/w/cpp/language/template_specialization)
But Visual Studio treats them as definitions anyway, leading to
multiple-definition errors. Note that we can't add 'extern' to
explicitly make it a declaration only, because storage-class specifiers
are not valid for class members. So we just omit the declaration for
compilers other than Clang, which don't have this warning enabled
anyway.
Bug: chromium:604888
Change-Id: I63b58ecdf956ff264e6d25738684b513f05b268b
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/55208
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by:
Nicolas Capens <nicolascapens@google.com>
Reviewed-by:
Alexis Hétu <sugoi@google.com>
Showing
This diff is collapsed.
Click to expand it.
Please
register
or
sign in
to comment