clang-tools 23.0.0git
InconsistentIfElseBracesCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Stmt.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Basic/SourceLocation.h"
15#include "clang/Lex/Lexer.h"
16
17using namespace clang::ast_matchers;
18
20
21/// Check that at least one branch of the \p If statement is a \c CompoundStmt.
22static bool shouldHaveBraces(const IfStmt *If) {
23 const Stmt *const Then = If->getThen()->stripLabelLikeStatements();
24 if (isa<CompoundStmt>(Then))
25 return true;
26
27 if (const Stmt *Else = If->getElse()) {
28 Else = Else->stripLabelLikeStatements();
29 if (const auto *NestedIf = dyn_cast<const IfStmt>(Else))
30 return shouldHaveBraces(NestedIf);
31
32 return isa<CompoundStmt>(Else);
33 }
34
35 return false;
36}
37
39 Finder->addMatcher(
40 ifStmt(hasElse(anything()),
41 unless(isConsteval()), // 'if consteval' always has braces
42 unless(hasParent(ifStmt())))
43 .bind("if_stmt"),
44 this);
45}
46
48 const MatchFinder::MatchResult &Result) {
49 const auto *MatchedIf = Result.Nodes.getNodeAs<IfStmt>("if_stmt");
50 if (!shouldHaveBraces(MatchedIf))
51 return;
52 checkIfStmt(Result, MatchedIf);
53}
54
55void InconsistentIfElseBracesCheck::checkIfStmt(
56 const MatchFinder::MatchResult &Result, const IfStmt *If) {
57 const Stmt *Then = If->getThen()->stripLabelLikeStatements();
58 if (const auto *NestedIf = dyn_cast<const IfStmt>(Then)) {
59 // If the then-branch is a nested IfStmt, first we need to add braces to
60 // it, then we need to check the inner IfStmt.
61 emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
62
63 if (shouldHaveBraces(NestedIf))
64 checkIfStmt(Result, NestedIf);
65 } else if (!isa<CompoundStmt>(Then)) {
66 emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
67 }
68
69 if (const Stmt *Else = If->getElse()) {
70 Else = Else->stripLabelLikeStatements();
71 if (const auto *NestedIf = dyn_cast<const IfStmt>(Else))
72 checkIfStmt(Result, NestedIf);
73 else if (!isa<CompoundStmt>(Else))
74 emitDiagnostic(Result, If->getElse(), If->getElseLoc());
75 }
76}
77
78void InconsistentIfElseBracesCheck::emitDiagnostic(
79 const MatchFinder::MatchResult &Result, const Stmt *S,
80 SourceLocation StartLoc, SourceLocation EndLocHint) {
81 if (StartLoc.isMacroID()) {
82 diag(StartLoc, "statement should have braces");
83 return;
84 }
85 const utils::BraceInsertionHints Hints = utils::getBraceInsertionsHints(
86 S, Result.Context->getLangOpts(), *Result.SourceManager, StartLoc,
87 EndLocHint);
88 assert(Hints && Hints.offersFixIts() && "Expected hints or fix-its");
89 diag(Hints.DiagnosticPos, "statement should have braces")
90 << Hints.openingBraceFixIt() << Hints.closingBraceFixIt();
91}
92
93} // namespace clang::tidy::readability
This file provides utilities to put braces around a statement.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static bool shouldHaveBraces(const IfStmt *If)
Check that at least one branch of the If statement is a CompoundStmt.
BraceInsertionHints getBraceInsertionsHints(const Stmt *const S, const LangOptions &LangOpts, const SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLocHint)
Create fix-it hints for braces that wrap the given statement when applied.