[clang-tools-extra] r294255 - [clang-tidy] safety-no-assembler
Jonathan Coe via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 6 14:57:15 PST 2017
Author: jbcoe
Date: Mon Feb 6 16:57:14 2017
New Revision: 294255
URL: http://llvm.org/viewvc/llvm-project?rev=294255&view=rev
Log:
[clang-tidy] safety-no-assembler
Summary:
Add a new clang-tidy module for safety-critical checks.
Include a check for inline assembler.
Reviewers: Prazek, dtarditi, malcolm.parsons, alexfh, aaron.ballman, idlecode
Reviewed By: idlecode
Subscribers: idlecode, JonasToth, Eugene.Zelenko, mgorny, JDevlieghere, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D29267
Added:
clang-tools-extra/trunk/clang-tidy/safety/
clang-tools-extra/trunk/clang-tidy/safety/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.cpp
clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.h
clang-tools-extra/trunk/clang-tidy/safety/SafetyTidyModule.cpp
clang-tools-extra/trunk/docs/clang-tidy/checks/safety-no-assembler.rst
clang-tools-extra/trunk/test/clang-tidy/safety-no-assembler.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
Modified: clang-tools-extra/trunk/clang-tidy/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/CMakeLists.txt?rev=294255&r1=294254&r2=294255&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/CMakeLists.txt Mon Feb 6 16:57:14 2017
@@ -38,4 +38,5 @@ add_subdirectory(modernize)
add_subdirectory(mpi)
add_subdirectory(performance)
add_subdirectory(readability)
+add_subdirectory(safety)
add_subdirectory(utils)
Added: clang-tools-extra/trunk/clang-tidy/safety/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/safety/CMakeLists.txt?rev=294255&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/safety/CMakeLists.txt (added)
+++ clang-tools-extra/trunk/clang-tidy/safety/CMakeLists.txt Mon Feb 6 16:57:14 2017
@@ -0,0 +1,14 @@
+set(LLVM_LINK_COMPONENTS support)
+
+add_clang_library(clangTidySafetyModule
+ NoAssemblerCheck.cpp
+ SafetyTidyModule.cpp
+
+ LINK_LIBS
+ clangAST
+ clangASTMatchers
+ clangBasic
+ clangLex
+ clangTidy
+ clangTidyUtils
+ )
Added: clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.cpp?rev=294255&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.cpp Mon Feb 6 16:57:14 2017
@@ -0,0 +1,52 @@
+//===--- NoAssemblerCheck.cpp - clang-tidy---------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "NoAssemblerCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace ast_matchers {
+AST_MATCHER(VarDecl, isAsm) { return Node.hasAttr<clang::AsmLabelAttr>(); }
+const internal::VariadicDynCastAllOfMatcher<Decl, FileScopeAsmDecl>
+ fileScopeAsmDecl;
+}
+}
+
+namespace clang {
+namespace tidy {
+namespace safety {
+
+void NoAssemblerCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(asmStmt().bind("asm-stmt"), this);
+ Finder->addMatcher(fileScopeAsmDecl().bind("asm-file-scope"), this);
+ Finder->addMatcher(varDecl(isAsm()).bind("asm-var"), this);
+}
+
+void NoAssemblerCheck::check(const MatchFinder::MatchResult &Result) {
+ Optional<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 safety
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.h?rev=294255&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/safety/NoAssemblerCheck.h Mon Feb 6 16:57:14 2017
@@ -0,0 +1,35 @@
+//===--- NoAssemblerCheck.h - clang-tidy-------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SAFETY_NO_ASSEMBLER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SAFETY_NO_ASSEMBLER_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace safety {
+
+/// Find assembler statements. No fix is offered.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/safety-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 safety
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SAFETY_NO_ASSEMBLER_H
Added: clang-tools-extra/trunk/clang-tidy/safety/SafetyTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/safety/SafetyTidyModule.cpp?rev=294255&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/safety/SafetyTidyModule.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/safety/SafetyTidyModule.cpp Mon Feb 6 16:57:14 2017
@@ -0,0 +1,38 @@
+//===------- SafetyTidyModule.cpp - clang-tidy ----------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "../ClangTidy.h"
+#include "../ClangTidyModule.h"
+#include "../ClangTidyModuleRegistry.h"
+#include "NoAssemblerCheck.h"
+
+namespace clang {
+namespace tidy {
+namespace safety {
+
+class SafetyModule : public ClangTidyModule {
+public:
+ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+ CheckFactories.registerCheck<NoAssemblerCheck>(
+ "safety-no-assembler");
+ }
+};
+
+// Register the SafetyModule using this statically initialized variable.
+static ClangTidyModuleRegistry::Add<SafetyModule>
+ X("safety-module", "Adds safety-critical checks.");
+
+} // namespace safety
+
+// This anchor is used to force the linker to link in the generated object file
+// and thus register the SafetyModule.
+volatile int SafetyModuleAnchorSource = 0;
+
+} // namespace tidy
+} // namespace clang
Modified: clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt?rev=294255&r1=294254&r2=294255&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt Mon Feb 6 16:57:14 2017
@@ -23,6 +23,7 @@ target_link_libraries(clang-tidy
clangTidyMPIModule
clangTidyPerformanceModule
clangTidyReadabilityModule
+ clangTidySafetyModule
clangTooling
)
Modified: clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp?rev=294255&r1=294254&r2=294255&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp Mon Feb 6 16:57:14 2017
@@ -475,6 +475,11 @@ extern volatile int ReadabilityModuleAnc
static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination =
ReadabilityModuleAnchorSource;
+// This anchor is used to force the linker to link the SafetyModule.
+extern volatile int SafetyModuleAnchorSource;
+static int LLVM_ATTRIBUTE_UNUSED SafetyModuleAnchorDestination =
+ SafetyModuleAnchorSource;
+
} // namespace tidy
} // namespace clang
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=294255&r1=294254&r2=294255&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Mon Feb 6 16:57:14 2017
@@ -57,7 +57,10 @@ The improvements are...
Improvements to clang-tidy
--------------------------
-The improvements are...
+- New `safety-no-assembler
+ <http://clang.llvm.org/extra/clang-tidy/checks/safety-no-assembler.html>`_ check
+
+ Finds uses of inline assembler.
Improvements to include-fixer
-----------------------------
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=294255&r1=294254&r2=294255&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Mon Feb 6 16:57:14 2017
@@ -152,3 +152,4 @@ Clang-Tidy Checks
readability-simplify-boolean-expr
readability-static-definition-in-anonymous-namespace
readability-uniqueptr-delete-release
+ safety-no-assembler
Added: clang-tools-extra/trunk/docs/clang-tidy/checks/safety-no-assembler.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/safety-no-assembler.rst?rev=294255&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/safety-no-assembler.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/safety-no-assembler.rst Mon Feb 6 16:57:14 2017
@@ -0,0 +1,10 @@
+.. title:: clang-tidy - safety-no-assembler
+
+safety-no-assembler
+===================
+
+Check for assembler statements. No fix is offered.
+
+Inline assembler is forbidden by safety-critical C++ standards like `High
+Intergrity C++ <http://www.codingstandard.com>`_ as it restricts the
+portability of code.
Added: clang-tools-extra/trunk/test/clang-tidy/safety-no-assembler.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/safety-no-assembler.cpp?rev=294255&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/safety-no-assembler.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/safety-no-assembler.cpp Mon Feb 6 16:57:14 2017
@@ -0,0 +1,13 @@
+// RUN: %check_clang_tidy %s safety-no-assembler %t
+
+__asm__(".symver foo, bar at v");
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not use inline assembler in safety-critical code [safety-no-assembler]
+
+static int s asm("spam");
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use inline assembler in safety-critical code [safety-no-assembler]
+
+void f() {
+ __asm("mov al, 2");
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use inline assembler in safety-critical code [safety-no-assembler]
+}
+
More information about the cfe-commits
mailing list