-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new check: qbytearray-conversion-to-c-style
Warns about usage of the implicit `QByteArray::operator const char *()`.
- Loading branch information
Showing
14 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# qbytearray-conversion-to-c-style | ||
|
||
Warns about usage of QByteArray to `const char *` implicit conversion operator. This | ||
sort of conversion is deemed dangerous given how `const char *` is a very common type. | ||
This can be fixed by making the conversion explicit, e.g. by using `QByteArray::constData()`. | ||
|
||
Note that this conversion operator can be disabled by defining the `QBYTEARRAY_NO_CAST_TO_ASCII` | ||
macro. | ||
|
||
#### Caveats | ||
For QByteArrayLiteral this check only works for Qt6 where QByteArrayLiteral is a macro, but not | ||
with Qt5 where QByteArrayLiteral is a lambda. | ||
|
||
#### Example | ||
|
||
void func(const char *); | ||
QByteArray ba = "some text"; | ||
func(ba); // Bad, implicit conversion to const char * | ||
func(ba.constData()); // Good, explicit conversion | ||
|
||
func(QByteArrayLiteral("some literal")); // Bad, implicit conversion plus QByteArray allocation | ||
func("some literal")); // Good, use the string literal directly | ||
|
||
#### Fixits | ||
|
||
This check supports a fixit to rewrite your code. See the README.md on how to enable it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/checks/manuallevel/qbytearray-conversion-to-c-style.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright (C) 2025 Ahmad Samir <[email protected]> | ||
SPDX-License-Identifier: LGPL-2.0-or-later | ||
*/ | ||
|
||
#include "qbytearray-conversion-to-c-style.h" | ||
#include "ClazyContext.h" | ||
#include "FixItUtils.h" | ||
#include "HierarchyUtils.h" | ||
#include "QtUtils.h" | ||
#include "TypeUtils.h" | ||
#include "Utils.h" | ||
|
||
#include <clang/AST/AST.h> | ||
|
||
using namespace clang; | ||
|
||
QBytearrayConversionToCStyle::QBytearrayConversionToCStyle(const std::string &name, ClazyContext *context) | ||
: CheckBase(name, context, Option_CanIgnoreIncludes) | ||
{ | ||
} | ||
|
||
void QBytearrayConversionToCStyle::VisitStmt(clang::Stmt *stmt) | ||
{ | ||
if (!stmt) | ||
return; | ||
|
||
auto memberExpr = dyn_cast<CXXMemberCallExpr>(stmt); | ||
if (!memberExpr) { | ||
return; | ||
} | ||
|
||
auto methodDecl = memberExpr->getMethodDecl(); | ||
if (!methodDecl) | ||
return; | ||
CXXRecordDecl *className = methodDecl->getParent(); | ||
if (!className || className->getName() != "QByteArray") { | ||
return; | ||
} | ||
|
||
constexpr const char byteArrayOp[] = "operator const char *"; | ||
auto *callee = dyn_cast<MemberExpr>(memberExpr->getCallee()); | ||
if (!callee || callee->getMemberNameInfo().getName().getAsString() != byteArrayOp) { | ||
return; | ||
} | ||
|
||
static const std::string msg = "Don't rely on the QByteArray implicit conversion to 'const char *'."; | ||
|
||
SourceRange sr = callee->getSourceRange(); | ||
CharSourceRange cr = Lexer::getAsCharRange(sr, sm(), lo()); | ||
std::string str = Lexer::getSourceText(cr, sm(), lo()).str(); | ||
if (clazy::getFirstChildOfType<DeclRefExpr>(stmt)) { // E.g. QByteArray ba = "foo" | ||
std::vector<FixItHint> fixits = {clazy::createReplacement(sr, str + ".constData()")}; | ||
emitWarning(sr.getEnd(), msg, fixits); | ||
return; | ||
} | ||
|
||
if (auto *funcCastExpr = clazy::getFirstChildOfType<CXXFunctionalCastExpr>(stmt)) { | ||
auto *literal = clazy::getFirstChildOfType<StringLiteral>(funcCastExpr); | ||
if (str.find("QByteArrayLiteral") != std::string_view::npos) { // QByteArrayLiteral("foo"). | ||
// This only works with Qt6 where QByteArrayLiteral is a macro, but not with | ||
// Qt5 where QByteArrayLiteral is a lambda. | ||
std::string replacement = '"' + literal->getString().str() + '"'; // "foo" | ||
// The replacement has to be where the macro is expanded `QByteArrayLiteral("foo")` | ||
// not where it's defined. | ||
CharSourceRange expRange = sm().getExpansionRange(funcCastExpr->getBeginLoc()); | ||
std::vector<FixItHint> fixits = {clazy::createReplacement(expRange.getAsRange(), replacement)}; | ||
emitWarning(expRange.getBegin(), msg, fixits); | ||
} else if (str.find("QByteArray") != std::string_view::npos) { // QByteArray("foo") | ||
CharSourceRange cr = Lexer::getAsCharRange(literal->getSourceRange(), sm(), lo()); | ||
std::string replacement = Lexer::getSourceText(cr, sm(), lo()).str(); // "foo" | ||
std::vector<FixItHint> fixits = {clazy::createReplacement(sr, replacement)}; | ||
emitWarning(sr.getBegin(), msg, fixits); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
Copyright (C) 2025 Ahmad Samir <[email protected]> | ||
SPDX-License-Identifier: LGPL-2.0-or-later | ||
*/ | ||
|
||
#ifndef CLAZY_QBYTEARRAY_CONVERSION_TO_C_STYLE | ||
#define CLAZY_QBYTEARRAY_CONVERSION_TO_C_STYLE | ||
|
||
#include "checkbase.h" | ||
|
||
/** | ||
* See README-qbytearray-conversion-to-c-style.md for more info. | ||
*/ | ||
class QBytearrayConversionToCStyle : public CheckBase | ||
{ | ||
public: | ||
explicit QBytearrayConversionToCStyle(const std::string &name, ClazyContext *context); | ||
void VisitStmt(clang::Stmt *) override; | ||
}; | ||
|
||
#endif // CLAZY_QBYTEARRAY_CONVERSION_TO_C_STYLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"tests" : [ | ||
{ | ||
"filename" : "main.cpp", | ||
"has_fixits" : true, | ||
"qt_major_versions": [6] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <QtCore/QByteArray> | ||
#include <QtCore/QString> | ||
static void func(const char *str) { } | ||
|
||
static void otherFunc(const char *str) { } | ||
|
||
#define TEXT "macrotext" | ||
|
||
void test() | ||
{ | ||
QByteArray ba = "this is a qbytearray"; | ||
|
||
const char *c = ba; | ||
func(ba); | ||
|
||
otherFunc(QByteArray("foo")); | ||
otherFunc(QByteArrayLiteral("ba-literal")); | ||
otherFunc(QByteArrayLiteral(TEXT " ba-literal")); | ||
|
||
QString utf16 = QString::fromLatin1("string"); | ||
func(std::exchange(utf16, {}).toLocal8Bit()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
qbytearray-conversion-to-c-style/main.cpp:13:21: warning: Don't rely on the QByteArray implicit conversion to 'const char *'. [-Wclazy-qbytearray-conversion-to-c-style] | ||
qbytearray-conversion-to-c-style/main.cpp:14:10: warning: Don't rely on the QByteArray implicit conversion to 'const char *'. [-Wclazy-qbytearray-conversion-to-c-style] | ||
qbytearray-conversion-to-c-style/main.cpp:16:15: warning: Don't rely on the QByteArray implicit conversion to 'const char *'. [-Wclazy-qbytearray-conversion-to-c-style] | ||
qbytearray-conversion-to-c-style/main.cpp:17:15: warning: Don't rely on the QByteArray implicit conversion to 'const char *'. [-Wclazy-qbytearray-conversion-to-c-style] | ||
qbytearray-conversion-to-c-style/main.cpp:18:15: warning: Don't rely on the QByteArray implicit conversion to 'const char *'. [-Wclazy-qbytearray-conversion-to-c-style] | ||
qbytearray-conversion-to-c-style/main.cpp:21:47: warning: Don't rely on the QByteArray implicit conversion to 'const char *'. [-Wclazy-qbytearray-conversion-to-c-style] |
22 changes: 22 additions & 0 deletions
22
tests/qbytearray-conversion-to-c-style/main.cpp.fixed.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <QtCore/QByteArray> | ||
#include <QtCore/QString> | ||
static void func(const char *str) { } | ||
|
||
static void otherFunc(const char *str) { } | ||
|
||
#define TEXT "macrotext" | ||
|
||
void test() | ||
{ | ||
QByteArray ba = "this is a qbytearray"; | ||
|
||
const char *c = ba.constData(); | ||
func(ba.constData()); | ||
|
||
otherFunc("foo"); | ||
otherFunc("ba-literal"); | ||
otherFunc("macrotext ba-literal"); | ||
|
||
QString utf16 = QString::fromLatin1("string"); | ||
func(std::exchange(utf16, {}).toLocal8Bit().constData()); | ||
} |