[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clang-tidy / hicpp / MultiwayPathsCoveredCheck.cpp
blob8b99a1058079d90a78efe3bd63fc2985f3553375
1 //===--- MultiwayPathsCoveredCheck.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 "MultiwayPathsCoveredCheck.h"
10 #include "clang/AST/ASTContext.h"
12 #include <limits>
14 using namespace clang::ast_matchers;
16 namespace clang {
17 namespace tidy {
18 namespace hicpp {
20 void MultiwayPathsCoveredCheck::storeOptions(
21 ClangTidyOptions::OptionMap &Opts) {
22 Options.store(Opts, "WarnOnMissingElse", WarnOnMissingElse);
25 void MultiwayPathsCoveredCheck::registerMatchers(MatchFinder *Finder) {
26 Finder->addMatcher(
27 switchStmt(
28 hasCondition(expr(
29 // Match on switch statements that have either a bit-field or
30 // an integer condition. The ordering in 'anyOf()' is
31 // important because the last condition is the most general.
32 anyOf(ignoringImpCasts(memberExpr(hasDeclaration(
33 fieldDecl(isBitField()).bind("bitfield")))),
34 ignoringImpCasts(declRefExpr().bind("non-enum-condition"))),
35 // 'unless()' must be the last match here and must be bound,
36 // otherwise the matcher does not work correctly, because it
37 // will not explicitly ignore enum conditions.
38 unless(ignoringImpCasts(
39 declRefExpr(hasType(hasCanonicalType(enumType())))
40 .bind("enum-condition"))))))
41 .bind("switch"),
42 this);
44 // This option is noisy, therefore matching is configurable.
45 if (WarnOnMissingElse) {
46 Finder->addMatcher(ifStmt(hasParent(ifStmt()), unless(hasElse(anything())))
47 .bind("else-if"),
48 this);
52 static std::pair<std::size_t, bool> countCaseLabels(const SwitchStmt *Switch) {
53 std::size_t CaseCount = 0;
54 bool HasDefault = false;
56 const SwitchCase *CurrentCase = Switch->getSwitchCaseList();
57 while (CurrentCase) {
58 ++CaseCount;
59 if (isa<DefaultStmt>(CurrentCase))
60 HasDefault = true;
62 CurrentCase = CurrentCase->getNextSwitchCase();
65 return std::make_pair(CaseCount, HasDefault);
68 /// This function calculate 2 ** Bits and returns
69 /// numeric_limits<std::size_t>::max() if an overflow occurred.
70 static std::size_t twoPow(std::size_t Bits) {
71 return Bits >= std::numeric_limits<std::size_t>::digits
72 ? std::numeric_limits<std::size_t>::max()
73 : static_cast<size_t>(1) << Bits;
76 /// Get the number of possible values that can be switched on for the type T.
77 ///
78 /// \return - 0 if bitcount could not be determined
79 /// - numeric_limits<std::size_t>::max() when overflow appeared due to
80 /// more than 64 bits type size.
81 static std::size_t getNumberOfPossibleValues(QualType T,
82 const ASTContext &Context) {
83 // `isBooleanType` must come first because `bool` is an integral type as well
84 // and would not return 2 as result.
85 if (T->isBooleanType())
86 return 2;
87 if (T->isIntegralType(Context))
88 return twoPow(Context.getTypeSize(T));
89 return 1;
92 void MultiwayPathsCoveredCheck::check(const MatchFinder::MatchResult &Result) {
93 if (const auto *ElseIfWithoutElse =
94 Result.Nodes.getNodeAs<IfStmt>("else-if")) {
95 diag(ElseIfWithoutElse->getBeginLoc(),
96 "potentially uncovered codepath; add an ending else statement");
97 return;
99 const auto *Switch = Result.Nodes.getNodeAs<SwitchStmt>("switch");
100 std::size_t SwitchCaseCount;
101 bool SwitchHasDefault;
102 std::tie(SwitchCaseCount, SwitchHasDefault) = countCaseLabels(Switch);
104 // Checks the sanity of 'switch' statements that actually do define
105 // a default branch but might be degenerated by having no or only one case.
106 if (SwitchHasDefault) {
107 handleSwitchWithDefault(Switch, SwitchCaseCount);
108 return;
110 // Checks all 'switch' statements that do not define a default label.
111 // Here the heavy lifting happens.
112 if (!SwitchHasDefault && SwitchCaseCount > 0) {
113 handleSwitchWithoutDefault(Switch, SwitchCaseCount, Result);
114 return;
116 // Warns for degenerated 'switch' statements that neither define a case nor
117 // a default label.
118 // FIXME: Evaluate, if emitting a fix-it to simplify that statement is
119 // reasonable.
120 if (!SwitchHasDefault && SwitchCaseCount == 0) {
121 diag(Switch->getBeginLoc(),
122 "switch statement without labels has no effect");
123 return;
125 llvm_unreachable("matched a case, that was not explicitly handled");
128 void MultiwayPathsCoveredCheck::handleSwitchWithDefault(
129 const SwitchStmt *Switch, std::size_t CaseCount) {
130 assert(CaseCount > 0 && "Switch statement with supposedly one default "
131 "branch did not contain any case labels");
132 if (CaseCount == 1 || CaseCount == 2)
133 diag(Switch->getBeginLoc(),
134 CaseCount == 1
135 ? "degenerated switch with default label only"
136 : "switch could be better written as an if/else statement");
139 void MultiwayPathsCoveredCheck::handleSwitchWithoutDefault(
140 const SwitchStmt *Switch, std::size_t CaseCount,
141 const MatchFinder::MatchResult &Result) {
142 // The matcher only works because some nodes are explicitly matched and
143 // bound but ignored. This is necessary to build the excluding logic for
144 // enums and 'switch' statements without a 'default' branch.
145 assert(!Result.Nodes.getNodeAs<DeclRefExpr>("enum-condition") &&
146 "switch over enum is handled by warnings already, explicitly ignoring "
147 "them");
148 // Determine the number of case labels. Because 'default' is not present
149 // and duplicating case labels is not allowed this number represents
150 // the number of codepaths. It can be directly compared to 'MaxPathsPossible'
151 // to see if some cases are missing.
152 // CaseCount == 0 is caught in DegenerateSwitch. Necessary because the
153 // matcher used for here does not match on degenerate 'switch'.
154 assert(CaseCount > 0 && "Switch statement without any case found. This case "
155 "should be excluded by the matcher and is handled "
156 "separately.");
157 std::size_t MaxPathsPossible = [&]() {
158 if (const auto *GeneralCondition =
159 Result.Nodes.getNodeAs<DeclRefExpr>("non-enum-condition")) {
160 return getNumberOfPossibleValues(GeneralCondition->getType(),
161 *Result.Context);
163 if (const auto *BitfieldDecl =
164 Result.Nodes.getNodeAs<FieldDecl>("bitfield")) {
165 return twoPow(BitfieldDecl->getBitWidthValue(*Result.Context));
168 return static_cast<std::size_t>(0);
169 }();
171 // FIXME: Transform the 'switch' into an 'if' for CaseCount == 1.
172 if (CaseCount < MaxPathsPossible)
173 diag(Switch->getBeginLoc(),
174 CaseCount == 1 ? "switch with only one case; use an if statement"
175 : "potential uncovered code path; add a default label");
177 } // namespace hicpp
178 } // namespace tidy
179 } // namespace clang