blob: 06969a873f443c4e1afe4f0a1d07b8749326e02e [file] [log] [blame]
Jonathan Coe3032d3c2017-02-06 22:57:14 +00001//===--- 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 Coe3032d3c2017-02-06 22:57:14 +000013
14using namespace clang::ast_matchers;
15
16namespace clang {
Jonathan Coe3032d3c2017-02-06 22:57:14 +000017namespace tidy {
Aaron Ballmandbdbabf2017-03-19 17:23:23 +000018namespace hicpp {
Jonathan Coe3032d3c2017-02-06 22:57:14 +000019
Benjamin Kramerf8c99292018-02-18 19:02:35 +000020namespace {
21AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); }
22const ast_matchers::internal::VariadicDynCastAllOfMatcher<Decl,
23 FileScopeAsmDecl>
24 fileScopeAsmDecl;
25} // namespace
26
Jonathan Coe3032d3c2017-02-06 22:57:14 +000027void 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
33void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) {
Jonathan Coe5f1a2c22017-02-07 06:19:38 +000034 SourceLocation ASMLocation;
Jonathan Coe3032d3c2017-02-06 22:57:14 +000035 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 Coe5f1a2c22017-02-07 06:19:38 +000045 diag(ASMLocation, "do not use inline assembler in safety-critical code");
Jonathan Coe3032d3c2017-02-06 22:57:14 +000046}
47
Aaron Ballmandbdbabf2017-03-19 17:23:23 +000048} // namespace hicpp
Jonathan Coe3032d3c2017-02-06 22:57:14 +000049} // namespace tidy
50} // namespace clang