Commit 31430670df468ee90e260e006e277074a2a9319f

Ran Benita 2014-01-11T16:40:42

Fix some cppcheck warnings Someone was nice enough to run this for us: ftp://ftp.sunet.se/pub/Linux/distributions/Debian/debian/pool/main/libx/libxkbcommon/libxkbcommon_0.3.1.orig.tar.gz [libxkbcommon-0.3.1/src/keymap.c:86]: (style) The scope of the variable 'j' can be reduced. [libxkbcommon-0.3.1/src/keymap.c:87]: (style) The scope of the variable 'key' can be reduced. [libxkbcommon-0.3.1/src/keysym-utf.c:843]: (style) The scope of the variable 'mid' can be reduced. [libxkbcommon-0.3.1/src/state.c:992]: (style) The scope of the variable 'str' can be reduced. [libxkbcommon-0.3.1/src/xkbcomp/action.c:467]: (style) The scope of the variable 'absolute' can be reduced. [libxkbcommon-0.3.1/src/xkbcomp/rules.c:468]: (style) The scope of the variable 'consumed' can be reduced. [libxkbcommon-0.3.1/src/xkbcomp/rules.c:862]: (style) The scope of the variable 'mlvo' can be reduced. [libxkbcommon-0.3.1/src/xkbcomp/rules.c:863]: (style) The scope of the variable 'kccgst' can be reduced. [libxkbcommon-0.3.1/src/xkbcomp/rules.c:865]: (style) The scope of the variable 'match_type' can be reduced. [libxkbcommon-0.3.1/src/xkbcomp/symbols.c:753]: (style) The scope of the variable 'toAct' can be reduced. [libxkbcommon-0.3.1/src/xkbcomp/symbols.c:1573]: (style) The scope of the variable 'key' can be reduced. [libxkbcommon-0.3.1/test/common.c:80]: (warning) %d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'. [libxkbcommon-0.3.1/test/interactive.c:358]: (style) The scope of the variable 'nevs' can be reduced. [libxkbcommon-0.3.1/test/interactive.c:236]: (style) Checking if unsigned variable 'nsyms' is less than zero. [libxkbcommon-0.3.1/test/interactive.c:226]: (style) Unused variable: unicode Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/keymap.c b/src/keymap.c
index 52d66f6..96d250c 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -105,18 +105,16 @@ xkb_keymap_ref(struct xkb_keymap *keymap)
 XKB_EXPORT void
 xkb_keymap_unref(struct xkb_keymap *keymap)
 {
-    unsigned int i, j;
-    struct xkb_key *key;
-
     if (!keymap || --keymap->refcnt > 0)
         return;
 
     if (keymap->keys) {
+        struct xkb_key *key;
         xkb_foreach_key(key, keymap) {
             if (key->groups) {
-                for (i = 0; i < key->num_groups; i++) {
+                for (unsigned i = 0; i < key->num_groups; i++) {
                     if (key->groups[i].levels) {
-                        for (j = 0; j < XkbKeyGroupWidth(key, i); j++)
+                        for (unsigned j = 0; j < XkbKeyGroupWidth(key, i); j++)
                             if (key->groups[i].levels[j].num_syms > 1)
                                 free(key->groups[i].levels[j].u.syms);
                         free(key->groups[i].levels);
@@ -128,7 +126,7 @@ xkb_keymap_unref(struct xkb_keymap *keymap)
         free(keymap->keys);
     }
     if (keymap->types) {
-        for (i = 0; i < keymap->num_types; i++) {
+        for (unsigned i = 0; i < keymap->num_types; i++) {
             free(keymap->types[i].entries);
             free(keymap->types[i].level_names);
         }
diff --git a/src/keysym-utf.c b/src/keysym-utf.c
index 9a96053..129da15 100644
--- a/src/keysym-utf.c
+++ b/src/keysym-utf.c
@@ -840,14 +840,13 @@ bin_search(const struct codepair *table, size_t length, xkb_keysym_t keysym)
 {
     int first = 0;
     int last = length;
-    int mid;
 
     if (keysym < table[0].keysym  || keysym > table[length].keysym)
         return 0;
 
     /* binary search in table */
     while (last >= first) {
-        mid = (first + last) / 2;
+        int mid = (first + last) / 2;
         if (table[mid].keysym < keysym)
             first = mid + 1;
         else if (table[mid].keysym > keysym)
diff --git a/src/state.c b/src/state.c
index e13d1fd..768d47c 100644
--- a/src/state.c
+++ b/src/state.c
@@ -1002,13 +1002,12 @@ xkb_state_mod_names_are_active(struct xkb_state *state,
 {
     va_list ap;
     xkb_mod_index_t idx = 0;
-    const char *str;
     xkb_mod_mask_t wanted = 0;
     int ret = 0;
 
     va_start(ap, match);
     while (1) {
-        str = va_arg(ap, const char *);
+        const char *str = va_arg(ap, const char *);
         if (str == NULL)
             break;
         idx = xkb_keymap_mod_get_index(state->keymap, str);
diff --git a/src/xkbcomp/action.c b/src/xkbcomp/action.c
index fed108f..9882c29 100644
--- a/src/xkbcomp/action.c
+++ b/src/xkbcomp/action.c
@@ -468,18 +468,14 @@ HandleMovePtr(struct xkb_keymap *keymap, union xkb_action *action,
               const ExprDef *value)
 {
     struct xkb_pointer_action *act = &action->ptr;
-    bool absolute;
 
     if (array_ndx && (field == ACTION_FIELD_X || field == ACTION_FIELD_Y))
         return ReportActionNotArray(keymap, action->type, field);
 
     if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
         int val;
-
-        if (value->expr.op == EXPR_NEGATE || value->expr.op == EXPR_UNARY_PLUS)
-            absolute = false;
-        else
-            absolute = true;
+        const bool absolute = (value->expr.op != EXPR_NEGATE &&
+                               value->expr.op != EXPR_UNARY_PLUS);
 
         if (!ExprResolveInteger(keymap->ctx, value, &val))
             return ReportMismatch(keymap, action->type, field, "integer");
diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c
index 36d87b8..56cce43 100644
--- a/src/xkbcomp/symbols.c
+++ b/src/xkbcomp/symbols.c
@@ -706,7 +706,6 @@ AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
     GroupInfo *groupi;
     unsigned int nActs;
     ExprDef *act;
-    union xkb_action *toAct;
 
     if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
         return false;
@@ -744,7 +743,7 @@ AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
 
     act = value->unary.child;
     for (i = 0; i < nActs; i++) {
-        toAct = &darray_item(groupi->levels, i).action;
+        union xkb_action *toAct = &darray_item(groupi->levels, i).action;
 
         if (!HandleActionDef(act, info->keymap, toAct, info->actions))
             log_err(info->keymap->ctx,
@@ -1528,7 +1527,6 @@ CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
 {
     KeyInfo *keyi;
     ModMapEntry *mm;
-    struct xkb_key *key;
 
     keymap->symbols_section_name = strdup_safe(info->name);
     XkbEscapeMapName(keymap->symbols_section_name);
@@ -1542,6 +1540,8 @@ CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
             info->errorCount++;
 
     if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
+        struct xkb_key *key;
+
         xkb_foreach_key(key, keymap) {
             if (key->name == XKB_ATOM_NONE)
                 continue;
diff --git a/test/common.c b/test/common.c
index a213588..c5a99fb 100644
--- a/test/common.c
+++ b/test/common.c
@@ -77,7 +77,7 @@ test_key_seq_va(struct xkb_keymap *keymap, va_list ap)
         op = va_arg(ap, int);
 
         nsyms = xkb_state_key_get_syms(state, kc, &syms);
-        fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, kc);
+        fprintf(stderr, "got %u syms for key 0x%x: [", nsyms, kc);
 
         if (op == DOWN || op == BOTH)
             xkb_state_update_key(state, kc, XKB_KEY_DOWN);
diff --git a/test/interactive.c b/test/interactive.c
index 341693f..eb3777b 100644
--- a/test/interactive.c
+++ b/test/interactive.c
@@ -216,17 +216,13 @@ free_keyboards(struct keyboard *kbds)
 static void
 print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
 {
-    unsigned int i;
     struct xkb_keymap *keymap;
     struct xkb_state *state;
 
     const xkb_keysym_t *syms;
-    unsigned int nsyms;
+    int nsyms;
     char s[16];
-    uint32_t unicode;
     xkb_layout_index_t layout;
-    xkb_mod_index_t mod;
-    xkb_led_index_t led;
 
     state = kbd->state;
     keymap = xkb_state_get_keymap(state);
@@ -243,7 +239,7 @@ print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
     }
     else {
         printf("keysyms [ ");
-        for (i = 0; i < nsyms; i++) {
+        for (int i = 0; i < nsyms; i++) {
             xkb_keysym_get_name(syms[i], s, sizeof(s));
             printf("%-*s ", (int) sizeof(s), s);
         }
@@ -256,8 +252,8 @@ print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
      */
 #ifdef __STDC_ISO_10646__
     printf("unicode [ ");
-    for (i = 0; i < nsyms; i++) {
-        unicode = xkb_keysym_to_utf32(syms[i]);
+    for (int i = 0; i < nsyms; i++) {
+        uint32_t unicode = xkb_keysym_to_utf32(syms[i]);
         printf("%lc ", (int)(unicode ? unicode : L' '));
     }
     printf("] ");
@@ -271,7 +267,7 @@ print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
            xkb_state_key_get_level(state, keycode, layout));
 
     printf("mods [ ");
-    for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
+    for (xkb_mod_index_t mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
         if (xkb_state_mod_index_is_active(state, mod,
                                           XKB_STATE_MODS_EFFECTIVE) <= 0)
             continue;
@@ -283,7 +279,7 @@ print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
     printf("] ");
 
     printf("leds [ ");
-    for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
+    for (xkb_led_index_t led = 0; led < xkb_keymap_num_leds(keymap); led++) {
         if (xkb_state_led_index_is_active(state, led) <= 0)
             continue;
         printf("%s ", xkb_keymap_led_get_name(keymap, led));
@@ -359,21 +355,18 @@ process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
 static int
 read_keyboard(struct keyboard *kbd)
 {
-    size_t i;
     ssize_t len;
     struct input_event evs[16];
-    size_t nevs;
 
     /* No fancy error checking here. */
     while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
-        nevs = len / sizeof(struct input_event);
-        for (i = 0; i < nevs; i++)
+        const size_t nevs = len / sizeof(struct input_event);
+        for (size_t i = 0; i < nevs; i++)
             process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
     }
 
     if (len < 0 && errno != EWOULDBLOCK) {
-        fprintf(stderr, "Couldn't read %s: %s\n", kbd->path,
-                strerror(errno));
+        fprintf(stderr, "Couldn't read %s: %s\n", kbd->path, strerror(errno));
         return -errno;
     }