| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "qenums.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "PreProcessorVisitor.h" |
| 10 |
#include "clazy_stl.h" |
| 11 |
|
| 12 |
#include <clang/Basic/IdentifierTable.h> |
| 13 |
#include <clang/Basic/SourceManager.h> |
| 14 |
#include <clang/Lex/Lexer.h> |
| 15 |
#include <clang/Lex/Token.h> |
| 16 |
#include <llvm/ADT/StringRef.h> |
| 17 |
|
| 18 |
using namespace clang; |
| 19 |
|
| 20 |
void QEnums::VisitMacroExpands(const Token &MacroNameTok, const SourceRange &range, const clang::MacroInfo *) |
| 21 |
{ |
| 22 |
const PreProcessorVisitor *preProcessorVisitor = m_context->preprocessorVisitor; |
| 23 |
if (!preProcessorVisitor || preProcessorVisitor->qtVersion() < 50500) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
IdentifierInfo *ii = MacroNameTok.getIdentifierInfo(); |
| 28 |
if (!ii || ii->getName() != "Q_ENUMS") { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
{ |
| 33 |
// Don't warn when importing enums of other classes, because Q_ENUM doesn't support it. |
| 34 |
// We simply check if :: is present because it's very cumbersome to to check for different classes when dealing with the pre-processor |
| 35 |
|
| 36 |
CharSourceRange crange = Lexer::getAsCharRange(range, sm(), lo()); |
| 37 |
std::string text = Lexer::getSourceText(crange, sm(), lo()).str(); |
| 38 |
if (clazy::contains(text, "::")) { |
| 39 |
return; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
if (range.getBegin().isMacroID()) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
if (sm().isInSystemHeader(range.getBegin())) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
emitWarning(range.getBegin(), "Use Q_ENUM instead of Q_ENUMS"); |
| 52 |
} |