-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathUseFreeFunctionVariantsCheck.cpp
79 lines (67 loc) · 3.07 KB
/
UseFreeFunctionVariantsCheck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//===--- UseFreeFunctionVariantsCheck.cpp - clang-tidy --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "UseFreeFunctionVariantsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include <iostream>
using namespace clang::ast_matchers;
namespace clang::tidy::misc {
void UseFreeFunctionVariantsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(cxxMemberCallExpr(on(expr().bind("base")),
callee(cxxMethodDecl(hasAnyName("isa", "dyn_cast", "cast", "dyn_cast_or_null", "isa_and_nonnull")))
).bind("castCall"),
this);
}
// TODO(askrebko): Add checker for types wrapped in pointers and optional
void UseFreeFunctionVariantsCheck::check(
const MatchFinder::MatchResult &Result) {
const auto* CallExpr = Result.Nodes.getNodeAs<CXXMemberCallExpr>("castCall");
const auto* BaseExpr = Result.Nodes.getNodeAs<Expr>("base");
if (!CallExpr || !BaseExpr) {
return;
}
SourceManager &SM = *Result.SourceManager;
LangOptions LangOpts = getLangOpts();
std::string BaseStr = Lexer::getSourceText(
CharSourceRange::getTokenRange(BaseExpr->getSourceRange()), SM, LangOpts).str();
const auto* MethodDecl = CallExpr->getMethodDecl();
if (!MethodDecl->getTemplateSpecializationArgs()) {
return;
}
std::string TemplateArgStr;
for (const auto& Arg : MethodDecl->getTemplateSpecializationArgs()->asArray()) {
if (Arg.getKind() == TemplateArgument::ArgKind::Type) {
PrintingPolicy Policy(LangOpts);
Policy.adjustForCPlusPlus();
if (!TemplateArgStr.empty()) {
TemplateArgStr += ", ";
}
TemplateArgStr += Arg.getAsType().getAsString(Policy);
} else if (Arg.getKind() == TemplateArgument::ArgKind::Pack) {
for (const auto& PackArg : Arg.pack_elements()) {
PrintingPolicy Policy(LangOpts);
Policy.adjustForCPlusPlus();
if (!TemplateArgStr.empty()) {
TemplateArgStr += ", ";
}
TemplateArgStr += PackArg.getAsType().getAsString(Policy);
}
} else {
std::cout << "Unsupported template argument kind: " << Arg.getKind() << std::endl;
return;
}
}
std::string FuncName = MethodDecl->getNameAsString();
std::string Replacement = "mlir::" + FuncName + "<" + TemplateArgStr + ">(" + BaseStr + ")";
diag(CallExpr->getBeginLoc(), "Replace 'type.isa<T>()' with 'mlir::isa<T>(type)'")
<< FixItHint::CreateReplacement(CallExpr->getSourceRange(), Replacement);
}
} // namespace clang::tidy::misc