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
clangcodeparser.cpp File Reference

(741ef948179d18205403d50c2517352d2e71af09)

#include "clangcodeparser.h"
#include "cppcodeparser.h"
#include "access.h"
#include "classnode.h"
#include "config.h"
#include "doc.h"
#include "enumnode.h"
#include "fnlookupresult.h"
#include "functionnode.h"
#include "genustypes.h"
#include "hiddenfriendtypekey.h"
#include "inclusionpolicy.h"
#include "namespacenode.h"
#include "propertynode.h"
#include "qdocdatabase.h"
#include "typedefnode.h"
#include "variablenode.h"
#include "sourcefileparser.h"
#include "utilities.h"
#include <QtCore/qdebug.h>
#include <QtCore/qdir.h>
#include <QtCore/qelapsedtimer.h>
#include <QtCore/qfile.h>
#include <QtCore/qregularexpression.h>
#include <QtCore/qscopedvaluerollback.h>
#include <QtCore/qtemporarydir.h>
#include <QtCore/qtextstream.h>
#include <QtCore/qvarlengtharray.h>
#include <clang-c/Index.h>
#include <clang/AST/ASTConcept.h>
#include <clang/AST/Decl.h>
#include <clang/AST/DeclFriend.h>
#include <clang/AST/DeclTemplate.h>
#include <clang/AST/Expr.h>
#include <clang/AST/ExprConcepts.h>
#include <clang/AST/Type.h>
#include <clang/AST/TypeLoc.h>
#include <clang/Basic/SourceLocation.h>
#include <clang/Frontend/ASTUnit.h>
#include <clang/Lex/Lexer.h>
#include <llvm/Support/Casting.h>
#include "clang/AST/QualTypeNames.h"
#include "template_declaration.h"
#include <algorithm>
#include <cstdio>
#include <optional>
#include <string_view>
Include dependency graph for clangcodeparser.cpp:

Go to the source code of this file.

Classes

struct  CompilationIndex
struct  TranslationUnit
struct  FileCacheEntry
 Returns the spelling in the file for a source range. More...
struct  FunctionCursorSignature
class  ClangVisitor

Enumerations

enum class  NodeLookupMode { Declaration , FnCommand , Declaration , FnCommand }
enum class  FnSignatureMatchMode {
  Ordinary , FnCommand , HiddenFriendInClassTemplate , Ordinary ,
  FnCommand , HiddenFriendInClassTemplate
}
enum class  NodeLookupMode { Declaration , FnCommand , Declaration , FnCommand }
enum class  FnSignatureMatchMode {
  Ordinary , FnCommand , HiddenFriendInClassTemplate , Ordinary ,
  FnCommand , HiddenFriendInClassTemplate
}

Functions

template<class T>
static QDebug operator<< (QDebug debug, const std::vector< T > &vv)
static void printDiagnostics (const CXTranslationUnit &translationUnit)
static const clang::Decl * get_cursor_declaration (CXCursor cursor)
 Returns the underlying Decl that cursor represents.
static std::string ensureAnonymousTagKeyword (std::string typeName, clang::QualType typetype)
 Returns a string representing the name of type as if it was referred to at the end of the translation unit that it was parsed from.
static std::string get_fully_qualified_type_name (clang::QualType typetype, const clang::ASTContext &declaration_context)
static QString cleanAnonymousTypeName (const QString &typeName)
static std::string get_expression_as_string (const clang::Expr *expression, const clang::ASTContext &declaration_context)
static void collect_concept_references (const clang::Stmt *node, std::vector< std::string > &out)
static std::string get_default_value_initializer_as_string (const clang::TemplateTypeParmDecl *parameter)
static std::string get_default_value_initializer_as_string (const clang::NonTypeTemplateParmDecl *parameter)
static std::string get_default_value_initializer_as_string (const clang::TemplateTemplateParmDecl *parameter)
static std::string get_default_value_initializer_as_string (const clang::ParmVarDecl *parameter)
static std::string get_default_value_initializer_as_string (const clang::NamedDecl *declaration)
template<typename T>
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 is callable with a CXCursor parameter and returns a CXChildVisitResult (in other word compatible with function<CXChildVisitResult(CXCursor)>.
static QString fromCXString (CXString &&stringstring)
 convert a CXString to a QString, and dispose the CXString
static const clang::TemplateSpecializationType * find_template_specialization_through_sugar (const clang::Type *typetype)
static bool is_enable_if_name (const std::string &qualified_name)
static std::optional< SfinaeConstraint > detect_sfinae_constraint (const clang::NonTypeTemplateParmDecl *paramparam)
static RelaxedTemplateDeclaration get_template_declaration (const clang::TemplateDecl *template_declaration)
static Location fromCXSourceLocation (CXSourceLocation locationlocation)
 convert a CXSourceLocation to a qdoc Location
static Access fromCX_CXXAccessSpecifier (CX_CXXAccessSpecifier spec)
 convert a CX_CXXAccessSpecifier to Node::Access
static QString fromCache (const QByteArray &cache, unsigned int offset1, unsigned int offset2)
static QString readFile (CXFile cxFile, unsigned int offset1, unsigned int offset2)
static QString getSpelling (CXSourceRange rangerange)
QString functionName (CXCursor cursor)
 Returns the function name from a given cursor representing a function declaration.
static QString reconstructQualifiedPathForCursor (CXCursor cur)
 Reconstruct the qualified path name of a function that is being overridden.
static std::optional< QString > classNameFromParameterType (clang::QualType param_type)
static void findHiddenFriendCandidates (QDocDatabase *qdb, const QString &funcName, const clang::FunctionDecl *func_decl, NodeVector &candidates)
static clang::QualType parameterLookupType (const clang::ParmVarDecl *parameter)
static bool functionSignatureMatches (FunctionNode *fn, FunctionCursorSignature &signature, FnSignatureMatchMode matchMode)
static Node * findNodeForCursor (QDocDatabase *qdb, CXCursor cur)
static FnLookupResult findFunctionForCursor (QDocDatabase *qdb, CXCursor cur, NodeLookupMode modemode)
static void setOverridesForFunction (FunctionNode *fn, CXCursor cursor)
static clang::SourceLocation macroBodyLocation (const clang::SourceManager &sourceManager, clang::SourceLocation locationlocation)
static bool mayDeclareConstexpr (const clang::ASTContext &context, clang::SourceLocation locationlocation, clang::SourceLocation endend, clang::SourceLocation readMacro)
static bool isConstexprAsDeclared (const clang::FunctionDecl *declaration)
static std::vector< const char * > toConstCharPointers (const std::vector< QByteArray > &args)
void getDefaultArgs (const QList< QByteArray > &defines, std::vector< QByteArray > &args)
 Load the default arguments and the defines into args.
void getMoreArgs (const std::vector< QByteArray > &include_paths, std::vector< QByteArray > &args)
 Load the include paths into args.
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 function, and it is called after the list of header files is complete.
static float getUnpatchedVersion (QString tt)

Variables

static const auto kClangDontDisplayDiagnostics = 0
static CXTranslationUnit_Flags flags_ = static_cast<CXTranslationUnit_Flags>(0)
constexpr const char fnDummyFileName [] = "/fn_dummyfile.cpp"
static const char * defaultArgs_ []

Enumeration Type Documentation

◆ FnSignatureMatchMode [1/2]

enum class FnSignatureMatchMode
strong
Enumerator
Ordinary 
FnCommand 
HiddenFriendInClassTemplate 
Ordinary 
FnCommand 
HiddenFriendInClassTemplate 

Definition at line 998 of file clangcodeparser.cpp.

◆ FnSignatureMatchMode [2/2]

enum class FnSignatureMatchMode
strong
Enumerator
Ordinary 
FnCommand 
HiddenFriendInClassTemplate 
Ordinary 
FnCommand 
HiddenFriendInClassTemplate 

Definition at line 998 of file clangcodeparser.cpp.

◆ NodeLookupMode [1/2]

enum class NodeLookupMode
strong
Enumerator
Declaration 
FnCommand 
Declaration 
FnCommand 

Definition at line 986 of file clangcodeparser.cpp.

◆ NodeLookupMode [2/2]

enum class NodeLookupMode
strong
Enumerator
Declaration 
FnCommand 
Declaration 
FnCommand 

Definition at line 986 of file clangcodeparser.cpp.

Function Documentation

◆ buildPCH()

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 function, and it is called after the list of header files is complete.

Definition at line 2461 of file clangcodeparser.cpp.

References flags_, kClangDontDisplayDiagnostics, printDiagnostics(), TranslationUnit::tu, and ClangVisitor::visitChildren().

Here is the call graph for this function:

◆ classNameFromParameterType()

std::optional< QString > classNameFromParameterType ( clang::QualType param_type)
static

Extract a class name from a Clang parameter type, stripping references, pointers, and qualifiers. Returns {std::nullopt} if the type doesn't represent a class.

Definition at line 912 of file clangcodeparser.cpp.

◆ cleanAnonymousTypeName()

QString cleanAnonymousTypeName ( const QString & typeName)
static

Definition at line 278 of file clangcodeparser.cpp.

◆ collect_concept_references()

void collect_concept_references ( const clang::Stmt * node,
std::vector< std::string > & out )
static

Definition at line 324 of file clangcodeparser.cpp.

◆ detect_sfinae_constraint()

std::optional< SfinaeConstraint > detect_sfinae_constraint ( const clang::NonTypeTemplateParmDecl * param)
static

Definition at line 533 of file clangcodeparser.cpp.

References find_template_specialization_through_sugar().

Here is the call graph for this function:

◆ ensureAnonymousTagKeyword()

std::string ensureAnonymousTagKeyword ( std::string typeName,
clang::QualType type )
static

Returns a string representing the name of type as if it was referred to at the end of the translation unit that it was parsed from.

For example, given the following code:

namespace foo {
template<typename T>
struct Bar {
using Baz = const T&;
void bam(Baz);
};
}
QString foo
[14]

Given a parsed translation unit and an AST node, say {decl}, representing the parameter declaration of the first argument of {bam}, calling {get_fully_qualified_name(decl->getType(), * decl->getASTContext())} would result in the string {foo::Bar<T>::Baz}.

This should generally be used every time the stringified representation of a type is acquired as part of parsing with Clang, so as to ensure a consistent behavior and output.

Definition at line 203 of file clangcodeparser.cpp.

◆ find_template_specialization_through_sugar()

const clang::TemplateSpecializationType * find_template_specialization_through_sugar ( const clang::Type * type)
static

Definition at line 470 of file clangcodeparser.cpp.

Referenced by detect_sfinae_constraint().

Here is the caller graph for this function:

◆ findFunctionForCursor()

FnLookupResult findFunctionForCursor ( QDocDatabase * qdb,
CXCursor cur,
NodeLookupMode mode )
static

Definition at line 1205 of file clangcodeparser.cpp.

References findNodeForCursor(), FnCommand, get_cursor_declaration(), and Node< _Tp >::isAggregate().

Here is the call graph for this function:

◆ findHiddenFriendCandidates()

void findHiddenFriendCandidates ( QDocDatabase * qdb,
const QString & funcName,
const clang::FunctionDecl * func_decl,
NodeVector & candidates )
static

Definition at line 957 of file clangcodeparser.cpp.

◆ findNodeForCursor()

Node * findNodeForCursor ( QDocDatabase * qdb,
CXCursor cur )
static

Definition at line 1265 of file clangcodeparser.cpp.

References findNodeForCursor(), fromCXSourceLocation(), Node< _Tp >::isAggregate(), Node< _Tp >::isClassNode(), Node< _Tp >::isFunction(), Node< _Tp >::isNamespace(), Node< _Tp >::isTypedef(), Node< _Tp >::isVariable(), and QDocDatabase::primaryTreeRoot().

Referenced by findFunctionForCursor(), and findNodeForCursor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromCache()

QString fromCache ( const QByteArray & cache,
unsigned int offset1,
unsigned int offset2 )
inlinestatic

Definition at line 795 of file clangcodeparser.cpp.

◆ fromCX_CXXAccessSpecifier()

Access fromCX_CXXAccessSpecifier ( CX_CXXAccessSpecifier spec)
static

convert a CX_CXXAccessSpecifier to Node::Access

Definition at line 771 of file clangcodeparser.cpp.

◆ fromCXSourceLocation()

Location fromCXSourceLocation ( CXSourceLocation location)
static

convert a CXSourceLocation to a qdoc Location

Definition at line 757 of file clangcodeparser.cpp.

References Location::setColumnNo(), and Location::setLineNo().

Referenced by findNodeForCursor(), and ClangCodeParser::parse_cpp_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromCXString()

QString fromCXString ( CXString && string)
static

convert a CXString to a QString, and dispose the CXString

Definition at line 455 of file clangcodeparser.cpp.

◆ functionName()

QString functionName ( CXCursor cursor)

Returns the function name from a given cursor representing a function declaration.

This is usually clang_getCursorSpelling, but not for the conversion function in which case it is a bit more complicated

Definition at line 846 of file clangcodeparser.cpp.

◆ functionSignatureMatches()

bool functionSignatureMatches ( FunctionNode * fn,
FunctionCursorSignature & signature,
FnSignatureMatchMode matchMode )
static

Returns true if the candidate fn matches the parsed signature.

Ordinary matching preserves declaration lookup, including Q_QDOC signatures. The hidden-friend mode additionally interprets the leading template declaration as enclosing-class context and compares parameter types using owned, binder-aware keys from Clang's canonical types.

Definition at line 1081 of file clangcodeparser.cpp.

References Parameters::at(), Parameters::count(), FunctionCursorSignature::cursor, FunctionCursorSignature::declaration, FnCommand, HiddenFriendInClassTemplate, FunctionNode::isConst(), Parameters::isEmpty(), FunctionNode::isHiddenFriend(), Parameters::isPrivateSignal(), FunctionNode::isRef(), FunctionNode::isRefRef(), FunctionCursorSignature::isVariadic, Parameters::last(), FunctionCursorSignature::numArgs, FunctionNode::parameters(), Node< _Tp >::parent(), and Node< _Tp >::templateDecl().

Here is the call graph for this function:

◆ get_cursor_declaration()

const clang::Decl * get_cursor_declaration ( CXCursor cursor)
static

Returns the underlying Decl that cursor represents.

This can be used to drop back down from a LibClang's CXCursor to the underlying C++ AST that Clang provides.

It should be used when LibClang does not expose certain functionalities that are available in the C++ AST.

The CXCursor should represent a declaration. Usages of this function on CXCursors that do not represent a declaration may produce undefined results.

Definition at line 148 of file clangcodeparser.cpp.

Referenced by findFunctionForCursor().

Here is the caller graph for this function:

◆ get_default_value_initializer_as_string() [1/5]

std::string get_default_value_initializer_as_string ( const clang::NamedDecl * declaration)
static

Definition at line 417 of file clangcodeparser.cpp.

◆ get_default_value_initializer_as_string() [2/5]

std::string get_default_value_initializer_as_string ( const clang::NonTypeTemplateParmDecl * parameter)
static

Definition at line 361 of file clangcodeparser.cpp.

◆ get_default_value_initializer_as_string() [3/5]

std::string get_default_value_initializer_as_string ( const clang::ParmVarDecl * parameter)
static

Definition at line 399 of file clangcodeparser.cpp.

◆ get_default_value_initializer_as_string() [4/5]

std::string get_default_value_initializer_as_string ( const clang::TemplateTemplateParmDecl * parameter)
static

Definition at line 375 of file clangcodeparser.cpp.

◆ get_default_value_initializer_as_string() [5/5]

std::string get_default_value_initializer_as_string ( const clang::TemplateTypeParmDecl * parameter)
static

Definition at line 346 of file clangcodeparser.cpp.

◆ get_expression_as_string()

std::string get_expression_as_string ( const clang::Expr * expression,
const clang::ASTContext & declaration_context )
static

Definition at line 300 of file clangcodeparser.cpp.

◆ get_fully_qualified_type_name()

std::string get_fully_qualified_type_name ( clang::QualType type,
const clang::ASTContext & declaration_context )
static

Definition at line 257 of file clangcodeparser.cpp.

References clang::TypeName::getFullyQualifiedName().

Here is the call graph for this function:

◆ get_template_declaration()

RelaxedTemplateDeclaration get_template_declaration ( const clang::TemplateDecl * template_declaration)
static

Definition at line 610 of file clangcodeparser.cpp.

◆ getDefaultArgs()

void getDefaultArgs ( const QList< QByteArray > & defines,
std::vector< QByteArray > & args )

Load the default arguments and the defines into args.

Clear args first.

Definition at line 2428 of file clangcodeparser.cpp.

References defaultArgs_.

◆ getMoreArgs()

void getMoreArgs ( const std::vector< QByteArray > & include_paths,
std::vector< QByteArray > & args )

Load the include paths into args.

Definition at line 2442 of file clangcodeparser.cpp.

◆ getSpelling()

QString getSpelling ( CXSourceRange range)
static

Definition at line 826 of file clangcodeparser.cpp.

◆ getUnpatchedVersion()

float getUnpatchedVersion ( QString t)
static

Definition at line 2604 of file clangcodeparser.cpp.

◆ is_enable_if_name()

bool is_enable_if_name ( const std::string & qualified_name)
static

Definition at line 498 of file clangcodeparser.cpp.

◆ isConstexprAsDeclared()

bool isConstexprAsDeclared ( const clang::FunctionDecl * declaration)
static

Definition at line 2013 of file clangcodeparser.cpp.

References macroBodyLocation(), and mayDeclareConstexpr().

Here is the call graph for this function:

◆ macroBodyLocation()

clang::SourceLocation macroBodyLocation ( const clang::SourceManager & sourceManager,
clang::SourceLocation location )
static

Definition at line 1958 of file clangcodeparser.cpp.

Referenced by isConstexprAsDeclared().

Here is the caller graph for this function:

◆ mayDeclareConstexpr()

bool mayDeclareConstexpr ( const clang::ASTContext & context,
clang::SourceLocation location,
clang::SourceLocation end,
clang::SourceLocation readMacro )
static

Definition at line 1974 of file clangcodeparser.cpp.

Referenced by isConstexprAsDeclared().

Here is the caller graph for this function:

◆ operator<<()

template<class T>
QDebug operator<< ( QDebug debug,
const std::vector< T > & v )
static

Definition at line 100 of file clangcodeparser.cpp.

◆ parameterLookupType()

clang::QualType parameterLookupType ( const clang::ParmVarDecl * parameter)
static

Definition at line 1027 of file clangcodeparser.cpp.

◆ printDiagnostics()

void printDiagnostics ( const CXTranslationUnit & translationUnit)
static

Definition at line 117 of file clangcodeparser.cpp.

Referenced by buildPCH(), FnCommandParser::operator()(), and ClangCodeParser::parse_cpp_file().

Here is the caller graph for this function:

◆ readFile()

QString readFile ( CXFile cxFile,
unsigned int offset1,
unsigned int offset2 )
static

Definition at line 801 of file clangcodeparser.cpp.

◆ reconstructQualifiedPathForCursor()

QString reconstructQualifiedPathForCursor ( CXCursor cur)
static

Reconstruct the qualified path name of a function that is being overridden.

Definition at line 874 of file clangcodeparser.cpp.

◆ setOverridesForFunction()

void setOverridesForFunction ( FunctionNode * fn,
CXCursor cursor )
static

Definition at line 1332 of file clangcodeparser.cpp.

References FunctionNode::setOverride().

Here is the call graph for this function:

◆ toConstCharPointers()

std::vector< const char * > toConstCharPointers ( const std::vector< QByteArray > & args)
static

Definition at line 2415 of file clangcodeparser.cpp.

◆ visitChildrenLambda()

template<typename T>
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 is callable with a CXCursor parameter and returns a CXChildVisitResult (in other word compatible with function<CXChildVisitResult(CXCursor)>.

Definition at line 443 of file clangcodeparser.cpp.

Variable Documentation

◆ defaultArgs_

const char * defaultArgs_
static
Initial value:
= {
"-std=c++23",
"-fPIC",
"-DQ_QDOC",
"-DQ_CLANG_QDOC",
"-DQT_DISABLE_DEPRECATED_UP_TO=0",
"-DQT_ANNOTATE_CLASS(type,...)=static_assert(sizeof(#__VA_ARGS__),#type);",
"-DQT_ANNOTATE_CLASS2(type,a1,a2)=static_assert(sizeof(#a1,#a2),#type);",
"-DQT_ANNOTATE_FUNCTION(a)=__attribute__((annotate(#a)))",
"-DQT_ANNOTATE_ACCESS_SPECIFIER(a)=__attribute__((annotate(#a)))",
"-Wno-constant-logical-operand",
"-Wno-macro-redefined",
"-Wno-nullability-completeness",
"-fvisibility=default",
"-ferror-limit=0",
"-xc++"
}

Definition at line 2393 of file clangcodeparser.cpp.

Referenced by getDefaultArgs().

◆ flags_

CXTranslationUnit_Flags flags_ = static_cast<CXTranslationUnit_Flags>(0)
static

Definition at line 94 of file clangcodeparser.cpp.

Referenced by buildPCH(), and ClangCodeParser::parse_cpp_file().

◆ fnDummyFileName

const char fnDummyFileName = "/fn_dummyfile.cpp"
constexpr

Definition at line 96 of file clangcodeparser.cpp.

Referenced by FnCommandParser::operator()().

◆ kClangDontDisplayDiagnostics

const auto kClangDontDisplayDiagnostics = 0
static