From 8a792c5d793d90f49d0cc3cbdb348545184aa0f2 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 14 Nov 2019 10:06:13 +0100 Subject: shiboken: Refactor the logic deciding whether headers should be parsed - Use cstring and prefix by std - Split the functions into base name and comparison functions - Use a non-type template to pass the size for startsWith() - Split out visitHeader() for clarity Task-number: PYSIDE-454 Change-Id: I48e2a9ae5fead892c20d9729cb90d61ff5d7fb0a Reviewed-by: Cristian Maureira-Fredes --- .../ApiExtractor/clangparser/clangbuilder.cpp | 79 +++++++++++++--------- 1 file changed, 48 insertions(+), 31 deletions(-) (limited to 'sources') diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp b/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp index 8d1b4debf..a5b153499 100644 --- a/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp +++ b/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp @@ -40,7 +40,7 @@ #include #include -#include +#include #include #if QT_VERSION < 0x050800 @@ -202,6 +202,8 @@ public: template void qualifyTypeDef(const CXCursor &typeRefCursor, const QSharedPointer &item) const; + bool visitHeader(const char *cFileName) const; + BaseVisitor *m_baseVisitor; CodeModel *m_model; @@ -665,31 +667,62 @@ Builder::~Builder() delete d; } -static inline bool compareHeaderName(const char *haystack, const char *needle) +static const char *cBaseName(const char *fileName) { - const char *lastSlash = strrchr(haystack, '/'); + const char *lastSlash = std::strrchr(fileName, '/'); #ifdef Q_OS_WIN if (lastSlash == nullptr) - lastSlash = strrchr(haystack, '\\'); + lastSlash = std::strrchr(fileName, '\\'); #endif - if (lastSlash == nullptr) - lastSlash = haystack; - else - ++lastSlash; + return lastSlash != nullptr ? (lastSlash + 1) : fileName; +} + +static inline bool cCompareFileName(const char *f1, const char *f2) +{ #ifdef Q_OS_WIN - return _stricmp(lastSlash, needle) == 0; + return _stricmp(f1, f2) == 0; #else - return strcmp(lastSlash, needle) == 0; + return std::strcmp(f1, f2) == 0; #endif } #ifdef Q_OS_UNIX -static bool cStringStartsWith(const char *prefix, const char *str) +template +static bool cStringStartsWith(const char *str, const char (&prefix)[N]) { - return strncmp(prefix, str, strlen(prefix)) == 0; + return std::strncmp(prefix, str, N - 1) == 0; } #endif +bool BuilderPrivate::visitHeader(const char *cFileName) const +{ + // Resolve OpenGL typedefs although the header is considered a system header. + const char *baseName = cBaseName(cFileName); + if (cCompareFileName(baseName, "gl.h")) + return true; +#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS) + if (cStringStartsWith(cFileName, "/usr/include/stdint.h")) + return true; +#endif +#ifdef Q_OS_LINUX + if (cStringStartsWith(cFileName, "/usr/include/stdlib.h") + || cStringStartsWith(cFileName, "/usr/include/sys/types.h")) { + return true; + } +#endif // Q_OS_LINUX +#ifdef Q_OS_MACOS + // Parse the following system headers to get the correct typdefs for types like + // int32_t, which are used in the macOS implementation of OpenGL framework. + if (cCompareFileName(baseName, "gltypes.h") + || cStringStartsWith(cFileName, "/usr/include/_types") + || cStringStartsWith(cFileName, "/usr/include/_types") + || cStringStartsWith(cFileName, "/usr/include/sys/_types")) { + return true; + } +#endif // Q_OS_MACOS + return false; +} + bool Builder::visitLocation(const CXSourceLocation &location) const { if (clang_Location_isInSystemHeader(location) == 0) @@ -701,28 +734,12 @@ bool Builder::visitLocation(const CXSourceLocation &location) const clang_getExpansionLocation(location, &file, &line, &column, &offset); const CXString cxFileName = clang_getFileName(file); // Has been observed to be 0 for invalid locations + bool result = false; if (const char *cFileName = clang_getCString(cxFileName)) { - // Resolve OpenGL typedefs although the header is considered a system header. - const bool visitHeader = compareHeaderName(cFileName, "gl.h") -#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS) - || cStringStartsWith("/usr/include/stdint.h", cFileName) -#endif -#if defined(Q_OS_LINUX) - || cStringStartsWith("/usr/include/stdlib.h", cFileName) - || cStringStartsWith("/usr/include/sys/types.h", cFileName) -#elif defined(Q_OS_MACOS) - // Parse the following system headers to get the correct typdefs for types like - // int32_t, which are used in the macOS implementation of OpenGL framework. - || compareHeaderName(cFileName, "gltypes.h") - || cStringStartsWith("/usr/include/_types", cFileName) - || cStringStartsWith("/usr/include/sys/_types", cFileName) -#endif - ; + result = d->visitHeader(cFileName); clang_disposeString(cxFileName); - if (visitHeader) - return true; } - return false; + return result; } FileModelItem Builder::dom() const -- cgit v1.2.3