Debian Codegraph @

src/checks/manuallevel/qt-keyword-emit.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2018 Sergio Martins <[email protected]>
3 SPDX-FileCopyrightText: 2023 Ahmad Samir <[email protected]>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8 #include "qt-keyword-emit.h"
9 #include "ClazyContext.h"
10 #include "FixItUtils.h"
11 #include "PreProcessorVisitor.h"
12 #include "clazy_stl.h"
13
14 #include <clang/Basic/Diagnostic.h>
15 #include <clang/Basic/IdentifierTable.h>
16 #include <clang/Basic/SourceManager.h>
17 #include <clang/Lex/MacroInfo.h>
18 #include <clang/Lex/Token.h>
19 #include <llvm/ADT/StringRef.h>
20
21 #include <string_view>
22 #include <vector>
23
24 using namespace clang;
25
26 void QtKeywordEmit::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 if (auto ppvisitor = m_context->preprocessorVisitor) {
33 // Save some CPU cycles. No point in running if QT_NO_KEYWORDS
34 if (ppvisitor->isQT_NO_KEYWORDS())
35 return;
36 }
37
38 static const std::string emitKeyword{"emit"};
39 if (emitKeyword != std::string_view{ii->getName()})
40 return;
41
42 // Make sure the macro is Qt's. It must be defined in Qt's headers, not 3rdparty
43 const std::string qtheader{sm().getFilename(sm().getSpellingLoc(minfo->getDefinitionLoc()))};
44 // qobjectdefs.h in Qt5 or qtmetamacros.h in Qt6
45 if (!clazy::endsWithAny(qtheader, {"qobjectdefs.h", "qtmetamacros.h"}))
46 return;
47
48 std::vector<FixItHint> fixits{clazy::createReplacement(range, "Q_EMIT")};
49 emitWarning(range.getBegin(), "Using Qt (" + emitKeyword + ") keyword", fixits);
50 }