2016-04-15 05:15:57 +08:00
|
|
|
//===--- MultipleStatementMacroCheck.cpp - clang-tidy----------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-15 05:15:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MultipleStatementMacroCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2017-11-24 22:16:29 +08:00
|
|
|
namespace bugprone {
|
2016-04-15 05:15:57 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
AST_MATCHER(Expr, isInMacro) { return Node.getBeginLoc().isMacroID(); }
|
2016-04-15 05:15:57 +08:00
|
|
|
|
|
|
|
/// \brief Find the next statement after `S`.
|
|
|
|
const Stmt *nextStmt(const MatchFinder::MatchResult &Result, const Stmt *S) {
|
|
|
|
auto Parents = Result.Context->getParents(*S);
|
|
|
|
if (Parents.empty())
|
|
|
|
return nullptr;
|
2016-12-14 23:29:23 +08:00
|
|
|
const auto *Parent = Parents[0].get<Stmt>();
|
2016-04-15 05:15:57 +08:00
|
|
|
if (!Parent)
|
|
|
|
return nullptr;
|
2016-11-08 15:50:19 +08:00
|
|
|
const Stmt *Prev = nullptr;
|
2016-04-15 05:15:57 +08:00
|
|
|
for (const Stmt *Child : Parent->children()) {
|
|
|
|
if (Prev == S)
|
|
|
|
return Child;
|
|
|
|
Prev = Child;
|
|
|
|
}
|
|
|
|
return nextStmt(Result, Parent);
|
|
|
|
}
|
|
|
|
|
2018-04-30 13:26:07 +08:00
|
|
|
using ExpansionRanges = std::vector<SourceRange>;
|
2016-04-15 05:15:57 +08:00
|
|
|
|
|
|
|
/// \bried Get all the macro expansion ranges related to `Loc`.
|
|
|
|
///
|
|
|
|
/// The result is ordered from most inner to most outer.
|
|
|
|
ExpansionRanges getExpansionRanges(SourceLocation Loc,
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
ExpansionRanges Locs;
|
|
|
|
while (Loc.isMacroID()) {
|
2018-04-30 13:26:07 +08:00
|
|
|
Locs.push_back(
|
|
|
|
Result.SourceManager->getImmediateExpansionRange(Loc).getAsRange());
|
|
|
|
Loc = Locs.back().getBegin();
|
2016-04-15 05:15:57 +08:00
|
|
|
}
|
|
|
|
return Locs;
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
} // namespace
|
2016-04-15 05:15:57 +08:00
|
|
|
|
|
|
|
void MultipleStatementMacroCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
const auto Inner = expr(isInMacro(), unless(compoundStmt())).bind("inner");
|
|
|
|
Finder->addMatcher(
|
|
|
|
stmt(anyOf(ifStmt(hasThen(Inner)), ifStmt(hasElse(Inner)).bind("else"),
|
|
|
|
whileStmt(hasBody(Inner)), forStmt(hasBody(Inner))))
|
|
|
|
.bind("outer"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultipleStatementMacroCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Inner = Result.Nodes.getNodeAs<Expr>("inner");
|
|
|
|
const auto *Outer = Result.Nodes.getNodeAs<Stmt>("outer");
|
|
|
|
const auto *Next = nextStmt(Result, Outer);
|
|
|
|
if (!Next)
|
|
|
|
return;
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation OuterLoc = Outer->getBeginLoc();
|
2016-04-15 05:15:57 +08:00
|
|
|
if (Result.Nodes.getNodeAs<Stmt>("else"))
|
|
|
|
OuterLoc = cast<IfStmt>(Outer)->getElseLoc();
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
auto InnerRanges = getExpansionRanges(Inner->getBeginLoc(), Result);
|
2016-04-15 05:15:57 +08:00
|
|
|
auto OuterRanges = getExpansionRanges(OuterLoc, Result);
|
2018-08-10 06:42:26 +08:00
|
|
|
auto NextRanges = getExpansionRanges(Next->getBeginLoc(), Result);
|
2016-04-15 05:15:57 +08:00
|
|
|
|
|
|
|
// Remove all the common ranges, starting from the top (the last ones in the
|
|
|
|
// list).
|
|
|
|
while (!InnerRanges.empty() && !OuterRanges.empty() && !NextRanges.empty() &&
|
|
|
|
InnerRanges.back() == OuterRanges.back() &&
|
|
|
|
InnerRanges.back() == NextRanges.back()) {
|
|
|
|
InnerRanges.pop_back();
|
|
|
|
OuterRanges.pop_back();
|
|
|
|
NextRanges.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inner and Next must have at least one more macro that Outer doesn't have,
|
|
|
|
// and that range must be common to both.
|
|
|
|
if (InnerRanges.empty() || NextRanges.empty() ||
|
|
|
|
InnerRanges.back() != NextRanges.back())
|
|
|
|
return;
|
|
|
|
|
2018-04-30 13:26:07 +08:00
|
|
|
diag(InnerRanges.back().getBegin(), "multiple statement macro used without "
|
|
|
|
"braces; some statements will be "
|
|
|
|
"unconditionally executed");
|
2016-04-15 05:15:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:16:29 +08:00
|
|
|
} // namespace bugprone
|
2016-04-15 05:15:57 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|