File indexing completed on 2024-05-12 17:13:15
0001 /* 0002 This file is part of the clazy static checker. 0003 0004 Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com 0005 Author: Sérgio Martins <sergio.martins@kdab.com> 0006 0007 Copyright (C) 2015 Sergio Martins <smartins@kde.org> 0008 0009 This library is free software; you can redistribute it and/or 0010 modify it under the terms of the GNU Library General Public 0011 License as published by the Free Software Foundation; either 0012 version 2 of the License, or (at your option) any later version. 0013 0014 This library is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0017 Library General Public License for more details. 0018 0019 You should have received a copy of the GNU Library General Public License 0020 along with this library; see the file COPYING.LIB. If not, write to 0021 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0022 Boston, MA 02110-1301, USA. 0023 */ 0024 0025 #ifndef CLAZY_STRING_ALLOCATIONS_H 0026 #define CLAZY_STRING_ALLOCATIONS_H 0027 0028 #include "checkbase.h" 0029 0030 #include <map> 0031 #include <vector> 0032 #include <string> 0033 0034 class ClazyContext; 0035 0036 namespace clang { 0037 class FixItHint; 0038 class ConditionalOperator; 0039 class CallExpr; 0040 class StringLiteral; 0041 class ConditionalOperator; 0042 class Stmt; 0043 } 0044 0045 struct Latin1Expr; 0046 0047 enum FromFunction { 0048 FromLatin1, 0049 FromUtf8 0050 }; 0051 0052 0053 /** 0054 * Finds places where there are unneeded memory allocations due to temporary QStrings. 0055 * 0056 * For example: 0057 * QString s = QLatin1String("foo"); // should be QStringLiteral 0058 * QString::fromLatin1("foo") and QString::fromUtf8("foo") // should be QStringLiteral, or QLatin1String if being passed to an overload taking QLatin1String 0059 * 0060 * See README-qstring-allocations for more information. 0061 */ 0062 class QStringAllocations 0063 : public CheckBase 0064 { 0065 public: 0066 QStringAllocations(const std::string &name, ClazyContext *context); 0067 void VisitStmt(clang::Stmt *stm) override; 0068 0069 private: 0070 void VisitCtor(clang::Stmt *); 0071 void VisitCtor(clang::CXXConstructExpr *); 0072 void VisitOperatorCall(clang::Stmt *); 0073 void VisitFromLatin1OrUtf8(clang::Stmt *); 0074 void VisitAssignOperatorQLatin1String(clang::Stmt *); 0075 0076 void maybeEmitWarning(clang::SourceLocation loc, std::string error, std::vector<clang::FixItHint> fixits = {}); 0077 std::vector<clang::FixItHint> fixItReplaceWordWithWord(clang::Stmt *begin, const std::string &replacement, const std::string &replacee); 0078 std::vector<clang::FixItHint> fixItReplaceWordWithWordInTernary(clang::ConditionalOperator *); 0079 std::vector<clang::FixItHint> fixItReplaceFromLatin1OrFromUtf8(clang::CallExpr *callExpr, FromFunction); 0080 std::vector<clang::FixItHint> fixItRawLiteral(clang::StringLiteral *stmt, const std::string &replacement); 0081 0082 Latin1Expr qlatin1CtorExpr(clang::Stmt *stm, clang::ConditionalOperator * &ternary); 0083 }; 0084 0085 #endif