1//===--- NoAssemblerCheck.cpp - clang-tidy---------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "NoAssemblerCheck.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11
12using namespace clang::ast_matchers;
13
14namespace clang::tidy::hicpp {
15
16namespace {
17AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); }
18const ast_matchers::internal::VariadicDynCastAllOfMatcher<Decl,
19 FileScopeAsmDecl>
20 fileScopeAsmDecl; // NOLINT(readability-identifier-*) preserve clang style
21} // namespace
22
23void NoAssemblerCheck::registerMatchers(MatchFinder *Finder) {
24 Finder->addMatcher(NodeMatch: asmStmt().bind(ID: "asm-stmt"), Action: this);
25 Finder->addMatcher(NodeMatch: fileScopeAsmDecl().bind(ID: "asm-file-scope"), Action: this);
26 Finder->addMatcher(NodeMatch: varDecl(isAsm()).bind(ID: "asm-var"), Action: this);
27}
28
29void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) {
30 SourceLocation ASMLocation;
31 if (const auto *ASM = Result.Nodes.getNodeAs<AsmStmt>(ID: "asm-stmt"))
32 ASMLocation = ASM->getAsmLoc();
33 else if (const auto *ASM =
34 Result.Nodes.getNodeAs<FileScopeAsmDecl>(ID: "asm-file-scope"))
35 ASMLocation = ASM->getAsmLoc();
36 else if (const auto *ASM = Result.Nodes.getNodeAs<VarDecl>(ID: "asm-var"))
37 ASMLocation = ASM->getLocation();
38 else
39 llvm_unreachable("Unhandled case in matcher.");
40
41 diag(Loc: ASMLocation, Description: "do not use inline assembler in safety-critical code");
42}
43
44} // namespace clang::tidy::hicpp
45

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang-tools-extra/clang-tidy/hicpp/NoAssemblerCheck.cpp