aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/readability/UseConcisePreprocessorDirectivesCheck.cpp
blob: 05c0088e6b41b73129ba04c0679bd9cd2c0fa1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===--- UseConcisePreprocessorDirectivesCheck.cpp - clang-tidy -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "UseConcisePreprocessorDirectivesCheck.h"
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"

#include <array>

namespace clang::tidy::readability {

namespace {

class IfPreprocessorCallbacks final : public PPCallbacks {
public:
  IfPreprocessorCallbacks(ClangTidyCheck &Check, const Preprocessor &PP)
      : Check(Check), PP(PP) {}

  void If(SourceLocation Loc, SourceRange ConditionRange,
          ConditionValueKind) override {
    impl(Loc, ConditionRange, {"ifdef", "ifndef"});
  }

  void Elif(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind,
            SourceLocation) override {
    if (PP.getLangOpts().C23 || PP.getLangOpts().CPlusPlus23)
      impl(Loc, ConditionRange, {"elifdef", "elifndef"});
  }

private:
  void impl(SourceLocation DirectiveLoc, SourceRange ConditionRange,
            const std::array<llvm::StringLiteral, 2> &Replacements) {
    // Lexer requires its input range to be null-terminated.
    SmallString<128> Condition =
        Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange),
                             PP.getSourceManager(), PP.getLangOpts());
    Condition.push_back('\0');
    Lexer Lex(DirectiveLoc, PP.getLangOpts(), Condition.data(),
              Condition.data(), Condition.data() + Condition.size() - 1);
    Token Tok;
    bool Inverted = false; // The inverted form of #*def is #*ndef.
    std::size_t ParensNestingDepth = 0;
    for (;;) {
      if (Lex.LexFromRawLexer(Tok))
        return;

      if (Tok.is(tok::TokenKind::exclaim) ||
          (PP.getLangOpts().CPlusPlus &&
           Tok.is(tok::TokenKind::raw_identifier) &&
           Tok.getRawIdentifier() == "not"))
        Inverted = !Inverted;
      else if (Tok.is(tok::TokenKind::l_paren))
        ++ParensNestingDepth;
      else
        break;
    }

    if (Tok.isNot(tok::TokenKind::raw_identifier) ||
        Tok.getRawIdentifier() != "defined")
      return;

    bool NoMoreTokens = Lex.LexFromRawLexer(Tok);
    if (Tok.is(tok::TokenKind::l_paren)) {
      if (NoMoreTokens)
        return;
      ++ParensNestingDepth;
      NoMoreTokens = Lex.LexFromRawLexer(Tok);
    }

    if (Tok.isNot(tok::TokenKind::raw_identifier))
      return;
    const StringRef Macro = Tok.getRawIdentifier();

    while (!NoMoreTokens) {
      NoMoreTokens = Lex.LexFromRawLexer(Tok);
      if (Tok.isNot(tok::TokenKind::r_paren))
        return;
      --ParensNestingDepth;
    }

    if (ParensNestingDepth != 0)
      return;

    Check.diag(
        DirectiveLoc,
        "preprocessor condition can be written more concisely using '#%0'")
        << FixItHint::CreateReplacement(DirectiveLoc, Replacements[Inverted])
        << FixItHint::CreateReplacement(ConditionRange, Macro)
        << Replacements[Inverted];
  }

  ClangTidyCheck &Check;
  const Preprocessor &PP;
};

} // namespace

void UseConcisePreprocessorDirectivesCheck::registerPPCallbacks(
    const SourceManager &, Preprocessor *PP, Preprocessor *) {
  PP->addPPCallbacks(std::make_unique<IfPreprocessorCallbacks>(*this, *PP));
}

} // namespace clang::tidy::readability