annotate clang-tools-extra/clang-tidy/bugprone/MacroRepeatedSideEffectsCheck.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- MacroRepeatedSideEffectsCheck.cpp - clang-tidy--------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "MacroRepeatedSideEffectsCheck.h"
anatofuz
parents:
diff changeset
10 #include "clang/Basic/Builtins.h"
anatofuz
parents:
diff changeset
11 #include "clang/Frontend/CompilerInstance.h"
anatofuz
parents:
diff changeset
12 #include "clang/Lex/MacroArgs.h"
anatofuz
parents:
diff changeset
13 #include "clang/Lex/PPCallbacks.h"
anatofuz
parents:
diff changeset
14 #include "clang/Lex/Preprocessor.h"
anatofuz
parents:
diff changeset
15
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
16 namespace clang::tidy::bugprone {
150
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 namespace {
anatofuz
parents:
diff changeset
19 class MacroRepeatedPPCallbacks : public PPCallbacks {
anatofuz
parents:
diff changeset
20 public:
anatofuz
parents:
diff changeset
21 MacroRepeatedPPCallbacks(ClangTidyCheck &Check, Preprocessor &PP)
anatofuz
parents:
diff changeset
22 : Check(Check), PP(PP) {}
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
anatofuz
parents:
diff changeset
25 SourceRange Range, const MacroArgs *Args) override;
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 private:
anatofuz
parents:
diff changeset
28 ClangTidyCheck &Check;
anatofuz
parents:
diff changeset
29 Preprocessor &PP;
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 unsigned countArgumentExpansions(const MacroInfo *MI,
anatofuz
parents:
diff changeset
32 const IdentifierInfo *Arg) const;
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 bool hasSideEffects(const Token *ResultArgToks) const;
anatofuz
parents:
diff changeset
35 };
anatofuz
parents:
diff changeset
36 } // End of anonymous namespace.
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 void MacroRepeatedPPCallbacks::MacroExpands(const Token &MacroNameTok,
anatofuz
parents:
diff changeset
39 const MacroDefinition &MD,
anatofuz
parents:
diff changeset
40 SourceRange Range,
anatofuz
parents:
diff changeset
41 const MacroArgs *Args) {
anatofuz
parents:
diff changeset
42 // Ignore macro argument expansions.
anatofuz
parents:
diff changeset
43 if (!Range.getBegin().isFileID())
anatofuz
parents:
diff changeset
44 return;
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 const MacroInfo *MI = MD.getMacroInfo();
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 // Bail out if the contents of the macro are containing keywords that are
anatofuz
parents:
diff changeset
49 // making the macro too complex.
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
50 if (llvm::any_of(MI->tokens(), [](const Token &T) {
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
51 return T.isOneOf(tok::kw_if, tok::kw_else, tok::kw_switch, tok::kw_case,
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
52 tok::kw_break, tok::kw_while, tok::kw_do, tok::kw_for,
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
53 tok::kw_continue, tok::kw_goto, tok::kw_return);
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
54 }))
150
anatofuz
parents:
diff changeset
55 return;
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 for (unsigned ArgNo = 0U; ArgNo < MI->getNumParams(); ++ArgNo) {
anatofuz
parents:
diff changeset
58 const IdentifierInfo *Arg = *(MI->param_begin() + ArgNo);
anatofuz
parents:
diff changeset
59 const Token *ResultArgToks = Args->getUnexpArgument(ArgNo);
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 if (hasSideEffects(ResultArgToks) &&
anatofuz
parents:
diff changeset
62 countArgumentExpansions(MI, Arg) >= 2) {
anatofuz
parents:
diff changeset
63 Check.diag(ResultArgToks->getLocation(),
anatofuz
parents:
diff changeset
64 "side effects in the %ordinal0 macro argument %1 are "
anatofuz
parents:
diff changeset
65 "repeated in macro expansion")
anatofuz
parents:
diff changeset
66 << (ArgNo + 1) << Arg;
anatofuz
parents:
diff changeset
67 Check.diag(MI->getDefinitionLoc(), "macro %0 defined here",
anatofuz
parents:
diff changeset
68 DiagnosticIDs::Note)
anatofuz
parents:
diff changeset
69 << MacroNameTok.getIdentifierInfo();
anatofuz
parents:
diff changeset
70 }
anatofuz
parents:
diff changeset
71 }
anatofuz
parents:
diff changeset
72 }
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 unsigned MacroRepeatedPPCallbacks::countArgumentExpansions(
anatofuz
parents:
diff changeset
75 const MacroInfo *MI, const IdentifierInfo *Arg) const {
anatofuz
parents:
diff changeset
76 // Current argument count. When moving forward to a different control-flow
anatofuz
parents:
diff changeset
77 // path this can decrease.
anatofuz
parents:
diff changeset
78 unsigned Current = 0;
anatofuz
parents:
diff changeset
79 // Max argument count.
anatofuz
parents:
diff changeset
80 unsigned Max = 0;
anatofuz
parents:
diff changeset
81 bool SkipParen = false;
anatofuz
parents:
diff changeset
82 int SkipParenCount = 0;
anatofuz
parents:
diff changeset
83 // Has a __builtin_constant_p been found?
anatofuz
parents:
diff changeset
84 bool FoundBuiltin = false;
anatofuz
parents:
diff changeset
85 bool PrevTokenIsHash = false;
anatofuz
parents:
diff changeset
86 // Count when "?" is reached. The "Current" will get this value when the ":"
anatofuz
parents:
diff changeset
87 // is reached.
anatofuz
parents:
diff changeset
88 std::stack<unsigned, SmallVector<unsigned, 8>> CountAtQuestion;
anatofuz
parents:
diff changeset
89 for (const auto &T : MI->tokens()) {
anatofuz
parents:
diff changeset
90 // The result of __builtin_constant_p(x) is 0 if x is a macro argument
anatofuz
parents:
diff changeset
91 // with side effects. If we see a __builtin_constant_p(x) followed by a
anatofuz
parents:
diff changeset
92 // "?" "&&" or "||", then we need to reason about control flow to report
anatofuz
parents:
diff changeset
93 // warnings correctly. Until such reasoning is added, bail out when this
anatofuz
parents:
diff changeset
94 // happens.
anatofuz
parents:
diff changeset
95 if (FoundBuiltin && T.isOneOf(tok::question, tok::ampamp, tok::pipepipe))
anatofuz
parents:
diff changeset
96 return Max;
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98 // Skip stringified tokens.
anatofuz
parents:
diff changeset
99 if (T.is(tok::hash)) {
anatofuz
parents:
diff changeset
100 PrevTokenIsHash = true;
anatofuz
parents:
diff changeset
101 continue;
anatofuz
parents:
diff changeset
102 }
anatofuz
parents:
diff changeset
103 if (PrevTokenIsHash) {
anatofuz
parents:
diff changeset
104 PrevTokenIsHash = false;
anatofuz
parents:
diff changeset
105 continue;
anatofuz
parents:
diff changeset
106 }
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 // Handling of ? and :.
anatofuz
parents:
diff changeset
109 if (T.is(tok::question)) {
anatofuz
parents:
diff changeset
110 CountAtQuestion.push(Current);
anatofuz
parents:
diff changeset
111 } else if (T.is(tok::colon)) {
anatofuz
parents:
diff changeset
112 if (CountAtQuestion.empty())
anatofuz
parents:
diff changeset
113 return 0;
anatofuz
parents:
diff changeset
114 Current = CountAtQuestion.top();
anatofuz
parents:
diff changeset
115 CountAtQuestion.pop();
anatofuz
parents:
diff changeset
116 }
anatofuz
parents:
diff changeset
117
anatofuz
parents:
diff changeset
118 // If current token is a parenthesis, skip it.
anatofuz
parents:
diff changeset
119 if (SkipParen) {
anatofuz
parents:
diff changeset
120 if (T.is(tok::l_paren))
anatofuz
parents:
diff changeset
121 SkipParenCount++;
anatofuz
parents:
diff changeset
122 else if (T.is(tok::r_paren))
anatofuz
parents:
diff changeset
123 SkipParenCount--;
anatofuz
parents:
diff changeset
124 SkipParen = (SkipParenCount != 0);
anatofuz
parents:
diff changeset
125 if (SkipParen)
anatofuz
parents:
diff changeset
126 continue;
anatofuz
parents:
diff changeset
127 }
anatofuz
parents:
diff changeset
128
anatofuz
parents:
diff changeset
129 IdentifierInfo *TII = T.getIdentifierInfo();
anatofuz
parents:
diff changeset
130 // If not existent, skip it.
anatofuz
parents:
diff changeset
131 if (TII == nullptr)
anatofuz
parents:
diff changeset
132 continue;
anatofuz
parents:
diff changeset
133
anatofuz
parents:
diff changeset
134 // If a __builtin_constant_p is found within the macro definition, don't
anatofuz
parents:
diff changeset
135 // count arguments inside the parentheses and remember that it has been
anatofuz
parents:
diff changeset
136 // seen in case there are "?", "&&" or "||" operators later.
anatofuz
parents:
diff changeset
137 if (TII->getBuiltinID() == Builtin::BI__builtin_constant_p) {
anatofuz
parents:
diff changeset
138 FoundBuiltin = true;
anatofuz
parents:
diff changeset
139 SkipParen = true;
anatofuz
parents:
diff changeset
140 continue;
anatofuz
parents:
diff changeset
141 }
anatofuz
parents:
diff changeset
142
anatofuz
parents:
diff changeset
143 // If another macro is found within the macro definition, skip the macro
anatofuz
parents:
diff changeset
144 // and the eventual arguments.
anatofuz
parents:
diff changeset
145 if (TII->hasMacroDefinition()) {
anatofuz
parents:
diff changeset
146 const MacroInfo *M = PP.getMacroDefinition(TII).getMacroInfo();
anatofuz
parents:
diff changeset
147 if (M != nullptr && M->isFunctionLike())
anatofuz
parents:
diff changeset
148 SkipParen = true;
anatofuz
parents:
diff changeset
149 continue;
anatofuz
parents:
diff changeset
150 }
anatofuz
parents:
diff changeset
151
anatofuz
parents:
diff changeset
152 // Count argument.
anatofuz
parents:
diff changeset
153 if (TII == Arg) {
anatofuz
parents:
diff changeset
154 Current++;
anatofuz
parents:
diff changeset
155 if (Current > Max)
anatofuz
parents:
diff changeset
156 Max = Current;
anatofuz
parents:
diff changeset
157 }
anatofuz
parents:
diff changeset
158 }
anatofuz
parents:
diff changeset
159 return Max;
anatofuz
parents:
diff changeset
160 }
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 bool MacroRepeatedPPCallbacks::hasSideEffects(
anatofuz
parents:
diff changeset
163 const Token *ResultArgToks) const {
anatofuz
parents:
diff changeset
164 for (; ResultArgToks->isNot(tok::eof); ++ResultArgToks) {
anatofuz
parents:
diff changeset
165 if (ResultArgToks->isOneOf(tok::plusplus, tok::minusminus))
anatofuz
parents:
diff changeset
166 return true;
anatofuz
parents:
diff changeset
167 }
anatofuz
parents:
diff changeset
168 return false;
anatofuz
parents:
diff changeset
169 }
anatofuz
parents:
diff changeset
170
anatofuz
parents:
diff changeset
171 void MacroRepeatedSideEffectsCheck::registerPPCallbacks(
anatofuz
parents:
diff changeset
172 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
anatofuz
parents:
diff changeset
173 PP->addPPCallbacks(::std::make_unique<MacroRepeatedPPCallbacks>(*this, *PP));
anatofuz
parents:
diff changeset
174 }
anatofuz
parents:
diff changeset
175
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
176 } // namespace clang::tidy::bugprone