Skip to content

Commit

Permalink
C++23 preprocessor extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
guwirth committed Aug 28, 2024
1 parent 41ab59e commit 908ba34
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
21 changes: 0 additions & 21 deletions cxx-squid/dox/diff-cpp20-cpp23_grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,24 +268,3 @@ export-declaration:

concept-definition:
concept concept-name attribute-specifier-seqopt = constraint-expression ;

**A.13 Preprocessing directives [gram.cpp]**

control-line:
# include pp-tokens new-line
pp-import
# define identifier replacement-list new-line
# define identifier lparen identifier-listopt ) replacement-list new-line
# define identifier lparen ... ) replacement-list new-line
# define identifier lparen identifier-list , ... ) replacement-list new-line
# undef identifier new-line
# line pp-tokens new-line
# error pp-tokensopt new-line
# warning pp-tokensopt new-line
# pragma pp-tokensopt new-line
# new-line

elif-group:
# elif constant-expression new-line groupopt
# elifdef identifier new-line groupopt
# elifndef identifier new-line groupopt
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ void lineLine() {
void errorLine() {
p.setRootRule(g.rule(PPGrammarImpl.errorLine));

assertThat(p).matches("#error foo");
assertThat(p).matches("#error token");
assertThat(p).matches("#error token token");
assertThat(p).matches("#error \"txt\"");
assertThat(p).matches("#error");
}

Expand All @@ -331,7 +333,10 @@ void pragmaLine() {
void warningLine() {
p.setRootRule(g.rule(PPGrammarImpl.warningLine));

assertThat(p).matches("#warning foo");
assertThat(p).matches("#warning token");
assertThat(p).matches("#warning token token");
assertThat(p).matches("#warning \"txt\"");
assertThat(p).matches("#warning");
}

@Test
Expand Down
48 changes: 48 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++23/elifxdef.cc
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

}
4 changes: 4 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++23/warning.cc
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)"

0 comments on commit 908ba34

Please sign in to comment.