[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / bugprone / OptionalValueConversionCheck.cpp
blob9ab59e6b0474f0b70d5d9654a72df2c230b56cc6
1 //===--- OptionalValueConversionCheck.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 //===----------------------------------------------------------------------===//
9 #include "OptionalValueConversionCheck.h"
10 #include "../utils/LexerUtils.h"
11 #include "../utils/Matchers.h"
12 #include "../utils/OptionsUtils.h"
13 #include "clang/AST/ASTContext.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
16 using namespace clang::ast_matchers;
18 namespace clang::tidy::bugprone {
20 namespace {
22 AST_MATCHER_P(QualType, hasCleanType, ast_matchers::internal::Matcher<QualType>,
23 InnerMatcher) {
24 return InnerMatcher.matches(
25 Node.getNonReferenceType().getUnqualifiedType().getCanonicalType(),
26 Finder, Builder);
29 } // namespace
31 OptionalValueConversionCheck::OptionalValueConversionCheck(
32 StringRef Name, ClangTidyContext *Context)
33 : ClangTidyCheck(Name, Context),
34 OptionalTypes(utils::options::parseStringList(
35 Options.get("OptionalTypes",
36 "::std::optional;::absl::optional;::boost::optional"))),
37 ValueMethods(utils::options::parseStringList(
38 Options.get("ValueMethods", "::value$;::get$"))) {}
40 std::optional<TraversalKind>
41 OptionalValueConversionCheck::getCheckTraversalKind() const {
42 return TK_AsIs;
45 void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
46 auto ConstructTypeMatcher =
47 qualType(hasCleanType(qualType().bind("optional-type")));
49 auto CallTypeMatcher =
50 qualType(hasCleanType(equalsBoundNode("optional-type")));
52 auto OptionalDereferenceMatcher = callExpr(
53 anyOf(
54 cxxOperatorCallExpr(hasOverloadedOperatorName("*"),
55 hasUnaryOperand(hasType(CallTypeMatcher)))
56 .bind("op-call"),
57 cxxMemberCallExpr(thisPointerType(CallTypeMatcher),
58 callee(cxxMethodDecl(anyOf(
59 hasOverloadedOperatorName("*"),
60 matchers::matchesAnyListedName(ValueMethods)))))
61 .bind("member-call")),
62 hasType(qualType().bind("value-type")));
64 auto StdMoveCallMatcher =
65 callExpr(argumentCountIs(1), callee(functionDecl(hasName("::std::move"))),
66 hasArgument(0, ignoringImpCasts(OptionalDereferenceMatcher)));
67 Finder->addMatcher(
68 cxxConstructExpr(
69 argumentCountIs(1U),
70 hasDeclaration(cxxConstructorDecl(
71 ofClass(matchers::matchesAnyListedName(OptionalTypes)))),
72 hasType(ConstructTypeMatcher),
73 hasArgument(0U, ignoringImpCasts(anyOf(OptionalDereferenceMatcher,
74 StdMoveCallMatcher))))
75 .bind("expr"),
76 this);
79 void OptionalValueConversionCheck::storeOptions(
80 ClangTidyOptions::OptionMap &Opts) {
81 Options.store(Opts, "OptionalTypes",
82 utils::options::serializeStringList(OptionalTypes));
83 Options.store(Opts, "ValueMethods",
84 utils::options::serializeStringList(ValueMethods));
87 void OptionalValueConversionCheck::check(
88 const MatchFinder::MatchResult &Result) {
89 const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>("expr");
90 const auto *OptionalType = Result.Nodes.getNodeAs<QualType>("optional-type");
91 const auto *ValueType = Result.Nodes.getNodeAs<QualType>("value-type");
93 diag(MatchedExpr->getExprLoc(),
94 "conversion from %0 into %1 and back into %0, remove potentially "
95 "error-prone optional dereference")
96 << *OptionalType << ValueType->getUnqualifiedType();
98 if (const auto *OperatorExpr =
99 Result.Nodes.getNodeAs<CXXOperatorCallExpr>("op-call")) {
100 diag(OperatorExpr->getExprLoc(), "remove '*' to silence this warning",
101 DiagnosticIDs::Note)
102 << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
103 OperatorExpr->getBeginLoc(), OperatorExpr->getExprLoc()));
104 return;
106 if (const auto *CallExpr =
107 Result.Nodes.getNodeAs<CXXMemberCallExpr>("member-call")) {
108 const SourceLocation Begin =
109 utils::lexer::getPreviousToken(CallExpr->getExprLoc(),
110 *Result.SourceManager, getLangOpts())
111 .getLocation();
112 auto Diag =
113 diag(CallExpr->getExprLoc(),
114 "remove call to %0 to silence this warning", DiagnosticIDs::Note);
115 Diag << CallExpr->getMethodDecl()
116 << FixItHint::CreateRemoval(
117 CharSourceRange::getTokenRange(Begin, CallExpr->getEndLoc()));
118 if (const auto *Member =
119 llvm::dyn_cast<MemberExpr>(CallExpr->getCallee()->IgnoreImplicit());
120 Member && Member->isArrow())
121 Diag << FixItHint::CreateInsertion(CallExpr->getBeginLoc(), "*");
122 return;
126 } // namespace clang::tidy::bugprone