| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "qt-macros.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "PreProcessorVisitor.h" |
| 10 |
#include "clazy_stl.h" |
| 11 |
|
| 12 |
#include <clang/Basic/IdentifierTable.h> |
| 13 |
#include <clang/Lex/Token.h> |
| 14 |
#include <llvm/ADT/StringRef.h> |
| 15 |
|
| 16 |
using namespace clang; |
| 17 |
|
| 18 |
void QtMacros::VisitMacroDefined(const Token &MacroNameTok) |
| 19 |
{ |
| 20 |
if (m_OSMacroExists) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
IdentifierInfo *ii = MacroNameTok.getIdentifierInfo(); |
| 25 |
if (ii && clazy::startsWith(ii->getName(), "Q_OS_")) { |
| 26 |
m_OSMacroExists = true; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
void QtMacros::checkIfDef(const Token ¯oNameTok, SourceLocation Loc) |
| 31 |
{ |
| 32 |
IdentifierInfo *ii = macroNameTok.getIdentifierInfo(); |
| 33 |
if (!ii) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
const PreProcessorVisitor *preProcessorVisitor = m_context->preprocessorVisitor; |
| 38 |
if (preProcessorVisitor && preProcessorVisitor->qtVersion() < 51204 && ii->getName() == "Q_OS_WINDOWS") { |
| 39 |
// Q_OS_WINDOWS was introduced in 5.12.4 |
| 40 |
emitWarning(Loc, "Q_OS_WINDOWS was only introduced in Qt 5.12.4, use Q_OS_WIN instead"); |
| 41 |
} else if (!m_OSMacroExists && clazy::startsWith(ii->getName(), "Q_OS_")) { |
| 42 |
emitWarning(Loc, "Include qglobal.h before testing Q_OS_ macros"); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
void QtMacros::VisitDefined(const Token ¯oNameTok, const SourceRange &range) |
| 47 |
{ |
| 48 |
if (!m_context->usingPreCompiledHeaders()) { |
| 49 |
checkIfDef(macroNameTok, range.getBegin()); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
void QtMacros::VisitIfdef(SourceLocation loc, const Token ¯oNameTok) |
| 54 |
{ |
| 55 |
if (!m_context->usingPreCompiledHeaders()) { |
| 56 |
checkIfDef(macroNameTok, loc); |
| 57 |
} |
| 58 |
} |