aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2019-11-14 10:06:13 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2019-11-15 09:25:58 +0100
commit8a792c5d793d90f49d0cc3cbdb348545184aa0f2 (patch)
treea033fb3c3580dd7f06952a9453102f4719ff4e09
parent6ea4edb3a600e3e9b723f2d1a045e490171385a0 (diff)
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 <cristian.maureira-fredes@qt.io>
-rw-r--r--sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp79
1 files changed, 48 insertions, 31 deletions
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 <QtCore/QStack>
#include <QtCore/QVector>
-#include <string.h>
+#include <cstring>
#include <ctype.h>
#if QT_VERSION < 0x050800
@@ -202,6 +202,8 @@ public:
template <class Item>
void qualifyTypeDef(const CXCursor &typeRefCursor, const QSharedPointer<Item> &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<size_t N>
+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