diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/cppcoreguidelines')
4 files changed, 72 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt index 791ec0394ef..cd81c4f6147 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt @@ -6,6 +6,7 @@ add_clang_library(clangTidyCppCoreGuidelinesModule ProTypeConstCastCheck.cpp ProTypeReinterpretCastCheck.cpp ProTypeStaticCastDowncastCheck.cpp + ProTypeUnionAccessCheck.cpp LINK_LIBS clangAST diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp index 0d239ff1b98..3d517902660 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -16,6 +16,7 @@ #include "ProTypeConstCastCheck.h" #include "ProTypeReinterpretCastCheck.h" #include "ProTypeStaticCastDowncastCheck.h" +#include "ProTypeUnionAccessCheck.h" namespace clang { namespace tidy { @@ -35,6 +36,8 @@ public: "cppcoreguidelines-pro-type-reinterpret-cast"); CheckFactories.registerCheck<ProTypeStaticCastDowncastCheck>( "cppcoreguidelines-pro-type-static-cast-downcast"); + CheckFactories.registerCheck<ProTypeUnionAccessCheck>( + "cppcoreguidelines-pro-type-union-access"); CheckFactories.registerCheck<misc::AssignOperatorSignatureCheck>( "cppcoreguidelines-c-copy-assignment-signature"); } diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp new file mode 100644 index 00000000000..64f51369bd3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp @@ -0,0 +1,33 @@ +//===--- ProTypeUnionAccessCheck.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 "ProTypeUnionAccessCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { + +void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus) + return; + + Finder->addMatcher(memberExpr(hasObjectExpression(hasType(recordDecl(isUnion())))).bind("expr"), this); +} + +void ProTypeUnionAccessCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Matched = Result.Nodes.getNodeAs<MemberExpr>("expr"); + diag(Matched->getMemberLoc(), "do not access members of unions; use (boost::)variant instead"); +} + +} // namespace tidy +} // namespace clang + diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.h new file mode 100644 index 00000000000..67ce6165811 --- /dev/null +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.h @@ -0,0 +1,35 @@ +//===--- ProTypeUnionAccessCheck.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_CPPCOREGUIDELINES_PRO_TYPE_UNION_ACCESS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_UNION_ACCESS_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { + +/// This check flags all access to members of unions. +/// Access to a union as a whole (e.g. passing to a function) is not flagged. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-union-access.html +class ProTypeUnionAccessCheck : public ClangTidyCheck { +public: + ProTypeUnionAccessCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_UNION_ACCESS_H + |

