| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]> |
| 3 |
SPDX-FileCopyrightText: 2015 Mathias Hasselmann <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "auto-unexpected-qstringbuilder.h" |
| 9 |
#include "FixItUtils.h" |
| 10 |
#include "StringUtils.h" |
| 11 |
#include "TypeUtils.h" |
| 12 |
|
| 13 |
#include <clang/AST/Decl.h> |
| 14 |
#include <clang/AST/DeclBase.h> |
| 15 |
#include <clang/AST/DeclCXX.h> |
| 16 |
#include <clang/AST/ExprCXX.h> |
| 17 |
#include <clang/AST/Stmt.h> |
| 18 |
#include <clang/AST/Type.h> |
| 19 |
#include <clang/Basic/Diagnostic.h> |
| 20 |
#include <clang/Basic/LLVM.h> |
| 21 |
#include <clang/Basic/SourceLocation.h> |
| 22 |
#include <llvm/ADT/StringRef.h> |
| 23 |
#include <llvm/Support/Casting.h> |
| 24 |
|
| 25 |
#include <vector> |
| 26 |
|
| 27 |
using namespace clang; |
| 28 |
|
| 29 |
static bool isQStringBuilder(QualType t) |
| 30 |
{ |
| 31 |
const CXXRecordDecl *record = clazy::typeAsRecord(t); |
| 32 |
return record && clazy::name(record) == "QStringBuilder"; |
| 33 |
} |
| 34 |
|
| 35 |
void AutoUnexpectedQStringBuilder::VisitDecl(Decl *decl) |
| 36 |
{ |
| 37 |
auto *varDecl = dyn_cast<VarDecl>(decl); |
| 38 |
if (!varDecl) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
QualType qualtype = varDecl->getType(); |
| 43 |
const Type *type = qualtype.getTypePtrOrNull(); |
| 44 |
if (!type || !type->isRecordType() || !dyn_cast<AutoType>(type) || !isQStringBuilder(qualtype)) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
std::string replacement = "QString " + clazy::name(varDecl).str(); |
| 49 |
|
| 50 |
if (qualtype.isConstQualified()) { |
| 51 |
replacement = "const " + replacement; |
| 52 |
} |
| 53 |
|
| 54 |
SourceLocation start = varDecl->getBeginLoc(); |
| 55 |
SourceLocation end = varDecl->getLocation(); |
| 56 |
std::vector<FixItHint> fixits; |
| 57 |
fixits.push_back(clazy::createReplacement({start, end}, replacement)); |
| 58 |
|
| 59 |
emitWarning(decl->getBeginLoc(), "auto deduced to be QStringBuilder instead of QString. Possible crash.", fixits); |
| 60 |
} |
| 61 |
|
| 62 |
void AutoUnexpectedQStringBuilder::VisitStmt(Stmt *stmt) |
| 63 |
{ |
| 64 |
auto *lambda = dyn_cast<LambdaExpr>(stmt); |
| 65 |
if (!lambda) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
CXXMethodDecl *method = lambda->getCallOperator(); |
| 70 |
if (!method || !isQStringBuilder(method->getReturnType())) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
emitWarning(stmt->getBeginLoc(), "lambda return type deduced to be QStringBuilder instead of QString. Possible crash."); |
| 75 |
} |