| 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 "non-pod-global-static.h" |
| 11 |
#include "ClazyContext.h" |
| 12 |
#include "QtUtils.h" |
| 13 |
#include "clazy_stl.h" |
| 14 |
|
| 15 |
#include <clang/AST/Decl.h> |
| 16 |
#include <clang/AST/DeclCXX.h> |
| 17 |
#include <clang/AST/ExprCXX.h> |
| 18 |
#include <clang/AST/Stmt.h> |
| 19 |
#include <clang/Basic/LLVM.h> |
| 20 |
#include <clang/Basic/SourceLocation.h> |
| 21 |
#include <clang/Basic/Specifiers.h> |
| 22 |
#include <clang/Frontend/CompilerInstance.h> |
| 23 |
#include <clang/Lex/Lexer.h> |
| 24 |
#include <llvm/ADT/StringRef.h> |
| 25 |
#include <llvm/Support/Casting.h> |
| 26 |
|
| 27 |
#include <vector> |
| 28 |
|
| 29 |
using namespace clang; |
| 30 |
|
| 31 |
static bool shouldIgnoreType(StringRef name) |
| 32 |
{ |
| 33 |
static std::vector<StringRef> blacklist = {"Holder", "AFUNC", "QLoggingCategory", "QThreadStorage", "QQmlModuleRegistration"}; |
| 34 |
return clazy::contains(blacklist, name); |
| 35 |
} |
| 36 |
|
| 37 |
NonPodGlobalStatic::NonPodGlobalStatic(const std::string &name, Options options) |
| 38 |
: CheckBase(name, options) |
| 39 |
{ |
| 40 |
m_filesToIgnore = {"main.cpp", "qrc_", "qdbusxml2cpp"}; |
| 41 |
} |
| 42 |
|
| 43 |
void NonPodGlobalStatic::VisitStmt(clang::Stmt *stm) |
| 44 |
{ |
| 45 |
VarDecl *varDecl = m_context->lastDecl ? dyn_cast<VarDecl>(m_context->lastDecl) : nullptr; |
| 46 |
if (!varDecl || varDecl->isConstexpr() || varDecl->isExternallyVisible() || !varDecl->isFileVarDecl()) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if (shouldIgnoreFile(stm->getBeginLoc())) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
StorageDuration sd = varDecl->getStorageDuration(); |
| 55 |
if (sd != StorageDuration::SD_Static) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
const SourceLocation declStart = varDecl->getBeginLoc(); |
| 60 |
|
| 61 |
if (declStart.isMacroID()) { |
| 62 |
SourceLocation immediateMacroLoc = declStart; |
| 63 |
|
| 64 |
// Walk up the macro expansion stack |
| 65 |
while (immediateMacroLoc.isMacroID()) { |
| 66 |
static const std::vector<std::string> macrosToIgnore{ |
| 67 |
"Q_IMPORT_PLUGIN", |
| 68 |
"Q_CONSTRUCTOR_FUNCTION", |
| 69 |
"Q_DESTRUCTOR_FUNCTION", |
| 70 |
"Q_COREAPP_STARTUP_FUNCTION", |
| 71 |
}; |
| 72 |
|
| 73 |
auto macroName = std::string_view(Lexer::getImmediateMacroName(immediateMacroLoc, sm(), lo())); |
| 74 |
if (clazy::startsWithAny(macroName, macrosToIgnore)) |
| 75 |
return; |
| 76 |
immediateMacroLoc = sm().getImmediateMacroCallerLoc(immediateMacroLoc); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
auto *ctorExpr = dyn_cast<CXXConstructExpr>(stm); |
| 81 |
if (!ctorExpr) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
auto *ctorDecl = ctorExpr->getConstructor(); |
| 86 |
auto *recordDecl = ctorDecl ? ctorDecl->getParent() : nullptr; |
| 87 |
if (!recordDecl) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
if (recordDecl->hasTrivialDestructor()) { |
| 92 |
// Has a trivial dtor, but now lets check the ctors. |
| 93 |
|
| 94 |
if (ctorDecl->isDefaultConstructor() && recordDecl->hasTrivialDefaultConstructor()) { |
| 95 |
// both dtor and called ctor are trivial, no warning |
| 96 |
return; |
| 97 |
} |
| 98 |
if (ctorDecl->isConstexpr()) { |
| 99 |
// Used ctor is constexpr, it's fine |
| 100 |
return; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (m_context->isQtDeveloper() && clazy::isBootstrapping(m_context->m_pp)) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
StringRef className = clazy::name(recordDecl); |
| 109 |
if (!shouldIgnoreType(className)) { |
| 110 |
const std::string varName = varDecl->getQualifiedNameAsString(); |
| 111 |
const std::string error = std::string("non-POD static (") + className.data() + " " + varName + std::string(")"); |
| 112 |
|
| 113 |
// Do not emit warnings for each entry in collection - this is overkill since we emit it for the declaration |
| 114 |
// Traversing the ParentMap unfortunately does not work, because it is cut of at the initializer list |
| 115 |
if (std::ranges::find(m_emittedWarnings, declStart.getRawEncoding()) != m_emittedWarnings.end()) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
m_emittedWarnings.push_back(declStart.getRawEncoding()); |
| 120 |
emitWarning(declStart, error); |
| 121 |
} |
| 122 |
} |