Skip to content

Commit 29611ee

Browse files
committed
fix: Temporary workaround to parse post increments #118
This is a temporary workaround to accept a larger subset of increments expressions.
1 parent 1592673 commit 29611ee

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

src/parser/cxx/parser.cc

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,29 @@
3535

3636
namespace 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+
7296
Parser::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 {
22892315
auto 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;

src/parser/cxx/translation_unit.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ TranslationUnit::TranslationUnit(Control* control,
5050

5151
TranslationUnit::~TranslationUnit() = default;
5252

53+
auto TranslationUnit::diagnosticsClient() const -> DiagnosticsClient* {
54+
return diagnosticsClient_;
55+
}
56+
57+
auto TranslationUnit::changeDiagnosticsClient(
58+
DiagnosticsClient* diagnosticsClient) -> DiagnosticsClient* {
59+
std::swap(diagnosticsClient_, diagnosticsClient);
60+
return diagnosticsClient;
61+
}
62+
5363
void TranslationUnit::setSource(std::string source, std::string fileName) {
5464
fileName_ = std::move(fileName);
5565
preprocessor_->preprocess(std::move(source), fileName_, tokens_);

src/parser/cxx/translation_unit.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class TranslationUnit {
4848

4949
[[nodiscard]] auto arena() const -> Arena* { return arena_.get(); }
5050

51-
[[nodiscard]] auto diagnosticsClient() const -> DiagnosticsClient* {
52-
return diagnosticsClient_;
53-
}
51+
[[nodiscard]] auto diagnosticsClient() const -> DiagnosticsClient*;
52+
53+
auto changeDiagnosticsClient(DiagnosticsClient* diagnosticsClient)
54+
-> DiagnosticsClient*;
5455

5556
[[nodiscard]] auto ast() const -> UnitAST* { return ast_; }
5657

tests/unit_tests/parser/postfix_expr_conflicts.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ auto decr(int x) -> int { return x--; }
55
auto call(int (*f)(int, int), int x) { return (f)(x, x); }
66
auto subscript(int a[], int n) -> int { return a[n]; }
77

8-
// expected-error@1 {{expected an expression}}
98
auto incr_1(int x) -> int { return (x)++; }
10-
11-
// expected-error@1 {{expected an expression}}
129
auto decr_1(int x) -> int { return (x)--; }
1310

1411
// expected-error@1 {{expected lambda declarator}}

0 commit comments

Comments
 (0)