llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Arjun Parmar (akparmar004)

<details>
<summary>Changes</summary>

Part of #<!-- -->183462.

closes #<!-- -->183466.

---
Full diff: https://github.com/llvm/llvm-project/pull/184030.diff


9 Files Affected:

- (modified) clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp (+3-2) 
- (modified) clang-tools-extra/clang-tidy/portability/CMakeLists.txt (+1) 
- (added) clang-tools-extra/clang-tidy/portability/NoAssemblerCheck.cpp (+37) 
- (added) clang-tools-extra/clang-tidy/portability/NoAssemblerCheck.h (+30) 
- (modified) clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp 
(+3) 
- (modified) clang-tools-extra/docs/clang-tidy/checks/hicpp/no-assembler.rst 
(+3-1) 
- (modified) clang-tools-extra/docs/clang-tidy/checks/list.rst (+2-1) 
- (added) clang-tools-extra/docs/clang-tidy/checks/portability/no-assembler.rst 
() 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/portability/no-assembler.cpp (+12) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp 
b/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp
index 2e0e64fbcd2a1..a4601d9cdde9f 100644
--- a/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp
@@ -30,6 +30,7 @@
 #include "../modernize/UseOverrideCheck.h"
 #include "../performance/MoveConstArgCheck.h"
 #include "../performance/NoexceptMoveConstructorCheck.h"
+#include "../portability/NoAssemblerCheck.h"
 #include "../readability/BracesAroundStatementsCheck.h"
 #include "../readability/FunctionSizeCheck.h"
 #include "../readability/NamedParameterCheck.h"
@@ -37,7 +38,6 @@
 #include "ExceptionBaseclassCheck.h"
 #include "IgnoredRemoveResultCheck.h"
 #include "MultiwayPathsCoveredCheck.h"
-#include "NoAssemblerCheck.h"
 #include "SignedBitwiseCheck.h"
 
 namespace clang::tidy {
@@ -81,7 +81,8 @@ class HICPPModule : public ClangTidyModule {
     CheckFactories
         .registerCheck<cppcoreguidelines::ProBoundsArrayToPointerDecayCheck>(
             "hicpp-no-array-decay");
-    CheckFactories.registerCheck<NoAssemblerCheck>("hicpp-no-assembler");
+    CheckFactories.registerCheck<portability::NoAssemblerCheck>(
+        "hicpp-no-assembler");
     CheckFactories.registerCheck<cppcoreguidelines::NoMallocCheck>(
         "hicpp-no-malloc");
     CheckFactories
diff --git a/clang-tools-extra/clang-tidy/portability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/portability/CMakeLists.txt
index 73d74a550afc0..170fedf52130e 100644
--- a/clang-tools-extra/clang-tidy/portability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/portability/CMakeLists.txt
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_clang_library(clangTidyPortabilityModule STATIC
   AvoidPragmaOnceCheck.cpp
+  NoAssemblerCheck.cpp
   PortabilityTidyModule.cpp
   RestrictSystemIncludesCheck.cpp
   SIMDIntrinsicsCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/portability/NoAssemblerCheck.cpp 
b/clang-tools-extra/clang-tidy/portability/NoAssemblerCheck.cpp
new file mode 100644
index 0000000000000..d9a20b97b2332
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/NoAssemblerCheck.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "NoAssemblerCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+void NoAssemblerCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(asmStmt().bind("asm-stmt"), this);
+  Finder->addMatcher(fileScopeAsmDecl().bind("asm-file-scope"), this);
+  Finder->addMatcher(varDecl(hasAttr(attr::AsmLabel)).bind("asm-var"), this);
+}
+
+void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) {
+  SourceLocation ASMLocation;
+  if (const auto *ASM = Result.Nodes.getNodeAs<AsmStmt>("asm-stmt"))
+    ASMLocation = ASM->getAsmLoc();
+  else if (const auto *ASM =
+               Result.Nodes.getNodeAs<FileScopeAsmDecl>("asm-file-scope"))
+    ASMLocation = ASM->getAsmLoc();
+  else if (const auto *ASM = Result.Nodes.getNodeAs<VarDecl>("asm-var"))
+    ASMLocation = ASM->getLocation();
+  else
+    llvm_unreachable("Unhandled case in matcher.");
+
+  diag(ASMLocation, "do not use inline assembler in safety-critical code");
+}
+
+} // namespace clang::tidy::portability
diff --git a/clang-tools-extra/clang-tidy/portability/NoAssemblerCheck.h 
b/clang-tools-extra/clang-tidy/portability/NoAssemblerCheck.h
new file mode 100644
index 0000000000000..15d646fd97af3
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/portability/NoAssemblerCheck.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_NOASSEMBLERCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_NOASSEMBLERCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::hicpp {
+
+/// Find assembler statements. No fix is offered.
+///
+/// For the user-facing documentation see:
+/// https://clang.llvm.org/extra/clang-tidy/checks/hicpp/no-assembler.html
+class NoAssemblerCheck : public ClangTidyCheck {
+public:
+  NoAssemblerCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace clang::tidy::hicpp
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_NOASSEMBLERCHECK_H
diff --git a/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp 
b/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp
index fda997a2a3df6..43898bff35618 100644
--- a/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp
@@ -9,6 +9,7 @@
 #include "../ClangTidy.h"
 #include "../ClangTidyModule.h"
 #include "AvoidPragmaOnceCheck.h"
+#include "NoAssemblerCheck.h"
 #include "RestrictSystemIncludesCheck.h"
 #include "SIMDIntrinsicsCheck.h"
 #include "StdAllocatorConstCheck.h"
@@ -23,6 +24,8 @@ class PortabilityModule : public ClangTidyModule {
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
     CheckFactories.registerCheck<AvoidPragmaOnceCheck>(
         "portability-avoid-pragma-once");
+    CheckFactories.registerCheck<NoAssemblerCheck>(
+        "portability-no-assembler");
     CheckFactories.registerCheck<RestrictSystemIncludesCheck>(
         "portability-restrict-system-includes");
     CheckFactories.registerCheck<SIMDIntrinsicsCheck>(
diff --git a/clang-tools-extra/docs/clang-tidy/checks/hicpp/no-assembler.rst 
b/clang-tools-extra/docs/clang-tidy/checks/hicpp/no-assembler.rst
index 55231fbd0a8da..14f5f07d9843a 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/hicpp/no-assembler.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/hicpp/no-assembler.rst
@@ -1,5 +1,7 @@
 .. title:: clang-tidy - hicpp-no-assembler
-
+.. meta::
+   :http-equiv=refresh: 0;URL=../portability/no-assembler.html
+   
 hicpp-no-assembler
 ==================
 
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst 
b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index c475870ed7b31..24eda5ba3e59c 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -243,7 +243,6 @@ Clang-Tidy Checks
    :doc:`hicpp-exception-baseclass <hicpp/exception-baseclass>`,
    :doc:`hicpp-ignored-remove-result <hicpp/ignored-remove-result>`,
    :doc:`hicpp-multiway-paths-covered <hicpp/multiway-paths-covered>`,
-   :doc:`hicpp-no-assembler <hicpp/no-assembler>`,
    :doc:`hicpp-signed-bitwise <hicpp/signed-bitwise>`,
    :doc:`linuxkernel-must-check-errs <linuxkernel/must-check-errs>`,
    :doc:`llvm-header-guard <llvm/header-guard>`,
@@ -370,6 +369,7 @@ Clang-Tidy Checks
    :doc:`performance-unnecessary-copy-initialization 
<performance/unnecessary-copy-initialization>`, "Yes"
    :doc:`performance-unnecessary-value-param 
<performance/unnecessary-value-param>`, "Yes"
    :doc:`portability-avoid-pragma-once <portability/avoid-pragma-once>`,
+   :doc:`portability-no-assembler <portability/no-assembler>`,
    :doc:`portability-restrict-system-includes 
<portability/restrict-system-includes>`, "Yes"
    :doc:`portability-simd-intrinsics <portability/simd-intrinsics>`,
    :doc:`portability-std-allocator-const <portability/std-allocator-const>`,
@@ -607,6 +607,7 @@ Check aliases
    :doc:`hicpp-named-parameter <hicpp/named-parameter>`, 
:doc:`readability-named-parameter <readability/named-parameter>`, "Yes"
    :doc:`hicpp-new-delete-operators <hicpp/new-delete-operators>`, 
:doc:`misc-new-delete-overloads <misc/new-delete-overloads>`,
    :doc:`hicpp-no-array-decay <hicpp/no-array-decay>`, 
:doc:`cppcoreguidelines-pro-bounds-array-to-pointer-decay 
<cppcoreguidelines/pro-bounds-array-to-pointer-decay>`,
+   :doc:`hicpp-no-assembler <hicpp/no-assembler>`, 
:doc:`portability-no-assembler <portability/no-assembler>`,
    :doc:`hicpp-no-malloc <hicpp/no-malloc>`, :doc:`cppcoreguidelines-no-malloc 
<cppcoreguidelines/no-malloc>`,
    :doc:`hicpp-noexcept-move <hicpp/noexcept-move>`, 
:doc:`performance-noexcept-move-constructor 
<performance/noexcept-move-constructor>`, "Yes"
    :doc:`hicpp-special-member-functions <hicpp/special-member-functions>`, 
:doc:`cppcoreguidelines-special-member-functions 
<cppcoreguidelines/special-member-functions>`,
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/portability/no-assembler.rst 
b/clang-tools-extra/docs/clang-tidy/checks/portability/no-assembler.rst
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/portability/no-assembler.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/portability/no-assembler.cpp
new file mode 100644
index 0000000000000..0e589b65df1ee
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/portability/no-assembler.cpp
@@ -0,0 +1,12 @@
+// RUN: %check_clang_tidy %s portability-no-assembler %t
+
+__asm__(".symver foo, bar@v");
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not use inline assembler in 
safety-critical code [portability-no-assembler]
+
+static int s asm("spam");
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use inline assembler in 
safety-critical code [portability-no-assembler]
+
+void f() {
+  __asm("mov al, 2");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use inline assembler in 
safety-critical code [portability-no-assembler]
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/184030
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to