RedundantPreprocessorCheck.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===--- RedundantPreprocessorCheck.cpp - clang-tidy ----------------------===//
  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. #include "RedundantPreprocessorCheck.h"
  9. #include "clang/Frontend/CompilerInstance.h"
  10. #include "clang/Lex/Lexer.h"
  11. #include "clang/Lex/PPCallbacks.h"
  12. #include "clang/Lex/Preprocessor.h"
  13. namespace clang::tidy::readability {
  14. namespace {
  15. /// Information about an opening preprocessor directive.
  16. struct PreprocessorEntry {
  17. SourceLocation Loc;
  18. /// Condition used after the preprocessor directive.
  19. std::string Condition;
  20. };
  21. const char WarningDescription[] =
  22. "nested redundant %select{#if|#ifdef|#ifndef}0; consider removing it";
  23. const char NoteDescription[] = "previous %select{#if|#ifdef|#ifndef}0 was here";
  24. class RedundantPreprocessorCallbacks : public PPCallbacks {
  25. enum DirectiveKind { DK_If = 0, DK_Ifdef = 1, DK_Ifndef = 2 };
  26. public:
  27. explicit RedundantPreprocessorCallbacks(ClangTidyCheck &Check,
  28. Preprocessor &PP)
  29. : Check(Check), PP(PP) {}
  30. void If(SourceLocation Loc, SourceRange ConditionRange,
  31. ConditionValueKind ConditionValue) override {
  32. StringRef Condition =
  33. Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange),
  34. PP.getSourceManager(), PP.getLangOpts());
  35. checkMacroRedundancy(Loc, Condition, IfStack, DK_If, DK_If, true);
  36. }
  37. void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
  38. const MacroDefinition &MacroDefinition) override {
  39. std::string MacroName = PP.getSpelling(MacroNameTok);
  40. checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifdef, DK_Ifdef, true);
  41. checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifdef, DK_Ifndef,
  42. false);
  43. }
  44. void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
  45. const MacroDefinition &MacroDefinition) override {
  46. std::string MacroName = PP.getSpelling(MacroNameTok);
  47. checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifndef, DK_Ifndef,
  48. true);
  49. checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifndef, DK_Ifdef,
  50. false);
  51. }
  52. void Endif(SourceLocation Loc, SourceLocation IfLoc) override {
  53. if (!IfStack.empty() && IfLoc == IfStack.back().Loc)
  54. IfStack.pop_back();
  55. if (!IfdefStack.empty() && IfLoc == IfdefStack.back().Loc)
  56. IfdefStack.pop_back();
  57. if (!IfndefStack.empty() && IfLoc == IfndefStack.back().Loc)
  58. IfndefStack.pop_back();
  59. }
  60. private:
  61. void checkMacroRedundancy(SourceLocation Loc, StringRef MacroName,
  62. SmallVector<PreprocessorEntry, 4> &Stack,
  63. DirectiveKind WarningKind, DirectiveKind NoteKind,
  64. bool Store) {
  65. if (PP.getSourceManager().isInMainFile(Loc)) {
  66. for (const auto &Entry : Stack) {
  67. if (Entry.Condition == MacroName) {
  68. Check.diag(Loc, WarningDescription) << WarningKind;
  69. Check.diag(Entry.Loc, NoteDescription, DiagnosticIDs::Note)
  70. << NoteKind;
  71. }
  72. }
  73. }
  74. if (Store)
  75. // This is an actual directive to be remembered.
  76. Stack.push_back({Loc, std::string(MacroName)});
  77. }
  78. ClangTidyCheck &Check;
  79. Preprocessor &PP;
  80. SmallVector<PreprocessorEntry, 4> IfStack;
  81. SmallVector<PreprocessorEntry, 4> IfdefStack;
  82. SmallVector<PreprocessorEntry, 4> IfndefStack;
  83. };
  84. } // namespace
  85. void RedundantPreprocessorCheck::registerPPCallbacks(
  86. const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
  87. PP->addPPCallbacks(
  88. ::std::make_unique<RedundantPreprocessorCallbacks>(*this, *PP));
  89. }
  90. } // namespace clang::tidy::readability