1//===--- ProTypeUnionAccessCheck.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
9#include "ProTypeUnionAccessCheck.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11
12using namespace clang::ast_matchers;
13
14namespace clang::tidy::cppcoreguidelines {
15
16void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) {
17 Finder->addMatcher(
18 NodeMatch: memberExpr(hasObjectExpression(InnerMatcher: hasType(InnerMatcher: recordDecl(isUnion()))))
19 .bind(ID: "expr"),
20 Action: this);
21}
22
23void ProTypeUnionAccessCheck::check(const MatchFinder::MatchResult &Result) {
24 const auto *Matched = Result.Nodes.getNodeAs<MemberExpr>(ID: "expr");
25 SourceLocation Loc = Matched->getMemberLoc();
26 if (Loc.isInvalid())
27 Loc = Matched->getBeginLoc();
28 diag(Loc, Description: "do not access members of unions; consider using (boost::)variant "
29 "instead");
30}
31
32} // namespace clang::tidy::cppcoreguidelines
33

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp