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