Skip to content

Commit 31ce6de

Browse files
committed
In C mode bind the enumerators only in the enclosed block and global scope
Fixes #566 Signed-off-by: Roberto Raggi <[email protected]>
1 parent 4f3193d commit 31ce6de

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/parser/cxx/binder.cc

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ auto Binder::control() const -> Control* {
4747
return unit_ ? unit_->control() : nullptr;
4848
}
4949

50+
auto Binder::is_parsing_c() const {
51+
return unit_->language() == LanguageKind::kC;
52+
}
53+
54+
auto Binder::is_parsing_cxx() const {
55+
return unit_->language() == LanguageKind::kCXX;
56+
}
57+
5058
auto Binder::reportErrors() const -> bool { return reportErrors_; }
5159

5260
void Binder::setReportErrors(bool reportErrors) {
@@ -344,13 +352,15 @@ void Binder::bind(DecltypeSpecifierAST* ast) {
344352

345353
void Binder::bind(EnumeratorAST* ast, const Type* type,
346354
std::optional<ConstValue> value) {
347-
auto symbol = control()->newEnumeratorSymbol(scope(), ast->identifierLoc);
348-
ast->symbol = symbol;
355+
if (is_parsing_cxx()) {
356+
auto symbol = control()->newEnumeratorSymbol(scope(), ast->identifierLoc);
357+
ast->symbol = symbol;
349358

350-
symbol->setName(ast->identifier);
351-
symbol->setType(type);
352-
ast->symbol->setValue(value);
353-
scope()->addSymbol(symbol);
359+
symbol->setName(ast->identifier);
360+
symbol->setType(type);
361+
ast->symbol->setValue(value);
362+
scope()->addSymbol(symbol);
363+
}
354364

355365
if (auto enumSymbol = symbol_cast<EnumSymbol>(scope()->owner())) {
356366
auto enumeratorSymbol =
@@ -361,6 +371,10 @@ void Binder::bind(EnumeratorAST* ast, const Type* type,
361371

362372
auto parentScope = enumSymbol->enclosingScope();
363373
parentScope->addSymbol(enumeratorSymbol);
374+
375+
if (!is_parsing_cxx()) {
376+
ast->symbol = enumeratorSymbol;
377+
}
364378
}
365379
}
366380

src/parser/cxx/binder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ class Binder {
158158
~ScopeGuard() { p->setScope(savedScope); }
159159
};
160160

161+
[[nodiscard]] auto is_parsing_c() const;
162+
[[nodiscard]] auto is_parsing_cxx() const;
163+
161164
private:
162165
TranslationUnit* unit_ = nullptr;
163166
Scope* scope_ = nullptr;

tests/unit_tests/sema/enum_c_01.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %cxx -verify -fcheck -dump-symbols -xc %s | %filecheck %s
2+
3+
enum X {
4+
A,
5+
B,
6+
C,
7+
};
8+
9+
enum X x;
10+
11+
// CHECK:namespace
12+
// CHECK-NEXT:enum X : int
13+
// CHECK-NEXT:enumerator X A
14+
// CHECK-NEXT:enumerator X B
15+
// CHECK-NEXT:enumerator X C
16+
// CHECK-NEXT:variable X x

0 commit comments

Comments
 (0)