NoAssemblerCheck.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #include "NoAssemblerCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::hicpp {
  13. namespace {
  14. AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); }
  15. const ast_matchers::internal::VariadicDynCastAllOfMatcher<Decl,
  16. FileScopeAsmDecl>
  17. fileScopeAsmDecl;
  18. } // namespace
  19. void NoAssemblerCheck::registerMatchers(MatchFinder *Finder) {
  20. Finder->addMatcher(asmStmt().bind("asm-stmt"), this);
  21. Finder->addMatcher(fileScopeAsmDecl().bind("asm-file-scope"), this);
  22. Finder->addMatcher(varDecl(isAsm()).bind("asm-var"), this);
  23. }
  24. void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) {
  25. SourceLocation ASMLocation;
  26. if (const auto *ASM = Result.Nodes.getNodeAs<AsmStmt>("asm-stmt"))
  27. ASMLocation = ASM->getAsmLoc();
  28. else if (const auto *ASM =
  29. Result.Nodes.getNodeAs<FileScopeAsmDecl>("asm-file-scope"))
  30. ASMLocation = ASM->getAsmLoc();
  31. else if (const auto *ASM = Result.Nodes.getNodeAs<VarDecl>("asm-var"))
  32. ASMLocation = ASM->getLocation();
  33. else
  34. llvm_unreachable("Unhandled case in matcher.");
  35. diag(ASMLocation, "do not use inline assembler in safety-critical code");
  36. }
  37. } // namespace clang::tidy::hicpp