Debian Codegraph @

src/AccessSpecifierManager.h

raw
1 /*
2 SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]>
3 SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8 #ifndef CLAZY_ACCESS_SPECIFIER_MANAGER_H
9 #define CLAZY_ACCESS_SPECIFIER_MANAGER_H
10
11 #include "checkbase.h"
12
13 #include <clang/Basic/SourceLocation.h>
14 #include <clang/Basic/Specifiers.h>
15 #include <clang/Frontend/CompilerInstance.h>
16 #include <llvm/ADT/StringRef.h>
17
18 #include <unordered_map>
19 #include <vector>
20
21 /*
22 clang supports "public", "private" and "protected"
23 This is a small wrapper to have support for "public slots", "signals", etc. so you can easily
24 query if a method is a signal or slot
25
26 This works in two steps, first the pre-processor kicks in and we use a PPCallbacks class to catch
27 Q_SLOTS and Q_SIGNALS, then the normal access specifiers come in from the AST, inside VisitDeclaration().
28
29 After that we just need to merge the two lists, and sort by source location. All the info is kept
30 inside m_specifiersMap, which is indexed by the class definition.
31 */
32
33 namespace clang
34 {
35 class SourceManager;
36 class LangOptions;
37 class Decl;
38 class CXXRecordDecl;
39 class SourceLocation;
40 class CXXMethodDecl;
41 class CompilerInstance;
42 }
43
44 class AccessSpecifierPreprocessorCallbacks;
45
46 enum QtAccessSpecifierType { QtAccessSpecifier_None, QtAccessSpecifier_Unknown, QtAccessSpecifier_Slot, QtAccessSpecifier_Signal, QtAccessSpecifier_Invokable };
47
48 struct ClazyAccessSpecifier {
49 clang::SourceLocation loc;
50 clang::AccessSpecifier accessSpecifier;
51 QtAccessSpecifierType qtAccessSpecifier;
52 };
53
54 using ClazySpecifierList = std::vector<ClazyAccessSpecifier>;
55
56 class AccessSpecifierManager
57 {
58 public:
59 explicit AccessSpecifierManager(clang::Preprocessor &pi, bool exportFixesEnabled);
60 void VisitDeclaration(clang::Decl *decl);
61
62 /**
63 * Returns if a method is a signal, a slot, or neither.
64 */
65 QtAccessSpecifierType qtAccessSpecifierType(const clang::CXXMethodDecl *) const;
66
67 /**
68 * Returns if a method is scriptable (Q_SCRIPTABLE)
69 */
70 bool isScriptable(const clang::CXXMethodDecl *) const;
71
72 /**
73 * Returns a string representations of a Qt Access Specifier Type
74 */
75 llvm::StringRef qtAccessSpecifierTypeStr(QtAccessSpecifierType) const;
76
77 clang::SourceLocation firstLocationOfSection(clang::AccessSpecifier specifier, clang::CXXRecordDecl *decl) const;
78 std::string qtNamespace() const;
79
80 private:
81 ClazySpecifierList &entryForClassDefinition(clang::CXXRecordDecl *);
82 const clang::CXXRecordDecl *classDefinitionForLoc(clang::SourceLocation loc) const;
83 const clang::SourceManager &m_sm;
84 const clang::LangOptions m_lo;
85 std::unordered_map<const clang::CXXRecordDecl *, ClazySpecifierList> m_specifiersMap;
86 AccessSpecifierPreprocessorCallbacks *const m_preprocessorCallbacks;
87 const bool m_fixitsEnabled;
88 bool m_visitsNonQObjects = false;
89 };
90
91 #endif