clang-tools 24.0.0git
UseConcisePreprocessorDirectivesCheck.cpp
Go to the documentation of this file.
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-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/LexerUtils.h"
11#include "clang/Basic/TokenKinds.h"
12#include "clang/Lex/PPCallbacks.h"
13#include "clang/Lex/Preprocessor.h"
14
15#include <array>
16
18
20
21namespace {
22
23class IfPreprocessorCallbacks final : public PPCallbacks {
24public:
25 IfPreprocessorCallbacks(ClangTidyCheck &Check, const Preprocessor &PP)
26 : Check(Check), PP(PP) {}
27
28 void If(SourceLocation Loc, SourceRange ConditionRange,
29 ConditionValueKind) override {
30 impl(Loc, ConditionRange, {"ifdef", "ifndef"});
31 }
32
33 void Elif(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind,
34 SourceLocation) override {
35 if (PP.getLangOpts().C23 || PP.getLangOpts().CPlusPlus23)
36 impl(Loc, ConditionRange, {"elifdef", "elifndef"});
37 }
38
39private:
40 void impl(SourceLocation DirectiveLoc, SourceRange ConditionRange,
41 const std::array<StringRef, 2> &Replacements) {
42 const std::vector<Token> Tokens = utils::lexer::getRawTokens(
43 CharSourceRange::getTokenRange(ConditionRange), PP.getSourceManager(),
44 PP.getLangOpts());
45 bool Inverted = false; // The inverted form of #*def is #*ndef.
46 std::size_t ParensNestingDepth = 0;
47 std::size_t Index = 0;
48 while (Index < Tokens.size()) {
49 const Token &Tok = Tokens[Index];
50 if (Tok.is(tok::TokenKind::exclaim) ||
51 (PP.getLangOpts().CPlusPlus &&
52 Tok.is(tok::TokenKind::raw_identifier) &&
53 getTokenName(Tok) == "not")) {
54 Inverted = !Inverted;
55 ++Index;
56 } else if (Tok.is(tok::TokenKind::l_paren)) {
57 ++ParensNestingDepth;
58 ++Index;
59 } else {
60 break;
61 }
62 }
63
64 if (Index >= Tokens.size() ||
65 Tokens[Index].isNot(tok::TokenKind::raw_identifier) ||
66 getTokenName(Tokens[Index]) != "defined")
67 return;
68 ++Index;
69
70 if (Index < Tokens.size() && Tokens[Index].is(tok::TokenKind::l_paren)) {
71 ++ParensNestingDepth;
72 ++Index;
73 }
74
75 if (Index >= Tokens.size() ||
76 Tokens[Index].isNot(tok::TokenKind::raw_identifier))
77 return;
78 const StringRef Macro = getTokenName(Tokens[Index++]);
79
80 while (Index < Tokens.size()) {
81 if (Tokens[Index++].isNot(tok::TokenKind::r_paren) ||
82 ParensNestingDepth == 0)
83 return;
84 --ParensNestingDepth;
85 }
86
87 if (ParensNestingDepth != 0)
88 return;
89
90 Check.diag(
91 DirectiveLoc,
92 "preprocessor condition can be written more concisely using '#%0'")
93 << FixItHint::CreateReplacement(
94 CharSourceRange::getCharRange(DirectiveLoc,
95 ConditionRange.getBegin()),
96 (Replacements[Inverted].str() + " "))
97 << FixItHint::CreateReplacement(ConditionRange, Macro)
98 << Replacements[Inverted];
99 }
100
101 ClangTidyCheck &Check;
102 const Preprocessor &PP;
103};
104
105} // namespace
106
108 const SourceManager &, Preprocessor *PP, Preprocessor *) {
109 PP->addPPCallbacks(std::make_unique<IfPreprocessorCallbacks>(*this, *PP));
110}
111
112} // namespace clang::tidy::readability
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
StringRef getTokenName(const Token &Tok)
Returns the spelling of an identifier token.
std::vector< Token > getRawTokens(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns the raw tokens in Range.
StringRef getTokenName(const Token &Tok)
Returns the spelling of an identifier token.