Skip to content

Commit b785407

Browse files
committedOct 16, 2015
[clang-tidy] add check cppcoreguidelines-pro-type-union-access
Summary: This check flags all access to members of unions. Passing unions as a whole is not flagged. Reading from a union member assumes that member was the last one written, and writing to a union member assumes another member with a nontrivial destructor had its destructor called. This is fragile because it cannot generally be enforced to be safe in the language and so relies on programmer discipline to get it right. This rule is part of the "Type safety" profile of the C++ Core Guidelines, see https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type7-avoid-accessing-members-of-raw-unions-prefer-variant-instead Reviewers: alexfh, sbenza, bkramer, aaron.ballman Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D13784 llvm-svn: 250537
1 parent a6c9ee7 commit b785407

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed
 

‎clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_clang_library(clangTidyCppCoreGuidelinesModule
66
ProTypeConstCastCheck.cpp
77
ProTypeReinterpretCastCheck.cpp
88
ProTypeStaticCastDowncastCheck.cpp
9+
ProTypeUnionAccessCheck.cpp
910

1011
LINK_LIBS
1112
clangAST

‎clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "ProTypeConstCastCheck.h"
1717
#include "ProTypeReinterpretCastCheck.h"
1818
#include "ProTypeStaticCastDowncastCheck.h"
19+
#include "ProTypeUnionAccessCheck.h"
1920

2021
namespace clang {
2122
namespace tidy {
@@ -35,6 +36,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
3536
"cppcoreguidelines-pro-type-reinterpret-cast");
3637
CheckFactories.registerCheck<ProTypeStaticCastDowncastCheck>(
3738
"cppcoreguidelines-pro-type-static-cast-downcast");
39+
CheckFactories.registerCheck<ProTypeUnionAccessCheck>(
40+
"cppcoreguidelines-pro-type-union-access");
3841
CheckFactories.registerCheck<misc::AssignOperatorSignatureCheck>(
3942
"cppcoreguidelines-c-copy-assignment-signature");
4043
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- ProTypeUnionAccessCheck.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 "ProTypeUnionAccessCheck.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+
19+
void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) {
20+
if (!getLangOpts().CPlusPlus)
21+
return;
22+
23+
Finder->addMatcher(memberExpr(hasObjectExpression(hasType(recordDecl(isUnion())))).bind("expr"), this);
24+
}
25+
26+
void ProTypeUnionAccessCheck::check(const MatchFinder::MatchResult &Result) {
27+
const auto *Matched = Result.Nodes.getNodeAs<MemberExpr>("expr");
28+
diag(Matched->getMemberLoc(), "do not access members of unions; use (boost::)variant instead");
29+
}
30+
31+
} // namespace tidy
32+
} // namespace clang
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- ProTypeUnionAccessCheck.h - clang-tidy------------------*- C++ -*-===//
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+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_UNION_ACCESS_H
11+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_UNION_ACCESS_H
12+
13+
#include "../ClangTidy.h"
14+
15+
namespace clang {
16+
namespace tidy {
17+
18+
/// This check flags all access to members of unions.
19+
/// Access to a union as a whole (e.g. passing to a function) is not flagged.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-union-access.html
23+
class ProTypeUnionAccessCheck : public ClangTidyCheck {
24+
public:
25+
ProTypeUnionAccessCheck(StringRef Name, ClangTidyContext *Context)
26+
: ClangTidyCheck(Name, Context) {}
27+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
28+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29+
};
30+
31+
} // namespace tidy
32+
} // namespace clang
33+
34+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_UNION_ACCESS_H
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cppcoreguidelines-pro-type-union-access
2+
=======================================
3+
4+
This check flags all access to members of unions. Passing unions as a whole is not flagged.
5+
6+
Reading from a union member assumes that member was the last one written, and writing to a union member assumes another member with a nontrivial destructor had its destructor called. This is fragile because it cannot generally be enforced to be safe in the language and so relies on programmer discipline to get it right.
7+
8+
This rule is part of the "Type safety" profile of the C++ Core Guidelines, see
9+
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type7-avoid-accessing-members-of-raw-unions-prefer-variant-instead

‎clang-tools-extra/docs/clang-tidy/checks/list.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ List of clang-tidy Checks
88
cppcoreguidelines-pro-type-const-cast
99
cppcoreguidelines-pro-type-reinterpret-cast
1010
cppcoreguidelines-pro-type-static-cast-downcast
11+
cppcoreguidelines-pro-type-union-access
1112
google-build-explicit-make-pair
1213
google-build-namespaces
1314
google-build-using-namespace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %python %S/check_clang_tidy.py %s cppcoreguidelines-pro-type-union-access %t
2+
3+
union U {
4+
bool union_member1;
5+
char union_member2;
6+
} u;
7+
8+
struct S {
9+
int non_union_member;
10+
union {
11+
bool union_member;
12+
};
13+
union {
14+
char union_member2;
15+
} u;
16+
} s;
17+
18+
19+
void f(char);
20+
void f2(U);
21+
void f3(U&);
22+
void f4(U*);
23+
24+
void check()
25+
{
26+
u.union_member1 = true;
27+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]
28+
auto b = u.union_member2;
29+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not access members of unions; use (boost::)variant instead
30+
auto a = &s.union_member;
31+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; use (boost::)variant instead
32+
f(s.u.union_member2);
33+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not access members of unions; use (boost::)variant instead
34+
35+
s.non_union_member = 2; // OK
36+
37+
U u2 = u; // OK
38+
f2(u); // OK
39+
f3(u); // OK
40+
f4(&u); // OK
41+
}

0 commit comments

Comments
 (0)
Please sign in to comment.