apache / celix

Apache Celix is a framework for C and C++14 to develop dynamic modular software applications using component and in-process service-oriented programming.
https://celix.apache.org/
Apache License 2.0
162 stars 86 forks source link

Nested Container Iteration #593

Closed PengZheng closed 1 year ago

PengZheng commented 1 year ago

The following code snippet triggers "declaration shadows a local variable" warning:

static void rsaShm_overlayProperties(celix_properties_t *additionalProperties, celix_properties_t *serviceProperties) {

    /*The property keys of a service are case-insensitive,while the property keys of the specified additional properties map are case sensitive.
     * A property key in the additional properties map must therefore override any case variant property key in the properties of the specified Service Reference.*/
    const char *additionalPropKey = NULL;
    const char *servicePropKey = NULL;
    PROPERTIES_FOR_EACH(additionalProperties, additionalPropKey) {
        if (strcmp(additionalPropKey,(char*) OSGI_FRAMEWORK_OBJECTCLASS) != 0
                && strcmp(additionalPropKey,(char*) OSGI_FRAMEWORK_SERVICE_ID) != 0) {
            bool propKeyCaseEqual = false;

            PROPERTIES_FOR_EACH(serviceProperties, servicePropKey) {
                if (strcasecmp(additionalPropKey,servicePropKey) == 0) {
                    const char* val = celix_properties_get(additionalProperties,additionalPropKey,NULL);
                    celix_properties_set(serviceProperties,servicePropKey,val);
                    propKeyCaseEqual = true;
                    break;
                }
            }

            if (!propKeyCaseEqual) {
                const char* val = celix_properties_get(additionalProperties,additionalPropKey,NULL);
                celix_properties_set(serviceProperties,additionalPropKey,val);
            }
        }
    }
    return;
}

A closer look reveals that the iter of the inner loop hides that of the outer loop.

#define PROPERTIES_FOR_EACH(props, key) \
    for(hash_map_iterator_t iter = hashMapIterator_construct(props); \
        hashMapIterator_hasNext(&iter), (key) = (const char*)hashMapIterator_nextKey(&iter);)