Files
TheRock/compiler/amd-llvm/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

107 lines
3.4 KiB
C++

//===----------------------------------------------------------------------===//
//
// 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 "AvoidUnconditionalPreprocessorIfCheck.h"
#include "../utils/LexerUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
using namespace clang::ast_matchers;
namespace clang::tidy::readability {
namespace {
struct AvoidUnconditionalPreprocessorIfPPCallbacks : public PPCallbacks {
explicit AvoidUnconditionalPreprocessorIfPPCallbacks(ClangTidyCheck &Check,
Preprocessor &PP)
: Check(Check), PP(PP) {}
void If(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue) override {
if (ConditionValue == CVK_NotEvaluated)
return;
SourceManager &SM = PP.getSourceManager();
if (!isImmutable(SM, PP.getLangOpts(), ConditionRange))
return;
if (ConditionValue == CVK_True)
Check.diag(Loc, "preprocessor condition is always 'true', consider "
"removing condition but leaving its contents");
else
Check.diag(Loc, "preprocessor condition is always 'false', consider "
"removing both the condition and its contents");
}
bool isImmutable(SourceManager &SM, const LangOptions &LangOpts,
SourceRange ConditionRange) {
const SourceLocation Loc = ConditionRange.getBegin();
if (Loc.isMacroID())
return false;
Token Tok;
if (Lexer::getRawToken(Loc, Tok, SM, LangOpts, true)) {
std::optional<Token> TokOpt =
utils::lexer::findNextTokenSkippingComments(Loc, SM, LangOpts);
if (!TokOpt || TokOpt->getLocation().isMacroID())
return false;
Tok = *TokOpt;
}
while (Tok.getLocation() <= ConditionRange.getEnd()) {
if (!isImmutableToken(Tok))
return false;
std::optional<Token> TokOpt = utils::lexer::findNextTokenSkippingComments(
Tok.getLocation(), SM, LangOpts);
if (!TokOpt || TokOpt->getLocation().isMacroID())
return false;
Tok = *TokOpt;
}
return true;
}
bool isImmutableToken(const Token &Tok) {
switch (Tok.getKind()) {
case tok::eod:
case tok::eof:
case tok::numeric_constant:
case tok::char_constant:
case tok::wide_char_constant:
case tok::utf8_char_constant:
case tok::utf16_char_constant:
case tok::utf32_char_constant:
case tok::string_literal:
case tok::wide_string_literal:
case tok::comment:
return true;
case tok::raw_identifier:
return (Tok.getRawIdentifier() == "true" ||
Tok.getRawIdentifier() == "false");
default:
return Tok.getKind() >= tok::l_square &&
Tok.getKind() <= tok::greatergreatergreater;
}
}
ClangTidyCheck &Check;
Preprocessor &PP;
};
} // namespace
void AvoidUnconditionalPreprocessorIfCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
PP->addPPCallbacks(
std::make_unique<AvoidUnconditionalPreprocessorIfPPCallbacks>(*this,
*PP));
}
} // namespace clang::tidy::readability