2015-06-17 22:19:35 +08:00
|
|
|
//===--- MacroRepeatedSideEffectsCheck.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
|
2015-06-17 22:19:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MacroRepeatedSideEffectsCheck.h"
|
2019-11-16 08:36:00 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2015-06-17 22:19:35 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2016-11-08 15:50:19 +08:00
|
|
|
#include "clang/Lex/MacroArgs.h"
|
2015-06-17 22:19:35 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2018-02-28 22:47:20 +08:00
|
|
|
namespace bugprone {
|
2015-06-17 22:19:35 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MacroRepeatedPPCallbacks : public PPCallbacks {
|
|
|
|
public:
|
2015-06-18 12:15:04 +08:00
|
|
|
MacroRepeatedPPCallbacks(ClangTidyCheck &Check, Preprocessor &PP)
|
|
|
|
: Check(Check), PP(PP) {}
|
2015-06-17 22:19:35 +08:00
|
|
|
|
|
|
|
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
|
|
|
|
SourceRange Range, const MacroArgs *Args) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ClangTidyCheck &Check;
|
|
|
|
Preprocessor &PP;
|
|
|
|
|
2015-07-02 15:49:55 +08:00
|
|
|
unsigned countArgumentExpansions(const MacroInfo *MI,
|
2015-06-17 22:19:35 +08:00
|
|
|
const IdentifierInfo *Arg) const;
|
|
|
|
|
2015-07-02 15:49:55 +08:00
|
|
|
bool hasSideEffects(const Token *ResultArgToks) const;
|
2015-06-17 22:19:35 +08:00
|
|
|
};
|
2015-07-02 15:49:55 +08:00
|
|
|
} // End of anonymous namespace.
|
2015-06-17 22:19:35 +08:00
|
|
|
|
|
|
|
void MacroRepeatedPPCallbacks::MacroExpands(const Token &MacroNameTok,
|
|
|
|
const MacroDefinition &MD,
|
|
|
|
SourceRange Range,
|
|
|
|
const MacroArgs *Args) {
|
|
|
|
// Ignore macro argument expansions.
|
|
|
|
if (!Range.getBegin().isFileID())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const MacroInfo *MI = MD.getMacroInfo();
|
|
|
|
|
|
|
|
// Bail out if the contents of the macro are containing keywords that are
|
|
|
|
// making the macro too complex.
|
|
|
|
if (std::find_if(
|
|
|
|
MI->tokens().begin(), MI->tokens().end(), [](const Token &T) {
|
2015-07-02 15:49:55 +08:00
|
|
|
return T.isOneOf(tok::kw_if, tok::kw_else, tok::kw_switch,
|
|
|
|
tok::kw_case, tok::kw_break, tok::kw_while,
|
|
|
|
tok::kw_do, tok::kw_for, tok::kw_continue,
|
|
|
|
tok::kw_goto, tok::kw_return);
|
2015-06-17 22:19:35 +08:00
|
|
|
}) != MI->tokens().end())
|
|
|
|
return;
|
|
|
|
|
2017-07-18 01:20:57 +08:00
|
|
|
for (unsigned ArgNo = 0U; ArgNo < MI->getNumParams(); ++ArgNo) {
|
|
|
|
const IdentifierInfo *Arg = *(MI->param_begin() + ArgNo);
|
2015-06-17 22:19:35 +08:00
|
|
|
const Token *ResultArgToks = Args->getUnexpArgument(ArgNo);
|
|
|
|
|
2015-07-02 15:49:55 +08:00
|
|
|
if (hasSideEffects(ResultArgToks) &&
|
|
|
|
countArgumentExpansions(MI, Arg) >= 2) {
|
2015-06-17 22:19:35 +08:00
|
|
|
Check.diag(ResultArgToks->getLocation(),
|
2016-04-07 22:55:25 +08:00
|
|
|
"side effects in the %ordinal0 macro argument %1 are "
|
2015-06-17 22:19:35 +08:00
|
|
|
"repeated in macro expansion")
|
2016-04-07 22:55:25 +08:00
|
|
|
<< (ArgNo + 1) << Arg;
|
2015-06-17 22:19:35 +08:00
|
|
|
Check.diag(MI->getDefinitionLoc(), "macro %0 defined here",
|
|
|
|
DiagnosticIDs::Note)
|
|
|
|
<< MacroNameTok.getIdentifierInfo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 15:49:55 +08:00
|
|
|
unsigned MacroRepeatedPPCallbacks::countArgumentExpansions(
|
2015-06-17 22:19:35 +08:00
|
|
|
const MacroInfo *MI, const IdentifierInfo *Arg) const {
|
2015-07-02 15:49:55 +08:00
|
|
|
// Current argument count. When moving forward to a different control-flow
|
|
|
|
// path this can decrease.
|
|
|
|
unsigned Current = 0;
|
|
|
|
// Max argument count.
|
|
|
|
unsigned Max = 0;
|
2015-06-17 22:19:35 +08:00
|
|
|
bool SkipParen = false;
|
|
|
|
int SkipParenCount = 0;
|
2015-07-02 15:49:55 +08:00
|
|
|
// Has a __builtin_constant_p been found?
|
|
|
|
bool FoundBuiltin = false;
|
2016-04-23 08:00:08 +08:00
|
|
|
bool PrevTokenIsHash = false;
|
2015-07-02 15:49:55 +08:00
|
|
|
// Count when "?" is reached. The "Current" will get this value when the ":"
|
|
|
|
// is reached.
|
|
|
|
std::stack<unsigned, SmallVector<unsigned, 8>> CountAtQuestion;
|
2015-06-17 22:19:35 +08:00
|
|
|
for (const auto &T : MI->tokens()) {
|
2015-07-02 15:49:55 +08:00
|
|
|
// The result of __builtin_constant_p(x) is 0 if x is a macro argument
|
|
|
|
// with side effects. If we see a __builtin_constant_p(x) followed by a
|
|
|
|
// "?" "&&" or "||", then we need to reason about control flow to report
|
|
|
|
// warnings correctly. Until such reasoning is added, bail out when this
|
|
|
|
// happens.
|
|
|
|
if (FoundBuiltin && T.isOneOf(tok::question, tok::ampamp, tok::pipepipe))
|
|
|
|
return Max;
|
|
|
|
|
2016-04-23 08:00:08 +08:00
|
|
|
// Skip stringified tokens.
|
|
|
|
if (T.is(tok::hash)) {
|
|
|
|
PrevTokenIsHash = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (PrevTokenIsHash) {
|
|
|
|
PrevTokenIsHash = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-02 15:49:55 +08:00
|
|
|
// Handling of ? and :.
|
|
|
|
if (T.is(tok::question)) {
|
|
|
|
CountAtQuestion.push(Current);
|
|
|
|
} else if (T.is(tok::colon)) {
|
|
|
|
if (CountAtQuestion.empty())
|
|
|
|
return 0;
|
|
|
|
Current = CountAtQuestion.top();
|
|
|
|
CountAtQuestion.pop();
|
|
|
|
}
|
|
|
|
|
2015-06-17 22:19:35 +08:00
|
|
|
// If current token is a parenthesis, skip it.
|
|
|
|
if (SkipParen) {
|
|
|
|
if (T.is(tok::l_paren))
|
|
|
|
SkipParenCount++;
|
|
|
|
else if (T.is(tok::r_paren))
|
|
|
|
SkipParenCount--;
|
|
|
|
SkipParen = (SkipParenCount != 0);
|
|
|
|
if (SkipParen)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentifierInfo *TII = T.getIdentifierInfo();
|
|
|
|
// If not existent, skip it.
|
|
|
|
if (TII == nullptr)
|
|
|
|
continue;
|
|
|
|
|
2015-07-02 15:49:55 +08:00
|
|
|
// If a __builtin_constant_p is found within the macro definition, don't
|
|
|
|
// count arguments inside the parentheses and remember that it has been
|
|
|
|
// seen in case there are "?", "&&" or "||" operators later.
|
|
|
|
if (TII->getBuiltinID() == Builtin::BI__builtin_constant_p) {
|
|
|
|
FoundBuiltin = true;
|
2015-06-17 22:19:35 +08:00
|
|
|
SkipParen = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If another macro is found within the macro definition, skip the macro
|
|
|
|
// and the eventual arguments.
|
|
|
|
if (TII->hasMacroDefinition()) {
|
|
|
|
const MacroInfo *M = PP.getMacroDefinition(TII).getMacroInfo();
|
|
|
|
if (M != nullptr && M->isFunctionLike())
|
|
|
|
SkipParen = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count argument.
|
2015-07-02 15:49:55 +08:00
|
|
|
if (TII == Arg) {
|
|
|
|
Current++;
|
|
|
|
if (Current > Max)
|
|
|
|
Max = Current;
|
|
|
|
}
|
2015-06-17 22:19:35 +08:00
|
|
|
}
|
2015-07-02 15:49:55 +08:00
|
|
|
return Max;
|
2015-06-17 22:19:35 +08:00
|
|
|
}
|
|
|
|
|
2015-07-02 15:49:55 +08:00
|
|
|
bool MacroRepeatedPPCallbacks::hasSideEffects(
|
2015-06-17 22:19:35 +08:00
|
|
|
const Token *ResultArgToks) const {
|
|
|
|
for (; ResultArgToks->isNot(tok::eof); ++ResultArgToks) {
|
|
|
|
if (ResultArgToks->isOneOf(tok::plusplus, tok::minusminus))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacroRepeatedSideEffectsCheck::registerPPCallbacks(
|
2019-03-23 02:58:12 +08:00
|
|
|
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
|
2019-08-15 07:52:23 +08:00
|
|
|
PP->addPPCallbacks(::std::make_unique<MacroRepeatedPPCallbacks>(*this, *PP));
|
2015-06-17 22:19:35 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:47:20 +08:00
|
|
|
} // namespace bugprone
|
2015-06-17 22:19:35 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|