Improve handling parameters clashing with language keywords
Previously, only Python tried to preserve the original parameter name (by prepending or appending an underscore to it, but otherwise keeping the original name) if it conflicted with one of the language keywords, while all the other languages replaced the parameter name with a meaningless "argN" in this case. Now do this for all languages as this results in more readable generated code and there doesn't seem to be any reason to restrict this to Python only.
This commit is contained in:
parent
fa0c3fe5c2
commit
3f5c17824c
2 changed files with 9 additions and 18 deletions
|
|
@ -3602,7 +3602,7 @@ String *Language::makeParameterName(Node *n, Parm *p, int arg_num, bool setter)
|
|||
String *arg = 0;
|
||||
String *pn = Getattr(p, "name");
|
||||
|
||||
// Use C parameter name unless it is a duplicate or an empty parameter name
|
||||
// Check if parameter name is a duplicate.
|
||||
int count = 0;
|
||||
ParmList *plist = Getattr(n, "parms");
|
||||
while (plist) {
|
||||
|
|
@ -3610,8 +3610,14 @@ String *Language::makeParameterName(Node *n, Parm *p, int arg_num, bool setter)
|
|||
count++;
|
||||
plist = nextSibling(plist);
|
||||
}
|
||||
String *wrn = pn ? Swig_name_warning(p, 0, pn, 0) : 0;
|
||||
arg = (!pn || (count > 1) || wrn) ? NewStringf("arg%d", arg_num) : Copy(pn);
|
||||
|
||||
// If the parameter has no name at all or has a non-unique name, replace it with "argN".
|
||||
if (!pn || count > 1) {
|
||||
arg = NewStringf("arg%d", arg_num);
|
||||
} else {
|
||||
// Otherwise, try to use the original C name, but modify it if necessary to avoid conflicting with the language keywords.
|
||||
arg = Swig_name_make(p, 0, pn, 0, 0);
|
||||
}
|
||||
|
||||
if (setter && Cmp(arg, "self") != 0) {
|
||||
// Some languages (C#) insist on calling the input variable "value" while
|
||||
|
|
|
|||
|
|
@ -1591,21 +1591,6 @@ public:
|
|||
return ds;
|
||||
}
|
||||
|
||||
virtual String *makeParameterName(Node *n, Parm *p, int arg_num, bool = false) const {
|
||||
// For the keyword arguments, we want to preserve the names as much as possible,
|
||||
// so we only minimally rename them in Swig_name_make(), e.g. replacing "keyword"
|
||||
// with "_keyword" if they have any name at all.
|
||||
if (check_kwargs(n)) {
|
||||
String *name = Getattr(p, "name");
|
||||
if (name)
|
||||
return Swig_name_make(p, 0, name, 0, 0);
|
||||
}
|
||||
|
||||
// For the other cases use the general function which replaces arguments whose
|
||||
// names clash with keywords with (less useful) "argN".
|
||||
return Language::makeParameterName(n, p, arg_num);
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* addMissingParameterNames()
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue