| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2018 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "ifndef-define-typo.h" |
| 8 |
#include "QtUtils.h" |
| 9 |
#include "Utils.h" |
| 10 |
#include "levenshteindistance.h" |
| 11 |
|
| 12 |
#include <clang/AST/AST.h> |
| 13 |
|
| 14 |
using namespace clang; |
| 15 |
|
| 16 |
void IfndefDefineTypo::VisitMacroDefined(const Token ¯oNameTok) |
| 17 |
{ |
| 18 |
if (!m_lastIfndef.empty()) { |
| 19 |
if (IdentifierInfo *ii = macroNameTok.getIdentifierInfo()) { |
| 20 |
maybeWarn(ii->getName().str(), macroNameTok.getLocation()); |
| 21 |
} |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
void IfndefDefineTypo::VisitDefined(const Token ¯oNameTok, const SourceRange &) |
| 26 |
{ |
| 27 |
if (!m_lastIfndef.empty()) { |
| 28 |
if (IdentifierInfo *ii = macroNameTok.getIdentifierInfo()) { |
| 29 |
maybeWarn(ii->getName().str(), macroNameTok.getLocation()); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
void IfndefDefineTypo::VisitIfdef(SourceLocation, const Token &) |
| 35 |
{ |
| 36 |
m_lastIfndef.clear(); |
| 37 |
} |
| 38 |
|
| 39 |
void IfndefDefineTypo::VisitIfndef(SourceLocation, const Token ¯oNameTok) |
| 40 |
{ |
| 41 |
if (IdentifierInfo *ii = macroNameTok.getIdentifierInfo()) { |
| 42 |
m_lastIfndef = ii->getName().str(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
void IfndefDefineTypo::VisitIf(SourceLocation, SourceRange, PPCallbacks::ConditionValueKind) |
| 47 |
{ |
| 48 |
m_lastIfndef.clear(); |
| 49 |
} |
| 50 |
|
| 51 |
void IfndefDefineTypo::VisitElif(SourceLocation, SourceRange, PPCallbacks::ConditionValueKind, SourceLocation) |
| 52 |
{ |
| 53 |
m_lastIfndef.clear(); |
| 54 |
} |
| 55 |
|
| 56 |
void IfndefDefineTypo::VisitElse(SourceLocation, SourceLocation) |
| 57 |
{ |
| 58 |
m_lastIfndef.clear(); |
| 59 |
} |
| 60 |
|
| 61 |
void IfndefDefineTypo::VisitEndif(SourceLocation, SourceLocation) |
| 62 |
{ |
| 63 |
m_lastIfndef.clear(); |
| 64 |
} |
| 65 |
|
| 66 |
void IfndefDefineTypo::maybeWarn(const std::string &define, SourceLocation loc) |
| 67 |
{ |
| 68 |
if (m_lastIfndef == "Q_CONSTRUCTOR_FUNCTION") { // Transform into a list if more false-positives need to be added |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if (define == m_lastIfndef) { |
| 73 |
m_lastIfndef.clear(); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if (define.length() < 4) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
const int levDistance = levenshtein_distance(define, m_lastIfndef); |
| 82 |
if (levDistance < 3) { |
| 83 |
emitWarning(loc, std::string("Possible typo in define. ") + m_lastIfndef + " vs " + define); |
| 84 |
} |
| 85 |
} |