Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 1 | //===--- NoAssemblerCheck.cpp - clang-tidy---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "NoAssemblerCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang { |
Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 17 | namespace tidy { |
Aaron Ballman | dbdbabf | 2017-03-19 17:23:23 +0000 | [diff] [blame] | 18 | namespace hicpp { |
Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 19 | |
Benjamin Kramer | f8c9929 | 2018-02-18 19:02:35 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); } |
| 22 | const ast_matchers::internal::VariadicDynCastAllOfMatcher<Decl, |
| 23 | FileScopeAsmDecl> |
| 24 | fileScopeAsmDecl; |
| 25 | } // namespace |
| 26 | |
Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 27 | void NoAssemblerCheck::registerMatchers(MatchFinder *Finder) { |
| 28 | Finder->addMatcher(asmStmt().bind("asm-stmt"), this); |
| 29 | Finder->addMatcher(fileScopeAsmDecl().bind("asm-file-scope"), this); |
| 30 | Finder->addMatcher(varDecl(isAsm()).bind("asm-var"), this); |
| 31 | } |
| 32 | |
| 33 | void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) { |
Jonathan Coe | 5f1a2c2 | 2017-02-07 06:19:38 +0000 | [diff] [blame] | 34 | SourceLocation ASMLocation; |
Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 35 | if (const auto *ASM = Result.Nodes.getNodeAs<AsmStmt>("asm-stmt")) |
| 36 | ASMLocation = ASM->getAsmLoc(); |
| 37 | else if (const auto *ASM = |
| 38 | Result.Nodes.getNodeAs<FileScopeAsmDecl>("asm-file-scope")) |
| 39 | ASMLocation = ASM->getAsmLoc(); |
| 40 | else if (const auto *ASM = Result.Nodes.getNodeAs<VarDecl>("asm-var")) |
| 41 | ASMLocation = ASM->getLocation(); |
| 42 | else |
| 43 | llvm_unreachable("Unhandled case in matcher."); |
| 44 | |
Jonathan Coe | 5f1a2c2 | 2017-02-07 06:19:38 +0000 | [diff] [blame] | 45 | diag(ASMLocation, "do not use inline assembler in safety-critical code"); |
Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Aaron Ballman | dbdbabf | 2017-03-19 17:23:23 +0000 | [diff] [blame] | 48 | } // namespace hicpp |
Jonathan Coe | 3032d3c | 2017-02-06 22:57:14 +0000 | [diff] [blame] | 49 | } // namespace tidy |
| 50 | } // namespace clang |