| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2023 Ahmad Samir <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "sanitize-inline-keyword.h" |
| 8 |
|
| 9 |
#include "ClazyContext.h" |
| 10 |
#include "FixItUtils.h" |
| 11 |
#include "QtUtils.h" |
| 12 |
#include "TypeUtils.h" |
| 13 |
#include "Utils.h" |
| 14 |
|
| 15 |
#include <clang/AST/AST.h> |
| 16 |
#include <clang/Lex/Token.h> |
| 17 |
|
| 18 |
using namespace std::string_literals; |
| 19 |
using namespace clang; |
| 20 |
|
| 21 |
void SanitizeInlineKeyword::VisitDecl(Decl *decl) |
| 22 |
{ |
| 23 |
auto *member = dyn_cast<CXXMethodDecl>(decl); |
| 24 |
if (!member) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
// The class this method belongs to |
| 29 |
auto *parentDecl = member->getParent(); |
| 30 |
// Only exported classes |
| 31 |
if (!parentDecl || parentDecl->getVisibility() == clang::HiddenVisibility) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
// constexpr methods are implicitly inline |
| 36 |
if (member->isConstexpr()) { |
| 37 |
return; |
| 38 |
} |
| 39 |
// Function templates are implicitly inline |
| 40 |
if (member->isTemplateDecl()) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Is this CXXMethodDecl* inside the class body? |
| 45 |
if (member->isOutOfLine()) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// Declared/defined in-class |
| 50 |
if (member->isThisDeclarationADefinition()) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
FunctionDecl *definition = member->getDefinition(); |
| 55 |
if (!definition) |
| 56 |
return; |
| 57 |
|
| 58 |
auto name = clazy::name(definition); |
| 59 |
|
| 60 |
auto *cxxDefinition = dyn_cast<CXXMethodDecl>(definition); |
| 61 |
if (!cxxDefinition) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
if (name.empty()) { |
| 66 |
name = clazy::name(cxxDefinition); // E.g. operator[] |
| 67 |
} |
| 68 |
|
| 69 |
if (name.empty()) { |
| 70 |
return; // Can't emit a warning without a method name |
| 71 |
} |
| 72 |
|
| 73 |
auto defHasInline = [](auto def) { |
| 74 |
return def->isInlineSpecified() && def->isThisDeclarationADefinition() && def->isOutOfLine(); |
| 75 |
}; |
| 76 |
|
| 77 |
if (!member->isInlineSpecified() && defHasInline(cxxDefinition)) { |
| 78 |
std::string msg = std::string(name) + "(): " |
| 79 |
+ "the 'inline' keyword is specified on the definition, but not the declaration. " |
| 80 |
"This could lead to hard-to-suppress warnings with some compilers (e.g. MinGW). " |
| 81 |
"The 'inline' keyword should be used for the declaration only."; |
| 82 |
|
| 83 |
SourceLocation loc = member->getBeginLoc(); |
| 84 |
std::vector<FixItHint> fixits{clazy::createInsertion(loc, "inline "s)}; |
| 85 |
|
| 86 |
SourceLocation def = cxxDefinition->getBeginLoc(); |
| 87 |
SourceLocation defEnd = cxxDefinition->getEndLoc(); |
| 88 |
Token tok; |
| 89 |
for (; def.isValid() && def != defEnd; def = Utils::locForNextToken(def, sm(), lo())) { |
| 90 |
if (!Lexer::getRawToken(def, tok, sm(), lo())) { // false means success! |
| 91 |
if (tok.is(tok::raw_identifier) && tok.getRawIdentifier() == "inline"s) { |
| 92 |
// Remove 'inline' from the definition |
| 93 |
fixits.emplace_back(clazy::createReplacement(def, std::string())); |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
emitWarning(loc, msg, fixits); |
| 100 |
} |
| 101 |
} |