summaryrefslogtreecommitdiff
path: root/src/qdoc/clangcodeparser.cpp
diff options
context:
space:
mode:
authorLuca Di Sera <luca.disera@qt.io>2023-02-16 13:00:15 +0100
committerLuca Di Sera <luca.disera@qt.io>2023-02-16 13:24:52 +0100
commit04ca86b6fe7c9ec6418ba5da5d61fcb3201830ab (patch)
tree80d0f48aa8d6f074c1a5ddc23a1d657a7ed23cff /src/qdoc/clangcodeparser.cpp
parentb29500cbbe3cd115009331bd412ad4d4e49b524b (diff)
downloadqttools-04ca86b6fe7c9ec6418ba5da5d61fcb3201830ab.tar.gz
QDoc: Use clang's C++ API to extract parameters' types from a function
QDoc uses clang's C API, LibClang, to parse the source code of C++ projects, so as to be able to extract the user-written documentation and provide certain guarantees about its correctness. When QDoc encounters a function, method or similar element, it extracts the types of the parameters of the element, both to show them to the user and to compare them to the one provided by the user in a certain document-block, to ensure consistency. LibClang has a good support for those kind of extraction, such that they are used to perform this effort in `ClangCodeParser::processFunction`, the method that generally execute this process to enrich a `FunctionNode`, the internal representation for documentable elements that are callable, with the relevant information. Recent changes to QDoc allows us to interleave usages of Clang's C and C++ API, something that was done for certain parts of `ClangCodeParser::processFunction` that could not be easily represented with the more limited LibClang. While there is no particular issue with the interleaving of the two APIs, the two axis on which information is retrieved tend to require certain overlapping operations, which lengthen the code and tend to not mesh well with one another. Thus, we refactor a further part of `ClangCodeParser::processFunction`, to slowly convert it completely to a Clang's C++ API based method. Hence, the code that takes care of extracting information about the type of the parameters of a callable element was rewritten using the equivalent Clang's C++ API calls. Change-Id: I365cd1257b2a6a07187dbf23a971244b4c1f2daf Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io>
Diffstat (limited to 'src/qdoc/clangcodeparser.cpp')
-rw-r--r--src/qdoc/clangcodeparser.cpp24
1 files changed, 14 insertions, 10 deletions
diff --git a/src/qdoc/clangcodeparser.cpp b/src/qdoc/clangcodeparser.cpp
index c28629941..ca8027909 100644
--- a/src/qdoc/clangcodeparser.cpp
+++ b/src/qdoc/clangcodeparser.cpp
@@ -1012,19 +1012,23 @@ void ClangVisitor::processFunction(FunctionNode *fn, CXCursor cursor)
if (!fn->isNonvirtual() && kind != CXCursor_Destructor)
setOverridesForFunction(fn, cursor);
- int numArg = clang_getNumArgTypes(funcType);
Parameters &parameters = fn->parameters();
parameters.clear();
- parameters.reserve(numArg);
-
- for (int i = 0; i < numArg; ++i) {
- CXType argType = clang_getArgType(funcType, i);
- parameters.append(adjustTypeName(fromCXString(clang_getTypeSpelling(argType))));
- if (argType.kind == CXType_Typedef || argType.kind == CXType_Elaborated) {
- parameters.last().setCanonicalType(fromCXString(
- clang_getTypeSpelling(clang_getCanonicalType(argType))));
- }
+ parameters.reserve(function_declaration->getNumParams());
+
+ const clang::LangOptions& lang_options = function_declaration->getASTContext().getLangOpts();
+ clang::PrintingPolicy p{lang_options};
+
+ for (clang::ParmVarDecl* const parameter_declaration : function_declaration->parameters()) {
+ clang::QualType parameter_type = parameter_declaration->getOriginalType();
+
+ parameters.append(adjustTypeName(QString::fromStdString(parameter_type.getAsString(p))));
+
+ if (!parameter_type.isCanonical())
+ parameters.last().setCanonicalType(QString::fromStdString(parameter_type.getCanonicalType().getAsString(p)));
}
+
+
if (parameters.count() > 0) {
if (parameters.last().type().endsWith(QLatin1String("QPrivateSignal"))) {
parameters.pop_back(); // remove the QPrivateSignal argument