3535
3636namespace cxx {
3737
38- static auto getFunctionDeclaratorHelper (DeclaratorAST* declarator)
38+ namespace {
39+
40+ class RecordingDiagnosticsClient : public DiagnosticsClient {
41+ public:
42+ void reset () { messages_.clear (); }
43+
44+ void reportTo (DiagnosticsClient* client) {
45+ for (const auto & message : messages_) {
46+ client->report (message);
47+ }
48+ }
49+
50+ auto messages () const -> const std::vector<Diagnostic>& { return messages_; }
51+
52+ void report (const Diagnostic& message) override {
53+ messages_.push_back (message);
54+ }
55+
56+ private:
57+ std::vector<Diagnostic> messages_;
58+ };
59+
60+ auto getFunctionDeclaratorHelper (DeclaratorAST* declarator)
3961 -> std::pair<FunctionDeclaratorAST*, bool> {
4062 if (!declarator) return std::make_pair (nullptr , false );
4163
@@ -64,11 +86,13 @@ static auto getFunctionDeclaratorHelper(DeclaratorAST* declarator)
6486 return std::make_pair (nullptr , false );
6587}
6688
67- static auto getFunctionDeclarator (DeclaratorAST* declarator)
89+ auto getFunctionDeclarator (DeclaratorAST* declarator)
6890 -> FunctionDeclaratorAST* {
6991 return get<0 >(getFunctionDeclaratorHelper (declarator));
7092}
7193
94+ } // namespace
95+
7296Parser::Parser (TranslationUnit* unit) : unit(unit) {
7397 control = unit->control ();
7498 cursor_ = 1 ;
@@ -1945,6 +1969,8 @@ auto Parser::parse_unop_expression(ExpressionAST*& yyast) -> bool {
19451969
19461970 if (!parse_cast_expression (ast->expression )) {
19471971 parse_error (" expected an expression" );
1972+ rewind (opLoc);
1973+ return false ;
19481974 }
19491975
19501976 return true ;
@@ -2289,7 +2315,18 @@ auto Parser::parse_delete_expression(ExpressionAST*& yyast) -> bool {
22892315auto Parser::parse_cast_expression (ExpressionAST*& yyast) -> bool {
22902316 const auto start = currentLocation ();
22912317
2292- if (parse_cast_expression_helper (yyast)) return true ;
2318+ RecordingDiagnosticsClient diags;
2319+
2320+ auto savedDiagnosticClient = unit->changeDiagnosticsClient (&diags);
2321+
2322+ auto parsedCastExpression = parse_cast_expression_helper (yyast);
2323+
2324+ unit->changeDiagnosticsClient (savedDiagnosticClient);
2325+
2326+ if (parsedCastExpression) {
2327+ diags.reportTo (unit->diagnosticsClient ());
2328+ return true ;
2329+ }
22932330
22942331 rewind (start);
22952332
@@ -2311,7 +2348,9 @@ auto Parser::parse_cast_expression_helper(ExpressionAST*& yyast) -> bool {
23112348
23122349 ExpressionAST* expression = nullptr ;
23132350
2314- if (!parse_cast_expression (expression)) return false ;
2351+ if (!parse_cast_expression (expression)) {
2352+ return false ;
2353+ }
23152354
23162355 auto ast = new (pool) CastExpressionAST ();
23172356 yyast = ast;
0 commit comments