10 #include "clang/Frontend/CompilerInstance.h"
11 #include "clang/Lex/Lexer.h"
12 #include "clang/Lex/PPCallbacks.h"
13 #include "clang/Lex/Preprocessor.h"
17 namespace readability {
21 struct PreprocessorEntry {
27 class RedundantPreprocessorCallbacks :
public PPCallbacks {
28 enum DirectiveKind { DK_If = 0, DK_Ifdef = 1, DK_Ifndef = 2 };
31 explicit RedundantPreprocessorCallbacks(ClangTidyCheck &Check,
33 : Check(Check), PP(PP),
34 WarningDescription(
"nested redundant %select{#if|#ifdef|#ifndef}0; "
35 "consider removing it"),
36 NoteDescription(
"previous %select{#if|#ifdef|#ifndef}0 was here") {}
38 void If(SourceLocation
Loc, SourceRange ConditionRange,
41 Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange),
42 PP.getSourceManager(), PP.getLangOpts());
43 CheckMacroRedundancy(
Loc,
Condition, IfStack, DK_If, DK_If,
true);
46 void Ifdef(SourceLocation
Loc,
const Token &MacroNameTok,
47 const MacroDefinition &MacroDefinition)
override {
48 std::string MacroName = PP.getSpelling(MacroNameTok);
49 CheckMacroRedundancy(
Loc, MacroName, IfdefStack, DK_Ifdef, DK_Ifdef,
true);
50 CheckMacroRedundancy(
Loc, MacroName, IfndefStack, DK_Ifdef, DK_Ifndef,
54 void Ifndef(SourceLocation
Loc,
const Token &MacroNameTok,
55 const MacroDefinition &MacroDefinition)
override {
56 std::string MacroName = PP.getSpelling(MacroNameTok);
57 CheckMacroRedundancy(
Loc, MacroName, IfndefStack, DK_Ifndef, DK_Ifndef,
59 CheckMacroRedundancy(
Loc, MacroName, IfdefStack, DK_Ifndef, DK_Ifdef,
63 void Endif(SourceLocation
Loc, SourceLocation IfLoc)
override {
64 if (!IfStack.empty() && IfLoc == IfStack.back().Loc)
66 if (!IfdefStack.empty() && IfLoc == IfdefStack.back().Loc)
67 IfdefStack.pop_back();
68 if (!IfndefStack.empty() && IfLoc == IfndefStack.back().Loc)
69 IfndefStack.pop_back();
73 void CheckMacroRedundancy(SourceLocation
Loc, StringRef MacroName,
74 SmallVector<PreprocessorEntry, 4> &Stack,
77 if (PP.getSourceManager().isInMainFile(
Loc)) {
78 for (
const auto &
Entry : Stack) {
79 if (
Entry.Condition == MacroName) {
80 Check.diag(
Loc, WarningDescription) << WarningKind;
81 Check.diag(
Entry.
Loc, NoteDescription, DiagnosticIDs::Note)
89 Stack.push_back({
Loc, std::string(MacroName)});
92 ClangTidyCheck &Check;
94 SmallVector<PreprocessorEntry, 4> IfStack;
95 SmallVector<PreprocessorEntry, 4> IfdefStack;
96 SmallVector<PreprocessorEntry, 4> IfndefStack;
97 const std::string WarningDescription;
98 const std::string NoteDescription;
103 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
105 ::std::make_unique<RedundantPreprocessorCallbacks>(*
this, *PP));