clang-tools 24.0.0git
MacroConditionCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/LexerUtils.h"
11#include "clang/Basic/DiagnosticIDs.h"
12#include "clang/Lex/Lexer.h"
13#include "clang/Lex/MacroInfo.h"
14#include "clang/Lex/PPCallbacks.h"
15#include "clang/Lex/Preprocessor.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallVector.h"
19#include <memory>
20#include <optional>
21#include <string>
22#include <utility>
23
24namespace clang::tidy::bugprone {
25
26namespace {
27class MacroConditionCallbacks : public PPCallbacks {
28public:
29 MacroConditionCallbacks(MacroConditionCheck *Check, const SourceManager &SM,
30 Preprocessor &PP, bool CheckDoubleUnderscoreMacros)
31 : Check(Check), SM(SM), PP(PP),
32 CheckDoubleUnderscoreMacros(CheckDoubleUnderscoreMacros) {}
33
34 void If(SourceLocation Loc, SourceRange ConditionRange,
35 ConditionValueKind ConditionValue) override;
36 void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
37 const MacroDefinition &MD) override;
38 void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
39 const MacroDefinition &MD) override;
40 void Elif(SourceLocation Loc, SourceRange ConditionRange,
41 ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
42 void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
43 const MacroDefinition &MD) override;
44 void Elifdef(SourceLocation Loc, SourceRange ConditionRange,
45 SourceLocation IfLoc) override;
46 void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
47 const MacroDefinition &MD) override;
48 void Elifndef(SourceLocation Loc, SourceRange ConditionRange,
49 SourceLocation IfLoc) override;
50 void Else(SourceLocation Loc, SourceLocation IfLoc) override;
51 void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
52 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
53
54private:
55 enum class ReferenceKind { Definition, NegatedDefinition, Value };
56
57 struct MacroReference {
58 std::string Name;
59 SourceLocation Loc;
60 ReferenceKind Kind;
61 };
62
63 struct MacroUsage {
64 SourceLocation DefinitionTestLoc;
65 SourceLocation ValueTestLoc;
66 bool Diagnosed = false;
67 };
68
69 struct DefinitionGuard {
70 std::string Name;
71 FileID File;
72 };
73
74 using ConditionReferences = SmallVector<MacroReference, 4>;
75 using DefinitionGuards = SmallVector<DefinitionGuard, 2>;
76
77 ConditionReferences referencesInCondition(SourceRange ConditionRange) const;
78 DefinitionGuards
79 definitionGuardsInCondition(SourceRange ConditionRange) const;
80 std::optional<std::string>
81 defaultedMacroInSkippedRange(SourceRange Range) const;
82 MacroReference referenceFromRange(SourceRange Range,
83 SourceLocation Loc) const;
84 std::optional<std::pair<SourceLocation, StringRef>>
85 conditionalBody(SourceLocation Loc) const;
86 bool branchUnconditionallyErrors(SourceLocation Loc) const;
87 bool branchUsesMacroValue(SourceLocation Loc, StringRef Name) const;
88 bool isEnclosedByDefinitionGuard(const MacroReference &Reference) const;
89 void beginConditional(DefinitionGuards Guards);
90 void changeConditional(DefinitionGuards Guards);
91 bool isIgnoredIdentifier(StringRef Name) const;
92 void processReferences(const ConditionReferences &References);
93 void processDefinitionReference(StringRef Name, SourceLocation Loc);
94 void checkReference(const MacroReference &Reference);
95
96 using FileUsages = llvm::DenseMap<FileID, MacroUsage>;
97 llvm::DenseMap<const MacroInfo *, FileUsages> MacroUsages;
98 SmallVector<DefinitionGuards, 8> ConditionalGuards;
99 MacroConditionCheck *Check;
100 const SourceManager &SM;
101 Preprocessor &PP;
102 const bool CheckDoubleUnderscoreMacros;
103};
104
105} // namespace
106
108
109static bool isOperatorToken(const Token &Token, tok::TokenKind Kind,
110 StringRef Alternative,
111 const LangOptions &LangOpts) {
112 return Token.is(Kind) ||
113 (LangOpts.CXXOperatorNames && Token.is(tok::raw_identifier) &&
114 Token.getRawIdentifier() == Alternative);
115}
116
117static bool skipFunctionLikeInvocation(ArrayRef<Token> Tokens, size_t &Index) {
118 if (Index + 1 >= Tokens.size() || Tokens[Index + 1].isNot(tok::l_paren))
119 return false;
120
121 unsigned ParenthesisDepth = 0;
122 do {
123 ++Index;
124 if (Tokens[Index].is(tok::l_paren))
125 ++ParenthesisDepth;
126 else if (Tokens[Index].is(tok::r_paren))
127 --ParenthesisDepth;
128 } while (Index + 1 < Tokens.size() && ParenthesisDepth != 0);
129 return true;
130}
131
132static bool isNegatedDefined(ArrayRef<Token> Tokens, size_t Index,
133 const LangOptions &LangOpts) {
134 unsigned Negations = 0;
135 while (Index > 0) {
136 while (Index > 0 && Tokens[Index - 1].is(tok::l_paren))
137 --Index;
138 if (Index == 0 ||
139 !isOperatorToken(Tokens[Index - 1], tok::exclaim, "not", LangOpts))
140 break;
141 --Index;
142 ++Negations;
143 }
144 return Negations % 2 != 0;
145}
146
147MacroConditionCallbacks::ConditionReferences
148MacroConditionCallbacks::referencesInCondition(
149 SourceRange ConditionRange) const {
150 ConditionReferences References;
151 const std::vector<Token> Tokens = utils::lexer::getRawTokens(
152 CharSourceRange::getTokenRange(ConditionRange), SM, PP.getLangOpts());
153
154 for (size_t Index = 0; Index < Tokens.size(); ++Index) {
155 const Token &Current = Tokens[Index];
156 if (!Current.is(tok::raw_identifier))
157 continue;
158
159 StringRef Name = Current.getRawIdentifier();
160 if (Name != "defined") {
161 if (skipFunctionLikeInvocation(Tokens, Index))
162 continue;
163 if (!isIgnoredIdentifier(Name))
164 References.push_back(
165 {Name.str(), Current.getLocation(), ReferenceKind::Value});
166 continue;
167 }
168
169 const SourceLocation DefinedLoc = Current.getLocation();
170 const bool IsNegated = isNegatedDefined(Tokens, Index, PP.getLangOpts());
171 ++Index;
172 if (Index < Tokens.size() && Tokens[Index].is(tok::l_paren))
173 ++Index;
174 if (Index < Tokens.size() && Tokens[Index].is(tok::raw_identifier))
175 References.push_back({Tokens[Index].getRawIdentifier().str(), DefinedLoc,
176 IsNegated ? ReferenceKind::NegatedDefinition
177 : ReferenceKind::Definition});
178 }
179 return References;
180}
181
182static bool parseDefinitionGuardExpression(ArrayRef<Token> Tokens,
183 size_t &Index,
184 SmallVectorImpl<std::string> &Guards,
185 const LangOptions &LangOpts);
186
187static bool parseDefinitionGuardPrimary(ArrayRef<Token> Tokens, size_t &Index,
188 SmallVectorImpl<std::string> &Guards,
189 const LangOptions &LangOpts) {
190 if (Index >= Tokens.size())
191 return false;
192
193 if (Tokens[Index].is(tok::l_paren)) {
194 ++Index;
195 if (!parseDefinitionGuardExpression(Tokens, Index, Guards, LangOpts) ||
196 Index >= Tokens.size() || Tokens[Index].isNot(tok::r_paren))
197 return false;
198 ++Index;
199 return true;
200 }
201
202 if (getTokenName(Tokens[Index]) != "defined")
203 return false;
204 ++Index;
205
206 const bool Parenthesized =
207 Index < Tokens.size() && Tokens[Index].is(tok::l_paren);
208 if (Parenthesized)
209 ++Index;
210 if (Index >= Tokens.size() || Tokens[Index].isNot(tok::raw_identifier))
211 return false;
212 Guards.push_back(Tokens[Index++].getRawIdentifier().str());
213 if (Parenthesized) {
214 if (Index >= Tokens.size() || Tokens[Index].isNot(tok::r_paren))
215 return false;
216 ++Index;
217 }
218 return true;
219}
220
221static bool parseDefinitionGuardExpression(ArrayRef<Token> Tokens,
222 size_t &Index,
223 SmallVectorImpl<std::string> &Guards,
224 const LangOptions &LangOpts) {
225 if (!parseDefinitionGuardPrimary(Tokens, Index, Guards, LangOpts))
226 return false;
227 while (Index < Tokens.size() &&
228 isOperatorToken(Tokens[Index], tok::ampamp, "and", LangOpts)) {
229 ++Index;
230 if (!parseDefinitionGuardPrimary(Tokens, Index, Guards, LangOpts))
231 return false;
232 }
233 return true;
234}
235
236MacroConditionCallbacks::DefinitionGuards
237MacroConditionCallbacks::definitionGuardsInCondition(
238 SourceRange ConditionRange) const {
239 const std::vector<Token> Tokens = utils::lexer::getRawTokens(
240 CharSourceRange::getTokenRange(ConditionRange), SM, PP.getLangOpts());
242 size_t Index = 0;
243 if (!parseDefinitionGuardExpression(Tokens, Index, Names, PP.getLangOpts()) ||
244 Index != Tokens.size())
245 return {};
246
247 DefinitionGuards Guards;
248 const FileID File =
249 SM.getFileID(SM.getSpellingLoc(ConditionRange.getBegin()));
250 for (std::string &Name : Names)
251 Guards.push_back({std::move(Name), File});
252 return Guards;
253}
254
255std::optional<std::pair<SourceLocation, StringRef>>
256MacroConditionCallbacks::conditionalBody(SourceLocation Loc) const {
257 const SourceLocation BeginLoc = SM.getExpansionLoc(Loc);
258 if (BeginLoc.isInvalid())
259 return std::nullopt;
260
261 const std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(BeginLoc);
262 bool Invalid = false;
263 StringRef Buffer = SM.getBufferData(Decomposed.first, &Invalid);
264 if (Invalid || Decomposed.second >= Buffer.size())
265 return std::nullopt;
266
267 size_t Body = Decomposed.second;
268 while (Body < Buffer.size()) {
269 if (Buffer[Body] != '\r' && Buffer[Body] != '\n') {
270 ++Body;
271 continue;
272 }
273 if (Body > Decomposed.second && Buffer[Body - 1] == '\\') {
274 if (Buffer[Body] == '\r' && Body + 1 < Buffer.size() &&
275 Buffer[Body + 1] == '\n')
276 ++Body;
277 ++Body;
278 continue;
279 }
280 break;
281 }
282 while (Body < Buffer.size() && (Buffer[Body] == '\r' || Buffer[Body] == '\n'))
283 ++Body;
284
285 const SourceLocation BodyLoc =
286 SM.getLocForStartOfFile(Decomposed.first).getLocWithOffset(Body);
287 return std::make_pair(BodyLoc, Buffer.drop_front(Body));
288}
289
290bool MacroConditionCallbacks::branchUnconditionallyErrors(
291 SourceLocation Loc) const {
292 const auto Body = conditionalBody(Loc);
293 if (!Body)
294 return false;
295
296 std::string Text = Body->second.str();
297 Lexer Lex(Body->first, PP.getLangOpts(), Text.data(), Text.data(),
298 Text.data() + Text.size());
299 unsigned Depth = 1;
300 Token Tok;
301 while (!Lex.LexFromRawLexer(Tok)) {
302 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
303 continue;
304
305 Token DirectiveTok;
306 if (Lex.LexFromRawLexer(DirectiveTok))
307 return false;
308 StringRef Directive = getTokenName(DirectiveTok);
309 if (Directive == "if" || Directive == "ifdef" || Directive == "ifndef") {
310 ++Depth;
311 continue;
312 }
313 if (Directive == "endif") {
314 if (--Depth == 0)
315 return false;
316 continue;
317 }
318 if (Depth != 1)
319 continue;
320 if (Directive == "else" || Directive.starts_with("elif"))
321 return false;
322 if (Directive == "error")
323 return true;
324 }
325 return false;
326}
327
328bool MacroConditionCallbacks::branchUsesMacroValue(SourceLocation Loc,
329 StringRef Name) const {
330 const auto Body = conditionalBody(Loc);
331 if (!Body)
332 return false;
333
334 std::string Text = Body->second.str();
335 Lexer Lex(Body->first, PP.getLangOpts(), Text.data(), Text.data(),
336 Text.data() + Text.size());
337 unsigned Depth = 1;
338 bool InDirective = false;
339 Token Tok;
340 while (!Lex.LexFromRawLexer(Tok)) {
341 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
342 Token DirectiveTok;
343 if (Lex.LexFromRawLexer(DirectiveTok))
344 return false;
345 StringRef Directive = getTokenName(DirectiveTok);
346 if (Directive == "if" || Directive == "ifdef" || Directive == "ifndef") {
347 ++Depth;
348 } else if (Directive == "endif") {
349 if (--Depth == 0)
350 return false;
351 } else if (Depth == 1 &&
352 (Directive == "else" || Directive.starts_with("elif"))) {
353 return false;
354 }
355 InDirective = true;
356 continue;
357 }
358 if (Tok.isAtStartOfLine())
359 InDirective = false;
360 if (!InDirective && Tok.is(tok::raw_identifier) &&
361 Tok.getRawIdentifier() == Name)
362 return true;
363 }
364 return false;
365}
366
367std::optional<std::string>
368MacroConditionCallbacks::defaultedMacroInSkippedRange(SourceRange Range) const {
369 const SourceLocation BeginLoc = SM.getExpansionLoc(Range.getBegin());
370 const SourceLocation EndLoc = SM.getExpansionLoc(Range.getEnd());
371 if (BeginLoc.isInvalid() || EndLoc.isInvalid())
372 return std::nullopt;
373
374 const std::pair<FileID, unsigned> Begin = SM.getDecomposedLoc(BeginLoc);
375 const std::pair<FileID, unsigned> End = SM.getDecomposedLoc(EndLoc);
376 if (Begin.first != End.first || Begin.second >= End.second)
377 return std::nullopt;
378
379 bool Invalid = false;
380 StringRef Buffer = SM.getBufferData(Begin.first, &Invalid);
381 if (Invalid || End.second > Buffer.size())
382 return std::nullopt;
383
384 std::string Text = Buffer.slice(Begin.second, End.second).str();
385 Lexer Lex(BeginLoc, PP.getLangOpts(), Text.data(), Text.data(),
386 Text.data() + Text.size());
387 SmallVector<Token, 32> Tokens;
388 Token Tok;
389 bool AtEnd = false;
390 do {
391 AtEnd = Lex.LexFromRawLexer(Tok);
392 if (Tok.isNot(tok::eof))
393 Tokens.push_back(Tok);
394 } while (!AtEnd);
395
396 if (Tokens.size() < 3 || Tokens[0].isNot(tok::hash) ||
397 !Tokens[0].isAtStartOfLine())
398 return std::nullopt;
399
400 size_t Index = 2;
401 std::string Name;
402 StringRef Directive = getTokenName(Tokens[1]);
403 if (Directive == "ifndef") {
404 if (!Tokens[Index].is(tok::raw_identifier))
405 return std::nullopt;
406 Name = Tokens[Index++].getRawIdentifier().str();
407 } else if (Directive == "if") {
408 if (!isOperatorToken(Tokens[Index], tok::exclaim, "not", PP.getLangOpts()))
409 return std::nullopt;
410 ++Index;
411 if (Index >= Tokens.size() || getTokenName(Tokens[Index]) != "defined")
412 return std::nullopt;
413 ++Index;
414 if (Index < Tokens.size() && Tokens[Index].is(tok::l_paren))
415 ++Index;
416 if (Index >= Tokens.size() || Tokens[Index].isNot(tok::raw_identifier))
417 return std::nullopt;
418 Name = Tokens[Index++].getRawIdentifier().str();
419 if (Index < Tokens.size() && Tokens[Index].is(tok::r_paren))
420 ++Index;
421 } else {
422 return std::nullopt;
423 }
424
425 if (Index < Tokens.size() && !Tokens[Index].isAtStartOfLine())
426 return std::nullopt;
427
428 unsigned Depth = 1;
429 for (; Index + 1 < Tokens.size(); ++Index) {
430 if (Tokens[Index].isNot(tok::hash) || !Tokens[Index].isAtStartOfLine())
431 continue;
432
433 StringRef NestedDirective = getTokenName(Tokens[++Index]);
434 if (NestedDirective == "if" || NestedDirective == "ifdef" ||
435 NestedDirective == "ifndef") {
436 ++Depth;
437 continue;
438 }
439 if (NestedDirective == "endif") {
440 if (--Depth == 0)
441 break;
442 continue;
443 }
444 if (Depth != 1)
445 continue;
446 if (NestedDirective == "else" || NestedDirective.starts_with("elif"))
447 break;
448 if (NestedDirective != "define" || Index + 2 >= Tokens.size() ||
449 getTokenName(Tokens[Index + 1]) != Name ||
450 Tokens[Index + 2].isAtStartOfLine())
451 continue;
452
453 const Token &MacroName = Tokens[Index + 1];
454 const Token &Replacement = Tokens[Index + 2];
455 const SourceLocation MacroNameEnd = Lexer::getLocForEndOfToken(
456 MacroName.getLocation(), 0, SM, PP.getLangOpts());
457 if (Replacement.is(tok::l_paren) &&
458 MacroNameEnd == Replacement.getLocation())
459 continue;
460 return Name;
461 }
462 return std::nullopt;
463}
464
465MacroConditionCallbacks::MacroReference
466MacroConditionCallbacks::referenceFromRange(SourceRange Range,
467 SourceLocation Loc) const {
468 ConditionReferences References = referencesInCondition(Range);
469 if (!References.empty()) {
470 References.front().Loc = Loc;
471 References.front().Kind = ReferenceKind::Definition;
472 return std::move(References.front());
473 }
474 return {{}, Loc, ReferenceKind::Definition};
475}
476
477bool MacroConditionCallbacks::isIgnoredIdentifier(StringRef Name) const {
478 const IdentifierInfo *Info = PP.getIdentifierInfo(Name);
479 return Name == "true" || Name == "false" ||
480 Info->isCPlusPlusOperatorKeyword();
481}
482
483bool MacroConditionCallbacks::isEnclosedByDefinitionGuard(
484 const MacroReference &Reference) const {
485 const FileID File = SM.getFileID(SM.getSpellingLoc(Reference.Loc));
486 for (const DefinitionGuards &Guards : reverse(ConditionalGuards)) {
487 for (const DefinitionGuard &Guard : Guards)
488 if (Guard.File == File && Guard.Name == Reference.Name)
489 return true;
490 }
491 return false;
492}
493
494void MacroConditionCallbacks::beginConditional(DefinitionGuards Guards) {
495 ConditionalGuards.push_back(std::move(Guards));
496}
497
498void MacroConditionCallbacks::changeConditional(DefinitionGuards Guards) {
499 if (!ConditionalGuards.empty())
500 ConditionalGuards.back() = std::move(Guards);
501}
502
503void MacroConditionCallbacks::processReferences(
504 const ConditionReferences &References) {
505 for (const MacroReference &Reference : References) {
506 bool IsCompoundReference = false;
507 for (const MacroReference &Other : References) {
508 if (Reference.Name == Other.Name && Reference.Kind != Other.Kind) {
509 IsCompoundReference = true;
510 break;
511 }
512 }
513 if (!IsCompoundReference)
514 checkReference(Reference);
515 }
516}
517
518void MacroConditionCallbacks::processDefinitionReference(StringRef Name,
519 SourceLocation Loc) {
520 if (!Name.empty())
521 checkReference({Name.str(), Loc, ReferenceKind::Definition});
522}
523
524void MacroConditionCallbacks::checkReference(const MacroReference &Reference) {
525 if ((!CheckDoubleUnderscoreMacros &&
526 StringRef(Reference.Name).starts_with("__")) ||
527 Reference.Kind == ReferenceKind::NegatedDefinition ||
528 (Reference.Kind == ReferenceKind::Value &&
529 isEnclosedByDefinitionGuard(Reference)))
530 return;
531
532 const IdentifierInfo *Info = PP.getIdentifierInfo(Reference.Name);
533 const MacroInfo *Macro = PP.getMacroDefinition(Info).getMacroInfo();
534 if (!Macro || Macro->isBuiltinMacro() || Macro->isFunctionLike() ||
535 Macro->tokens().empty())
536 return;
537 if (Reference.Kind == ReferenceKind::Definition &&
538 branchUsesMacroValue(Reference.Loc, Reference.Name))
539 return;
540
541 const SourceLocation SpellingLoc = SM.getSpellingLoc(Reference.Loc);
542 if (SpellingLoc.isInvalid())
543 return;
544
545 MacroUsage &Usage = MacroUsages[Macro][SM.getFileID(SpellingLoc)];
546 const bool IsDefinition = Reference.Kind == ReferenceKind::Definition;
547 SourceLocation &CurrentLoc =
548 IsDefinition ? Usage.DefinitionTestLoc : Usage.ValueTestLoc;
549 const SourceLocation OtherLoc =
550 IsDefinition ? Usage.ValueTestLoc : Usage.DefinitionTestLoc;
551 if (CurrentLoc.isInvalid())
552 CurrentLoc = Reference.Loc;
553 if (Usage.Diagnosed || OtherLoc.isInvalid())
554 return;
555
556 const unsigned Kind = IsDefinition ? 0 : 1;
557 Check->diag(Reference.Loc,
558 "Macro '%0' checked here for %select{definition|value}1 after "
559 "being checked for %select{value|definition}1")
560 << Reference.Name << Kind;
561 Check->diag(OtherLoc,
562 "Macro '%0' first checked here for "
563 "%select{value|definition}1",
564 DiagnosticIDs::Note)
565 << Reference.Name << Kind;
566 Usage.Diagnosed = true;
567}
568
569void MacroConditionCallbacks::If(SourceLocation Loc, SourceRange ConditionRange,
570 ConditionValueKind ConditionValue) {
571 if (!branchUnconditionallyErrors(Loc))
572 processReferences(referencesInCondition(ConditionRange));
573 beginConditional(definitionGuardsInCondition(ConditionRange));
574}
575
576void MacroConditionCallbacks::Ifdef(SourceLocation Loc,
577 const Token &MacroNameTok,
578 const MacroDefinition &MD) {
579 const StringRef Name = getTokenName(MacroNameTok);
580 if (!branchUnconditionallyErrors(Loc))
581 processDefinitionReference(Name, Loc);
582 DefinitionGuards Guards;
583 if (!Name.empty())
584 Guards.push_back({Name.str(), SM.getFileID(SM.getSpellingLoc(
585 MacroNameTok.getLocation()))});
586 beginConditional(std::move(Guards));
587}
588
589void MacroConditionCallbacks::Ifndef(SourceLocation, const Token &,
590 const MacroDefinition &) {
591 beginConditional({});
592}
593
594void MacroConditionCallbacks::Elif(SourceLocation Loc,
595 SourceRange ConditionRange,
596 ConditionValueKind ConditionValue,
597 SourceLocation IfLoc) {
598 changeConditional({});
599 if (!branchUnconditionallyErrors(Loc))
600 processReferences(referencesInCondition(ConditionRange));
601 changeConditional(definitionGuardsInCondition(ConditionRange));
602}
603
604void MacroConditionCallbacks::Elifdef(SourceLocation Loc,
605 const Token &MacroNameTok,
606 const MacroDefinition &MD) {
607 changeConditional({});
608 const StringRef Name = getTokenName(MacroNameTok);
609 if (!branchUnconditionallyErrors(Loc))
610 processDefinitionReference(Name, Loc);
611 DefinitionGuards Guards;
612 if (!Name.empty())
613 Guards.push_back({Name.str(), SM.getFileID(SM.getSpellingLoc(
614 MacroNameTok.getLocation()))});
615 changeConditional(std::move(Guards));
616}
617
618void MacroConditionCallbacks::Elifdef(SourceLocation Loc,
619 SourceRange ConditionRange,
620 SourceLocation IfLoc) {
621 changeConditional({});
622 MacroReference Reference = referenceFromRange(ConditionRange, Loc);
623 if (!Reference.Name.empty() && !branchUnconditionallyErrors(Loc))
624 checkReference(Reference);
625 DefinitionGuards Guards;
626 if (!Reference.Name.empty())
627 Guards.push_back({std::move(Reference.Name),
628 SM.getFileID(SM.getSpellingLoc(Reference.Loc))});
629 changeConditional(std::move(Guards));
630}
631
632void MacroConditionCallbacks::Elifndef(SourceLocation, const Token &,
633 const MacroDefinition &) {
634 changeConditional({});
635}
636
637void MacroConditionCallbacks::Elifndef(SourceLocation, SourceRange,
638 SourceLocation) {
639 changeConditional({});
640}
641
642void MacroConditionCallbacks::Else(SourceLocation, SourceLocation) {
643 changeConditional({});
644}
645
646void MacroConditionCallbacks::Endif(SourceLocation, SourceLocation) {
647 if (!ConditionalGuards.empty())
648 ConditionalGuards.pop_back();
649}
650
651void MacroConditionCallbacks::SourceRangeSkipped(SourceRange Range,
652 SourceLocation EndifLoc) {
653 std::optional<std::string> Name = defaultedMacroInSkippedRange(Range);
654 if (!Name)
655 return;
656
657 const IdentifierInfo *Info = PP.getIdentifierInfo(*Name);
658 const MacroInfo *Macro = PP.getMacroDefinition(Info).getMacroInfo();
659 const auto MacroIt = MacroUsages.find(Macro);
660 if (MacroIt == MacroUsages.end())
661 return;
662
663 const SourceLocation GuardLoc = SM.getSpellingLoc(Range.getBegin());
664 const FileID GuardFile = SM.getFileID(GuardLoc);
665 const auto FileIt = MacroIt->second.find(GuardFile);
666 if (FileIt == MacroIt->second.end())
667 return;
668
669 SourceLocation &DefinitionLoc = FileIt->second.DefinitionTestLoc;
670 if (DefinitionLoc.isValid() && SM.getSpellingLineNumber(DefinitionLoc) ==
671 SM.getSpellingLineNumber(GuardLoc))
672 DefinitionLoc = {};
673}
674
676 ClangTidyContext *Context)
677 : ClangTidyCheck(Name, Context),
678 CheckDoubleUnderscoreMacros(
679 Options.get("CheckDoubleUnderscoreMacros", false)) {}
680
682 Options.store(Opts, "CheckDoubleUnderscoreMacros",
683 CheckDoubleUnderscoreMacros);
684}
685
686void MacroConditionCheck::registerPPCallbacks(const SourceManager &SM,
687 Preprocessor *PP,
688 Preprocessor *ModuleExpanderPP) {
689 PP->addPPCallbacks(std::make_unique<MacroConditionCallbacks>(
690 this, SM, *PP, CheckDoubleUnderscoreMacros));
691}
692
693} // namespace clang::tidy::bugprone
const char Usage[]
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for Markdown output.")
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
MacroConditionCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
@ Info
An information message.
Definition Protocol.h:755
static bool parseDefinitionGuardExpression(ArrayRef< Token > Tokens, size_t &Index, SmallVectorImpl< std::string > &Guards, const LangOptions &LangOpts)
StringRef getTokenName(const Token &Tok)
Returns the spelling of an identifier token.
static bool skipFunctionLikeInvocation(ArrayRef< Token > Tokens, size_t &Index)
static bool parseDefinitionGuardPrimary(ArrayRef< Token > Tokens, size_t &Index, SmallVectorImpl< std::string > &Guards, const LangOptions &LangOpts)
static bool isNegatedDefined(ArrayRef< Token > Tokens, size_t Index, const LangOptions &LangOpts)
static bool isOperatorToken(const Token &Token, tok::TokenKind Kind, StringRef Alternative, const LangOptions &LangOpts)
std::vector< Token > getRawTokens(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns the raw tokens in Range.
StringRef getTokenName(const Token &Tok)
Returns the spelling of an identifier token.
llvm::StringMap< ClangTidyValue > OptionMap