File indexing completed on 2024-05-12 05:40:59
0001 /* 0002 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com 0003 SPDX-FileContributor: Sérgio Martins <sergio.martins@kdab.com> 0004 0005 SPDX-FileCopyrightText: 2015 Sergio Martins <smartins@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #include "global-const-char-pointer.h" 0011 0012 #include <clang/AST/Decl.h> 0013 #include <clang/AST/DeclBase.h> 0014 #include <clang/AST/Type.h> 0015 #include <clang/Basic/LLVM.h> 0016 #include <llvm/Support/Casting.h> 0017 0018 #include <vector> 0019 0020 class ClazyContext; 0021 0022 using namespace clang; 0023 0024 GlobalConstCharPointer::GlobalConstCharPointer(const std::string &name, ClazyContext *context) 0025 : CheckBase(name, context) 0026 { 0027 m_filesToIgnore = {"3rdparty", "mysql.h", "qpicture.cpp"}; 0028 } 0029 0030 void GlobalConstCharPointer::VisitDecl(clang::Decl *decl) 0031 { 0032 auto *varDecl = dyn_cast<VarDecl>(decl); 0033 if (!varDecl || !varDecl->hasGlobalStorage() || varDecl->isCXXClassMember() || !varDecl->hasExternalFormalLinkage() || decl->isInAnonymousNamespace() 0034 || varDecl->hasExternalStorage()) { 0035 return; 0036 } 0037 0038 if (shouldIgnoreFile(decl->getBeginLoc())) { 0039 return; 0040 } 0041 0042 QualType qt = varDecl->getType(); 0043 const Type *type = qt.getTypePtrOrNull(); 0044 if (!type || !type->isPointerType() || qt.isConstQualified() || varDecl->isStaticLocal()) { 0045 return; 0046 } 0047 0048 QualType pointeeQt = type->getPointeeType(); 0049 const Type *pointeeType = pointeeQt.getTypePtrOrNull(); 0050 if (!pointeeType || !pointeeType->isCharType()) { 0051 return; 0052 } 0053 0054 emitWarning(decl->getBeginLoc(), "non const global char *"); 0055 }