From 6e5fee3730ad2e443d9ec93eb88f06ee7f8b33e2 Mon Sep 17 00:00:00 2001 From: Sergio Martins Date: Tue, 9 Oct 2018 11:37:05 +0100 Subject: empty-qstringliteral: Don't warn for uic generated files Since 5.12 this is no longer an issue, since QStringLiteral was abolished from ui_*.h files. BUG: 399544 --- src/checks/level0/empty-qstringliteral.cpp | 17 +++++++++ src/checks/level0/empty-qstringliteral.h | 1 + tests/empty-qstringliteral/main.cpp | 2 +- tests/empty-qstringliteral/ui_qstringliteral.h | 50 ++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/empty-qstringliteral/ui_qstringliteral.h diff --git a/src/checks/level0/empty-qstringliteral.cpp b/src/checks/level0/empty-qstringliteral.cpp index 359ccd03..492c9073 100644 --- a/src/checks/level0/empty-qstringliteral.cpp +++ b/src/checks/level0/empty-qstringliteral.cpp @@ -24,6 +24,9 @@ #include "HierarchyUtils.h" #include "QtUtils.h" #include "TypeUtils.h" +#include "SourceCompatibilityHelpers.h" +#include "PreProcessorVisitor.h" +#include "ClazyContext.h" #include @@ -59,5 +62,19 @@ void EmptyQStringliteral::VisitStmt(clang::Stmt *stmt) if (!getLocStart(stmt).isMacroID()) return; + if (maybeIgnoreUic(getLocStart(stmt))) + return; + emitWarning(stmt, "Use QString instead of an empty QStringLiteral"); } + +bool EmptyQStringliteral::maybeIgnoreUic(SourceLocation loc) const +{ + PreProcessorVisitor *preProcessorVisitor = m_context->preprocessorVisitor; + + // Since 5.12 uic no longer uses QStringLiteral("") + if (preProcessorVisitor && preProcessorVisitor->qtVersion() >= 051200) + return false; + + return clazy::isUIFile(loc, sm()); +} diff --git a/src/checks/level0/empty-qstringliteral.h b/src/checks/level0/empty-qstringliteral.h index d1acf6a2..c8155e2b 100644 --- a/src/checks/level0/empty-qstringliteral.h +++ b/src/checks/level0/empty-qstringliteral.h @@ -32,6 +32,7 @@ class EmptyQStringliteral : public CheckBase public: explicit EmptyQStringliteral(const std::string &name, ClazyContext *context); void VisitStmt(clang::Stmt *) override; + bool maybeIgnoreUic(clang::SourceLocation) const; private: }; diff --git a/tests/empty-qstringliteral/main.cpp b/tests/empty-qstringliteral/main.cpp index 0429205e..3439f1fb 100644 --- a/tests/empty-qstringliteral/main.cpp +++ b/tests/empty-qstringliteral/main.cpp @@ -1,6 +1,6 @@ #include #include - +#include "ui_qstringliteral.h" // OK void test() { QStringLiteral("foo"); diff --git a/tests/empty-qstringliteral/ui_qstringliteral.h b/tests/empty-qstringliteral/ui_qstringliteral.h new file mode 100644 index 00000000..0fb2675c --- /dev/null +++ b/tests/empty-qstringliteral/ui_qstringliteral.h @@ -0,0 +1,50 @@ +/******************************************************************************** +** Form generated from reading UI file 'untitled.ui' +** +** Created by: Qt User Interface Compiler version 5.9.7 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef UNTITLED_H +#define UNTITLED_H + +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_Form +{ +public: + + void setupUi(QWidget *Form) + { + if (Form->objectName().isEmpty()) + Form->setObjectName(QStringLiteral("Form")); + Form->resize(400, 300); + Form->setStyleSheet(QStringLiteral("")); + + retranslateUi(Form); + + QMetaObject::connectSlotsByName(Form); + } // setupUi + + void retranslateUi(QWidget *Form) + { + Form->setWindowTitle(QApplication::translate("Form", "Form", Q_NULLPTR)); + } // retranslateUi + +}; + +namespace Ui { + class Form: public Ui_Form {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UNTITLED_H -- cgit v1.2.3