Fix the symbols tree hierarchy when several tags have the same name
commit6522332ba9d54da7d750b1f2d719b07af6e861cf
authorColomban Wendling <ban@herbesfolles.org>
Fri, 1 Sep 2017 23:22:11 +0000 (1 16:22 -0700)
committerColomban Wendling <ban@herbesfolles.org>
Sat, 2 Sep 2017 00:31:56 +0000 (1 17:31 -0700)
tree255237fc56b2791a56b636733f8f29083c5b209c
parent0dc1e4c6d3129135b768f20b848c653e84c135ee
Fix the symbols tree hierarchy when several tags have the same name

Fix the symbols tree hierarchy by considering the whole scope when
adding a tag, avoiding choosing the wrong parent when several tags have
the same name.  Until now, to avoid such misbehavior we only used to
choose the parent candidate that appeared last (line-wise) before the
child.  It works in most typical situations as generally tag names are
fairly unique, and children appear right after their parent.

However, there are cases that are trickier and cannot be handled that
way.  In the following valid C++ snippet, it is impossible to know
whether `function` should be listed under the namespace `A` or the
class `A` without looking at its full scope:

```C++
namespace A {
    namespace B {
        class A {
            void method() {}
        };
    };
    void function() {}
};
```

And it is a real-world problem for some parsers like the JSON parser
that generates numeric indices for array elements name, often leading
to several possibly close duplicates.

Additionally, to prevent trying to set a tag as its own parent, the
code guarded against accepting a parent if the child had the same name,
lading to incorrect hierarchy for `method` in cases like this:

```C++
namespace A {
    class A {
        void method() {}
    };
};
```

So to fix this, consider the whole hierarchy of a tag for choosing its
parent, when that information is available from the parser.

Fixes #1583.
HACKING
src/symbols.c
src/tagmanager/tm_tag.c
src/tagmanager/tm_tag.h