Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in _add_use #80

Closed
amai2012 opened this issue Nov 23, 2015 · 12 comments
Closed

Error in _add_use #80

amai2012 opened this issue Nov 23, 2015 · 12 comments

Comments

@amai2012
Copy link

Running 793bd44 on my 64bit cygwin I get an error somewhere in the middle of a run (--verbose is active):

Processing ./PlatformSupport/numerictypeinfo.h
Traceback (most recent call last):
  File "/usr/bin/cppclean", line 145, in <module>
    sys.exit(main())
  File "/usr/bin/cppclean", line 138, in main
    quiet=args.quiet):
  File "/usr/lib/python2.7/site-packages/cpp/find_warnings.py", line 524, in run
    hunter.find_warnings()
  File "/usr/lib/python2.7/site-packages/cpp/find_warnings.py", line 119, in find_warnings
    self._find_header_warnings()
  File "/usr/lib/python2.7/site-packages/cpp/find_warnings.py", line 396, in _find_header_warnings
    self._find_unused_warnings(included_files, forward_declarations)
  File "/usr/lib/python2.7/site-packages/cpp/find_warnings.py", line 387, in _find_unused_warnings
    forward_declarations)
  File "/usr/lib/python2.7/site-packages/cpp/find_warnings.py", line 354, in _determine_uses
    _process_function_body(node, node.namespace)
  File "/usr/lib/python2.7/site-packages/cpp/find_warnings.py", line 323, in _process_function_body
    _add_use(t.name, namespace)
  File "/usr/lib/python2.7/site-packages/cpp/find_warnings.py", line 276, in _add_use
    name = file_use_node[1].filename
KeyError: 1
@amai2012
Copy link
Author

That seems to be the related code:

#ifndef NUMERICTYPEINFO_H
#define NUMERICTYPEINFO_H

#include <string>
#include <limits>

    namespace PlatformSupport {

        template <typename T>
        std::string PrintNumericTypeInfo(const char *pTypename)
        {
            if (std::numeric_limits<T>::is_integer)
            {
                if (std::numeric_limits<T>::is_signed)
                {
                    return ::extstl::toString("%s: MIN=%lld,MAX=%lld",
                        pTypename, (long long)std::numeric_limits<T>::min(), (long long)std::numeric_limits<T>::max());
                }
                else
                {
                    return ::extstl::toString("%s: MAX=%llu",
                        pTypename, (unsigned long long)std::numeric_limits<T>::max());
                }
            }
            else
            {
                return ::extstl::toString("%s: MIN=%Le,MAX=%Le,EPS=%Le",
                    pTypename, (long double)std::numeric_limits<T>::min(), (long double)std::numeric_limits<T>::max(), (long double)std::numeric_limits<T>::epsilon());
            }
        }

    }

#endif //NUMERICTYPEINFO_H

@myint
Copy link
Owner

myint commented Nov 24, 2015

I can't seem to reproduce the stack trace.

$ cppclean --verbose numerictypeinfo.h
Processing numerictypeinfo.h
$ echo $?
0

@amai2012
Copy link
Author

I confirm... I was reducing the example too much. I get many failures on my code base :-(
I'll postpone that one and try to report more simple ones first.

@TManhente
Copy link

I also got that same error. I tried to debug it and it seems that this error happens when CppClean the first line of a function template defined inline which contains a using declaration.

Unfortunately I can't share the offending code. I'll try to reproduce bellow the structure of the code (with fake names) where the KeyError is raised:

// First.h file:
#include "Second.h"  // The header which defines a class names 'Second' which will be used bellow

namespace a {
namespace b {

class First
{
    ...   // Other stuff

    template<class A, class B>
    static First findOrCreate( const char * name );
};

template<class A, class B>
First First::findOrCreate( const char * name )
{
    using second::Second;   // KeyError is raised when processing this line
    ...  // Other function body steps
}

}  // namespace b
}  // namespace a

The namespace second is bellow a::b (i.e. it's fully qualified name is ::a::b::second). Checking the stack trace shows _process_function_body() calling _add_use() with name='second' and namespace=['a', 'b'].

I'm running CppClean with Python 2.7.10.

I noticed the following comments on _process_function_body():

  • # TODO(nnorwitz): handle :: names.
  • # TODO(nnorwitz): handle using statements in function.

Could those be the cause for this error?

I'll try to see if I can build a smaller and complete code example which would trigger that KeyError. If I manage to do so, I'll post it here.

@amai2012
Copy link
Author

Now I also managed to extract a self-contained example which consists out of two headers, running cppclean CW.h

CW.h

#ifndef HEADER2
#define HEADER2

#include "C.h"

namespace MyNamespace                        
{
    namespace BRK
    {
        class CW : public CI
        {
            int cf()
            {
                return MyNamespace::BRK::CI::cf();
            }
        };
    }
}
#endif

C.h

#ifndef HEADER1
#define HEADER1
namespace MyNamespace                        
{
    namespace BRK                        
    {
        class CI
        {
        public:
            int cf();
        };
    }
}
#endif

@r-e-d
Copy link
Collaborator

r-e-d commented Nov 26, 2015

Hi,

I made a fix for this problem. It's not perfect but it should at least fix the crash.

Can you try on your code to see if it's ok ?

@TManhente
Copy link

I've tried this fix on my code and the KeyError persists.

The not t.name in namespace check had no effect as t.name='second' but namespace=['a', 'b']

Recall that the namespace second in my example is bellow a::b (i.e. it's fully qualified name is ::a::b::second). The function with the offending line using second::Second, however, is defined bellow a::b only (that's why the using declaration is needed).

@r-e-d
Copy link
Collaborator

r-e-d commented Nov 26, 2015

My bad, I only look at the sample from amai2012, I didn't check the other samples. My fix only works for some simple and specific patterns. We will need something more complex to handle the other cases.

@TManhente
Copy link

@r-e-d: No problem, I've commented on this issue just because I thought it could be the same problem. If needed, I can open a new issue for the example code I've posted. Just let me know if that would be better.

@r-e-d
Copy link
Collaborator

r-e-d commented Nov 26, 2015

This time, my fix should handle the more general cases :)

@TManhente
Copy link

This new fix solved the problem for me. Thanks! 👍

@amai2012
Copy link
Author

I'll report further issues in another ticket, so close that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants