Debian Codegraph @

src/checks/level1/qproperty-without-notify.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "qproperty-without-notify.h"
8 #include "clazy_stl.h"
9
10 #include <clang/Basic/IdentifierTable.h>
11 #include <clang/Basic/SourceManager.h>
12 #include <clang/Lex/Lexer.h>
13 #include <clang/Lex/Token.h>
14 #include <llvm/ADT/StringRef.h>
15
16 #include <vector>
17
18 using namespace clang;
19
20 void QPropertyWithoutNotify::VisitMacroExpands(const clang::Token &MacroNameTok, const clang::SourceRange &range, const MacroInfo *)
21 {
22 IdentifierInfo *ii = MacroNameTok.getIdentifierInfo();
23 if (!ii) {
24 return;
25 }
26
27 if (ii->getName() == "Q_GADGET") {
28 m_lastIsGadget = true;
29 return;
30 }
31
32 if (ii->getName() == "Q_OBJECT") {
33 m_lastIsGadget = false;
34 return;
35 }
36
37 // Gadgets can't have NOTIFY
38 if (m_lastIsGadget || ii->getName() != "Q_PROPERTY") {
39 return;
40 }
41
42 if (sm().isInSystemHeader(range.getBegin())) {
43 return;
44 }
45 CharSourceRange crange = Lexer::getAsCharRange(range, sm(), lo());
46
47 std::string text = Lexer::getSourceText(crange, sm(), lo()).str();
48 if (text.empty()) {
49 // If the text is empty, it is more likely there is an error
50 // in parsing than an empty Q_PROPERTY macro call (which would
51 // be a moc error anyhow).
52 return;
53 }
54
55 if (text.back() == ')') {
56 text.pop_back();
57 }
58
59 std::vector<std::string> split = clazy::splitString(text, ' ');
60
61 bool found_read = false;
62 bool found_constant = false;
63 bool found_notify = false;
64 for (std::string &token : split) {
65 clazy::rtrim(/*by-ref*/ token);
66 if (!found_read && token == "READ") {
67 found_read = true;
68 continue;
69 }
70
71 if (!found_constant && token == "CONSTANT") {
72 found_constant = true;
73 continue;
74 }
75
76 if (!found_notify && token == "NOTIFY") {
77 found_notify = true;
78 continue;
79 }
80 }
81
82 if (!found_read || (found_notify || found_constant)) {
83 return;
84 }
85
86 emitWarning(range.getBegin(), "Q_PROPERTY should have either NOTIFY or CONSTANT");
87 }