| 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 "global-const-char-pointer.h" |
| 11 |
|
| 12 |
#include <clang/AST/Decl.h> |
| 13 |
#include <clang/AST/DeclBase.h> |
| 14 |
#include <clang/AST/Type.h> |
| 15 |
#include <clang/Basic/LLVM.h> |
| 16 |
#include <llvm/Support/Casting.h> |
| 17 |
|
| 18 |
#include <vector> |
| 19 |
|
| 20 |
using namespace clang; |
| 21 |
|
| 22 |
GlobalConstCharPointer::GlobalConstCharPointer(const std::string &name, Options options) |
| 23 |
: CheckBase(name, options) |
| 24 |
{ |
| 25 |
m_filesToIgnore = {"3rdparty", "mysql.h", "qpicture.cpp"}; |
| 26 |
} |
| 27 |
|
| 28 |
void GlobalConstCharPointer::VisitDecl(clang::Decl *decl) |
| 29 |
{ |
| 30 |
auto *varDecl = dyn_cast<VarDecl>(decl); |
| 31 |
if (!varDecl || !varDecl->hasGlobalStorage() || varDecl->isCXXClassMember() || !varDecl->hasExternalFormalLinkage() || decl->isInAnonymousNamespace() |
| 32 |
|| varDecl->hasExternalStorage()) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
if (shouldIgnoreFile(decl->getBeginLoc())) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
QualType qt = varDecl->getType(); |
| 41 |
const Type *type = qt.getTypePtrOrNull(); |
| 42 |
if (!type || !type->isPointerType() || qt.isConstQualified() || varDecl->isStaticLocal()) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
QualType pointeeQt = type->getPointeeType(); |
| 47 |
const Type *pointeeType = pointeeQt.getTypePtrOrNull(); |
| 48 |
if (!pointeeType || !pointeeType->isCharType()) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
emitWarning(decl->getBeginLoc(), "non const global char *"); |
| 53 |
} |