10#include "clang/Basic/Builtins.h"
11#include "clang/Frontend/CompilerInstance.h"
12#include "clang/Lex/MacroArgs.h"
13#include "clang/Lex/PPCallbacks.h"
14#include "clang/Lex/Preprocessor.h"
19class MacroRepeatedPPCallbacks :
public PPCallbacks {
21 MacroRepeatedPPCallbacks(ClangTidyCheck &Check, Preprocessor &PP)
22 : Check(Check), PP(PP) {}
24 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &MD,
25 SourceRange
Range,
const MacroArgs *
Args)
override;
28 ClangTidyCheck &Check;
31 unsigned countArgumentExpansions(
const MacroInfo *MI,
32 const IdentifierInfo *Arg)
const;
34 bool hasSideEffects(
const Token *ResultArgToks)
const;
38void MacroRepeatedPPCallbacks::MacroExpands(
const Token &MacroNameTok,
39 const MacroDefinition &MD,
41 const MacroArgs *
Args) {
43 if (!
Range.getBegin().isFileID())
46 const MacroInfo *MI = MD.getMacroInfo();
50 if (llvm::any_of(MI->tokens(), [](
const Token &T) {
51 return T.isOneOf(tok::kw_if, tok::kw_else, tok::kw_switch, tok::kw_case,
52 tok::kw_break, tok::kw_while, tok::kw_do, tok::kw_for,
53 tok::kw_continue, tok::kw_goto, tok::kw_return);
57 for (
unsigned ArgNo = 0U; ArgNo < MI->getNumParams(); ++ArgNo) {
58 const IdentifierInfo *Arg = *(MI->param_begin() + ArgNo);
59 const Token *ResultArgToks =
Args->getUnexpArgument(ArgNo);
61 if (hasSideEffects(ResultArgToks) &&
62 countArgumentExpansions(MI, Arg) >= 2) {
63 Check.diag(ResultArgToks->getLocation(),
64 "side effects in the %ordinal0 macro argument %1 are "
65 "repeated in macro expansion")
66 << (ArgNo + 1) << Arg;
67 Check.diag(MI->getDefinitionLoc(),
"macro %0 defined here",
69 << MacroNameTok.getIdentifierInfo();
74unsigned MacroRepeatedPPCallbacks::countArgumentExpansions(
75 const MacroInfo *MI,
const IdentifierInfo *Arg)
const {
81 bool SkipParen =
false;
82 int SkipParenCount = 0;
84 bool FoundBuiltin =
false;
85 bool PrevTokenIsHash =
false;
88 std::stack<unsigned, SmallVector<unsigned, 8>> CountAtQuestion;
89 for (
const auto &T : MI->tokens()) {
95 if (FoundBuiltin && T.isOneOf(tok::question, tok::ampamp, tok::pipepipe))
99 if (T.is(tok::hash)) {
100 PrevTokenIsHash =
true;
103 if (PrevTokenIsHash) {
104 PrevTokenIsHash =
false;
109 if (T.is(tok::question)) {
110 CountAtQuestion.push(Current);
111 }
else if (T.is(tok::colon)) {
112 if (CountAtQuestion.empty())
114 Current = CountAtQuestion.top();
115 CountAtQuestion.pop();
120 if (T.is(tok::l_paren))
122 else if (T.is(tok::r_paren))
124 SkipParen = (SkipParenCount != 0);
129 IdentifierInfo *TII = T.getIdentifierInfo();
137 if (TII->getBuiltinID() == Builtin::BI__builtin_constant_p) {
145 if (TII->hasMacroDefinition()) {
146 const MacroInfo *
M =
PP.getMacroDefinition(TII).getMacroInfo();
147 if (
M !=
nullptr &&
M->isFunctionLike())
162bool MacroRepeatedPPCallbacks::hasSideEffects(
163 const Token *ResultArgToks)
const {
164 for (; ResultArgToks->isNot(tok::eof); ++ResultArgToks) {
165 if (ResultArgToks->isOneOf(tok::plusplus, tok::minusminus))
172 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
173 PP->addPPCallbacks(::std::make_unique<MacroRepeatedPPCallbacks>(*
this, *
PP));
CharSourceRange Range
SourceRange for the file name.
const google::protobuf::Message & M
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.