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

(4a368a06afa5929d8674d2e94c2d7cbd6ad85d4e)

#include "clangcodeparser.h"
#include "cppcodeparser.h"
#include "access.h"
#include "classnode.h"
#include "config.h"
#include "enumnode.h"
#include "functionnode.h"
#include "namespacenode.h"
#include "propertynode.h"
#include "qdocdatabase.h"
#include "typedefnode.h"
#include "variablenode.h"
#include "utilities.h"
#include <QtCore/qdebug.h>
#include <QtCore/qelapsedtimer.h>
#include <QtCore/qfile.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/Decl.h>
#include <clang/AST/DeclFriend.h>
#include <clang/AST/DeclTemplate.h>
#include <clang/AST/Expr.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 <cstdio>
+ 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...
 
class  ClangVisitor
 

Functions

template<class T >
static QDebug operator<< (QDebug debug, const std::vector< T > &v)
 
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 get_fully_qualified_type_name (clang::QualType type, const clang::ASTContext &declaration_context)
 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_expression_as_string (const clang::Expr *expression, const clang::ASTContext &declaration_context)
 
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 &&string)
 convert a CXString to a QString, and dispose the CXString
 
static RelaxedTemplateDeclaration get_template_declaration (const clang::TemplateDecl *template_declaration)
 
static Location fromCXSourceLocation (CXSourceLocation location)
 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 range)
 
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 NodefindNodeForCursor (QDocDatabase *qdb, CXCursor cur)
 Find the node from the QDocDatabase qdb that corresponds to the declaration represented by the cursor cur, if it exists.
 
static void setOverridesForFunction (FunctionNode *fn, CXCursor cursor)
 
void getDefaultArgs (const QList< QByteArray > &defines, std::vector< const char * > &args)
 Load the default arguments and the defines into args.
 
static QList< QByteArrayincludePathsFromHeaders (const std::set< Config::HeaderFilePath > &allHeaders)
 
void getMoreArgs (const std::vector< QByteArray > &include_paths, const std::set< Config::HeaderFilePath > &all_headers, std::vector< const char * > &args)
 Load the include paths into moreArgs.
 
std::optional< PCHFilebuildPCH (QDocDatabase *qdb, QString module_header, const std::set< Config::HeaderFilePath > &all_headers, const std::vector< QByteArray > &include_paths, const QList< QByteArray > &defines)
 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 t)
 

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_ []
 

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 )

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 1551 of file clangcodeparser.cpp.

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

+ Here is the call graph for this function:

◆ findNodeForCursor()

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

Find the node from the QDocDatabase qdb that corresponds to the declaration represented by the cursor cur, if it exists.

Definition at line 565 of file clangcodeparser.cpp.

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

Referenced by findNodeForCursor().

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

◆ fromCache()

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

Definition at line 451 of file clangcodeparser.cpp.

◆ fromCX_CXXAccessSpecifier()

static Access fromCX_CXXAccessSpecifier ( CX_CXXAccessSpecifier spec)
static

convert a CX_CXXAccessSpecifier to Node::Access

Definition at line 427 of file clangcodeparser.cpp.

◆ fromCXSourceLocation()

static Location fromCXSourceLocation ( CXSourceLocation location)
static

convert a CXSourceLocation to a qdoc Location

Definition at line 413 of file clangcodeparser.cpp.

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

Referenced by ClangCodeParser::parse_cpp_file().

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

◆ fromCXString()

static QString fromCXString ( CXString && string)
static

convert a CXString to a QString, and dispose the CXString

Definition at line 331 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 502 of file clangcodeparser.cpp.

References get_cursor_declaration().

+ Here is the call graph for this function:

◆ get_cursor_declaration()

static 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 133 of file clangcodeparser.cpp.

Referenced by findNodeForCursor(), and functionName().

+ Here is the caller graph for this function:

◆ get_default_value_initializer_as_string() [1/5]

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

Definition at line 293 of file clangcodeparser.cpp.

◆ get_default_value_initializer_as_string() [2/5]

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

Definition at line 231 of file clangcodeparser.cpp.

◆ get_default_value_initializer_as_string() [3/5]

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

Definition at line 275 of file clangcodeparser.cpp.

◆ get_default_value_initializer_as_string() [4/5]

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

Definition at line 251 of file clangcodeparser.cpp.

◆ get_default_value_initializer_as_string() [5/5]

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

Definition at line 209 of file clangcodeparser.cpp.

◆ get_expression_as_string()

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

Definition at line 185 of file clangcodeparser.cpp.

◆ get_fully_qualified_type_name()

static std::string get_fully_qualified_type_name ( clang::QualType type,
const clang::ASTContext & declaration_context )
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
[0]

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 167 of file clangcodeparser.cpp.

References clang::TypeName::getFullyQualifiedName().

+ Here is the call graph for this function:

◆ get_template_declaration()

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

Definition at line 342 of file clangcodeparser.cpp.

◆ getDefaultArgs()

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

Load the default arguments and the defines into args.

Clear args first.

Definition at line 1497 of file clangcodeparser.cpp.

References defaultArgs_.

◆ getMoreArgs()

void getMoreArgs ( const std::vector< QByteArray > & include_paths,
const std::set< Config::HeaderFilePath > & all_headers,
std::vector< const char * > & args )

Load the include paths into moreArgs.

If no include paths were provided, try to guess reasonable include paths.

Definition at line 1523 of file clangcodeparser.cpp.

◆ getSpelling()

static QString getSpelling ( CXSourceRange range)
static

Definition at line 482 of file clangcodeparser.cpp.

◆ getUnpatchedVersion()

static float getUnpatchedVersion ( QString t)
static

Definition at line 1679 of file clangcodeparser.cpp.

◆ includePathsFromHeaders()

static QList< QByteArray > includePathsFromHeaders ( const std::set< Config::HeaderFilePath > & allHeaders)
static

Definition at line 1507 of file clangcodeparser.cpp.

◆ operator<<()

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

Definition at line 85 of file clangcodeparser.cpp.

◆ printDiagnostics()

static void printDiagnostics ( const CXTranslationUnit & translationUnit)
static

Definition at line 102 of file clangcodeparser.cpp.

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

+ Here is the caller graph for this function:

◆ readFile()

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

Definition at line 457 of file clangcodeparser.cpp.

◆ reconstructQualifiedPathForCursor()

static QString reconstructQualifiedPathForCursor ( CXCursor cur)
static

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

Definition at line 530 of file clangcodeparser.cpp.

◆ setOverridesForFunction()

static void setOverridesForFunction ( FunctionNode * fn,
CXCursor cursor )
static

Definition at line 695 of file clangcodeparser.cpp.

References FunctionNode::setOverride().

+ Here is the call graph for this function:

◆ 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 319 of file clangcodeparser.cpp.

Variable Documentation

◆ defaultArgs_

static const char * defaultArgs_
static
Initial value:
= {
"-std=c++20",
"-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",
("-I" CLANG_RESOURCE_DIR)
}

Definition at line 1461 of file clangcodeparser.cpp.

Referenced by getDefaultArgs().

◆ flags_

static CXTranslationUnit_Flags flags_ = static_cast<CXTranslationUnit_Flags>(0)
static

Definition at line 79 of file clangcodeparser.cpp.

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

◆ fnDummyFileName

const char fnDummyFileName = "/fn_dummyfile.cpp"
constexpr

Definition at line 81 of file clangcodeparser.cpp.

Referenced by FnCommandParser::operator()().

◆ kClangDontDisplayDiagnostics

static const auto kClangDontDisplayDiagnostics = 0
static