Skip to content

Commit

Permalink
Prohibit function parameter redefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
splasky committed May 9, 2018
1 parent b3f2e3f commit 5e3476f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions shivyc/tree/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ def _generate_array_ctype(self, decl, prev_ctype):

def _generate_func_ctype(self, decl, prev_ctype):
"""Generate a function ctype from a given a decl_node."""
# save identifiers
identifiers = []

# Prohibit storage class specifiers in parameters.
for param in decl.args:
Expand All @@ -608,6 +610,14 @@ def _generate_func_ctype(self, decl, prev_ctype):
err = "storage class specified for function parameter"
raise CompilerError(err, decl_info.range)

if decl_info.identifier:
identifier = str(decl_info.identifier)
if identifier not in identifiers:
identifiers.append(str(decl_info.identifier))
else:
err = f"redefinition of '{identifier}'"
raise CompilerError(err, decl_info.range)

# Create a new scope because if we create a new struct type inside
# the function parameters, it should be local to those parameters.
self.symbol_table.new_scope()
Expand Down
3 changes: 3 additions & 0 deletions tests/feature_tests/error_declaration.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ int var1;
// error: redeclared 'var1' with different linkage
static int var1;

// error: redefinition of 'a'
void repeat_param(int a, int a);

int main() {
// error: variable of incomplete type declared
void a;
Expand Down

0 comments on commit 5e3476f

Please sign in to comment.