| 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 |
#include "qgetenv.h" |
| 11 |
#include "FixItUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
#include "Utils.h" |
| 14 |
|
| 15 |
#include <clang/AST/Decl.h> |
| 16 |
#include <clang/AST/DeclCXX.h> |
| 17 |
#include <clang/AST/Expr.h> |
| 18 |
#include <clang/AST/ExprCXX.h> |
| 19 |
#include <clang/AST/Stmt.h> |
| 20 |
#include <clang/Basic/Diagnostic.h> |
| 21 |
#include <clang/Basic/LLVM.h> |
| 22 |
#include <llvm/ADT/StringRef.h> |
| 23 |
#include <llvm/Support/Casting.h> |
| 24 |
|
| 25 |
#include <vector> |
| 26 |
|
| 27 |
using namespace clang; |
| 28 |
|
| 29 |
void QGetEnv::VisitStmt(clang::Stmt *stmt) |
| 30 |
{ |
| 31 |
// Lets check only in function calls. Otherwise there are too many false positives, it's common |
| 32 |
// to implicit cast to bool when checking pointers for validity, like if (ptr) |
| 33 |
|
| 34 |
auto *memberCall = dyn_cast<CXXMemberCallExpr>(stmt); |
| 35 |
if (!memberCall) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
CXXMethodDecl *method = memberCall->getMethodDecl(); |
| 40 |
if (!method) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if (const CXXRecordDecl *record = method->getParent(); !record || clazy::name(record) != "QByteArray") { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
std::vector<CallExpr *> calls = Utils::callListForChain(memberCall); |
| 49 |
if (calls.size() != 2) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
CallExpr *qgetEnvCall = calls.back(); |
| 54 |
if (const FunctionDecl *func = qgetEnvCall->getDirectCallee(); !func || clazy::name(func) != "qgetenv") { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
StringRef methodname = clazy::name(method); |
| 59 |
std::string errorMsg; |
| 60 |
std::string replacement; |
| 61 |
bool shouldIncludeOkParameter = false; |
| 62 |
bool changesToBaseAutodetection = false; |
| 63 |
if (methodname == "isEmpty") { |
| 64 |
errorMsg = "qgetenv().isEmpty() allocates."; |
| 65 |
replacement = "qEnvironmentVariableIsEmpty"; |
| 66 |
} else if (methodname == "isNull") { |
| 67 |
errorMsg = "qgetenv().isNull() allocates."; |
| 68 |
replacement = "qEnvironmentVariableIsSet"; |
| 69 |
} else if (methodname == "toInt") { |
| 70 |
errorMsg = "qgetenv().toInt() is slow."; |
| 71 |
replacement = "qEnvironmentVariableIntValue"; |
| 72 |
for (unsigned int i = 0; i < memberCall->getNumArgs(); ++i) { |
| 73 |
auto *arg = memberCall->getArg(i); |
| 74 |
if (i == 0 && !isa<CXXDefaultArgExpr>(arg)) { |
| 75 |
if (!isa<CastExpr>(arg) || !isa<CXXNullPtrLiteralExpr>(dyn_cast<CastExpr>(arg)->getSubExpr())) { |
| 76 |
shouldIncludeOkParameter = true; |
| 77 |
} |
| 78 |
} else if (i == 1) { |
| 79 |
if (auto *intLiteral = dyn_cast<IntegerLiteral>(arg)) { |
| 80 |
if (intLiteral->getValue() != 0) { |
| 81 |
return; // Custom base is specified - ignore this case |
| 82 |
} |
| 83 |
} else if (isa<CXXDefaultArgExpr>(arg)) { |
| 84 |
changesToBaseAutodetection = true; |
| 85 |
} else { |
| 86 |
return; // If the base is neither the 0 literal or the default, skip checking it |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} else { |
| 91 |
return; // Some different method on QByteArray, nothing to warn about |
| 92 |
} |
| 93 |
|
| 94 |
std::string getEnvArgStr = Lexer::getSourceText(CharSourceRange::getTokenRange(qgetEnvCall->getArg(0)->getSourceRange()), sm(), lo()).str(); |
| 95 |
if (shouldIncludeOkParameter) { |
| 96 |
getEnvArgStr += ", " + Lexer::getSourceText(CharSourceRange::getTokenRange(memberCall->getArg(0)->getSourceRange()), sm(), lo()).str(); |
| 97 |
} |
| 98 |
|
| 99 |
errorMsg += " Use " + replacement + "() instead"; |
| 100 |
if (changesToBaseAutodetection) { |
| 101 |
errorMsg += ". This uses internally a base of 0, supporting decimal, hex and octal values"; |
| 102 |
} |
| 103 |
emitWarning(memberCall->getBeginLoc(), errorMsg, {FixItHint::CreateReplacement(stmt->getSourceRange(), replacement + "(" + getEnvArgStr + ")")}); |
| 104 |
} |