| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileCopyrightText: 2015-2016 Sergio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef MOREWARNINGS_UTILS_H |
| 9 |
#define MOREWARNINGS_UTILS_H |
| 10 |
|
| 11 |
#include <clang/AST/DeclCXX.h> |
| 12 |
#include <clang/AST/DeclTemplate.h> |
| 13 |
#include <clang/AST/Expr.h> |
| 14 |
#include <clang/AST/ExprCXX.h> |
| 15 |
#include <clang/AST/Stmt.h> |
| 16 |
#include <clang/Basic/SourceLocation.h> |
| 17 |
#include <clang/Basic/SourceManager.h> |
| 18 |
#include <llvm/ADT/ArrayRef.h> |
| 19 |
#include <llvm/ADT/StringRef.h> |
| 20 |
#include <llvm/Config/llvm-config.h> |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
// TODO: this is a dumping ground, most of these functions should be moved to the other *Utils classes |
| 26 |
|
| 27 |
namespace clang |
| 28 |
{ |
| 29 |
class CXXNamedCastExpr; |
| 30 |
class CXXRecordDecl; |
| 31 |
class CXXMemberCallExpr; |
| 32 |
class CXXConstructExpr; |
| 33 |
class CompilerInstance; |
| 34 |
class ClassTemplateSpecializationDecl; |
| 35 |
class Decl; |
| 36 |
class ParentMap; |
| 37 |
class SourceManager; |
| 38 |
class Stmt; |
| 39 |
class SourceLocation; |
| 40 |
class ExprWithCleanups; |
| 41 |
class ValueDecl; |
| 42 |
class ConditionalOperator; |
| 43 |
class CXXMethodDecl; |
| 44 |
class BinaryOperator; |
| 45 |
class CXXOperatorCallExpr; |
| 46 |
class CallExpr; |
| 47 |
class DeclStmt; |
| 48 |
class Expr; |
| 49 |
class FunctionDecl; |
| 50 |
class LangOptions; |
| 51 |
class ParmVarDecl; |
| 52 |
class StringLiteral; |
| 53 |
class UserDefinedLiteral; |
| 54 |
class VarDecl; |
| 55 |
} |
| 56 |
|
| 57 |
struct StmtBodyRange; |
| 58 |
|
| 59 |
namespace Utils |
| 60 |
{ |
| 61 |
/// Returns true if the class has at least one constexpr ctor |
| 62 |
bool hasConstexprCtor(clang::CXXRecordDecl *decl); |
| 63 |
|
| 64 |
/// Returns the type we're casting *from* |
| 65 |
clang::CXXRecordDecl *namedCastInnerDecl(clang::CXXNamedCastExpr *staticOrDynamicCast); |
| 66 |
|
| 67 |
/// Returns the type we're casting *to* |
| 68 |
clang::CXXRecordDecl *namedCastOuterDecl(clang::CXXNamedCastExpr *staticOrDynamicCast); |
| 69 |
|
| 70 |
/// Returns the class declaration from a variable declaration |
| 71 |
// So, if the var decl is "Foo f"; it returns the declaration of Foo |
| 72 |
clang::CXXRecordDecl *recordFromVarDecl(clang::Decl *); |
| 73 |
|
| 74 |
/// Returns the template specialization from a variable declaration |
| 75 |
// So, if the var decl is "QList<Foo> f;", returns the template specialization QList<Foo> |
| 76 |
clang::ClassTemplateSpecializationDecl *templateSpecializationFromVarDecl(clang::Decl *); |
| 77 |
|
| 78 |
/// Returns true if all the child member function calls are const functions. |
| 79 |
bool allChildrenMemberCallsConst(clang::Stmt *stm); |
| 80 |
|
| 81 |
/// Returns true if at least a UnaryOperator, BinaryOperator or non-const function call is found |
| 82 |
bool childsHaveSideEffects(clang::Stmt *stm); |
| 83 |
|
| 84 |
/// Receives a member call, such as "list.reserve()" and returns the declaration of the variable list |
| 85 |
// such as "QList<F> list" |
| 86 |
clang::ValueDecl *valueDeclForMemberCall(clang::CXXMemberCallExpr *); |
| 87 |
|
| 88 |
/// Receives an operator call, such as "list << fooo" and returns the declaration of the variable list |
| 89 |
// such as "QList<F> list" |
| 90 |
clang::ValueDecl *valueDeclForOperatorCall(clang::CXXOperatorCallExpr *); |
| 91 |
|
| 92 |
// overload |
| 93 |
clang::ValueDecl *valueDeclForCallExpr(clang::CallExpr *); |
| 94 |
|
| 95 |
// Returns true of this value decl is a member variable of a class or struct |
| 96 |
// returns null if not |
| 97 |
clang::CXXRecordDecl *isMemberVariable(clang::ValueDecl *); |
| 98 |
|
| 99 |
// Returns true if a body of statements contains a non const member call on object declared by varDecl |
| 100 |
// For example: |
| 101 |
// Foo foo; // this is the varDecl |
| 102 |
// while (bar) { foo.setValue(); // non-const call } |
| 103 |
bool containsNonConstMemberCall(clang::ParentMap *map, clang::Stmt *body, const clang::VarDecl *varDecl); |
| 104 |
|
| 105 |
// Returns true if there's an assignment to varDecl in body |
| 106 |
// Example: our_var = something_else |
| 107 |
bool isAssignedFrom(clang::Stmt *body, const clang::VarDecl *varDecl); |
| 108 |
|
| 109 |
// Returns true if varDecl is assigned to something |
| 110 |
// Example: something_else = our_var |
| 111 |
bool isAssignedTo(clang::Stmt *body, const clang::VarDecl *varDecl); |
| 112 |
|
| 113 |
// Returns whether the variable is returned in body |
| 114 |
bool isReturned(clang::Stmt *body, const clang::VarDecl *varDecl); |
| 115 |
|
| 116 |
// Returns true if a body of statements contains a function call that takes our variable (varDecl) |
| 117 |
// By ref or pointer |
| 118 |
bool isPassedToFunction(const StmtBodyRange &bodyRange, const clang::VarDecl *varDecl, bool byRefOrPtrOnly); |
| 119 |
|
| 120 |
// Returns true if we take the address of varDecl, such as: &foo |
| 121 |
bool addressIsTaken(clang::Stmt *body, const clang::ValueDecl *valDecl); |
| 122 |
|
| 123 |
// QString::fromLatin1("foo") -> true |
| 124 |
// QString::fromLatin1("foo", 1) -> false |
| 125 |
bool callHasDefaultArguments(clang::CallExpr *expr); |
| 126 |
|
| 127 |
// If there's a child of type StringLiteral, returns true |
| 128 |
// if allowEmpty is false, "" will be ignored |
| 129 |
bool containsStringLiteral(clang::Stmt *, bool allowEmpty = true, int depth = -1); |
| 130 |
|
| 131 |
bool isInsideOperatorCall(clang::ParentMap *map, clang::Stmt *s, const std::vector<llvm::StringRef> &anyOf); |
| 132 |
|
| 133 |
bool insideCTORCall(clang::ParentMap *map, clang::Stmt *s, const std::vector<llvm::StringRef> &anyOf); |
| 134 |
|
| 135 |
// returns true if the ternary operator has two string literal arguments, such as: |
| 136 |
// foo ? "bar" : "baz" |
| 137 |
bool ternaryOperatorIsOfStringLiteral(clang::ConditionalOperator *); |
| 138 |
|
| 139 |
bool isAssignOperator(clang::CXXOperatorCallExpr *op, llvm::StringRef className, llvm::StringRef argumentType, const clang::LangOptions &lo); |
| 140 |
|
| 141 |
bool isImplicitCastTo(clang::Stmt *, const std::string &); |
| 142 |
|
| 143 |
bool presumedLocationsEqual(const clang::PresumedLoc &l1, const clang::PresumedLoc &l2); |
| 144 |
|
| 145 |
// Returns the list of methods with name methodName that the class/struct record contains |
| 146 |
std::vector<clang::CXXMethodDecl *> methodsFromString(const clang::CXXRecordDecl *record, const std::string &methodName); |
| 147 |
|
| 148 |
// Returns the most derived class. (CXXMemberCallExpr::getRecordDecl() return the first base class with the method) |
| 149 |
// The returned callee is the name of the variable on which the member call was made: |
| 150 |
// o1->foo() => "o1" |
| 151 |
// foo() => "this" |
| 152 |
const clang::CXXRecordDecl *recordForMemberCall(clang::CXXMemberCallExpr *call, std::string &implicitCallee); |
| 153 |
|
| 154 |
bool isAscii(clang::StringLiteral *lt); |
| 155 |
|
| 156 |
// Checks if Statement s inside an operator* call |
| 157 |
bool isInDerefExpression(clang::Stmt *s, clang::ParentMap *map); |
| 158 |
|
| 159 |
// For a a chain called expression like foo().bar().baz() returns a list of calls |
| 160 |
// {baz(), bar(), foo()} |
| 161 |
|
| 162 |
// the parameter lastCallExpr to pass would be baz() |
| 163 |
// No need to specify the other callexprs, they are children of the first one, since the AST looks like: |
| 164 |
// - baz |
| 165 |
// -- bar |
| 166 |
// --- foo |
| 167 |
std::vector<clang::CallExpr *> callListForChain(clang::CallExpr *lastCallExpr); |
| 168 |
|
| 169 |
// Returns the first base class |
| 170 |
clang::CXXRecordDecl *rootBaseClass(clang::CXXRecordDecl *derived); |
| 171 |
|
| 172 |
// Returns the copy ctor for this class |
| 173 |
clang::CXXConstructorDecl *copyCtor(const clang::CXXRecordDecl *); |
| 174 |
|
| 175 |
// Returns the copy-assignment operator for this class |
| 176 |
clang::CXXMethodDecl *copyAssign(const clang::CXXRecordDecl *); |
| 177 |
|
| 178 |
bool hasMember(clang::CXXRecordDecl *record, const std::string &memberTypeName); |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns true if varDecl is initialized externally. |
| 182 |
* Example: |
| 183 |
* QList<Foo> list = getList(); // true |
| 184 |
* QList<int> list = list2; // true |
| 185 |
* QList<int> list = {1, 2, 3}; // false |
| 186 |
* QList<int> list; // false |
| 187 |
*/ |
| 188 |
bool isInitializedExternally(clang::VarDecl *varDecl); |
| 189 |
|
| 190 |
/** |
| 191 |
* Returns true if declStmt refers to varDecl |
| 192 |
*/ |
| 193 |
bool referencesVarDecl(clang::DeclStmt *declStmt, clang::VarDecl *varDecl); |
| 194 |
|
| 195 |
/** |
| 196 |
* Returns true if the body of a function is empty. |
| 197 |
* Returns false if either function or it's body are null. |
| 198 |
*/ |
| 199 |
bool functionHasEmptyBody(clang::FunctionDecl *func); |
| 200 |
|
| 201 |
/** |
| 202 |
* If stm is an UnaryOperator or BinaryOperator that writes to the variable it returns the expression |
| 203 |
* that represents the variable (Usually a MemberExpr or DeclRefExpr for local variables). |
| 204 |
* |
| 205 |
* Otherwise returns nullptr. |
| 206 |
* |
| 207 |
* The operators that write to the variable are operator=, operator+=, operator++, etc. |
| 208 |
*/ |
| 209 |
clang::Expr *isWriteOperator(clang::Stmt *stm); |
| 210 |
|
| 211 |
/** |
| 212 |
* Gets the UserDefinedLiteral of type @p type which is somewhere in the ast of @p stm. |
| 213 |
* Returns nullptr if there's no such UserDefinedLiteral. |
| 214 |
*/ |
| 215 |
clang::UserDefinedLiteral *userDefinedLiteral(clang::Stmt *stm, const std::string &type, const clang::LangOptions &lo); |
| 216 |
|
| 217 |
/** |
| 218 |
* Returns the function parameters from @p func |
| 219 |
* This should be used instead of calling FunctionDecl::params() since it changed signature in |
| 220 |
* clang 3.9. |
| 221 |
*/ |
| 222 |
clang::ArrayRef<clang::ParmVarDecl *> functionParameters(clang::FunctionDecl *func); |
| 223 |
|
| 224 |
/** |
| 225 |
* For the given ctor, and ctor param, returns the ctor member initializers that used that param. |
| 226 |
* Example: |
| 227 |
* MyCtor(int a, int b) : c(a), d(b) {} |
| 228 |
* auto result = Utils::ctorInitializer(MyCtor, b); // Result is the statement "d(b)" |
| 229 |
*/ |
| 230 |
std::vector<clang::CXXCtorInitializer *> ctorInitializer(clang::CXXConstructorDecl *ctor, clang::ParmVarDecl *param); |
| 231 |
|
| 232 |
/** |
| 233 |
* Returns true if a ctor initializer contains a std::move() |
| 234 |
* Example |
| 235 |
* MyCtor(Foo a) : c(move(a)) {} // Would return true for this init list |
| 236 |
*/ |
| 237 |
bool ctorInitializerContainsMove(clang::CXXCtorInitializer *); |
| 238 |
|
| 239 |
// Overload that receives a vector and returns true if any ctor initializer contains a move() |
| 240 |
bool ctorInitializerContainsMove(const std::vector<clang::CXXCtorInitializer *> &); |
| 241 |
|
| 242 |
/** |
| 243 |
* Returns the filename for the source location loc |
| 244 |
*/ |
| 245 |
std::string filenameForLoc(clang::SourceLocation loc, const clang::SourceManager &sm); |
| 246 |
|
| 247 |
/** |
| 248 |
* Returns the location after the lexer token that is at loc. |
| 249 |
* For example: |
| 250 |
* emit sig(); |
| 251 |
* If loc refers to the location of 'emit', then this function will return the source location if |
| 252 |
* the sig() call. |
| 253 |
*/ |
| 254 |
clang::SourceLocation locForNextToken(clang::SourceLocation loc, const clang::SourceManager &sm, const clang::LangOptions &lo); |
| 255 |
|
| 256 |
inline bool isMainFile(const clang::SourceManager &sm, clang::SourceLocation loc) |
| 257 |
{ |
| 258 |
if (loc.isMacroID()) { |
| 259 |
loc = sm.getExpansionLoc(loc); |
| 260 |
} |
| 261 |
|
| 262 |
return sm.isInFileID(loc, sm.getMainFileID()); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Returns true if the string literal contains escaped bytes, such as \x12, \123, \u00F6. |
| 267 |
*/ |
| 268 |
bool literalContainsEscapedBytes(clang::StringLiteral *lt, const clang::SourceManager &sm, const clang::LangOptions &lo); |
| 269 |
|
| 270 |
/** |
| 271 |
* Returns true if this method overrides one from the base class |
| 272 |
*/ |
| 273 |
inline bool methodOverrides(clang::CXXMethodDecl *method) |
| 274 |
{ |
| 275 |
return method && method->isVirtual() && method->size_overridden_methods() > 0; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
#endif |