clang-tools 8.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 tidy {
18 namespace hicpp {
19
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
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) {
34 SourceLocation ASMLocation;
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
45 diag(ASMLocation, "do not use inline assembler in safety-critical code");
46 }
47
48 } // namespace hicpp
49 } // namespace tidy
50 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::tidy::matchers::AST_MATCHER
AST_MATCHER(BinaryOperator, isAssignmentOperator)
Definition: Matchers.h:20
NoAssemblerCheck.h
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18