File indexing completed on 2024-05-12 05:40:57

0001 /*
0002     SPDX-FileCopyrightText: 2016 Sergio Martins <smartins@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "qt-macros.h"
0008 #include "ClazyContext.h"
0009 #include "PreProcessorVisitor.h"
0010 #include "clazy_stl.h"
0011 
0012 #include <clang/Basic/IdentifierTable.h>
0013 #include <clang/Lex/Token.h>
0014 #include <llvm/ADT/StringRef.h>
0015 
0016 using namespace clang;
0017 
0018 QtMacros::QtMacros(const std::string &name, ClazyContext *context)
0019     : CheckBase(name, context)
0020 {
0021     enablePreProcessorCallbacks();
0022     context->enablePreprocessorVisitor();
0023 }
0024 
0025 void QtMacros::VisitMacroDefined(const Token &MacroNameTok)
0026 {
0027     if (m_OSMacroExists) {
0028         return;
0029     }
0030 
0031     IdentifierInfo *ii = MacroNameTok.getIdentifierInfo();
0032     if (ii && clazy::startsWith(static_cast<std::string>(ii->getName()), "Q_OS_")) {
0033         m_OSMacroExists = true;
0034     }
0035 }
0036 
0037 void QtMacros::checkIfDef(const Token &macroNameTok, SourceLocation Loc)
0038 {
0039     IdentifierInfo *ii = macroNameTok.getIdentifierInfo();
0040     if (!ii) {
0041         return;
0042     }
0043 
0044     PreProcessorVisitor *preProcessorVisitor = m_context->preprocessorVisitor;
0045     if (preProcessorVisitor && preProcessorVisitor->qtVersion() < 51204 && ii->getName() == "Q_OS_WINDOWS") {
0046         // Q_OS_WINDOWS was introduced in 5.12.4
0047         emitWarning(Loc, "Q_OS_WINDOWS was only introduced in Qt 5.12.4, use Q_OS_WIN instead");
0048     } else if (!m_OSMacroExists && clazy::startsWith(static_cast<std::string>(ii->getName()), "Q_OS_")) {
0049         emitWarning(Loc, "Include qglobal.h before testing Q_OS_ macros");
0050     }
0051 }
0052 
0053 void QtMacros::VisitDefined(const Token &macroNameTok, const SourceRange &range)
0054 {
0055     if (!m_context->usingPreCompiledHeaders()) {
0056         checkIfDef(macroNameTok, range.getBegin());
0057     }
0058 }
0059 
0060 void QtMacros::VisitIfdef(SourceLocation loc, const Token &macroNameTok)
0061 {
0062     if (!m_context->usingPreCompiledHeaders()) {
0063         checkIfDef(macroNameTok, loc);
0064     }
0065 }