Debian Codegraph @

src/ClazyVisitHelper.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2025 Alexander Lohnau <[email protected]>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6 #include "ClazyVisitHelper.h"
7 #include "AccessSpecifierManager.h"
8 #include "Utils.h"
9 #include "clang/AST/ParentMap.h"
10
11 namespace clazy::VisitHelper
12 {
13
14 using namespace clang;
15
16 bool VisitDecl(Decl *decl, ClazyContext *context, const Visitors &visitors)
17 {
18 if (AccessSpecifierManager *a = context->accessSpecifierManager) { // Needs to visit system headers too (qobject.h for example)
19 a->VisitDeclaration(decl);
20 }
21
22 const SourceLocation locStart = decl->getBeginLoc();
23 if (locStart.isInvalid() || context->sm.isInSystemHeader(locStart)) {
24 if (isa<TypedefNameDecl>(decl)) {
25 for (CheckBase *check : visitors.visitAllTypedefDecls) {
26 check->VisitDecl(decl);
27 }
28 }
29 return true;
30 }
31
32 const bool isFromIgnorableInclude = context->ignoresIncludedFiles() && !Utils::isMainFile(context->sm, locStart);
33
34 context->lastDecl = decl;
35
36 if (auto *fdecl = dyn_cast<FunctionDecl>(decl)) {
37 context->lastFunctionDecl = fdecl;
38 if (auto *mdecl = dyn_cast<CXXMethodDecl>(fdecl)) {
39 context->lastMethodDecl = mdecl;
40 }
41 }
42
43 for (CheckBase *check : visitors.visitDecls) {
44 if (!(isFromIgnorableInclude && check->canIgnoreIncludes())) {
45 check->VisitDecl(decl);
46 }
47 }
48
49 return true;
50 }
51
52 bool VisitStmt(Stmt *stmt, ClazyContext *context, const Visitors &visitors)
53 {
54 const SourceLocation locStart = stmt->getBeginLoc();
55 if (locStart.isInvalid() || context->sm.isInSystemHeader(locStart)) {
56 return true;
57 }
58
59 if (!context->parentMap) {
60 if (context->astContext->getDiagnostics().hasUnrecoverableErrorOccurred()) {
61 return false; // ParentMap sometimes crashes when there were errors. Doesn't like a botched AST.
62 }
63
64 context->parentMap = new ParentMap(stmt);
65 }
66
67 // clang::ParentMap takes a root statement, but there's no root statement in the AST, the root is a declaration
68 // So add to parent map each time we go into a different hierarchy
69 if (!context->parentMap->hasParent(stmt)) {
70 context->parentMap->addStmt(stmt);
71 }
72
73 const bool isFromIgnorableInclude = context->ignoresIncludedFiles() && !Utils::isMainFile(context->sm, locStart);
74 for (CheckBase *check : visitors.visitStmts) {
75 if (!(isFromIgnorableInclude && check->canIgnoreIncludes())) {
76 check->VisitStmt(stmt);
77 }
78 }
79
80 return true;
81 }
82
83 }