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 const char WarningDescription[] =
28 "nested redundant %select{#if|#ifdef|#ifndef}0; consider removing it";
29 const char NoteDescription[] =
"previous %select{#if|#ifdef|#ifndef}0 was here";
31 class RedundantPreprocessorCallbacks :
public PPCallbacks {
32 enum DirectiveKind { DK_If = 0, DK_Ifdef = 1, DK_Ifndef = 2 };
35 explicit RedundantPreprocessorCallbacks(ClangTidyCheck &Check,
37 : Check(Check), PP(PP) {}
39 void If(SourceLocation
Loc, SourceRange ConditionRange,
43 PP.getSourceManager(), PP.getLangOpts());
44 checkMacroRedundancy(
Loc,
Condition, IfStack, DK_If, DK_If,
true);
47 void Ifdef(SourceLocation
Loc,
const Token &MacroNameTok,
48 const MacroDefinition &MacroDefinition)
override {
49 std::string MacroName = PP.getSpelling(MacroNameTok);
50 checkMacroRedundancy(
Loc, MacroName, IfdefStack, DK_Ifdef, DK_Ifdef,
true);
51 checkMacroRedundancy(
Loc, MacroName, IfndefStack, DK_Ifdef, DK_Ifndef,
55 void Ifndef(SourceLocation
Loc,
const Token &MacroNameTok,
56 const MacroDefinition &MacroDefinition)
override {
57 std::string MacroName = PP.getSpelling(MacroNameTok);
58 checkMacroRedundancy(
Loc, MacroName, IfndefStack, DK_Ifndef, DK_Ifndef,
60 checkMacroRedundancy(
Loc, MacroName, IfdefStack, DK_Ifndef, DK_Ifdef,
64 void Endif(SourceLocation
Loc, SourceLocation IfLoc)
override {
65 if (!IfStack.empty() && IfLoc == IfStack.back().Loc)
67 if (!IfdefStack.empty() && IfLoc == IfdefStack.back().Loc)
68 IfdefStack.pop_back();
69 if (!IfndefStack.empty() && IfLoc == IfndefStack.back().Loc)
70 IfndefStack.pop_back();
74 void checkMacroRedundancy(SourceLocation
Loc, StringRef MacroName,
75 SmallVector<PreprocessorEntry, 4> &Stack,
78 if (PP.getSourceManager().isInMainFile(
Loc)) {
79 for (
const auto &
Entry : Stack) {
80 if (
Entry.Condition == MacroName) {
81 Check.diag(
Loc, WarningDescription) << WarningKind;
82 Check.diag(
Entry.
Loc, NoteDescription, DiagnosticIDs::Note)
90 Stack.push_back({
Loc, std::string(MacroName)});
93 ClangTidyCheck &Check;
95 SmallVector<PreprocessorEntry, 4> IfStack;
96 SmallVector<PreprocessorEntry, 4> IfdefStack;
97 SmallVector<PreprocessorEntry, 4> IfndefStack;
102 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
104 ::std::make_unique<RedundantPreprocessorCallbacks>(*
this, *PP));