clang-tools 24.0.0git
AvoidUnconditionalPreprocessorIfCheck.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/AST/ASTContext.h"
12#include "clang/Lex/PPCallbacks.h"
13#include "clang/Lex/Preprocessor.h"
14#include "llvm/ADT/STLExtras.h"
15
16using namespace clang::ast_matchers;
17
19
20namespace {
21struct AvoidUnconditionalPreprocessorIfPPCallbacks : public PPCallbacks {
22 explicit AvoidUnconditionalPreprocessorIfPPCallbacks(ClangTidyCheck &Check,
23 Preprocessor &PP)
24 : Check(Check), PP(PP) {}
25
26 void If(SourceLocation Loc, SourceRange ConditionRange,
27 ConditionValueKind ConditionValue) override {
28 if (ConditionValue == CVK_NotEvaluated)
29 return;
30 SourceManager &SM = PP.getSourceManager();
31 if (!isImmutable(SM, PP.getLangOpts(), ConditionRange))
32 return;
33
34 if (ConditionValue == CVK_True)
35 Check.diag(Loc, "preprocessor condition is always 'true', consider "
36 "removing condition but leaving its contents");
37 else
38 Check.diag(Loc, "preprocessor condition is always 'false', consider "
39 "removing both the condition and its contents");
40 }
41
42 bool isImmutable(SourceManager &SM, const LangOptions &LangOpts,
43 SourceRange ConditionRange) {
44 const SourceLocation Loc = ConditionRange.getBegin();
45 if (Loc.isMacroID())
46 return false;
47
48 const std::vector<Token> Tokens = utils::lexer::getRawTokens(
49 CharSourceRange::getTokenRange(ConditionRange), SM, LangOpts);
50 if (Tokens.empty())
51 return false;
52 return llvm::all_of(
53 Tokens, [this](const Token &Tok) { return isImmutableToken(Tok); });
54 }
55
56 bool isImmutableToken(const Token &Tok) {
57 switch (Tok.getKind()) {
58 case tok::eod:
59 case tok::eof:
60 case tok::numeric_constant:
61 case tok::char_constant:
62 case tok::wide_char_constant:
63 case tok::utf8_char_constant:
64 case tok::utf16_char_constant:
65 case tok::utf32_char_constant:
66 case tok::string_literal:
67 case tok::wide_string_literal:
68 case tok::comment:
69 return true;
70 case tok::raw_identifier:
71 return (Tok.getRawIdentifier() == "true" ||
72 Tok.getRawIdentifier() == "false");
73 default:
74 return Tok.getKind() >= tok::l_square &&
75 Tok.getKind() <= tok::greatergreatergreater;
76 }
77 }
78
79 ClangTidyCheck &Check;
80 Preprocessor &PP;
81};
82
83} // namespace
84
86 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
87 PP->addPPCallbacks(
88 std::make_unique<AvoidUnconditionalPreprocessorIfPPCallbacks>(*this,
89 *PP));
90}
91
92} // namespace clang::tidy::readability
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
std::vector< Token > getRawTokens(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns the raw tokens in Range.