Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fnlookupresult.h
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef FNLOOKUPRESULT_H
5#define FNLOOKUPRESULT_H
6
7#include <QtCore/qset.h>
8
9#include <type_traits>
10
12
13class Node;
14
15/*
16 The outcome of resolving a function signature against the
17 documentation node database. Lookup either finds one node, finds no
18 match, finds ambiguous fallback nodes, or deliberately ignores a symbol.
19 These states are mutually exclusive. A matched result always carries a
20 non-null node; an ambiguous result carries none, because an ambiguous
21 resolution must not silently pick one of the candidates.
22 */
23template <typename NodeType>
25{
26public:
27 enum class Status
28 {
29 NoMatch, // No candidate matches the signature.
30 Matched, // Exactly one node is selected.
31 Ambiguous, // Multiple distinct fallback nodes match.
32 Ignored // The symbol is explicitly excluded from lookup.
33 };
34
35 FnLookupResultFor() = default;
36
37 Status status() const { return m_status; }
38 bool hasMatch() const { return m_status == Status::Matched; }
39 bool isAmbiguous() const { return m_status == Status::Ambiguous; }
40
41 // Only a matched outcome exposes a candidate.
42 NodeType *node() const
43 {
44 return m_status == Status::Matched ? m_node : nullptr;
45 }
46
47 void reset()
48 {
49 m_status = Status::NoMatch;
50 m_node = nullptr;
51 }
52
53 void setMatched(NodeType *node)
54 {
55 Q_ASSERT(node != nullptr);
56 m_status = Status::Matched;
57 m_node = node;
58 }
59
61 {
62 m_status = Status::Ambiguous;
63 m_node = nullptr;
64 }
65
67 {
68 m_status = Status::Ignored;
69 m_node = nullptr;
70 }
71
72private:
73 Status m_status = Status::NoMatch;
74 NodeType *m_node = nullptr; // valid only in the Matched state
75};
76
77// The outcome depends on node identity, not on the node graph implementation.
79
81{
82 Prepare, // A later generate pass owns unresolved-command diagnostics.
83 Generate // Includes single-execution mode, where there is no later pass.
84};
85
86template <typename NodeType>
88{
89 using Status = typename FnLookupResultFor<NodeType>::Status;
90 return phase == FnDiagnosticPhase::Generate
91 && (result.status() == Status::NoMatch || result.isAmbiguous());
92}
93
94/*
95 Reduces a collection of matching candidates to a lookup result:
96 identical node pointers are deduplicated, a single distinct node
97 yields a matched result, two or more distinct nodes yield an
98 ambiguous result, and an empty collection yields no match.
99 */
100template <typename Container>
101auto resolveFnLookupMatches(const Container &matches)
102{
103 QSet<typename Container::value_type> distinct(matches.cbegin(), matches.cend());
104 FnLookupResultFor<std::remove_pointer_t<typename Container::value_type>> result;
105 if (distinct.size() == 1)
106 result.setMatched(*distinct.cbegin());
107 else if (distinct.size() > 1)
108 result.setAmbiguous();
109 return result;
110}
111
112// Prefer existing ordinary matches over renamed template parameters. Both
113// passes preserve first-match behavior; only the fallback requires uniqueness.
114template <typename Container, typename OrdinaryMatcher, typename RenamedMatcher,
115 typename FallbackMatcher>
116auto resolveFnLookupCandidates(const Container &candidates, OrdinaryMatcher ordinary,
117 RenamedMatcher renamed, FallbackMatcher fallback)
118{
119 FnLookupResultFor<std::remove_pointer_t<typename Container::value_type>> result;
120 for (auto *candidate : candidates) {
121 if (ordinary(candidate)) {
122 result.setMatched(candidate);
123 return result;
124 }
125 }
126
127 for (auto *candidate : candidates) {
128 if (renamed(candidate)) {
129 result.setMatched(candidate);
130 return result;
131 }
132 }
133
134 QSet<typename Container::value_type> matches;
135 for (auto *candidate : candidates) {
136 if (fallback(candidate))
137 matches.insert(candidate);
138 }
139 return resolveFnLookupMatches(matches);
140}
141
142QT_END_NAMESPACE
143
144#endif // FNLOOKUPRESULT_H
static const clang::Decl * get_cursor_declaration(CXCursor cursor)
Returns the underlying Decl that cursor represents.
static QString reconstructQualifiedPathForCursor(CXCursor cur)
Reconstruct the qualified path name of a function that is being overridden.
static void findHiddenFriendCandidates(QDocDatabase *qdb, const QString &funcName, const clang::FunctionDecl *func_decl, NodeVector &candidates)
static std::optional< QString > classNameFromParameterType(clang::QualType param_type)
QString functionName(CXCursor cursor)
Returns the function name from a given cursor representing a function declaration.
static std::string get_default_value_initializer_as_string(const clang::TemplateTemplateParmDecl *parameter)
static QString fromCXString(CXString &&string)
convert a CXString to a QString, and dispose the CXString
static QDebug operator<<(QDebug debug, const std::vector< T > &v)
static QString getSpelling(CXSourceRange range)
static void setOverridesForFunction(FunctionNode *fn, CXCursor cursor)
static bool mayDeclareConstexpr(const clang::ASTContext &context, clang::SourceLocation location, clang::SourceLocation end, clang::SourceLocation readMacro)
static const auto kClangDontDisplayDiagnostics
static const clang::TemplateSpecializationType * find_template_specialization_through_sugar(const clang::Type *type)
static std::string get_default_value_initializer_as_string(const clang::ParmVarDecl *parameter)
static FnLookupResult findFunctionForCursor(QDocDatabase *qdb, CXCursor cur, NodeLookupMode mode)
static void collect_concept_references(const clang::Stmt *node, std::vector< std::string > &out)
static std::optional< SfinaeConstraint > detect_sfinae_constraint(const clang::NonTypeTemplateParmDecl *param)
static std::string get_expression_as_string(const clang::Expr *expression, const clang::ASTContext &declaration_context)
static clang::QualType parameterLookupType(const clang::ParmVarDecl *parameter)
bool visitChildrenLambda(CXCursor cursor, T &&lambda)
Call clang_visitChildren on the given cursor with the lambda as a callback T can be any functor that ...
static std::string get_default_value_initializer_as_string(const clang::NamedDecl *declaration)
static RelaxedTemplateDeclaration get_template_declaration(const clang::TemplateDecl *template_declaration)
static std::vector< const char * > toConstCharPointers(const std::vector< QByteArray > &args)
static std::string get_default_value_initializer_as_string(const clang::NonTypeTemplateParmDecl *parameter)
static QString fromCache(const QByteArray &cache, unsigned int offset1, unsigned int offset2)
static clang::SourceLocation macroBodyLocation(const clang::SourceManager &sourceManager, clang::SourceLocation location)
static bool is_enable_if_name(const std::string &qualified_name)
static bool isConstexprAsDeclared(const clang::FunctionDecl *declaration)
static float getUnpatchedVersion(QString t)
void getMoreArgs(const std::vector< QByteArray > &include_paths, std::vector< QByteArray > &args)
Load the include paths into args.
static Location fromCXSourceLocation(CXSourceLocation location)
convert a CXSourceLocation to a qdoc Location
static QString cleanAnonymousTypeName(const QString &typeName)
static Access fromCX_CXXAccessSpecifier(CX_CXXAccessSpecifier spec)
convert a CX_CXXAccessSpecifier to Node::Access
static std::string get_default_value_initializer_as_string(const clang::TemplateTypeParmDecl *parameter)
static bool functionSignatureMatches(FunctionNode *fn, FunctionCursorSignature &signature, FnSignatureMatchMode matchMode)
void getDefaultArgs(const QList< QByteArray > &defines, std::vector< QByteArray > &args)
Load the default arguments and the defines into args.
constexpr const char fnDummyFileName[]
static CXTranslationUnit_Flags flags_
static Node * findNodeForCursor(QDocDatabase *qdb, CXCursor cur)
static std::string get_fully_qualified_type_name(clang::QualType type, const clang::ASTContext &declaration_context)
FnSignatureMatchMode
static void printDiagnostics(const CXTranslationUnit &translationUnit)
static const char * defaultArgs_[]
std::optional< PCHFile > buildPCH(QDocDatabase *qdb, QString module_header, const std::set< Config::HeaderFilePath > &all_headers, const std::vector< QByteArray > &include_paths, const QList< QByteArray > &defines, const InclusionPolicy &policy)
Building the PCH must be possible when there are no .cpp files, so it is moved here to its own member...
static QString readFile(CXFile cxFile, unsigned int offset1, unsigned int offset2)
static std::string ensureAnonymousTagKeyword(std::string typeName, clang::QualType type)
Returns a string representing the name of type as if it was referred to at the end of the translation...
NodeLookupMode
void addChild(Node *child)
Adds the child to this node's child list and sets the child's parent pointer to this Aggregate.
ParsedCppFileIR parse_cpp_file(const QString &filePath)
Get ready to parse the C++ cpp file identified by filePath and add its parsed contents to the databas...
ClangCodeParser(QDocDatabase *qdb, Config &, const std::vector< QByteArray > &include_paths, const QList< QByteArray > &defines, std::optional< std::reference_wrapper< const PCHFile > > pch)
Node * nodeForCommentAtLocation(CXSourceLocation loc, CXSourceLocation nextCommentLoc)
Given a comment at location loc, return a Node for this comment nextCommentLoc is the location of the...
CXChildVisitResult visitChildren(CXCursor cursor)
QDocDatabase * qdocDB()
ClangVisitor(QDocDatabase *qdb, const std::set< Config::HeaderFilePath > &allHeaders, const Config::InternalFilePatterns &internalFilePatterns)
FnLookupResult visitFnArg(CXCursor cursor)
The ClassNode represents a C++ class.
Definition classnode.h:23
static bool isWorthWarningAbout(const Doc &doc)
Test for whether a doc comment warrants warnings.
The Config class contains the configuration variables for controlling how qdoc produces documentation...
Definition config.h:95
Definition doc.h:32
const Location & location() const
Returns the starting location of a qdoc comment.
Definition doc.cpp:89
TopicList topicsUsed() const
Returns a reference to the list of topic commands used in the current qdoc comment.
Definition doc.cpp:277
NodeType * node() const
void setMatched(NodeType *node)
bool hasMatch() const
bool isAmbiguous() const
FnLookupResultFor()=default
Status status() const
This node is used to represent any kind of function being documented.
void setConst(bool b)
void markDeletedAsWritten()
void setStatic(bool b)
bool isRef() const
void setVirtualness(Virtualness virtualness)
bool isNonvirtual() const
void setInvokable(bool b)
bool isHiddenFriend() const
void setRef(bool b)
void setOverride(bool b)
void setRefRef(bool b)
bool isConst() const
void markConstexpr()
bool isRefRef() const
void markExplicit()
void markExplicitlyDefaulted()
void setMetaness(Metaness metaness)
const std::optional< QString > & trailingRequiresClause() const
Parameters & parameters()
void setHiddenFriend(bool b)
The Location class provides a way to mark a location in a file.
Definition location.h:20
void setColumnNo(int no)
Definition location.h:43
void setLineNo(int no)
Definition location.h:42
This class represents a C++ namespace.
This class describes one instance of using the Q_PROPERTY macro.
\inmodule QtCore
Definition qbytearray.h:58
This class provides exclusive access to the qdoc database, which consists of a forrest of trees and a...
NamespaceNode * primaryTreeRoot()
Returns a pointer to the root node of the primary tree.
Status
Specifies the status of the QQmlIncubator.
#define COMMAND_SINCE
Definition codeparser.h:77
#define COMMAND_FN
Definition codeparser.h:27
#define COMMAND_PAGE
Definition codeparser.h:45
#define CONFIG_VERSION
Definition config.h:470
#define CONFIG_DOCUMENTATIONINHEADERS
Definition config.h:396
bool hasTooManyTopics(const Doc &doc)
Checks if there are too many topic commands in doc.
bool shouldDiagnoseFnLookup(const FnLookupResultFor< NodeType > &result, FnDiagnosticPhase phase)
auto resolveFnLookupMatches(const Container &matches)
FnDiagnosticPhase
auto resolveFnLookupCandidates(const Container &candidates, OrdinaryMatcher ordinary, RenamedMatcher renamed, FallbackMatcher fallback)
FnLookupResultFor< Node > FnLookupResult
NodeType
Definition genustypes.h:165
QmlNativeTypeAttribute
Defines QML-specific attributes affecting QmlTypeNode instances.
Definition genustypes.h:214
Metaness
Specifies the kind of function a FunctionNode represents.
Definition genustypes.h:242
Combined button and popup list for selecting options.
This namespace holds QDoc-internal utility methods.
Definition utilities.h:21
std::string getFullyQualifiedName(QualType QT, const ASTContext &Ctx, const PrintingPolicy &Policy, bool WithGlobalNsPrefix=false)
QList< Node * > NodeVector
Definition node.h:47
#define assert
@ Internal
Definition status.h:15
Returns the spelling in the file for a source range.
std::variant< Node *, FnMatchError > operator()(const Location &location, const QString &fnSignature, const QString &idTag, QStringList context)
Use clang to parse the function signature from a function command.
Encapsulates information about.
Definition parsererror.h:13
QVarLengthArray< QString, 20 > canonicalSpellings
QString typeSpelling(int index)
QVarLengthArray< QString, 20 > argumentSpellings
std::optional< RelaxedTemplateDeclaration > templateDeclaration
QString canonicalTypeSpelling(int index)
QList< QByteArray > templateTypeKeys
const clang::FunctionDecl * declaration
The Node class is the base class for all the nodes in QDoc's parse tree.
void setAccess(Access t)
Sets the node's access type to t.
Definition node.h:172
bool isNamespace() const
Returns true if the node type is Namespace.
Definition node.h:110
bool isTypedef() const
Returns true if the node type is Typedef.
Definition node.h:128
Aggregate * parent() const
Returns the node's parent pointer, that is, the aggregate under which the node is documented.
Definition node.h:211
bool isVariable() const
Returns true if the node type is Variable.
Definition node.h:133
void setLocation(const Location &t)
Sets the node's declaration location, its definition location, or both, depending on the suffix of th...
Definition node.cpp:909
virtual bool isAggregate() const
Returns true if this node is an aggregate, which means it inherits Aggregate and can therefore have c...
Definition node.h:138
virtual void setRelatedNonmember(bool b)
Sets a flag in the node indicating whether this node is a related nonmember of something.
Definition node.h:188
const std::optional< RelaxedTemplateDeclaration > & templateDecl() const
Definition node.h:247
bool isFunction(Genus g=Genus::DontCare) const
Returns true if this is a FunctionNode and its Genus is set to g.
Definition node.h:101
bool isClass() const
Returns true if the node type is Class.
Definition node.h:91
virtual bool isClassNode() const
Returns true if this is an instance of ClassNode.
Definition node.h:145
A class for parsing and managing a function parameter list.
Definition main.cpp:28
Parameter & operator[](int index)
Definition parameters.h:39
bool isEmpty() const
Definition parameters.h:32
void pop_back()
Definition parameters.h:43
void reserve(int count)
Definition parameters.h:35
void clear()
Definition parameters.h:24
Parameter & last()
Definition parameters.h:37
bool isPrivateSignal() const
Definition parameters.h:31
const Parameter & at(int i) const
Definition parameters.h:36
const Parameter & last() const
Definition parameters.h:38
int count() const
Definition parameters.h:34
void setPrivateSignal()
Definition parameters.h:44
Holds the source-level alias with its template arguments for a SFINAE constraint detected in a non-ty...
CXTranslationUnit tu