10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Basic/TargetInfo.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/Support/ManagedStatic.h"
15#include "llvm/Support/Regex.h"
16#include "llvm/TargetParser/Triple.h"
28 bool IsVector =
Node.getReturnType()->isVectorType();
29 for (
const ParmVarDecl *
Parm :
Node.parameters()) {
31 if (
Type->isPointerType())
33 if (
Type->isVectorType())
42 if (!
Name.consume_front(
"vec_"))
45 return llvm::StringSwitch<StringRef>(
Name)
47 .Case(
"max",
"$std::max")
48 .Case(
"min",
"$std::min")
50 .Case(
"add",
"operator+ on $simd objects")
51 .Case(
"sub",
"operator- on $simd objects")
52 .Case(
"mul",
"operator* on $simd objects")
57 if (!(
Name.consume_front(
"_mm_") ||
Name.consume_front(
"_mm256_") ||
58 Name.consume_front(
"_mm512_")))
62 if (
Name.starts_with(
"max_"))
64 if (
Name.starts_with(
"min_"))
68 if (
Name.starts_with(
"add_"))
69 return "operator+ on $simd objects";
70 if (
Name.starts_with(
"sub_"))
71 return "operator- on $simd objects";
72 if (
Name.starts_with(
"mul_"))
73 return "operator* on $simd objects";
81 Suggest(Options.get(
"Suggest", false)) {}
92 Std =
getLangOpts().CPlusPlus20 ?
"std" :
"std::experimental";
94 Finder->addMatcher(callExpr(callee(functionDecl(
95 matchesName(
"^::(_mm_|_mm256_|_mm512_|vec_)"),
97 unless(isExpansionInSystemHeader()))
103 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
104 assert(Call !=
nullptr);
105 const FunctionDecl *Callee = Call->getDirectCallee();
109 StringRef Old = Callee->getName();
111 llvm::Triple::ArchType Arch =
112 Result.Context->getTargetInfo().getTriple().getArch();
119 case llvm::Triple::ppc:
120 case llvm::Triple::ppc64:
121 case llvm::Triple::ppc64le:
124 case llvm::Triple::x86:
125 case llvm::Triple::x86_64:
135 static const llvm::Regex StdRegex(
"\\$std"), SimdRegex(
"\\$simd");
136 diag(Call->getExprLoc(),
"'%0' can be replaced by %1")
138 << SimdRegex.sub(SmallString<32>({Std,
"::simd"}),
139 StdRegex.sub(Std, New));
141 diag(Call->getExprLoc(),
"'%0' is a non-portable %1 intrinsic function")
142 << Old << llvm::Triple::getArchTypeName(Arch);
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
SIMDIntrinsicsCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
static StringRef trySuggestX86(StringRef Name)
static StringRef trySuggestPpc(StringRef Name)
llvm::StringMap< ClangTidyValue > OptionMap