| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Sérgio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef CLAZY_STRING_ALLOCATIONS_H |
| 11 |
#define CLAZY_STRING_ALLOCATIONS_H |
| 12 |
|
| 13 |
#include "checkbase.h" |
| 14 |
|
| 15 |
#include <string> |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
namespace clang |
| 19 |
{ |
| 20 |
class FixItHint; |
| 21 |
class CallExpr; |
| 22 |
class StringLiteral; |
| 23 |
class ConditionalOperator; |
| 24 |
} |
| 25 |
|
| 26 |
struct Latin1Expr; |
| 27 |
|
| 28 |
enum FromFunction { FromLatin1, FromUtf8 }; |
| 29 |
|
| 30 |
/** |
| 31 |
* Finds places where there are unneeded memory allocations due to temporary QStrings. |
| 32 |
* |
| 33 |
* For example: |
| 34 |
* QString s = QLatin1String("foo"); // should be QStringLiteral |
| 35 |
* QString::fromLatin1("foo") and QString::fromUtf8("foo") // should be QStringLiteral, or QLatin1String if being passed to an overload taking QLatin1String |
| 36 |
* |
| 37 |
* See README-qstring-allocations for more information. |
| 38 |
*/ |
| 39 |
class QStringAllocations : public CheckBase |
| 40 |
{ |
| 41 |
public: |
| 42 |
using CheckBase::CheckBase; |
| 43 |
void VisitStmt(clang::Stmt *stm) override; |
| 44 |
|
| 45 |
private: |
| 46 |
void VisitCtor(clang::Stmt *); |
| 47 |
void VisitCtor(clang::CXXConstructExpr *); |
| 48 |
void VisitOperatorCall(clang::Stmt *); |
| 49 |
void VisitFromLatin1OrUtf8(clang::Stmt *); |
| 50 |
void VisitAssignOperatorQLatin1String(clang::Stmt *); |
| 51 |
|
| 52 |
void maybeEmitWarning(clang::SourceLocation loc, std::string error, std::vector<clang::FixItHint> fixits = {}); |
| 53 |
std::vector<clang::FixItHint> fixItReplaceWordWithWord(clang::Stmt *begin, const std::string &replacement, const std::string &replacee); |
| 54 |
std::vector<clang::FixItHint> fixItReplaceWordWithWordInTernary(clang::ConditionalOperator *); |
| 55 |
std::vector<clang::FixItHint> fixItReplaceFromLatin1OrFromUtf8(clang::CallExpr *callExpr, FromFunction); |
| 56 |
std::vector<clang::FixItHint> fixItRawLiteral(clang::StringLiteral *stmt, const std::string &replacement, clang::CXXOperatorCallExpr *operatorCall); |
| 57 |
|
| 58 |
Latin1Expr qlatin1CtorExpr(clang::Stmt *stm, clang::ConditionalOperator *&ternary); |
| 59 |
}; |
| 60 |
|
| 61 |
#endif |