clang-tools  5.0.0
NoAssemblerCheck.cpp
Go to the documentation of this file.
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"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace ast_matchers {
18 AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); }
19 const internal::VariadicDynCastAllOfMatcher<Decl, FileScopeAsmDecl>
21 }
22 }
23 
24 namespace clang {
25 namespace tidy {
26 namespace hicpp {
27 
28 void NoAssemblerCheck::registerMatchers(MatchFinder *Finder) {
29  Finder->addMatcher(asmStmt().bind("asm-stmt"), this);
30  Finder->addMatcher(fileScopeAsmDecl().bind("asm-file-scope"), this);
31  Finder->addMatcher(varDecl(isAsm()).bind("asm-var"), this);
32 }
33 
34 void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) {
35  SourceLocation ASMLocation;
36  if (const auto *ASM = Result.Nodes.getNodeAs<AsmStmt>("asm-stmt"))
37  ASMLocation = ASM->getAsmLoc();
38  else if (const auto *ASM =
39  Result.Nodes.getNodeAs<FileScopeAsmDecl>("asm-file-scope"))
40  ASMLocation = ASM->getAsmLoc();
41  else if (const auto *ASM = Result.Nodes.getNodeAs<VarDecl>("asm-var"))
42  ASMLocation = ASM->getLocation();
43  else
44  llvm_unreachable("Unhandled case in matcher.");
45 
46  diag(ASMLocation, "do not use inline assembler in safety-critical code");
47 }
48 
49 } // namespace hicpp
50 } // namespace tidy
51 } // namespace clang
const internal::VariadicDynCastAllOfMatcher< Decl, FileScopeAsmDecl > fileScopeAsmDecl
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:275
AST_MATCHER(VarDecl, isAsm)