forked from SonarOpenCommunity/sonar-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [#elifdef and #elifndef](https://en.cppreference.com/w/cpp/preprocessor/conditional) [P2334R1](https://wg21.link/P2334R1) - [#warning](https://en.cppreference.com/w/cpp/preprocessor/error) [P2437R1](https://wg21.link/P2437R1) - linked to SonarOpenCommunity#2536
- Loading branch information
Showing
4 changed files
with
59 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
void sample() { | ||
|
||
// Note that if a compiler does not support C++23's #elifdef/#elifndef | ||
// directives then the "unexpected" block (see below) will be selected. | ||
#ifdef CPU | ||
std::cout << "4: no1\n"; | ||
#elifdef GPU | ||
std::cout << "4: no2\n"; | ||
#elifndef RAM | ||
std::cout << "4: yes\n"; // expected block | ||
#else | ||
std::cout << "4: no!\n"; // unexpectedly selects this block by skipping | ||
// unknown directives and "jumping" directly | ||
// from "#ifdef CPU" to this "#else" block | ||
#endif | ||
|
||
// To fix the problem above we may conditionally define the | ||
// macro ELIFDEF_SUPPORTED only if the C++23 directives | ||
// #elifdef/#elifndef are supported. | ||
#if 0 | ||
#elifndef UNDEFINED_MACRO | ||
#define ELIFDEF_SUPPORTED | ||
#else | ||
#endif | ||
|
||
#ifdef ELIFDEF_SUPPORTED | ||
#ifdef CPU | ||
std::cout << "4: no1\n"; | ||
#elifdef GPU | ||
std::cout << "4: no2\n"; | ||
#elifndef RAM | ||
std::cout << "4: yes\n"; // expected block | ||
#else | ||
std::cout << "4: no3\n"; | ||
#endif | ||
#else // when #elifdef unsupported use old verbose `#elif defined` | ||
#ifdef CPU | ||
std::cout << "4: no1\n"; | ||
#elif defined GPU | ||
std::cout << "4: no2\n"; | ||
#elif !defined RAM | ||
std::cout << "4: yes\n"; // expected block | ||
#else | ||
std::cout << "4: no3\n"; | ||
#endif | ||
#endif | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#error | ||
#error "diagnostic-message" | ||
#warning | ||
#warning "diagnostic-message (since C++23)" |