Files
TheRock/compiler/amd-llvm/clang-tools-extra/clang-tidy/readability/RedundantPreprocessorCheck.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

141 lines
5.0 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 "RedundantPreprocessorCheck.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
namespace clang::tidy::readability {
static StringRef getConditionText(SourceLocation Loc, const SourceManager &SM,
const LangOptions &LangOpts) {
bool Invalid = false;
const FileID FID = SM.getFileID(Loc);
const StringRef Buffer = SM.getBufferData(FID, &Invalid);
if (Invalid)
return {};
// Initialize a raw lexer starting exactly at the condition's location
Lexer RawLexer(SM.getLocForStartOfFile(FID), LangOpts, Buffer.begin(),
SM.getCharacterData(Loc), Buffer.end());
RawLexer.SetCommentRetentionState(true);
Token Tok;
// Lex the 'if' token itself
RawLexer.LexFromRawLexer(Tok);
const unsigned StartOffset = SM.getFileOffset(Tok.getEndLoc());
unsigned EndOffset = StartOffset;
// Lex tokens until we hit the start of a new line or EOF.
// The lexer handles backslash line continuations automatically.
while (!RawLexer.LexFromRawLexer(Tok)) {
if (Tok.isAtStartOfLine() || Tok.is(tok::eof))
break;
EndOffset = SM.getFileOffset(Tok.getLocation()) + Tok.getLength();
}
if (EndOffset <= StartOffset)
return {};
// Extract the raw text from the buffer to preserve original spacing
return Buffer.substr(StartOffset, EndOffset - StartOffset).trim();
}
namespace {
/// Information about an opening preprocessor directive.
struct PreprocessorEntry {
SourceLocation Loc;
/// Condition used after the preprocessor directive.
std::string Condition;
};
const char WarningDescription[] =
"nested redundant %select{#if|#ifdef|#ifndef}0; consider removing it";
const char NoteDescription[] = "previous %select{#if|#ifdef|#ifndef}0 was here";
class RedundantPreprocessorCallbacks : public PPCallbacks {
enum DirectiveKind { DK_If = 0, DK_Ifdef = 1, DK_Ifndef = 2 };
public:
explicit RedundantPreprocessorCallbacks(ClangTidyCheck &Check,
Preprocessor &PP)
: Check(Check), PP(PP) {}
void If(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue) override {
const StringRef Condition =
getConditionText(Loc, PP.getSourceManager(), PP.getLangOpts());
checkMacroRedundancy(Loc, Condition, IfStack, DK_If, DK_If, true);
}
void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MacroDefinition) override {
const std::string MacroName = PP.getSpelling(MacroNameTok);
checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifdef, DK_Ifdef, true);
checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifdef, DK_Ifndef,
false);
}
void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MacroDefinition) override {
const std::string MacroName = PP.getSpelling(MacroNameTok);
checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifndef, DK_Ifndef,
true);
checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifndef, DK_Ifdef,
false);
}
void Endif(SourceLocation Loc, SourceLocation IfLoc) override {
if (!IfStack.empty() && IfLoc == IfStack.back().Loc)
IfStack.pop_back();
if (!IfdefStack.empty() && IfLoc == IfdefStack.back().Loc)
IfdefStack.pop_back();
if (!IfndefStack.empty() && IfLoc == IfndefStack.back().Loc)
IfndefStack.pop_back();
}
private:
void checkMacroRedundancy(SourceLocation Loc, StringRef MacroName,
SmallVector<PreprocessorEntry, 4> &Stack,
DirectiveKind WarningKind, DirectiveKind NoteKind,
bool Store) {
if (PP.getSourceManager().isInMainFile(Loc)) {
for (const auto &Entry : Stack) {
if (Entry.Condition == MacroName) {
Check.diag(Loc, WarningDescription) << WarningKind;
Check.diag(Entry.Loc, NoteDescription, DiagnosticIDs::Note)
<< NoteKind;
}
}
}
if (Store)
// This is an actual directive to be remembered.
Stack.push_back({Loc, std::string(MacroName)});
}
ClangTidyCheck &Check;
Preprocessor &PP;
SmallVector<PreprocessorEntry, 4> IfStack;
SmallVector<PreprocessorEntry, 4> IfdefStack;
SmallVector<PreprocessorEntry, 4> IfndefStack;
};
} // namespace
void RedundantPreprocessorCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
PP->addPPCallbacks(
::std::make_unique<RedundantPreprocessorCallbacks>(*this, *PP));
}
} // namespace clang::tidy::readability