| 1 | //===--- UseConcisePreprocessorDirectivesCheck.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 | |
| 9 | #include "UseConcisePreprocessorDirectivesCheck.h" |
| 10 | #include "clang/Basic/TokenKinds.h" |
| 11 | #include "clang/Lex/Lexer.h" |
| 12 | #include "clang/Lex/PPCallbacks.h" |
| 13 | #include "clang/Lex/Preprocessor.h" |
| 14 | |
| 15 | #include <array> |
| 16 | |
| 17 | namespace clang::tidy::readability { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class IfPreprocessorCallbacks final : public PPCallbacks { |
| 22 | public: |
| 23 | IfPreprocessorCallbacks(ClangTidyCheck &Check, const Preprocessor &PP) |
| 24 | : Check(Check), PP(PP) {} |
| 25 | |
| 26 | void If(SourceLocation Loc, SourceRange ConditionRange, |
| 27 | ConditionValueKind) override { |
| 28 | impl(DirectiveLoc: Loc, ConditionRange, Replacements: {"ifdef" , "ifndef" }); |
| 29 | } |
| 30 | |
| 31 | void Elif(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind, |
| 32 | SourceLocation) override { |
| 33 | if (PP.getLangOpts().C23 || PP.getLangOpts().CPlusPlus23) |
| 34 | impl(DirectiveLoc: Loc, ConditionRange, Replacements: {"elifdef" , "elifndef" }); |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | void impl(SourceLocation DirectiveLoc, SourceRange ConditionRange, |
| 39 | const std::array<llvm::StringLiteral, 2> &Replacements) { |
| 40 | // Lexer requires its input range to be null-terminated. |
| 41 | SmallString<128> Condition = |
| 42 | Lexer::getSourceText(Range: CharSourceRange::getTokenRange(R: ConditionRange), |
| 43 | SM: PP.getSourceManager(), LangOpts: PP.getLangOpts()); |
| 44 | Condition.push_back(Elt: '\0'); |
| 45 | Lexer Lex(DirectiveLoc, PP.getLangOpts(), Condition.data(), |
| 46 | Condition.data(), Condition.data() + Condition.size() - 1); |
| 47 | Token Tok; |
| 48 | bool Inverted = false; // The inverted form of #*def is #*ndef. |
| 49 | std::size_t ParensNestingDepth = 0; |
| 50 | for (;;) { |
| 51 | if (Lex.LexFromRawLexer(Result&: Tok)) |
| 52 | return; |
| 53 | |
| 54 | if (Tok.is(K: tok::TokenKind::exclaim) || |
| 55 | (PP.getLangOpts().CPlusPlus && |
| 56 | Tok.is(K: tok::TokenKind::raw_identifier) && |
| 57 | Tok.getRawIdentifier() == "not" )) |
| 58 | Inverted = !Inverted; |
| 59 | else if (Tok.is(K: tok::TokenKind::l_paren)) |
| 60 | ++ParensNestingDepth; |
| 61 | else |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | if (Tok.isNot(K: tok::TokenKind::raw_identifier) || |
| 66 | Tok.getRawIdentifier() != "defined" ) |
| 67 | return; |
| 68 | |
| 69 | bool NoMoreTokens = Lex.LexFromRawLexer(Result&: Tok); |
| 70 | if (Tok.is(K: tok::TokenKind::l_paren)) { |
| 71 | if (NoMoreTokens) |
| 72 | return; |
| 73 | ++ParensNestingDepth; |
| 74 | NoMoreTokens = Lex.LexFromRawLexer(Result&: Tok); |
| 75 | } |
| 76 | |
| 77 | if (Tok.isNot(K: tok::TokenKind::raw_identifier)) |
| 78 | return; |
| 79 | const StringRef Macro = Tok.getRawIdentifier(); |
| 80 | |
| 81 | while (!NoMoreTokens) { |
| 82 | NoMoreTokens = Lex.LexFromRawLexer(Result&: Tok); |
| 83 | if (Tok.isNot(K: tok::TokenKind::r_paren)) |
| 84 | return; |
| 85 | --ParensNestingDepth; |
| 86 | } |
| 87 | |
| 88 | if (ParensNestingDepth != 0) |
| 89 | return; |
| 90 | |
| 91 | Check.diag( |
| 92 | Loc: DirectiveLoc, |
| 93 | Description: "preprocessor condition can be written more concisely using '#%0'" ) |
| 94 | << FixItHint::CreateReplacement(RemoveRange: DirectiveLoc, Code: Replacements[Inverted]) |
| 95 | << FixItHint::CreateReplacement(RemoveRange: ConditionRange, Code: Macro) |
| 96 | << Replacements[Inverted]; |
| 97 | } |
| 98 | |
| 99 | ClangTidyCheck &Check; |
| 100 | const Preprocessor &PP; |
| 101 | }; |
| 102 | |
| 103 | } // namespace |
| 104 | |
| 105 | void UseConcisePreprocessorDirectivesCheck::registerPPCallbacks( |
| 106 | const SourceManager &, Preprocessor *PP, Preprocessor *) { |
| 107 | PP->addPPCallbacks(C: std::make_unique<IfPreprocessorCallbacks>(args&: *this, args&: *PP)); |
| 108 | } |
| 109 | |
| 110 | } // namespace clang::tidy::readability |
| 111 | |