clang-tools 22.0.0git
UseConcisePreprocessorDirectivesCheck.cpp
Go to the documentation of this file.
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
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
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 else
62 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(DirectiveLoc, Replacements[Inverted])
95 << FixItHint::CreateReplacement(ConditionRange, Macro)
96 << Replacements[Inverted];
97 }
98
99 ClangTidyCheck &Check;
100 const Preprocessor &PP;
101};
102
103} // namespace
104
106 const SourceManager &, Preprocessor *PP, Preprocessor *) {
107 PP->addPPCallbacks(std::make_unique<IfPreprocessorCallbacks>(*this, *PP));
108}
109
110} // namespace clang::tidy::readability
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override