Bug 368067 - Wrong declaration is caught when class name as template parameter
Summary: Wrong declaration is caught when class name as template parameter
Status: RESOLVED FIXED
Alias: None
Product: kdevelop
Classification: Applications
Component: Language Support: CPP (old) (show other bugs)
Version: 5.0.0
Platform: Appimage Linux
: NOR normal
Target Milestone: ---
Assignee: kdevelop-bugs-null
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-31 18:28 UTC by Afief Halumi
Modified: 2016-09-07 13:01 UTC (History)
0 users

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 Afief Halumi 2016-08-31 18:28:53 UTC
In the following code highlighting Foo in `void Foo::foo() {...}` shows the declaration to be the template parameter `typename Foo` instead of either the forward declaration or the actual declaration of the class.

class Foo;
template<typename Foo> class Bar;

class Foo
{
    void foo();
};

void Foo::foo() { std::cout << "foo\n"; }



Reproducible: Always

Steps to Reproduce:
1. Paste the above code into KDevelop
2. hover over Foo
3. click on the declaration hyperlink

Actual Results:  
Jumps to the template parameter i.e. the Foo in `template<typename Foo> class Bar;`

Expected Results:  
should jump to either the forward declaration `class Foo;` or preferably the actual declaration of the class Foo.

Sorry for the terrible code, but these things do exist in old code bases and aren't removed until they make trouble. I removed it from mine so it doesn't bug me anymore, but it probably indicates something wrong with the C++ support.
Comment 1 Kevin Funk 2016-08-31 18:39:23 UTC
Confirmed.
Comment 2 Milian Wolff 2016-09-07 12:20:36 UTC
We put the declarations of the template type parameters into the parent context, which confuses our lookup. The solution would be to wrap those into their own context, and then import that one into the ClassTemplate context. Or maybe put them directly into the class template.

clang gives us:

AST tree successfully generated
ClassDecl (4) | type: "Foo" (105) | display: "Foo" | loc: tests/files/bug367893.cpp@[(1,1),(1,10)] | sp-name-range: [(1,7),(1,10)] | isDecl
ClassTemplate (31) | display: "Bar<Foo>" | loc: tests/files/bug367893.cpp@[(2,1),(2,33)] | sp-name-range: [(2,30),(2,33)] | isDecl
  TemplateTypeParameter (27) | type: "Foo" (1) | canonical type: "type-parameter-0-0" (1) | display: "Foo" | loc: tests/files/bug367893.cpp@[(2,10),(2,22)] | sp-name-range: [(2,19),(2,22)] | isDecl
ClassDecl (4) | type: "Foo" (105) | display: "Foo" | loc: tests/files/bug367893.cpp@[(4,1),(4,26)] | sp-name-range: [(4,7),(4,10)] | isDecl
  CXXMethod (21) | type: "void ()" (111) | display: "foo()" | loc: tests/files/bug367893.cpp@[(4,13),(4,23)] | sp-name-range: [(4,18),(4,21)] | isDecl
CXXMethod (21) | type: "void ()" (111) | display: "foo()" | loc: tests/files/bug367893.cpp@[(6,1),(6,21)] | sp-name-range: [(6,11),(6,14)] | isDecl
  TypeRef (43) | type: "Foo" (105) | display: "class Foo" | loc: tests/files/bug367893.cpp@[(6,6),(6,9)] | sp-name-range: [(6,6),(6,9)] | isUse
  CompoundStmt (202) | loc: tests/files/bug367893.cpp@[(6,17),(6,21)] | sp-name-range: [(0,0),(0,0)] 
no problems encountered during parsing

we build:

  Top-Context "Global" (owner:  0x0 ) 0x7f3fcc004460 "" [ "" ] [ (0, 0)  ->  (2147483647, 2147483647) ] "/tmp/testfile_Bh5300.cpp" 
     Declaration: "Foo" [ "Foo" ] 0x7f3fcc04a400 (internal ctx: 0x0 ) [ (1, 14)  ->  (1, 17) ] , no definition,  1 use(s). 
       File: "/tmp/testfile_Bh5300.cpp" 
         Use: [ (6, 13)  ->  (6, 16) ] 
     Declaration: "Foo Foo" [ "Foo" ] 0x7f3fcc086f60 (internal ctx: 0x0 ) [ (2, 26)  ->  (2, 29) ] , no definition,  1 use(s). 
       File: "/tmp/testfile_Bh5300.cpp" 
         Use: [ (6, 13)  ->  (6, 16) ] 
     Declaration: "Bar" [ "Bar" ] 0x7f3fcc086e40 (internal ctx: 0x0 ) [ (2, 37)  ->  (2, 40) ] , no definition,  0 use(s). 
     Declaration: "class Foo" [ "Foo" ] 0x7f3fcc0873a0 (internal ctx: 0x7f3fcc072cb0 ) [ (4, 14)  ->  (4, 17) ] , defined,  1 use(s). 
       File: "/tmp/testfile_Bh5300.cpp" 
         Use: [ (6, 13)  ->  (6, 16) ] 
    Context "Class" (owner:  0x7f3fcc0873a0 ) 0x7f3fcc072cb0 "Foo" [ "Foo" ] [ (4, 8)  ->  (4, 33) ] "" 
       1 Declarations, 1 child-contexts 
      Context "Function" (owner:  0x7f3fcc087e90 ) 0x7f3fcc086e10 "foo" [ "Foo::foo" ] [ (4, 20)  ->  (4, 30) ] "" 
         0 Declarations, 0 child-contexts 
    Context "Helper" (owner:  0x0 ) 0x7f3fcc087010 "Foo" [ "Foo" ] [ (6, 8)  ->  (6, 28) ] "" 
       1 Declarations, 1 child-contexts 
      Context "Function" (owner:  0x7f3fcc0879c0 ) 0x7f3fcc087a00 "foo" [ "Foo::foo" ] [ (6, 8)  ->  (6, 28) ] "" 
         0 Declarations, 1 child-contexts 
        Context "Other" (owner:  0x0 ) 0x7f3fcc0a2170 "" [ "Foo::foo" ] [ (6, 24)  ->  (6, 28) ] "" 
           0 Declarations, 0 child-contexts
Comment 3 Milian Wolff 2016-09-07 13:01:18 UTC
Git commit a453eec244cd056aa9dc55162eb0b5c8a69d0d12 by Milian Wolff.
Committed on 07/09/2016 at 12:59.
Pushed by mwolff into branch '5.0'.

Always open a context for class templates, i.e. also for declarations.

Forward declarations of class templates leaked their template paramter
declarations into the surrounding context, leading to potentially
wrong associations when looking for declarations based on their
identifier.

M  +5    -1    languages/clang/duchain/builder.cpp
M  +23   -0    languages/clang/tests/test_duchain.cpp
M  +1    -0    languages/clang/tests/test_duchain.h

http://commits.kde.org/kdevelop/a453eec244cd056aa9dc55162eb0b5c8a69d0d12