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/// Look through AttributedStmt wrappers to find the underlying statement.
22static const Stmt *ignoreAttributed(const Stmt *S) {
23 if (const auto *AS = dyn_cast<AttributedStmt>(S))
24 return AS->getSubStmt();
25 return S;
26}
27
28/// Check that at least one branch of the \p If statement is a \c CompoundStmt.
29static bool shouldHaveBraces(const IfStmt *If) {
30 const Stmt *const Then = ignoreAttributed(If->getThen());
31 if (isa<CompoundStmt>(Then))
32 return true;
33
34 if (const Stmt *Else = If->getElse()) {
35 Else = ignoreAttributed(Else);
36 if (const auto *NestedIf = dyn_cast<const IfStmt>(Else))
37 return shouldHaveBraces(NestedIf);
38
39 return isa<CompoundStmt>(Else);
40 }
41
42 return false;
43}
44
46 Finder->addMatcher(
47 ifStmt(hasElse(anything()),
48 unless(isConsteval()), // 'if consteval' always has braces
49 unless(hasParent(ifStmt())))
50 .bind("if_stmt"),
51 this);
52}
53
55 const MatchFinder::MatchResult &Result) {
56 const auto *MatchedIf = Result.Nodes.getNodeAs<IfStmt>("if_stmt");
57 if (!shouldHaveBraces(MatchedIf))
58 return;
59 checkIfStmt(Result, MatchedIf);
60}
61
62void InconsistentIfElseBracesCheck::checkIfStmt(
63 const MatchFinder::MatchResult &Result, const IfStmt *If) {
64 const Stmt *Then = ignoreAttributed(If->getThen());
65 if (const auto *NestedIf = dyn_cast<const IfStmt>(Then)) {
66 // If the then-branch is a nested IfStmt, first we need to add braces to
67 // it, then we need to check the inner IfStmt.
68 emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
69
70 if (shouldHaveBraces(NestedIf))
71 checkIfStmt(Result, NestedIf);
72 } else if (!isa<CompoundStmt>(Then)) {
73 emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
74 }
75
76 if (const Stmt *Else = If->getElse()) {
77 Else = ignoreAttributed(Else);
78 if (const auto *NestedIf = dyn_cast<const IfStmt>(Else))
79 checkIfStmt(Result, NestedIf);
80 else if (!isa<CompoundStmt>(Else))
81 emitDiagnostic(Result, If->getElse(), If->getElseLoc());
82 }
83}
84
85void InconsistentIfElseBracesCheck::emitDiagnostic(
86 const MatchFinder::MatchResult &Result, const Stmt *S,
87 SourceLocation StartLoc, SourceLocation EndLocHint) {
88 if (StartLoc.isMacroID()) {
89 diag(StartLoc, "statement should have braces");
90 return;
91 }
92 const utils::BraceInsertionHints Hints = utils::getBraceInsertionsHints(
93 S, Result.Context->getLangOpts(), *Result.SourceManager, StartLoc,
94 EndLocHint);
95 assert(Hints && Hints.offersFixIts() && "Expected hints or fix-its");
96 diag(Hints.DiagnosticPos, "statement should have braces")
97 << Hints.openingBraceFixIt() << Hints.closingBraceFixIt();
98}
99
100} // 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 const Stmt * ignoreAttributed(const Stmt *S)
Look through AttributedStmt wrappers to find the underlying statement.
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.