| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2025 Alexander Lohnau <[email protected]> |
| 3 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "AccessSpecifierManager.h" |
| 7 |
#include "ClazyContext.h" |
| 8 |
#include "ClazyVisitHelper.h" |
| 9 |
#include "TypeUtils.h" |
| 10 |
#include "Utils.h" |
| 11 |
#include "checkbase.h" |
| 12 |
#include "checkmanager.h" |
| 13 |
#include "clang-tidy/ClangTidyCheck.h" |
| 14 |
#include "clang-tidy/ClangTidyModule.h" |
| 15 |
#include "clang/AST/ParentMap.h" |
| 16 |
#include "clang/AST/RecursiveASTVisitor.h" |
| 17 |
#include "clang/ASTMatchers/ASTMatchers.h" |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
#if LLVM_VERSION_MAJOR < 22 |
| 21 |
#include "clang-tidy/ClangTidyModuleRegistry.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
using namespace clang::ast_matchers; |
| 25 |
using namespace clang::tidy; |
| 26 |
using namespace clang; |
| 27 |
|
| 28 |
class FullASTVisitor : public RecursiveASTVisitor<FullASTVisitor> |
| 29 |
{ |
| 30 |
public: |
| 31 |
explicit FullASTVisitor(ClazyContext *context, ClangTidyCheck &Check, const clazy::VisitHelper::Visitors &visitors) |
| 32 |
: m_context(context) |
| 33 |
, m_visitors(visitors) |
| 34 |
|
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
bool VisitStmt(Stmt *stmt) |
| 39 |
{ |
| 40 |
return clazy::VisitHelper::VisitStmt(stmt, m_context, m_visitors); |
| 41 |
} |
| 42 |
|
| 43 |
bool VisitDecl(Decl *decl) |
| 44 |
{ |
| 45 |
return clazy::VisitHelper::VisitDecl(decl, m_context, m_visitors); |
| 46 |
} |
| 47 |
|
| 48 |
private: |
| 49 |
ClazyContext *const m_context; |
| 50 |
const clazy::VisitHelper::Visitors &m_visitors; |
| 51 |
}; |
| 52 |
|
| 53 |
std::vector<std::string> s_enabledChecks; |
| 54 |
|
| 55 |
class ClazyCheck : public ClangTidyCheck |
| 56 |
{ |
| 57 |
public: |
| 58 |
ClazyCheck(StringRef CheckName, ClangTidyContext *Context) |
| 59 |
: ClangTidyCheck(CheckName, Context) |
| 60 |
, m_shouldRunClazyChecks(s_enabledChecks.empty()) |
| 61 |
, clangTidyContext(Context) |
| 62 |
, clazyContext(nullptr) |
| 63 |
{ |
| 64 |
// so that we later know which check was registered |
| 65 |
s_enabledChecks.emplace_back(CheckName); |
| 66 |
} |
| 67 |
|
| 68 |
~ClazyCheck() override |
| 69 |
{ |
| 70 |
for (auto &checkPair : m_allChecks) { |
| 71 |
delete checkPair.first; |
| 72 |
} |
| 73 |
m_allChecks.clear(); |
| 74 |
delete clazyContext; |
| 75 |
} |
| 76 |
|
| 77 |
void registerMatchers(ast_matchers::MatchFinder *Finder) override |
| 78 |
{ |
| 79 |
if (!m_shouldRunClazyChecks) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
Finder->addMatcher(translationUnitDecl().bind("tu"), this); |
| 84 |
const auto checks = CheckManager::instance()->availableChecks(ManualCheckLevel); |
| 85 |
for (const auto &availCheck : checks) { |
| 86 |
const std::string checkName = "clazy-" + availCheck.name; |
| 87 |
if (std::ranges::find(s_enabledChecks, checkName) == s_enabledChecks.end()) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
auto *check = availCheck.factory(); |
| 91 |
m_visitors.addCheck(availCheck.options, check); |
| 92 |
|
| 93 |
check->registerASTMatchers(*Finder); |
| 94 |
m_allChecks.emplace_back(check, availCheck.options); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void check(const ast_matchers::MatchFinder::MatchResult &Result) override |
| 99 |
{ |
| 100 |
if (!m_shouldRunClazyChecks) { |
| 101 |
return; |
| 102 |
} |
| 103 |
clazyContext->astContext = Result.Context; |
| 104 |
|
| 105 |
FullASTVisitor visitor(clazyContext, *this, m_visitors); |
| 106 |
auto translationUnit = const_cast<TranslationUnitDecl *>(Result.Nodes.getNodeAs<TranslationUnitDecl>("tu")); |
| 107 |
visitor.TraverseDecl(translationUnit); |
| 108 |
} |
| 109 |
|
| 110 |
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override |
| 111 |
{ |
| 112 |
if (!m_shouldRunClazyChecks) { |
| 113 |
return; |
| 114 |
} |
| 115 |
clazyContext = new ClazyContext(nullptr, PP->getSourceManager(), getLangOpts(), PP->getPreprocessorOpts(), "", "", "", {}, {}, emitDiagnostic, true); |
| 116 |
clazyContext->registerPreprocessorCallbacks(*PP); |
| 117 |
|
| 118 |
for (const auto &[check, options] : m_allChecks) { |
| 119 |
check->m_context = clazyContext; |
| 120 |
if (options & RegisteredCheck::Option_PreprocessorCallbacks) { |
| 121 |
check->enablePreProcessorCallbacks(*PP); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
const bool m_shouldRunClazyChecks; |
| 127 |
|
| 128 |
ClangTidyContext *clangTidyContext; |
| 129 |
ClazyContext *clazyContext = nullptr; |
| 130 |
std::vector<std::pair<CheckBase *, RegisteredCheck::Options>> m_allChecks; |
| 131 |
clazy::VisitHelper::Visitors m_visitors; |
| 132 |
// setting the engine fixes a weird crash, but we still run in a codepath where we do not know the check name in the end |
| 133 |
const ClazyContext::WarningReporter emitDiagnostic = // |
| 134 |
[this](const std::string &checkName, |
| 135 |
const clang::SourceLocation &loc, |
| 136 |
clang::DiagnosticIDs::Level level, |
| 137 |
std::string error, |
| 138 |
const std::vector<clang::FixItHint> &fixits) { |
| 139 |
clangTidyContext->diag("clazy-" + checkName, loc, error, level) << fixits; |
| 140 |
}; |
| 141 |
}; |
| 142 |
|
| 143 |
class ClazyModule : public ClangTidyModule |
| 144 |
{ |
| 145 |
public: |
| 146 |
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override |
| 147 |
{ |
| 148 |
for (const auto &check : CheckManager::instance()->availableChecks(CheckLevel::ManualCheckLevel)) { |
| 149 |
CheckFactories.registerCheck<ClazyCheck>("clazy-" + check.name); |
| 150 |
} |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
namespace clang::tidy |
| 155 |
{ |
| 156 |
static ClangTidyModuleRegistry::Add<ClazyModule> X("clazy-module", "Adds all Clazy checks to clang-tidy."); |
| 157 |
volatile int ClazyModuleAnchorSource = 0; |
| 158 |
} |