Re: [cdt-dev] Identifying library usages
> My question is how CDT achieves binding resolution while
> writing code (and auto completion).
CDT has a parser which parses C++ code into an AST, and
semantic analysis code which runs on that AST to do things
like connect names to the entities ("bindings") they name,
much like a compiler's front-end would.
CDT also maintains a project-wide (in fact, workspace-wide
in the case of dependent projects) queryable database of
semantic information about the code, called the "index". The
index is built by constructing an AST and performing
semantic analysis for each file in the project, and traversing
the analyzed AST to populate the index with information.
The semantic analysis is designed in such a way that it can
consume information from the index, and this is leveraged to
avoid parsing header files once for every file they are
#included in. So, when parsing a file, only code located in the
file directly (as opposed to code included via #includes) is
parsed, and semantic analysis for the file gets information
about the included code from the index.
Let me know if that answers your question; if you have more
specific questions, I'm happy to try to answer them.
> I am asking this because I am doing something similar for
> a research project, i.e., I want to identify library usages
> (classes, functions, variables) in a codebase. I solve this
> by generating the ASTs and use the class/function/variable
> bindings to check if any binding (definition/declaration)
> comes from the library headers.
> I want to find out if there is a better way to do it.
Depending on your needs, you may be able to obtain the
information you need from the index, and thereby avoid
parsing the files in the project again (they are parsed once
while building the index).
Here's an outline of how that might be done.
- Use IIndex.getAllFiles() to get a list of all files about
which information is stored in the index.
- Identify which files belong to the library (for example,
by examining their filenames/paths).
- For each file in the library, use IIndexFile.findNames()
to get a list of all names in the file.
- Filter the list of names to those for which
IName.isDefinition() is true (i.e. those which are
- Use IIndex.findBinding(IName) to get the binding
being defined by the name.
- Use IIndex.findReferences(IBinding) to get a list of
all references to that binding in the project.
I'm not sure if these steps are the most efficient way to
obtain this information from the index. It seems like using
IPDOMVisitor might be more efficient, although I don't see
us exposing a way to use that via public API (though
maybe I'm overlooking something).
Hope that helps,