Debian Codegraph @

src/checks/manuallevel/qt-keywords.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2018 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "qt-keywords.h"
8 #include "ClazyContext.h"
9 #include "FixItUtils.h"
10 #include "PreProcessorVisitor.h"
11 #include "clazy_stl.h"
12
13 #include <clang/Basic/Diagnostic.h>
14 #include <clang/Basic/IdentifierTable.h>
15 #include <clang/Basic/SourceManager.h>
16 #include <clang/Lex/MacroInfo.h>
17 #include <clang/Lex/Token.h>
18 #include <llvm/ADT/StringRef.h>
19
20 #include <algorithm>
21 #include <cctype>
22 #include <vector>
23
24 using namespace clang;
25
26 void QtKeywords::VisitMacroExpands(const Token &macroNameTok, const SourceRange &range, const clang::MacroInfo *minfo)
27 {
28 IdentifierInfo *ii = macroNameTok.getIdentifierInfo();
29 if (!ii || !minfo) {
30 return;
31 }
32
33 if (auto *ppvisitor = m_context->preprocessorVisitor) {
34 // Save some CPU cycles. No point in running if QT_NO_KEYWORDS
35 if (ppvisitor->isQT_NO_KEYWORDS()) {
36 return;
37 }
38 }
39
40 static const std::vector<StringRef> keywords = {"foreach", "signals", "slots", "emit"};
41 std::string name = ii->getName().str();
42 if (!clazy::contains(keywords, name)) {
43 return;
44 }
45
46 // Make sure the macro is Qt's. It must be defined in Qt's headers, not 3rdparty
47 std::string qtheader = sm().getFilename(sm().getSpellingLoc(minfo->getDefinitionLoc())).str();
48 if (!clazy::endsWithAny(qtheader, {"qglobal.h", "qobjectdefs.h", "qtmetamacros.h", "qforeach.h"})) {
49 return;
50 }
51
52 std::vector<FixItHint> fixits;
53 std::string replacement = "Q_" + name;
54 std::transform(replacement.begin(), replacement.end(), replacement.begin(), ::toupper);
55 fixits.push_back(clazy::createReplacement(range, replacement));
56
57 emitWarning(range.getBegin(), "Using a Qt keyword (" + ii->getName().str() + ")", fixits);
58 }