Bug 267877 - cpp code completion: non-const methods hidden even when overload-resolution could find non-const method
Summary: cpp code completion: non-const methods hidden even when overload-resolution c...
Status: RESOLVED FIXED
Alias: None
Product: kdevelop
Classification: Applications
Component: Language Support: CPP (old) (show other bugs)
Version: git master
Platform: Gentoo Packages Linux
: VHI major
Target Milestone: 4.2.0
Assignee: kdevelop-bugs-null
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-07 15:01 UTC by Eugene Shalygin
Modified: 2013-03-31 00:50 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eugene Shalygin 2011-03-07 15:01:25 UTC
Version:           git master (using KDE 4.6.1) 
OS:                Linux

Because KDevelop C++ parser lacks sunderstanding of size_t type which is used in containers declarations, it fails to parse usual operator[] declarations and thus does not show completion in code like this: "vec[i]." 

Reproducible: Didn't try

Steps to Reproduce:
example:

class A{
public:
	void f();
};

//typedef int size_t; // if uncomment this, parsing works

class Container{
public:
	A& operator[](size_t i);
	A at(size_t i);
};


void main(){
	Container c;
	c[0].f();
//          ^ there is no completion support here
	c.at(i).f();
//             ^ but here it works
}



There are the same problems with ptrdiff_t type
Comment 1 Eugene Shalygin 2011-03-07 15:04:19 UTC
Hm, formatting in bug input form differ from formatting in final bug window
correct locations of markers, of course, is following:
void main(){
    Container c;
    c[0].f();
//      ^ there is no completion support here
    c.at(i).f();
//         ^ but here it works
}
Comment 2 Milian Wolff 2011-03-07 22:10:33 UTC
size_t is not a C++ default type, see also: http://www.cplusplus.com/reference/clibrary/cstring/size_t/

for ptrdiff_t see http://www.cplusplus.com/reference/clibrary/cstddef/ptrdiff_t/

so supposedly including that header should make kdevelop work properly.

but it doesn#t for me, lets investigate...
Comment 3 Milian Wolff 2011-03-07 22:22:49 UTC
reason is the extension cache in the language controller, since cstddef has no extension the file contents won't be taken into account when looking at the mime type, I'll start discussion on this and improve the situation.

thanks for the report, I wonder why we have not spotted this sooner.
Comment 4 Eugene Shalygin 2011-03-07 23:20:32 UTC
For me including of ant of headers that define size_t also does not help. 

I forgot to add it to the example because in real world it obviously included some how by container's include file and KDevelop doesn't see size_t definition neverheless
Comment 5 David Nolden 2011-03-21 18:49:41 UTC
The reason are (probably) problems in the preprocessor. Should be workaroundable by pushing the "Reparse ..." button in the "Problems" toolview.
Comment 6 David Nolden 2011-03-21 18:51:52 UTC
The source of the problem is probably such an ugly construct:

#ifndef HAVE_SIZE_T
typedef unsigned int size_t;
#endif

And at some point, the file will be updated with "HAVE_SIZE_T" set, because it was included multiple times, or something like that. The exact issue needs some more research, and as first, a way to reliable reproduce the issue.
Comment 7 David Nolden 2011-03-21 18:52:30 UTC
I mean this:

#ifndef HAVE_SIZE_T
typedef unsigned int size_t;
#define HAVE_SIZE_T
#endif
Comment 8 Milian Wolff 2012-02-28 15:54:13 UTC
this example works for me, note the "using namespace std;" and the include of "cstddef".

~~~~~~

#include <cstddef>

using namespace std;

class A{
public:
    void f();
};

//typedef int size_t; // if uncomment this, parsing works

class Container{
public:
    A& operator[](size_t i);
    A at(size_t i);
};


void main(){
    Container c;
    c[0].f();
//          ^ there is no completion support here
    c.at(1).f();
//             ^ but here it works
}

~~~~~~

if it's still not working for anyone, please give us a way to reproduce it.
Comment 9 Milian Wolff 2012-02-28 15:59:58 UTC
ah but if you use std::vector [0].f() will fail because kdev only "sees" the const& version of operator[] and hides the non-const method foo() then...
Comment 10 Milian Wolff 2012-02-28 19:26:58 UTC
Git commit 50b8d362e01e0ed3fed8e875902e1595be31bb41 by Milian Wolff.
Committed on 28/02/2012 at 20:23.
Pushed by mwolff into branch '4.3'.

properly take constness during overload resolution into account

we prefer non-const overloads to const ones if we are accessing
a non-const object now, this is also what g++ does by default
as far as I can see

M  +13   -0    languages/cpp/cppduchain/expressionvisitor.cpp
M  +9    -4    languages/cpp/cppduchain/overloadresolution.cpp
M  +14   -2    languages/cpp/cppduchain/overloadresolution.h
M  +12   -3    languages/cpp/cppduchain/overloadresolutionhelper.cpp
M  +8    -1    languages/cpp/cppduchain/overloadresolutionhelper.h
M  +78   -0    languages/cpp/cppduchain/tests/test_expressionparser.cpp
M  +2    -0    languages/cpp/cppduchain/tests/test_expressionparser.h
M  +15   -1    languages/cpp/cppduchain/viablefunctions.cpp
M  +4    -1    languages/cpp/cppduchain/viablefunctions.h

http://commits.kde.org/kdevelop/50b8d362e01e0ed3fed8e875902e1595be31bb41
Comment 11 Milian Wolff 2012-02-28 19:26:58 UTC
Git commit 3a546dd106794dc12ce0cf6893b02142f566d2b3 by Milian Wolff.
Committed on 28/02/2012 at 20:21.
Pushed by mwolff into branch '4.3'.

properly take constness during code completion into account

- when accessing a const object, only show const methods
- when accessign a non-const object, prefer non-const overloads
to the const ones, but still show const methods if they are not overloaded

M  +11   -9    languages/cpp/codecompletion/context.cpp
M  +2    -2    languages/cpp/codecompletion/context.h
M  +40   -7    languages/cpp/cppduchain/cppduchain.cpp
M  +13   -5    languages/cpp/cppduchain/cppduchain.h
M  +52   -0    languages/cpp/tests/test_cppcodecompletion.cpp
M  +1    -0    languages/cpp/tests/test_cppcodecompletion.h

http://commits.kde.org/kdevelop/3a546dd106794dc12ce0cf6893b02142f566d2b3
Comment 12 Aleix Pol 2013-03-31 00:50:37 UTC
Moving all the bugs from the CPP Parser. It was not well defined the difference between it and C++ Language Support and people kept reporting in both places indistinctively