forked from qt-creator/qt-creator
QmlDesigner: Add PropertyNameLiteral
That hopefully makes it really hard to create a literal from dynamic or automatic memory. Only global string literals should be used. In header you should use: MakeHeaderPropertyViewLiteral(foo, "Foo"); Makes foo constexpr and provides internal linkage. In cpp files you should use: MakeHeaderPropertyViewLiteral(foo, "Foo"); Makes foo constexpr and provides external linkage. In a function scope you should use: MakeCppPropertyViewLiteral(foo, "Foo"); Makes foo constexpr but does not add it to the stack. All three provide a const QString& on demand. So no code is executed as the program is started. Change-Id: I924efbb1f5c2b2c811f3eeae042ff2eec8de3faf Reviewed-by: Aleksei German <aleksei.german@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
This commit is contained in:
@@ -7,11 +7,46 @@
|
||||
#include "qmldesignercorelib_exports.h"
|
||||
|
||||
#include <utils/smallstringview.h>
|
||||
#include <utils/span.h>
|
||||
|
||||
namespace QmlDesigner {
|
||||
using PropertyName = QByteArray;
|
||||
using PropertyNameView = QByteArrayView;
|
||||
using PropertyNameList = QList<PropertyName>;
|
||||
|
||||
template<std::size_t Size, const char Text[Size]>
|
||||
class ByteArrayViewLiteral : public QByteArrayView
|
||||
{
|
||||
public:
|
||||
constexpr ByteArrayViewLiteral() noexcept
|
||||
: QByteArrayView{Text, Size - 1}
|
||||
{}
|
||||
|
||||
const QByteArray &toByteArray() const noexcept
|
||||
{
|
||||
static auto b = QByteArray::fromRawData(Text, Size - 1);
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
operator const QByteArray &() const noexcept { return toByteArray(); }
|
||||
};
|
||||
|
||||
#define MakeCppPropertyViewLiteral(name, text) \
|
||||
constexpr char name##InternalString[] = text; \
|
||||
constexpr ByteArrayViewLiteral<sizeof(name##InternalString), name##InternalString> name{}; \
|
||||
/* */
|
||||
|
||||
#define MakeHeaderPropertyViewLiteral(name, text) \
|
||||
inline constexpr char name##InternalString[] = text; \
|
||||
inline constexpr ByteArrayViewLiteral<sizeof(name##InternalString), name##InternalString> name{}; \
|
||||
/* */
|
||||
|
||||
#define MakeFunctionPropertyViewLiteral(name, text) \
|
||||
static constexpr char name##InternalString[] = text; \
|
||||
static constexpr ByteArrayViewLiteral<sizeof(name##InternalString), name##InternalString> name{}; \
|
||||
/* */
|
||||
|
||||
using TypeName = QByteArray;
|
||||
using PropertyTypeList = QList<PropertyName>;
|
||||
using IdName = QByteArray;
|
||||
|
Reference in New Issue
Block a user