Skip to content

Commit e6760ab

Browse files
committed
Exclude fold expressions and for range loops in C mode
1 parent 98d24b7 commit e6760ab

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

packages/cxx-gen-ast/src/tokens.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ export const C_TOKEN_ALIASES = {
365365
_Bool: "BOOL",
366366
_Static_assert: "STATIC_ASSERT",
367367
_Thread_local: "THREAD_LOCAL",
368+
__typeof__: "typeof",
369+
__typeof: "typeof",
368370
};
369371

370372
export const C_AND_CXX_KEYWORDS = Array.from(

src/parser/cxx/parser.cc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ auto Parser::prec(TokenKind tk) -> Parser::Prec {
246246
} // switch
247247
}
248248

249+
auto Parser::is_c() const -> bool {
250+
return unit->language() == LanguageKind::kC;
251+
}
252+
253+
auto Parser::is_cxx() const -> bool {
254+
return unit->language() == LanguageKind::kCXX;
255+
}
256+
249257
auto Parser::LA(int n) const -> const Token& {
250258
return unit->tokenAt(SourceLocation(cursor_ + n));
251259
}
@@ -1462,6 +1470,8 @@ auto Parser::parse_nested_expession(ExpressionAST*& yyast,
14621470

14631471
auto Parser::parse_fold_expression(ExpressionAST*& yyast,
14641472
const ExprContext& ctx) -> bool {
1473+
if (!is_cxx()) return false;
1474+
14651475
if (!lookat(TokenKind::T_LPAREN)) return false;
14661476

14671477
if (parse_left_fold_expression(yyast, ctx)) return true;
@@ -3513,6 +3523,8 @@ auto Parser::parse_for_statement(StatementAST*& yyast) -> bool {
35133523
SourceLocation colonLoc;
35143524

35153525
auto lookat_for_range_declaration = [&] {
3526+
if (!is_cxx()) return false;
3527+
35163528
LookaheadParser lookahead{this};
35173529

35183530
if (!parse_for_range_declaration(rangeDeclaration)) return false;
@@ -6021,9 +6033,7 @@ auto Parser::parse_initializer_list(List<ExpressionAST*>*& yyast,
60216033
auto Parser::lookat_designator() -> bool {
60226034
if (lookat(TokenKind::T_DOT)) return true;
60236035

6024-
if (unit->language() == LanguageKind::kCXX) return false;
6025-
6026-
if (lookat(TokenKind::T_LBRACKET)) return true;
6036+
if (is_c() && lookat(TokenKind::T_LBRACKET)) return true;
60276037

60286038
return false;
60296039
}
@@ -6072,7 +6082,7 @@ auto Parser::parse_designated_initializer_clause(
60726082
*it = make_list_node(pool_, designator);
60736083
it = &(*it)->next;
60746084

6075-
if (unit->language() == LanguageKind::kC) {
6085+
if (is_c()) {
60766086
while (lookat_designator()) {
60776087
DesignatorAST* designator = nullptr;
60786088
parse_designator(designator);

src/parser/cxx/parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,9 @@ class Parser final {
739739
[[nodiscard]] auto parse_identifier_list(List<NameIdAST*>*& yyast) -> bool;
740740

741741
private:
742+
[[nodiscard]] auto is_c() const -> bool;
743+
[[nodiscard]] auto is_cxx() const -> bool;
744+
742745
[[nodiscard]] auto lookat(auto... tokens) {
743746
return lookatHelper(0, tokens...);
744747
}

src/parser/cxx/private/c_keywords-priv.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,16 @@ static inline auto classifyC8(const char* s) -> cxx::TokenKind {
620620
}
621621
}
622622
}
623+
} else if (s[3] == 'y') {
624+
if (s[4] == 'p') {
625+
if (s[5] == 'e') {
626+
if (s[6] == 'o') {
627+
if (s[7] == 'f') {
628+
return cxx::TokenKind::T___TYPEOF;
629+
}
630+
}
631+
}
632+
}
623633
}
624634
}
625635
} else if (s[1] == 'A') {
@@ -777,6 +787,22 @@ static inline auto classifyC10(const char* s) -> cxx::TokenKind {
777787
}
778788
}
779789
}
790+
} else if (s[2] == 't') {
791+
if (s[3] == 'y') {
792+
if (s[4] == 'p') {
793+
if (s[5] == 'e') {
794+
if (s[6] == 'o') {
795+
if (s[7] == 'f') {
796+
if (s[8] == '_') {
797+
if (s[9] == '_') {
798+
return cxx::TokenKind::T___TYPEOF__;
799+
}
800+
}
801+
}
802+
}
803+
}
804+
}
805+
}
780806
}
781807
}
782808
}

src/parser/cxx/token_fwd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ class Token;
276276
V(_BOOL, BOOL) \
277277
V(_STATIC_ASSERT, STATIC_ASSERT) \
278278
V(_THREAD_LOCAL, THREAD_LOCAL) \
279+
V(__TYPEOF__, DECLTYPE) \
280+
V(__TYPEOF, DECLTYPE) \
279281
V(AND_EQ, AMP_EQUAL) \
280282
V(AND, AMP_AMP) \
281283
V(BITAND, AMP) \
@@ -293,8 +295,6 @@ class Token;
293295
V(__DECLTYPE__, DECLTYPE) \
294296
V(__DECLTYPE, DECLTYPE) \
295297
V(__RESTRICT, __RESTRICT__) \
296-
V(__TYPEOF__, DECLTYPE) \
297-
V(__TYPEOF, DECLTYPE) \
298298
V(__VOLATILE__, VOLATILE) \
299299
V(__VOLATILE, VOLATILE)
300300

0 commit comments

Comments
 (0)