clangd icon indicating copy to clipboard operation
clangd copied to clipboard

clangd vscode can't find corresponding header file from template implementation file

Open MartyMcFlyInTheSky opened this issue 2 years ago • 2 comments

When I press Alt + O on my template implementation file, it can't find the corresponding header file. The error message is:

"Didn't find a corresponding file"

The implementation file extension is maybe a little bit unconventional in our codebase, we use name.i.h. But as I see it, you basically have two shots:

  1. Template implementation file extension follows a convention (e.g. *.ipp, *.tcc, *.inl etc...) (on that note: what are the supported extensions?)
  2. The template implementation file includes the header file (even though not needed but for context and consistency's sake)

We do number 2) in our codebase (not my decision) but it seems that clangd doesn't follow this hint in any case. So this could be added.

Operating system: Ubuntu 20.04

MartyMcFlyInTheSky avatar Oct 19 '23 08:10 MartyMcFlyInTheSky

what are the supported extensions?

The extensions currently supported by clangd's "switch source/header" command are these ones:

  llvm::StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
                                        ".c++", ".m", ".mm"};
  llvm::StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};

We could add additional extensions here (though in the case of .i.h we'd also have to modify the logic of how to compute the extension, since currently it only looks at what's after the last dot.)

One thing I wonder about template implementation files, though: might there be cases where a header defines a mixture of templates and non-templates (for example, a non-template class with some templated methods and some non-templated methods), and has both a template implementation file (for the implementations of the templates) and a regular implementation file (for the implementations of the non-templates), so that e.g. foo.h, foo.ipp, and foo.cpp all exist? I wonder what the behaviour should be in such cases.

HighCommander4 avatar Oct 20 '23 08:10 HighCommander4

Well there might be such cases, I can remember that I probably did that sometimes. I mean in theory you could also have multiple *.cpp implementation files for one header file. But I think we should opt for the most idiomatic case, which I suppose is to have either one *.cpp or one *.ipp file for non-templated / templated implementations respectively. In most cases I saw the implementation is actually for a class which is templated by itself, so certainly all methods must be templated on the class template parameters as well.

In other cases: What speaks about a three-way or four way toggle? Basically just "showing" all the "connected" files to the user in a row when pressing Alt + O instead of just switching between two files.

MartyMcFlyInTheSky avatar Jan 07 '24 09:01 MartyMcFlyInTheSky