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"
27class MacroConditionCallbacks :
public PPCallbacks {
29 MacroConditionCallbacks(MacroConditionCheck *Check,
const SourceManager &SM,
30 Preprocessor &PP,
bool CheckDoubleUnderscoreMacros)
31 : Check(Check), SM(SM), PP(PP),
32 CheckDoubleUnderscoreMacros(CheckDoubleUnderscoreMacros) {}
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;
55 enum class ReferenceKind {
Definition, NegatedDefinition, Value };
57 struct MacroReference {
64 SourceLocation DefinitionTestLoc;
65 SourceLocation ValueTestLoc;
66 bool Diagnosed =
false;
69 struct DefinitionGuard {
74 using ConditionReferences = SmallVector<MacroReference, 4>;
75 using DefinitionGuards = SmallVector<DefinitionGuard, 2>;
77 ConditionReferences referencesInCondition(SourceRange ConditionRange)
const;
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);
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;
102 const bool CheckDoubleUnderscoreMacros;
110 StringRef Alternative,
111 const LangOptions &LangOpts) {
112 return Token.is(Kind) ||
113 (LangOpts.CXXOperatorNames && Token.is(tok::raw_identifier) &&
114 Token.getRawIdentifier() == Alternative);
118 if (Index + 1 >= Tokens.size() || Tokens[Index + 1].isNot(tok::l_paren))
121 unsigned ParenthesisDepth = 0;
124 if (Tokens[Index].is(tok::l_paren))
126 else if (Tokens[Index].is(tok::r_paren))
128 }
while (Index + 1 < Tokens.size() && ParenthesisDepth != 0);
133 const LangOptions &LangOpts) {
134 unsigned Negations = 0;
136 while (Index > 0 && Tokens[Index - 1].is(tok::l_paren))
144 return Negations % 2 != 0;
147MacroConditionCallbacks::ConditionReferences
148MacroConditionCallbacks::referencesInCondition(
149 SourceRange ConditionRange)
const {
150 ConditionReferences References;
152 CharSourceRange::getTokenRange(ConditionRange), SM, PP.getLangOpts());
154 for (
size_t Index = 0; Index < Tokens.size(); ++Index) {
155 const Token &Current = Tokens[Index];
156 if (!Current.is(tok::raw_identifier))
159 StringRef Name = Current.getRawIdentifier();
160 if (Name !=
"defined") {
161 if (skipFunctionLikeInvocation(Tokens, Index))
163 if (!isIgnoredIdentifier(Name))
164 References.push_back(
165 {Name.str(), Current.getLocation(), ReferenceKind::Value});
169 const SourceLocation DefinedLoc = Current.getLocation();
170 const bool IsNegated = isNegatedDefined(Tokens, Index, PP.getLangOpts());
172 if (Index < Tokens.size() && Tokens[Index].is(tok::l_paren))
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});
184 SmallVectorImpl<std::string> &Guards,
185 const LangOptions &LangOpts);
188 SmallVectorImpl<std::string> &Guards,
189 const LangOptions &LangOpts) {
190 if (Index >= Tokens.size())
193 if (Tokens[Index].is(tok::l_paren)) {
196 Index >= Tokens.size() || Tokens[Index].isNot(tok::r_paren))
206 const bool Parenthesized =
207 Index < Tokens.size() && Tokens[Index].is(tok::l_paren);
210 if (Index >= Tokens.size() || Tokens[Index].isNot(tok::raw_identifier))
212 Guards.push_back(Tokens[Index++].getRawIdentifier().str());
214 if (Index >= Tokens.size() || Tokens[Index].isNot(tok::r_paren))
223 SmallVectorImpl<std::string> &Guards,
224 const LangOptions &LangOpts) {
227 while (Index < Tokens.size() &&
236MacroConditionCallbacks::DefinitionGuards
237MacroConditionCallbacks::definitionGuardsInCondition(
238 SourceRange ConditionRange)
const {
240 CharSourceRange::getTokenRange(ConditionRange), SM, PP.getLangOpts());
243 if (!parseDefinitionGuardExpression(Tokens, Index, Names, PP.getLangOpts()) ||
244 Index != Tokens.size())
247 DefinitionGuards Guards;
249 SM.getFileID(SM.getSpellingLoc(ConditionRange.getBegin()));
250 for (std::string &Name : Names)
251 Guards.push_back({std::move(Name), File});
255std::optional<std::pair<SourceLocation, StringRef>>
256MacroConditionCallbacks::conditionalBody(SourceLocation Loc)
const {
257 const SourceLocation BeginLoc = SM.getExpansionLoc(Loc);
258 if (BeginLoc.isInvalid())
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())
267 size_t Body = Decomposed.second;
268 while (Body < Buffer.size()) {
269 if (Buffer[Body] !=
'\r' && Buffer[Body] !=
'\n') {
273 if (Body > Decomposed.second && Buffer[Body - 1] ==
'\\') {
274 if (Buffer[Body] ==
'\r' && Body + 1 < Buffer.size() &&
275 Buffer[Body + 1] ==
'\n')
282 while (Body < Buffer.size() && (Buffer[Body] ==
'\r' || Buffer[Body] ==
'\n'))
285 const SourceLocation BodyLoc =
286 SM.getLocForStartOfFile(Decomposed.first).getLocWithOffset(Body);
287 return std::make_pair(BodyLoc, Buffer.drop_front(Body));
290bool MacroConditionCallbacks::branchUnconditionallyErrors(
291 SourceLocation Loc)
const {
292 const auto Body = conditionalBody(Loc);
296 std::string
Text = Body->second.str();
297 Lexer Lex(Body->first, PP.getLangOpts(),
Text.data(),
Text.data(),
301 while (!Lex.LexFromRawLexer(Tok)) {
302 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
306 if (Lex.LexFromRawLexer(DirectiveTok))
309 if (Directive ==
"if" || Directive ==
"ifdef" || Directive ==
"ifndef") {
313 if (Directive ==
"endif") {
320 if (Directive ==
"else" || Directive.starts_with(
"elif"))
322 if (Directive ==
"error")
328bool MacroConditionCallbacks::branchUsesMacroValue(SourceLocation Loc,
329 StringRef Name)
const {
330 const auto Body = conditionalBody(Loc);
334 std::string
Text = Body->second.str();
335 Lexer Lex(Body->first, PP.getLangOpts(),
Text.data(),
Text.data(),
338 bool InDirective =
false;
340 while (!Lex.LexFromRawLexer(Tok)) {
341 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
343 if (Lex.LexFromRawLexer(DirectiveTok))
346 if (Directive ==
"if" || Directive ==
"ifdef" || Directive ==
"ifndef") {
348 }
else if (Directive ==
"endif") {
351 }
else if (Depth == 1 &&
352 (Directive ==
"else" || Directive.starts_with(
"elif"))) {
358 if (Tok.isAtStartOfLine())
360 if (!InDirective && Tok.is(tok::raw_identifier) &&
361 Tok.getRawIdentifier() == Name)
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())
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)
379 bool Invalid =
false;
380 StringRef Buffer = SM.getBufferData(Begin.first, &Invalid);
381 if (Invalid || End.second > Buffer.size())
384 std::string
Text = Buffer.slice(Begin.second, End.second).str();
385 Lexer Lex(BeginLoc, PP.getLangOpts(),
Text.data(),
Text.data(),
387 SmallVector<Token, 32> Tokens;
391 AtEnd = Lex.LexFromRawLexer(Tok);
392 if (Tok.isNot(tok::eof))
393 Tokens.push_back(Tok);
396 if (Tokens.size() < 3 || Tokens[0].isNot(tok::hash) ||
397 !Tokens[0].isAtStartOfLine())
403 if (Directive ==
"ifndef") {
404 if (!Tokens[Index].is(tok::raw_identifier))
406 Name = Tokens[Index++].getRawIdentifier().str();
407 }
else if (Directive ==
"if") {
408 if (!
isOperatorToken(Tokens[Index], tok::exclaim,
"not", PP.getLangOpts()))
411 if (Index >= Tokens.size() ||
getTokenName(Tokens[Index]) !=
"defined")
414 if (Index < Tokens.size() && Tokens[Index].is(tok::l_paren))
416 if (Index >= Tokens.size() || Tokens[Index].isNot(tok::raw_identifier))
418 Name = Tokens[Index++].getRawIdentifier().str();
419 if (Index < Tokens.size() && Tokens[Index].is(tok::r_paren))
425 if (Index < Tokens.size() && !Tokens[Index].isAtStartOfLine())
429 for (; Index + 1 < Tokens.size(); ++Index) {
430 if (Tokens[Index].isNot(tok::hash) || !Tokens[Index].isAtStartOfLine())
433 StringRef NestedDirective =
getTokenName(Tokens[++Index]);
434 if (NestedDirective ==
"if" || NestedDirective ==
"ifdef" ||
435 NestedDirective ==
"ifndef") {
439 if (NestedDirective ==
"endif") {
446 if (NestedDirective ==
"else" || NestedDirective.starts_with(
"elif"))
448 if (NestedDirective !=
"define" || Index + 2 >= Tokens.size() ||
450 Tokens[Index + 2].isAtStartOfLine())
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())
465MacroConditionCallbacks::MacroReference
466MacroConditionCallbacks::referenceFromRange(SourceRange Range,
467 SourceLocation Loc)
const {
468 ConditionReferences
References = referencesInCondition(Range);
471 References.front().Kind = ReferenceKind::Definition;
474 return {{}, Loc, ReferenceKind::Definition};
477bool MacroConditionCallbacks::isIgnoredIdentifier(StringRef Name)
const {
478 const IdentifierInfo *
Info = PP.getIdentifierInfo(Name);
479 return Name ==
"true" || Name ==
"false" ||
480 Info->isCPlusPlusOperatorKeyword();
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)
494void MacroConditionCallbacks::beginConditional(DefinitionGuards Guards) {
495 ConditionalGuards.push_back(std::move(Guards));
498void MacroConditionCallbacks::changeConditional(DefinitionGuards Guards) {
499 if (!ConditionalGuards.empty())
500 ConditionalGuards.back() = std::move(Guards);
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;
513 if (!IsCompoundReference)
514 checkReference(Reference);
518void MacroConditionCallbacks::processDefinitionReference(StringRef Name,
519 SourceLocation Loc) {
521 checkReference({Name.str(), Loc, ReferenceKind::Definition});
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)))
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())
537 if (Reference.Kind == ReferenceKind::Definition &&
538 branchUsesMacroValue(Reference.Loc, Reference.Name))
541 const SourceLocation SpellingLoc = SM.getSpellingLoc(Reference.Loc);
542 if (SpellingLoc.isInvalid())
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())
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",
565 << Reference.Name << Kind;
566 Usage.Diagnosed =
true;
569void MacroConditionCallbacks::If(SourceLocation Loc, SourceRange ConditionRange,
570 ConditionValueKind ConditionValue) {
571 if (!branchUnconditionallyErrors(Loc))
572 processReferences(referencesInCondition(ConditionRange));
573 beginConditional(definitionGuardsInCondition(ConditionRange));
576void MacroConditionCallbacks::Ifdef(SourceLocation Loc,
577 const Token &MacroNameTok,
578 const MacroDefinition &
MD) {
580 if (!branchUnconditionallyErrors(Loc))
581 processDefinitionReference(Name, Loc);
582 DefinitionGuards Guards;
584 Guards.push_back({Name.str(), SM.getFileID(SM.getSpellingLoc(
585 MacroNameTok.getLocation()))});
586 beginConditional(std::move(Guards));
589void MacroConditionCallbacks::Ifndef(SourceLocation,
const Token &,
590 const MacroDefinition &) {
591 beginConditional({});
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));
604void MacroConditionCallbacks::Elifdef(SourceLocation Loc,
605 const Token &MacroNameTok,
606 const MacroDefinition &
MD) {
607 changeConditional({});
609 if (!branchUnconditionallyErrors(Loc))
610 processDefinitionReference(Name, Loc);
611 DefinitionGuards Guards;
613 Guards.push_back({Name.str(), SM.getFileID(SM.getSpellingLoc(
614 MacroNameTok.getLocation()))});
615 changeConditional(std::move(Guards));
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));
632void MacroConditionCallbacks::Elifndef(SourceLocation,
const Token &,
633 const MacroDefinition &) {
634 changeConditional({});
637void MacroConditionCallbacks::Elifndef(SourceLocation, SourceRange,
639 changeConditional({});
642void MacroConditionCallbacks::Else(SourceLocation, SourceLocation) {
643 changeConditional({});
646void MacroConditionCallbacks::Endif(SourceLocation, SourceLocation) {
647 if (!ConditionalGuards.empty())
648 ConditionalGuards.pop_back();
651void MacroConditionCallbacks::SourceRangeSkipped(SourceRange Range,
652 SourceLocation EndifLoc) {
653 std::optional<std::string> Name = defaultedMacroInSkippedRange(Range);
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())
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())
669 SourceLocation &DefinitionLoc = FileIt->second.DefinitionTestLoc;
670 if (DefinitionLoc.isValid() && SM.getSpellingLineNumber(DefinitionLoc) ==
671 SM.getSpellingLineNumber(GuardLoc))
678 CheckDoubleUnderscoreMacros(
679 Options.get(
"CheckDoubleUnderscoreMacros", false)) {}
682 Options.store(Opts,
"CheckDoubleUnderscoreMacros",
683 CheckDoubleUnderscoreMacros);
688 Preprocessor *ModuleExpanderPP) {
689 PP->addPPCallbacks(std::make_unique<MacroConditionCallbacks>(
690 this, SM, *
PP, CheckDoubleUnderscoreMacros));
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.
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