Commit 801fabc3 authored by Lev Walkin's avatar Lev Walkin

checking reserved keywords and double identifiers

parent 23fd2fa6
This diff is collapsed.
......@@ -13,8 +13,6 @@ static int emit_size_determination_code(arg_t *arg, asn1p_expr_type_e etype);
static asn1p_expr_type_e _find_terminal_type(arg_t *arg);
static int emit_range_comparison_code(arg_t *arg, asn1cnst_range_t *range, const char *varname, asn1c_integer_t natural_start, asn1c_integer_t natural_stop);
#define MKID(id) asn1c_make_identifier(0, (id), 0)
static int global_compile_mark;
int
......
......@@ -3,17 +3,35 @@
#include <asn1fix_export.h>
/*
* Checks that the given string is not a reserved C/C++ keyword.
*/
static char *res_kwd[] = {
"char", "int", "long",
"float", "double",
"struct", "typedef", "class" };
static int
reserved_keyword(const char *str) {
int i;
for(i = 0 ; i < sizeof(res_kwd)/sizeof(res_kwd[0]); i++) {
if(strcmp(str, res_kwd[i]) == 0)
return 1;
}
return 0;
}
/*
* Construct identifier from multiple parts.
* Convert unsafe characters to underscores.
*/
char *
asn1c_make_identifier(int unsafe_only_spaces, char *arg1, ...) {
asn1c_make_identifier(enum ami_flags_e flags, char *arg1, ...) {
static char *storage;
static int storage_size;
int nodelimiter = 0;
va_list ap;
char *str;
char *nextstr;
int size;
char *p;
......@@ -49,8 +67,9 @@ asn1c_make_identifier(int unsafe_only_spaces, char *arg1, ...) {
va_start(ap, arg1);
str = arg1;
p = storage;
for(str = arg1; str; str = va_arg(ap, char *)) {
for(str = arg1; str; str = nextstr) {
int subst_made = 0;
nextstr = va_arg(ap, char *);
if(str[0] == ' ' && str[1] == '\0') {
*p++ = ' ';
......@@ -62,12 +81,23 @@ asn1c_make_identifier(int unsafe_only_spaces, char *arg1, ...) {
*p++ = '_'; /* Delimiter between tokens */
nodelimiter = 0;
/*
* If it is a single argument, check that it does not clash
* with C/C++ language keywords.
*/
if((flags & AMI_CHECK_RESERVED)
&& str == arg1 && !nextstr && reserved_keyword(str)) {
*p++ = toupper(*str++);
/* Fall through */
}
for(; *str; str++) {
if(isalnum(*str)) {
*p++ = *str;
subst_made = 0;
} else if(!subst_made++) {
if(unsafe_only_spaces && !isspace(*str)) {
if((flags & AMI_MASK_ONLY_SPACES)
&& !isspace(*str)) {
*p ++ = *str;
} else {
*p++ = '_';
......@@ -87,6 +117,9 @@ char *
asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
asn1p_expr_t *top_parent;
char *typename;
enum ami_flags_e ami_flags = (_format & TNF_CHECK)
? AMI_CHECK_RESERVED : 0;
_format &= ~TNF_CHECK;
/* Rewind to the topmost parent expression */
if((top_parent = expr->parent_expr))
......@@ -180,13 +213,19 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
switch(_format) {
case TNF_UNMODIFIED:
case TNF_INCLUDE:
return asn1c_make_identifier(1, typename, 0);
assert(ami_flags == 0); /* (TNF_INCLUDE | TNF_CHECK)?! */
ami_flags |= AMI_MASK_ONLY_SPACES;
return asn1c_make_identifier(ami_flags, typename, 0);
case TNF_SAFE:
return asn1c_make_identifier(0, typename, 0);
return asn1c_make_identifier(ami_flags, typename, 0);
case TNF_CTYPE:
return asn1c_make_identifier(0, typename, "t", 0);
return asn1c_make_identifier(ami_flags, typename, "t", 0);
case TNF_RSAFE:
return asn1c_make_identifier(0, "struct", " ", typename, 0);
return asn1c_make_identifier(ami_flags, "struct", " ", typename, 0);
case TNF_NORCHECK:
case TNF_CHECK:
assert(_format != TNF_NORCHECK);
assert(_format != TNF_CHECK);
}
assert(!"unreachable");
......
......@@ -6,17 +6,23 @@
* The function will concatenate the names and replace unsafe characters
* with safe ones.
*/
char *asn1c_make_identifier(int unsafe_only_spaces, char *arg1, ...);
enum ami_flags_e {
AMI_MASK_ONLY_SPACES = 1, /* Mask only spaces, everything else's safe */
AMI_CHECK_RESERVED = 2, /* Check against reserved keywords */
};
char *asn1c_make_identifier(enum ami_flags_e, char *arg1, ...);
/*
* Return the type name of the specified expression.
*/
enum tnfmt {
TNF_UNMODIFIED, /* Return unmodified type name */
TNF_INCLUDE, /* Format for #include <> */
TNF_CTYPE, /* Format as normal C-ish type (append "_t") */
TNF_SAFE, /* Replace unsafe characters with _ */
TNF_RSAFE, /* Recursion-safe C type format */
TNF_NORCHECK = 0x00,
TNF_CHECK = 0x01,
TNF_UNMODIFIED = 0x10, /* Return unmodified type name */
TNF_INCLUDE = 0x20, /* Format for #include <> */
TNF_CTYPE = 0x30, /* Format as normal C-ish type (append "_t") */
TNF_SAFE = 0x40, /* Replace unsafe characters with _ */
TNF_RSAFE = 0x50, /* Recursion-safe C type format */
};
char *asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format);
......
......@@ -39,6 +39,10 @@ enum asn1c_flags {
* Do not generate constraint checking code.
*/
A1C_NO_CONSTRAINTS = 0x0080,
/*
* Generate type_id_PR_member things identifiers of id_PR_member.
*/
A1C_DOUBLE_IDENTIFIERS = 0x0100,
};
/*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment