HighCommander4 wrote:

Apologies for the long delay in getting back to this.

I've taken a fresh look at this, and realized there is a problem with the 
approach taken in the patch that I had previously overlooked.

The problem stems from the way clangd's background indexer works: every file, 
including headers, gets its own shard, and a header included by several source 
files will only get its shard written the **first** time it's processed as part 
of a source file.

Since symbols representing instantiations of class templates are stored in the 
shard of the file containing the template definition, this means that for a 
class template defined in the header, the background index will only store 
instantiations that exist in the first TU that included the header.

This can be illustrated with a small sample project containing the following 
files:

header.hpp:
```c++
class Concrete {};
template <typename T> struct Inherit : T {};
```

first.cpp:
```c++
// A TU that includes header.hpp but does not instantiate Inherit
#include "header.hpp"
```

second.cpp:
```c++
// A TU that includes header.hpp and instantiates Inherit
#include "header.hpp"
class Battler : Inherit<Concrete> {};
```

<details>
<summary>compile_commands.json, listing first.cpp before second.cpp:</summary>

```json
[
  {
    "file": "first.cpp",
    "arguments": [
      "/usr/bin/g++",
      "-c",
      "-o",
      "first.o",
      "first.cpp"
    ],
    "directory": "/tmp/pr-177273",
    "output": "first.o"
  },
  {
    "file": "second.cpp",
    "arguments": [
      "/usr/bin/g++",
      "-c",
      "-o",
      "second.o",
      "second.cpp"
    ],
    "directory": "/tmp/pr-177273",
    "output": "second.o"
  }
]
```

</details>

Steps to reproduce:

 * First, the control: comment out the include in first.cpp
    * Ensure `.cache/clangd/index` is empty, so indexing is starting from 
scratch
    * Open the project and wait for background indexing to complete
    * Invoke "Find implementations" on `Concrete`, and observe that 
`Inherit<Concrete>` correctly appears as a result
 * Now, the bug: uncomment the include in first.cpp
    * Ensure `.cache/clangd/index` is empty, so indexing is starting from 
scratch
    * Open the project and wait for background indexing to complete
    * Invoke "Find implementations" on `Concrete`, and observe that there are 
no results

This is illustrating that if first.cpp includes header.hpp, it gets to be the 
one to write the header.hpp shard, and that shard will contain no 
instantiations.

I'll follow up with some ideas about how this could be solved, but for now I 
wanted to share the finding.

https://github.com/llvm/llvm-project/pull/177273
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to