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

source code of clang-tools-extra/clang-tidy/bugprone/MultipleStatementMacroCheck.cpp