Coder Social home page Coder Social logo

Comments (5)

mikehouse avatar mikehouse commented on August 16, 2024

clang cannot compile project because of this.

error: declaration of non-local variable in 'for' loop
    hashmap_foreach(key, b, &map) {
    ^
hashmap.h:324:5: note: expanded from macro 'hashmap_foreach'
    __HASHMAP_FOREACH(__HASHMAP_MAKE_UNIQUE(__map), (key), (data), (h))
    ^
hashmap.h:32:10: note: expanded from macro '__HASHMAP_FOREACH'
    for (HASHMAP_ITER(*(h)) __HASHMAP_UNIQUE(x, it) = hashmap_iter(h, &__HASHMAP_UNIQUE(x, it)); \
         ^
hashmap.h:96:5: note: expanded from macro 'HASHMAP_ITER'
    struct {                                                            \
    ^
1 error generated.

from hashmap.

DavidLeeds avatar DavidLeeds commented on August 16, 2024

Sorry about that. I used GNU toolchains when I was developing this project, and GCC gives more leeway and allows declaring an anonymous struct in a for loop. There is a heated discussion on this topic here: https://stackoverflow.com/questions/11903232/declaring-anonymous-struct-in-for-loop-clang-fails-to-compile

The naïve fix for this issue would be to require the caller to declare the iterator externally, but this would significantly reduce the usability of the hashmap_foreach() macro. You'd be looking at something like:

HASHMAP_ITER(map) it;
hashmap_foreach(it, key, val, &map) { ... }

I'll think about a solution. I may be able to define the iterator type in the hashmap structure, which will allow me to avoid declaring it in the for loop, and placate more strict compilers like Clang and Visual Studio.

from hashmap.

DavidLeeds avatar DavidLeeds commented on August 16, 2024

@NEU-Liu I just merged a fix. I added Clang to my continuous integration test build, and it's passing, so should be good to go.

from hashmap.

bogen85 avatar bogen85 commented on August 16, 2024
for (struct {int foo; int bar;} i = {0}; i.foo < 10; i.foo++);

That works on clang 12 on Linux with -std=gnu17 and -std=gnu2x, but not on FreeBSD with clang 13... Tried both -std=gnu17 and -std=gnu2x...

from hashmap.

DavidLeeds avatar DavidLeeds commented on August 16, 2024

Hi @bogen85 I will investigate differences in the C standard in the FreeBSD toolchain variants when time allows, but in the meantime, you might consider using the iterator interface instead of the foreach macros. Here is an example:

HASHMAP_ITER(my_map) it;

for (it = hashmap_iter(&my_map); hashmap_iter_valid(&it); hashmap_iter_next(&it)) {
    /* Access entry using hashmap_iter_*() functions */
}

The iterator interface is used internally by the foreach macros, but has less "edgy" code in it that picky toolchains might balk at.

from hashmap.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.