| 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 | |
| 12 | using namespace clang::ast_matchers; |
| 13 | |
| 14 | namespace clang::tidy::cppcoreguidelines { |
| 15 | |
| 16 | void 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 | |
| 23 | void 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 |
