clang-tools 22.0.0git
MultipleStatementMacroCheck.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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::bugprone {
16
17namespace {
18
19AST_MATCHER(Expr, isInMacro) { return Node.getBeginLoc().isMacroID(); }
20
21} // namespace
22
23/// Find the next statement after `S`.
24static const Stmt *nextStmt(const MatchFinder::MatchResult &Result,
25 const Stmt *S) {
26 auto Parents = Result.Context->getParents(*S);
27 if (Parents.empty())
28 return nullptr;
29 const auto *Parent = Parents[0].get<Stmt>();
30 if (!Parent)
31 return nullptr;
32 const Stmt *Prev = nullptr;
33 for (const Stmt *Child : Parent->children()) {
34 if (Prev == S)
35 return Child;
36 Prev = Child;
37 }
38 return nextStmt(Result, Parent);
39}
40
41using ExpansionRanges = std::vector<SourceRange>;
42
43/// \brief Get all the macro expansion ranges related to `Loc`.
44///
45/// The result is ordered from most inner to most outer.
46static ExpansionRanges
47getExpansionRanges(SourceLocation Loc, const MatchFinder::MatchResult &Result) {
48 ExpansionRanges Locs;
49 while (Loc.isMacroID()) {
50 Locs.push_back(
51 Result.SourceManager->getImmediateExpansionRange(Loc).getAsRange());
52 Loc = Locs.back().getBegin();
53 }
54 return Locs;
55}
56
58 const auto Inner = expr(isInMacro(), unless(compoundStmt())).bind("inner");
59 Finder->addMatcher(
60 stmt(anyOf(ifStmt(hasThen(Inner)), ifStmt(hasElse(Inner)).bind("else"),
61 whileStmt(hasBody(Inner)), forStmt(hasBody(Inner))))
62 .bind("outer"),
63 this);
64}
65
67 const MatchFinder::MatchResult &Result) {
68 const auto *Inner = Result.Nodes.getNodeAs<Expr>("inner");
69 const auto *Outer = Result.Nodes.getNodeAs<Stmt>("outer");
70 const auto *Next = nextStmt(Result, Outer);
71 if (!Next)
72 return;
73
74 SourceLocation OuterLoc = Outer->getBeginLoc();
75 if (Result.Nodes.getNodeAs<Stmt>("else"))
76 OuterLoc = cast<IfStmt>(Outer)->getElseLoc();
77
78 auto InnerRanges = getExpansionRanges(Inner->getBeginLoc(), Result);
79 auto OuterRanges = getExpansionRanges(OuterLoc, Result);
80 auto NextRanges = getExpansionRanges(Next->getBeginLoc(), Result);
81
82 // Remove all the common ranges, starting from the top (the last ones in the
83 // list).
84 while (!InnerRanges.empty() && !OuterRanges.empty() && !NextRanges.empty() &&
85 InnerRanges.back() == OuterRanges.back() &&
86 InnerRanges.back() == NextRanges.back()) {
87 InnerRanges.pop_back();
88 OuterRanges.pop_back();
89 NextRanges.pop_back();
90 }
91
92 // Inner and Next must have at least one more macro that Outer doesn't have,
93 // and that range must be common to both.
94 if (InnerRanges.empty() || NextRanges.empty() ||
95 InnerRanges.back() != NextRanges.back())
96 return;
97
98 diag(InnerRanges.back().getBegin(), "multiple statement macro used without "
99 "braces; some statements will be "
100 "unconditionally executed");
101}
102
103} // namespace clang::tidy::bugprone
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static const Stmt * nextStmt(const MatchFinder::MatchResult &Result, const Stmt *S)
Find the next statement after S.
std::vector< SourceRange > ExpansionRanges
static ExpansionRanges getExpansionRanges(SourceLocation Loc, const MatchFinder::MatchResult &Result)
Get all the macro expansion ranges related to Loc.
AST_MATCHER(BinaryOperator, isRelationalOperator)