aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergio Martins <smartins@kde.org>2020-04-19 22:19:01 +0100
committerSergio Martins <smartins@kde.org>2020-04-19 22:19:01 +0100
commit6eab2735dc7c0a0d24554343d7b9c33a19df2a5f (patch)
tree7721cf4ef1598fd2fe6a0827c0b419129ac0002a
parentdd4a977d316658e684c198146b1eaf4da504f9c2 (diff)
qstring-allocations: Fix unit-tests with llvm-10
Wasn't warning for: QStringList list = { "foo" }; The child CXXConstructExprs appear in the AST but are never visited.
-rw-r--r--src/checks/level2/qstring-allocations.cpp37
-rw-r--r--src/checks/level2/qstring-allocations.h1
2 files changed, 32 insertions, 6 deletions
diff --git a/src/checks/level2/qstring-allocations.cpp b/src/checks/level2/qstring-allocations.cpp
index 999ee1e9..e09745ea 100644
--- a/src/checks/level2/qstring-allocations.cpp
+++ b/src/checks/level2/qstring-allocations.cpp
@@ -176,14 +176,39 @@ static StringLiteral* stringLiteralForCall(Stmt *call)
void QStringAllocations::VisitCtor(Stmt *stm)
{
auto ctorExpr = dyn_cast<CXXConstructExpr>(stm);
+ if (!ctorExpr)
+ return;
+
if (!Utils::containsStringLiteral(ctorExpr, /**allowEmpty=*/ true))
return;
CXXConstructorDecl *ctorDecl = ctorExpr->getConstructor();
+#if LLVM_VERSION_MAJOR >= 10
+ // With llvm 10, for some reason, the child CXXConstructExpr of QStringList foo = {"foo}; aren't visited :(.
+ // Do it manually.
+ if (clazy::isOfClass(ctorDecl, "QStringList")) {
+ auto p = clazy::getFirstChildOfType2<CXXConstructExpr>(ctorExpr);
+ while (p) {
+ if (clazy::isOfClass(p, "QString")) {
+ VisitCtor(p);
+ }
+ p = clazy::getFirstChildOfType2<CXXConstructExpr>(p);
+ }
+ } else {
+ VisitCtor(ctorExpr);
+ }
+#else
+ VisitCtor(ctorExpr);
+#endif
+}
+
+void QStringAllocations::VisitCtor(CXXConstructExpr *ctorExpr)
+{
+ CXXConstructorDecl *ctorDecl = ctorExpr->getConstructor();
if (!clazy::isOfClass(ctorDecl, "QString"))
return;
- if (Utils::insideCTORCall(m_context->parentMap, stm, { "QRegExp", "QIcon" })) {
+ if (Utils::insideCTORCall(m_context->parentMap, ctorExpr, { "QRegExp", "QIcon" })) {
// https://blogs.kde.org/2015/11/05/qregexp-qstringliteral-crash-exit
return;
}
@@ -193,7 +218,7 @@ void QStringAllocations::VisitCtor(Stmt *stm)
if (initializerList != nullptr)
return; // Nothing to do here, MSVC doesn't like it
- StringLiteral *lt = stringLiteralForCall(stm);
+ StringLiteral *lt = stringLiteralForCall(ctorExpr);
if (lt && lt->getNumConcatenated() > 1) {
return; // Nothing to do here, MSVC doesn't like it
}
@@ -214,7 +239,7 @@ void QStringAllocations::VisitCtor(Stmt *stm)
if (isQLatin1String) {
ConditionalOperator *ternary = nullptr;
- Latin1Expr qlatin1expr = qlatin1CtorExpr(stm, ternary);
+ Latin1Expr qlatin1expr = qlatin1CtorExpr(ctorExpr, ternary);
if (!qlatin1expr.isValid()) {
return;
}
@@ -233,7 +258,7 @@ void QStringAllocations::VisitCtor(Stmt *stm)
if (!clazy::getLocStart(qlatin1Ctor).isMacroID()) {
if (!ternary) {
fixits = fixItReplaceWordWithWord(qlatin1Ctor, "QStringLiteral", "QLatin1String");
- bool shouldRemoveQString = clazy::getLocStart(qlatin1Ctor).getRawEncoding() != clazy::getLocStart(stm).getRawEncoding() && dyn_cast_or_null<CXXBindTemporaryExpr>(clazy::parent(m_context->parentMap, ctorExpr));
+ bool shouldRemoveQString = clazy::getLocStart(qlatin1Ctor).getRawEncoding() != clazy::getLocStart(ctorExpr).getRawEncoding() && dyn_cast_or_null<CXXBindTemporaryExpr>(clazy::parent(m_context->parentMap, ctorExpr));
if (shouldRemoveQString) {
// This is the case of QString(QLatin1String("foo")), which we just fixed to be QString(QStringLiteral("foo)), so now remove QString
auto removalFixits = clazy::fixItRemoveToken(&m_astContext, ctorExpr, true);
@@ -251,7 +276,7 @@ void QStringAllocations::VisitCtor(Stmt *stm)
}
}
- maybeEmitWarning(clazy::getLocStart(stm), msg, fixits);
+ maybeEmitWarning(clazy::getLocStart(ctorExpr), msg, fixits);
} else {
vector<FixItHint> fixits;
if (clazy::hasChildren(ctorExpr)) {
@@ -293,7 +318,7 @@ void QStringAllocations::VisitCtor(Stmt *stm)
}
}
- maybeEmitWarning(clazy::getLocStart(stm), msg, fixits);
+ maybeEmitWarning(clazy::getLocStart(ctorExpr), msg, fixits);
}
}
diff --git a/src/checks/level2/qstring-allocations.h b/src/checks/level2/qstring-allocations.h
index 560be7b6..5bbd794f 100644
--- a/src/checks/level2/qstring-allocations.h
+++ b/src/checks/level2/qstring-allocations.h
@@ -68,6 +68,7 @@ public:
private:
void VisitCtor(clang::Stmt *);
+ void VisitCtor(clang::CXXConstructExpr *);
void VisitOperatorCall(clang::Stmt *);
void VisitFromLatin1OrUtf8(clang::Stmt *);
void VisitAssignOperatorQLatin1String(clang::Stmt *);