File indexing completed on 2024-04-28 05:38:31

0001 /*
0002     SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com
0003     SPDX-FileContributor: SĂ©rgio Martins <sergio.martins@kdab.com>
0004 
0005     SPDX-FileCopyrightText: 2015 Sergio Martins <smartins@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef CLAZY_STRING_ALLOCATIONS_H
0011 #define CLAZY_STRING_ALLOCATIONS_H
0012 
0013 #include "checkbase.h"
0014 
0015 #include <string>
0016 #include <vector>
0017 
0018 class ClazyContext;
0019 
0020 namespace clang
0021 {
0022 class FixItHint;
0023 class ConditionalOperator;
0024 class CallExpr;
0025 class StringLiteral;
0026 class ConditionalOperator;
0027 class Stmt;
0028 }
0029 
0030 struct Latin1Expr;
0031 
0032 enum FromFunction { FromLatin1, FromUtf8 };
0033 
0034 /**
0035  * Finds places where there are unneeded memory allocations due to temporary QStrings.
0036  *
0037  * For example:
0038  * QString s = QLatin1String("foo"); // should be QStringLiteral
0039  * QString::fromLatin1("foo") and QString::fromUtf8("foo") // should be QStringLiteral, or QLatin1String if being passed to an overload taking QLatin1String
0040  *
0041  * See README-qstring-allocations for more information.
0042  */
0043 class QStringAllocations : public CheckBase
0044 {
0045 public:
0046     QStringAllocations(const std::string &name, ClazyContext *context);
0047     void VisitStmt(clang::Stmt *stm) override;
0048 
0049 private:
0050     void VisitCtor(clang::Stmt *);
0051     void VisitCtor(clang::CXXConstructExpr *);
0052     void VisitOperatorCall(clang::Stmt *);
0053     void VisitFromLatin1OrUtf8(clang::Stmt *);
0054     void VisitAssignOperatorQLatin1String(clang::Stmt *);
0055 
0056     void maybeEmitWarning(clang::SourceLocation loc, std::string error, std::vector<clang::FixItHint> fixits = {});
0057     std::vector<clang::FixItHint> fixItReplaceWordWithWord(clang::Stmt *begin, const std::string &replacement, const std::string &replacee);
0058     std::vector<clang::FixItHint> fixItReplaceWordWithWordInTernary(clang::ConditionalOperator *);
0059     std::vector<clang::FixItHint> fixItReplaceFromLatin1OrFromUtf8(clang::CallExpr *callExpr, FromFunction);
0060     std::vector<clang::FixItHint> fixItRawLiteral(clang::StringLiteral *stmt, const std::string &replacement, clang::CXXOperatorCallExpr *operatorCall);
0061 
0062     Latin1Expr qlatin1CtorExpr(clang::Stmt *stm, clang::ConditionalOperator *&ternary);
0063 };
0064 
0065 #endif