114 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//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 17namespace clang::tidy::readability {18 19namespace {20 21class IfPreprocessorCallbacks final : public PPCallbacks {22public: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(Loc, ConditionRange, {"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(Loc, ConditionRange, {"elifdef", "elifndef"});35 }36 37private: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(CharSourceRange::getTokenRange(ConditionRange),43 PP.getSourceManager(), PP.getLangOpts());44 Condition.push_back('\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(Tok))52 return;53 54 if (Tok.is(tok::TokenKind::exclaim) ||55 (PP.getLangOpts().CPlusPlus &&56 Tok.is(tok::TokenKind::raw_identifier) &&57 Tok.getRawIdentifier() == "not"))58 Inverted = !Inverted;59 else if (Tok.is(tok::TokenKind::l_paren))60 ++ParensNestingDepth;61 else62 break;63 }64 65 if (Tok.isNot(tok::TokenKind::raw_identifier) ||66 Tok.getRawIdentifier() != "defined")67 return;68 69 bool NoMoreTokens = Lex.LexFromRawLexer(Tok);70 if (Tok.is(tok::TokenKind::l_paren)) {71 if (NoMoreTokens)72 return;73 ++ParensNestingDepth;74 NoMoreTokens = Lex.LexFromRawLexer(Tok);75 }76 77 if (Tok.isNot(tok::TokenKind::raw_identifier))78 return;79 const StringRef Macro = Tok.getRawIdentifier();80 81 while (!NoMoreTokens) {82 NoMoreTokens = Lex.LexFromRawLexer(Tok);83 if (Tok.isNot(tok::TokenKind::r_paren))84 return;85 --ParensNestingDepth;86 }87 88 if (ParensNestingDepth != 0)89 return;90 91 Check.diag(92 DirectiveLoc,93 "preprocessor condition can be written more concisely using '#%0'")94 << FixItHint::CreateReplacement(95 CharSourceRange::getCharRange(DirectiveLoc,96 ConditionRange.getBegin()),97 (Replacements[Inverted].str() + " "))98 << FixItHint::CreateReplacement(ConditionRange, Macro)99 << Replacements[Inverted];100 }101 102 ClangTidyCheck &Check;103 const Preprocessor &PP;104};105 106} // namespace107 108void UseConcisePreprocessorDirectivesCheck::registerPPCallbacks(109 const SourceManager &, Preprocessor *PP, Preprocessor *) {110 PP->addPPCallbacks(std::make_unique<IfPreprocessorCallbacks>(*this, *PP));111}112 113} // namespace clang::tidy::readability114