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