Bug 107439 - mesa defines likely/unlikely and these are C++20 keywords
Summary: mesa defines likely/unlikely and these are C++20 keywords
Status: RESOLVED MOVED
Alias: None
Product: Mesa
Classification: Unclassified
Component: Mesa core (show other bugs)
Version: git
Hardware: Other All
: medium normal
Assignee: mesa-dev
QA Contact: mesa-dev
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-07-31 23:47 UTC by Bruce Dawson
Modified: 2019-09-18 20:27 UTC (History)
1 user (show)

See Also:
i915 platform:
i915 features:


Attachments

Description Bruce Dawson 2018-07-31 23:47:36 UTC
In util/macros.h mesa #defines likely and unlikely. This used to be a reasonable and legal thing to do but with the advent of C++20 this is no longer legal.

C++20 treats likely and unlikely as reserved keywords for use as attributes.
https://en.cppreference.com/w/cpp/language/attributes/likely

This means that a conforming C++20 implementation may reject code that includes mesa's util/macros.h:
https://cgit.freedesktop.org/mesa/mesa/tree/src/util/macros.h#n50

The concrete manifestation of this problem is that future versions of VC++ (unknown release date) will treat definitions of likely and unlikely as hard errors. These errors can temporarily be avoided by using _ALLOW_KEYWORD_MACROS but that is clearly a temporary hack.

So, mesa should switch from likely/unlikely to LIKELY/UNLIKELY or some other variant.
Comment 1 Gustaw Smolarczyk 2018-08-01 10:50:10 UTC
Are these really reserved keywords? I thought the attribute namespace was different from the keyword namespace.

These are not listed as keywords, even for C++20:

https://en.cppreference.com/w/cpp/keyword

You should be able to define variables called "likely", for example.

https://wandbox.org/permlink/hFczuKlzyOjFjvkA

Using the [[likely]] syntax while having likely() defined as a macro might be a problem though, but that is a separate issue.
Comment 2 Casey Carter 2018-08-01 22:10:26 UTC
They're not technically keywords, they are "attribute-tokens". The problem is that there is library wording in the Standard which forbids defining macros with the same names as keywords, context-sensitive identifiers, and attribute-tokens. C++17 [macro.names] para 2:

> A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table 4, or to the attribute-tokens described in [dcl.attr].

When WG21 added "likely" and "unlikely" to [dcl.attr] in the C++20 working draft, that made it implicitly forbidden to define them as macros. (Since The change is a core language change the library groups never saw the proposal to notice the problem.)

I've filed an issue with WG21 to change [macro.names]/2 to allow definitions of "likely" and "unlikely" as function-like macros, which doesn't interfere with use of the attributes [[likely]] and [[unlikely]] since there are no parentheses to trigger expansion of a function-like macro. Assuming that the rest of the Library Working Group agrees with me that we can't simply break this much code for no good reason, you shouldn't need to change anything.
Comment 3 GitLab Migration User 2019-09-18 20:27:18 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to freedesktop.org's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.freedesktop.org/mesa/mesa/issues/1029.


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.