| 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 | |
| 12 | using namespace clang::ast_matchers; |
| 13 | |
| 14 | namespace clang::tidy::hicpp { |
| 15 | |
| 16 | namespace { |
| 17 | AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); } |
| 18 | const ast_matchers::internal::VariadicDynCastAllOfMatcher<Decl, |
| 19 | FileScopeAsmDecl> |
| 20 | fileScopeAsmDecl; // NOLINT(readability-identifier-*) preserve clang style |
| 21 | } // namespace |
| 22 | |
| 23 | void 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 | |
| 29 | void 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 |
