ccls
ccls copied to clipboard
Force indexing of header files
Observed behavior
I provide a trivial illustrating example. Assume two header files
header0.hpp
class base {}
header1.hpp
#include "header0.hpp"
class derived : base {}
and a .ccls file pointing to their directory.
You open both files in vim, using coc, but the first active buffer is header0.hpp containing the base class. At this point you try to list references to the class base, which returns an empty list. You open the buffer with header1.hpp containing the derived class. Immediately you switch back to buffer with heade0.hpp, trying to get references for base. This time, they show up, because you already opened the header1.hpp.
Desired behavior
If one could add a flag to .ccls, to index a certain directory, with headers included, we would get the desired behavior, where information would be readily available. This is pertaining to the following quote from the project setup.
Recursively listed source files (headers excluded) will be indexed.
Current hack to get the desired behavior
In the root of the project, I add a file
dummy.cpp
#include "header0.hpp"
#include "header1.hpp"
which contains nothing but includes of all the headers in the header only library I am developing. With this, I can open only header0.hpp and all the information is readily available, i.e. references, calls, inheritance hierarchy, etc.
I find great use in ccls, I use it daily and it has freed me from ever having to program outside of my comfy vim setup. Seeing how the silly hack can solve the issue, perhaps an option could be added to automate this.
As a designer of mostly header-only libraries, I would be most pleased to see this. Perhaps something akin to
.ccls
clang++
-I/path/to/include
-S/path/to/directory/to/be/always/indexed/as/source
where -Sonly has an additive effect, in the sense, ccls operates as always, except that it also indexes files found in the specified location, if it would not have done so otherwise.
If I misunderstood something, please forgive, and thank you for ccls.
It seems about index.initialNoLinkage or you can add those in index.initialWhitelist
@rhcher I tried setting
"index": {
"initialWhitelist": [".*hpp$"]
}
and numerous other variations, even [".*"], but each time the behavior was as described above (and not the desired behavior, like the one the dummy.cpp provides).
In the root of the project, I add a file
dummy.cpp
#include "header0.hpp"
#include "header1.hpp"
Yes, a non-header source file is required. The rationale is that compiler options for headers are usually unknown. They are inferred from the containing source files. If you have tests, likely that the tests will include the header files.
@MaskRay Thank you for the response. Would it be much work to add a flag in the .ccls file, for the case when all the files have the same flags. This matches current the behavior when one only has a .ccls file and no compile_commands.json, correct? Or perhaps a more flexible option, where one may specify
-S /path/to/directory/
where all the files (recursively) found in the specified directory are treated as sources, i.e. get their index built, using the flags from the .cclsfile.
I would not mind implementing this myself if you point me in the right direction - either for personal use if this is not something you would want, or as a contribution. ccls is my daily driver :)
Any solution for it?