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/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::hicpp {
16
17namespace {
18AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); }
19const ast_matchers::internal::VariadicDynCastAllOfMatcher<Decl,
20 FileScopeAsmDecl>
21 fileScopeAsmDecl;
22} // namespace
23
24void NoAssemblerCheck::registerMatchers(MatchFinder *Finder) {
25 Finder->addMatcher(NodeMatch: asmStmt().bind(ID: "asm-stmt"), Action: this);
26 Finder->addMatcher(NodeMatch: fileScopeAsmDecl().bind(ID: "asm-file-scope"), Action: this);
27 Finder->addMatcher(NodeMatch: varDecl(isAsm()).bind(ID: "asm-var"), Action: this);
28}
29
30void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) {
31 SourceLocation ASMLocation;
32 if (const auto *ASM = Result.Nodes.getNodeAs<AsmStmt>(ID: "asm-stmt"))
33 ASMLocation = ASM->getAsmLoc();
34 else if (const auto *ASM =
35 Result.Nodes.getNodeAs<FileScopeAsmDecl>(ID: "asm-file-scope"))
36 ASMLocation = ASM->getAsmLoc();
37 else if (const auto *ASM = Result.Nodes.getNodeAs<VarDecl>(ID: "asm-var"))
38 ASMLocation = ASM->getLocation();
39 else
40 llvm_unreachable("Unhandled case in matcher.");
41
42 diag(Loc: ASMLocation, Description: "do not use inline assembler in safety-critical code");
43}
44
45} // namespace clang::tidy::hicpp
46

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