From a844f33f2475eb6d62285a1ce267e0196355b473 Mon Sep 17 00:00:00 2001 From: Alex Sepkowski <5620315+alsepkow@users.noreply.github.com> Date: Fri, 20 Jun 2025 17:52:32 -0700 Subject: [PATCH] [NFC] Address compiler warnings: C4146 - Trivial std::numeric_limits cases (#7559) Addresses #7558. There is also one trivial change to use the ~ operator included in LEB128.h. My notes on the files were wrong and suggested that it should use std::numeric_limits but looking at it again using ~0ULL made more sense. --- .../llvm/DebugInfo/DWARF/DWARFDebugAranges.h | 11 +++--- include/llvm/Support/BlockFrequency.h | 5 ++- lib/Analysis/LoopAccessAnalysis.cpp | 2 +- .../InstCombine/InstructionCombining.cpp | 3 +- lib/Transforms/Scalar/LoadCombine.cpp | 4 +-- tools/clang/include/clang/AST/Expr.h | 4 ++- tools/clang/lib/AST/Expr.cpp | 34 ++++++++----------- tools/clang/lib/CodeGen/CGExprScalar.cpp | 3 +- tools/clang/lib/Lex/Lexer.cpp | 2 +- tools/clang/lib/Sema/SemaExpr.cpp | 5 +-- tools/clang/lib/Sema/SemaType.cpp | 4 +-- utils/TableGen/FixedLenDecoderEmitter.cpp | 9 ++--- 12 files changed, 46 insertions(+), 40 deletions(-) diff --git a/include/llvm/DebugInfo/DWARF/DWARFDebugAranges.h b/include/llvm/DebugInfo/DWARF/DWARFDebugAranges.h index 791f010a88..c34cfab284 100644 --- a/include/llvm/DebugInfo/DWARF/DWARFDebugAranges.h +++ b/include/llvm/DebugInfo/DWARF/DWARFDebugAranges.h @@ -32,12 +32,13 @@ class DWARFDebugAranges { void construct(); struct Range { - explicit Range(uint64_t LowPC = -1ULL, uint64_t HighPC = -1ULL, - uint32_t CUOffset = -1U) - : LowPC(LowPC), Length(HighPC - LowPC), CUOffset(CUOffset) {} + explicit Range(uint64_t LowPC = std::numeric_limits::max(), + uint64_t HighPC = std::numeric_limits::max(), + uint32_t CUOffset = std::numeric_limits::max()) + : LowPC(LowPC), Length(HighPC - LowPC), CUOffset(CUOffset) {} void setHighPC(uint64_t HighPC) { - if (HighPC == -1ULL || HighPC <= LowPC) + if (HighPC == std::numeric_limits::max() || HighPC <= LowPC) Length = 0; else Length = HighPC - LowPC; @@ -45,7 +46,7 @@ class DWARFDebugAranges { uint64_t HighPC() const { if (Length) return LowPC + Length; - return -1ULL; + return std::numeric_limits::max(); } bool containsAddress(uint64_t Address) const { diff --git a/include/llvm/Support/BlockFrequency.h b/include/llvm/Support/BlockFrequency.h index 4304a253b2..d7d6d741f4 100644 --- a/include/llvm/Support/BlockFrequency.h +++ b/include/llvm/Support/BlockFrequency.h @@ -15,6 +15,7 @@ #define LLVM_SUPPORT_BLOCKFREQUENCY_H #include "llvm/Support/DataTypes.h" +#include namespace llvm { @@ -29,7 +30,9 @@ class BlockFrequency { BlockFrequency(uint64_t Freq = 0) : Frequency(Freq) { } /// \brief Returns the maximum possible frequency, the saturation value. - static uint64_t getMaxFrequency() { return -1ULL; } + static uint64_t getMaxFrequency() { + return std::numeric_limits::max(); + } /// \brief Returns the frequency as a fixpoint number scaled by the entry /// frequency. diff --git a/lib/Analysis/LoopAccessAnalysis.cpp b/lib/Analysis/LoopAccessAnalysis.cpp index d6316dc75b..7e5e3e5ebd 100644 --- a/lib/Analysis/LoopAccessAnalysis.cpp +++ b/lib/Analysis/LoopAccessAnalysis.cpp @@ -1179,7 +1179,7 @@ bool MemoryDepChecker::areDepsSafe(DepCandidates &AccessSets, MemAccessInfoSet &CheckDeps, const ValueToValueMap &Strides) { - MaxSafeDepDistBytes = -1U; + MaxSafeDepDistBytes = std::numeric_limits::max(); while (!CheckDeps.empty()) { MemAccessInfo CurAccess = *CheckDeps.begin(); diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 6bc322fa92..c93232b67f 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1937,7 +1937,8 @@ Instruction *InstCombiner::visitAllocSite(Instruction &MI) { } else if (IntrinsicInst *II = dyn_cast(I)) { if (II->getIntrinsicID() == Intrinsic::objectsize) { ConstantInt *CI = cast(II->getArgOperand(1)); - uint64_t DontKnow = CI->isZero() ? -1ULL : 0; + uint64_t DontKnow = + CI->isZero() ? std::numeric_limits::max() : 0; ReplaceInstUsesWith(*I, ConstantInt::get(I->getType(), DontKnow)); } } diff --git a/lib/Transforms/Scalar/LoadCombine.cpp b/lib/Transforms/Scalar/LoadCombine.cpp index 6d358744ef..8f22bb337d 100644 --- a/lib/Transforms/Scalar/LoadCombine.cpp +++ b/lib/Transforms/Scalar/LoadCombine.cpp @@ -131,10 +131,10 @@ bool LoadCombine::aggregateLoads(SmallVectorImpl &Loads) { LoadInst *BaseLoad = nullptr; SmallVector AggregateLoads; bool Combined = false; - uint64_t PrevOffset = -1ull; + uint64_t PrevOffset = std::numeric_limits::max(); uint64_t PrevSize = 0; for (auto &L : Loads) { - if (PrevOffset == -1ull) { + if (PrevOffset == std::numeric_limits::max()) { BaseLoad = L.Load; PrevOffset = L.POP.Offset; PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize( diff --git a/tools/clang/include/clang/AST/Expr.h b/tools/clang/include/clang/AST/Expr.h index 26eff309f7..55fd184a79 100644 --- a/tools/clang/include/clang/AST/Expr.h +++ b/tools/clang/include/clang/AST/Expr.h @@ -4510,7 +4510,9 @@ class GenericSelectionExpr : public Expr { Expr *getControllingExpr() { return cast(SubExprs[CONTROLLING]); } /// Whether this generic selection is result-dependent. - bool isResultDependent() const { return ResultIndex == -1U; } + bool isResultDependent() const { + return ResultIndex == std::numeric_limits::max(); + } /// The zero-based index of the result expression's generic association in /// the generic selection's association list. Defined only if the diff --git a/tools/clang/lib/AST/Expr.cpp b/tools/clang/lib/AST/Expr.cpp index c6dc21217e..65484ba336 100644 --- a/tools/clang/lib/AST/Expr.cpp +++ b/tools/clang/lib/AST/Expr.cpp @@ -3886,25 +3886,21 @@ GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context, std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR); } -GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context, - SourceLocation GenericLoc, Expr *ControllingExpr, - ArrayRef AssocTypes, - ArrayRef AssocExprs, - SourceLocation DefaultLoc, - SourceLocation RParenLoc, - bool ContainsUnexpandedParameterPack) - : Expr(GenericSelectionExprClass, - Context.DependentTy, - VK_RValue, - OK_Ordinary, - /*isTypeDependent=*/true, - /*isValueDependent=*/true, - /*isInstantiationDependent=*/true, - ContainsUnexpandedParameterPack), - AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]), - SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]), - NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc), - DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) { +GenericSelectionExpr::GenericSelectionExpr( + const ASTContext &Context, SourceLocation GenericLoc, Expr *ControllingExpr, + ArrayRef AssocTypes, ArrayRef AssocExprs, + SourceLocation DefaultLoc, SourceLocation RParenLoc, + bool ContainsUnexpandedParameterPack) + : Expr(GenericSelectionExprClass, Context.DependentTy, VK_RValue, + OK_Ordinary, + /*isTypeDependent=*/true, + /*isValueDependent=*/true, + /*isInstantiationDependent=*/true, ContainsUnexpandedParameterPack), + AssocTypes(new(Context) TypeSourceInfo *[AssocTypes.size()]), + SubExprs(new(Context) Stmt *[END_EXPR + AssocExprs.size()]), + NumAssocs(AssocExprs.size()), + ResultIndex(std::numeric_limits::max()), GenericLoc(GenericLoc), + DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) { SubExprs[CONTROLLING] = ControllingExpr; assert(AssocTypes.size() == AssocExprs.size()); std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes); diff --git a/tools/clang/lib/CodeGen/CGExprScalar.cpp b/tools/clang/lib/CodeGen/CGExprScalar.cpp index 530c791fcc..50aae94505 100644 --- a/tools/clang/lib/CodeGen/CGExprScalar.cpp +++ b/tools/clang/lib/CodeGen/CGExprScalar.cpp @@ -2559,7 +2559,8 @@ void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck( llvm::Value *IntMin = Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth())); - llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL); + llvm::Value *NegOne = + llvm::ConstantInt::get(Ty, std::numeric_limits::max()); llvm::Value *LHSCmp = Builder.CreateICmpNE(Ops.LHS, IntMin); llvm::Value *RHSCmp = Builder.CreateICmpNE(Ops.RHS, NegOne); diff --git a/tools/clang/lib/Lex/Lexer.cpp b/tools/clang/lib/Lex/Lexer.cpp index 089e76b78b..e39573ca34 100644 --- a/tools/clang/lib/Lex/Lexer.cpp +++ b/tools/clang/lib/Lex/Lexer.cpp @@ -2737,7 +2737,7 @@ uint32_t Lexer::tryReadUCN(const char *&StartPtr, const char *SlashLoc, char C = getCharAndSize(CurPtr, CharSize); unsigned Value = llvm::hexDigitValue(C); - if (Value == -1U) { + if (Value == std::numeric_limits::max()) { if (Result && !isLexingRawMode()) { if (i == 0) { Diag(BufferPtr, diag::warn_ucn_escape_no_digits) diff --git a/tools/clang/lib/Sema/SemaExpr.cpp b/tools/clang/lib/Sema/SemaExpr.cpp index 389fcfc3ff..d5d20e49b1 100644 --- a/tools/clang/lib/Sema/SemaExpr.cpp +++ b/tools/clang/lib/Sema/SemaExpr.cpp @@ -1466,7 +1466,7 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, ContainsUnexpandedParameterPack); SmallVector CompatIndices; - unsigned DefaultIndex = -1U; + unsigned DefaultIndex = std::numeric_limits::max(); for (unsigned i = 0; i < NumAssocs; ++i) { if (!Types[i]) DefaultIndex = i; @@ -1498,7 +1498,8 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, // C11 6.5.1.1p2 "If a generic selection has no default generic association, // its controlling expression shall have type compatible with exactly one of // the types named in its generic association list." - if (DefaultIndex == -1U && CompatIndices.size() == 0) { + if (DefaultIndex == std::numeric_limits::max() && + CompatIndices.size() == 0) { // We strip parens here because the controlling expression is typically // parenthesized in macro definitions. ControllingExpr = ControllingExpr->IgnoreParens(); diff --git a/tools/clang/lib/Sema/SemaType.cpp b/tools/clang/lib/Sema/SemaType.cpp index ff3b0dbac7..f08ae486b5 100644 --- a/tools/clang/lib/Sema/SemaType.cpp +++ b/tools/clang/lib/Sema/SemaType.cpp @@ -462,7 +462,7 @@ distributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state, // objc_gc goes on the innermost pointer to something that's not a // pointer. - unsigned innermost = -1U; + unsigned innermost = std::numeric_limits::max(); bool considerDeclSpec = true; for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { DeclaratorChunk &chunk = declarator.getTypeObject(i); @@ -501,7 +501,7 @@ distributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state, // Otherwise, if we found an appropriate chunk, splice the attribute // into it. - if (innermost != -1U) { + if (innermost != std::numeric_limits::max()) { moveAttrFromListToList(attr, declarator.getAttrListRef(), declarator.getTypeObject(innermost).getAttrListRef()); return; diff --git a/utils/TableGen/FixedLenDecoderEmitter.cpp b/utils/TableGen/FixedLenDecoderEmitter.cpp index c5ef9d0e99..d356971f24 100644 --- a/utils/TableGen/FixedLenDecoderEmitter.cpp +++ b/utils/TableGen/FixedLenDecoderEmitter.cpp @@ -547,10 +547,11 @@ void Filter::recurse() { // Delegates to an inferior filter chooser for further processing on this // group of instructions whose segment values are variable. - FilterChooserMap.insert( - std::make_pair(-1U, llvm::make_unique( - Owner->AllInstructions, VariableInstructions, - Owner->Operands, BitValueArray, *Owner))); + FilterChooserMap.insert(std::make_pair( + std::numeric_limits::max(), + llvm::make_unique(Owner->AllInstructions, + VariableInstructions, Owner->Operands, + BitValueArray, *Owner))); } // No need to recurse for a singleton filtered instruction.