Bogus warning when comparing nocast short variables
Hello!
This test file causes a warning:
typedef short __attribute__((nocast)) u16;
int main(int argc, char **argv)
{
u16 i = (u16)argc;
u16 j = (u16)2;
return (i == j);
}$ sparse test1.c test1.c:6:17: warning: implicit cast from nocast type test1.c:6:22: warning: implicit cast from nocast type
But this file doesn't cause any warning:
typedef short __attribute__((nocast)) u16;
int main(int argc, char **argv)
{
u16 i = (u16)argc;
u16 j = (u16)2;
switch (i) {
case j: return 1;;
default: return 0;;
}
}I think the warnings are bogus. No cast should be needed to compare variables of the same type.
The warning was actually found in the current Linux kernel, driver zd1121rw. A fix was proposed, but I thing the fix belongs to the sparse code.
Signature · 2 lines
Regards, Pavel Roskin
Quoted 12 lines · Pavel Roskin
Hello!
This test file causes a warning:
typedef short __attribute__((nocast)) u16;
intmain(int argc, char **argv)
{
u16 i = (u16)argc;
u16 j = (u16)2;
return (i == j);
}
Quoted 3 lines
$ sparse test1.c test1.c:6:17: warning: implicit cast from nocast type test1.c:6:22: warning: implicit cast from nocast type
The warning come from the implicit cast in the compare expression. It try to cast "i" and "j" into "int" type before the compare.
The following patch allow the test-inspect to show the cast expression.
No patch for the fix yet.
Chris
ast-inspect.c +19 -1
@@ -66,6 +66,12 @@ void inspect_statement(AstNode *node)ast_append_child (node,"post_statement:" , stmt->iterator_post_statement, inspect_statement);break ; + +case STMT_RETURN : +ast_append_child (node,"ret_value:" , stmt->ret_value, inspect_expression); +ast_append_child (node,"ret_target:" , stmt->ret_target, inspect_symbol); +break ; +default :break ; }
@@ -109,7 +115,7 @@ void inspect_symbol(AstNode *node) {struct symbol *sym = node->ptr; node->text =g_strdup_printf ("%s %s: %s" , node->text,symbol_type_name (sym->type), -show_ident (sym->ident)); +builtin_typename (sym) ?:show_ident (sym->ident));ast_append_child (node,"ctype.base_type:" , sym->ctype.base_type,inspect_symbol);switch (sym->type) {
@@ -181,6 +187,18 @@ void inspect_expression(AstNode *node)ast_append_child (node,"left:" , expr->left, inspect_expression);ast_append_child (node,"right:" , expr->right, inspect_expression);break ; + +case EXPR_CAST : +case EXPR_FORCE_CAST : +case EXPR_IMPLIED_CAST : +ast_append_child (node,"cast_type:" , expr->cast_type, inspect_symbol); +ast_append_child (node,"cast_expression:" , expr->cast_expression, inspect_expression); +break ; + +case EXPR_PREOP : +ast_append_child (node,"unop:" , expr->unop, inspect_expression); +break ; +default :break ; }