Skip to content

Commit 7c16a31

Browse files
authored
Address a handful of C4146 compiler warnings where literals can be replaced with std::numeric_limits (#147623)
This PR addresses instances of compiler warning C4146 that can be replaced with std::numeric_limits. Specifically, these are cases where a literal such as '-1ULL' was used to assign a value to a uint64_t variable. The intent is much cleaner if we use the appropriate std::numeric_limits value<Type>::max() for these cases. Addresses #147439
1 parent ddf9b91 commit 7c16a31

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,11 +1428,11 @@ namespace {
14281428
}
14291429
bool destroy(bool RunDestructors = true) {
14301430
bool OK = cleanup(Info, RunDestructors, OldStackSize);
1431-
OldStackSize = -1U;
1431+
OldStackSize = std::numeric_limits<unsigned>::max();
14321432
return OK;
14331433
}
14341434
~ScopeRAII() {
1435-
if (OldStackSize != -1U)
1435+
if (OldStackSize != std::numeric_limits<unsigned>::max())
14361436
destroy(false);
14371437
// Body moved to a static method to encourage the compiler to inline away
14381438
// instances of this class.

clang/lib/Format/Format.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include "UnwrappedLineFormatter.h"
2323
#include "UsingDeclarationsSorter.h"
2424
#include "clang/Tooling/Inclusions/HeaderIncludes.h"
25+
2526
#include "llvm/ADT/Sequence.h"
27+
#include <limits>
2628

2729
#define DEBUG_TYPE "format-formatter"
2830

@@ -777,7 +779,7 @@ template <> struct MappingTraits<FormatStyle::SpacesInLineComment> {
777779
IO.mapOptional("Maximum", signedMaximum);
778780
Space.Maximum = static_cast<unsigned>(signedMaximum);
779781

780-
if (Space.Maximum != -1u)
782+
if (Space.Maximum < std::numeric_limits<unsigned>::max())
781783
Space.Minimum = std::min(Space.Minimum, Space.Maximum);
782784
}
783785
};
@@ -1672,7 +1674,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16721674
LLVMStyle.SpacesBeforeTrailingComments = 1;
16731675
LLVMStyle.SpacesInAngles = FormatStyle::SIAS_Never;
16741676
LLVMStyle.SpacesInContainerLiterals = true;
1675-
LLVMStyle.SpacesInLineCommentPrefix = {/*Minimum=*/1, /*Maximum=*/-1u};
1677+
LLVMStyle.SpacesInLineCommentPrefix = {
1678+
/*Minimum=*/1, /*Maximum=*/std::numeric_limits<unsigned>::max()};
16761679
LLVMStyle.SpacesInParens = FormatStyle::SIPO_Never;
16771680
LLVMStyle.SpacesInSquareBrackets = false;
16781681
LLVMStyle.Standard = FormatStyle::LS_Latest;
@@ -3168,11 +3171,12 @@ static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
31683171
// the index of the first of the duplicates as the others are going to be
31693172
// removed. OffsetToEOL describes the cursor's position relative to the end of
31703173
// its current line.
3171-
// If `Cursor` is not on any #include, `Index` will be UINT_MAX.
3174+
// If `Cursor` is not on any #include, `Index` will be
3175+
// std::numeric_limits<unsigned>::max().
31723176
static std::pair<unsigned, unsigned>
31733177
FindCursorIndex(const ArrayRef<IncludeDirective> &Includes,
31743178
const ArrayRef<unsigned> &Indices, unsigned Cursor) {
3175-
unsigned CursorIndex = UINT_MAX;
3179+
unsigned CursorIndex = std::numeric_limits<unsigned>::max();
31763180
unsigned OffsetToEOL = 0;
31773181
for (int i = 0, e = Includes.size(); i != e; ++i) {
31783182
unsigned Start = Includes[Indices[i]].Offset;
@@ -3440,11 +3444,12 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
34403444
return Replaces;
34413445
}
34423446

3443-
// Returns group number to use as a first order sort on imports. Gives UINT_MAX
3444-
// if the import does not match any given groups.
3447+
// Returns group number to use as a first order sort on imports. Gives
3448+
// std::numeric_limits<unsigned>::max() if the import does not match any given
3449+
// groups.
34453450
static unsigned findJavaImportGroup(const FormatStyle &Style,
34463451
StringRef ImportIdentifier) {
3447-
unsigned LongestMatchIndex = UINT_MAX;
3452+
unsigned LongestMatchIndex = std::numeric_limits<unsigned>::max();
34483453
unsigned LongestMatchLength = 0;
34493454
for (unsigned I = 0; I < Style.JavaImportGroups.size(); I++) {
34503455
const std::string &GroupPrefix = Style.JavaImportGroups[I];
@@ -3673,13 +3678,15 @@ formatReplacements(StringRef Code, const tooling::Replacements &Replaces,
36733678
namespace {
36743679

36753680
inline bool isHeaderInsertion(const tooling::Replacement &Replace) {
3676-
return Replace.getOffset() == UINT_MAX && Replace.getLength() == 0 &&
3681+
return Replace.getOffset() == std::numeric_limits<unsigned>::max() &&
3682+
Replace.getLength() == 0 &&
36773683
tooling::HeaderIncludes::IncludeRegex.match(
36783684
Replace.getReplacementText());
36793685
}
36803686

36813687
inline bool isHeaderDeletion(const tooling::Replacement &Replace) {
3682-
return Replace.getOffset() == UINT_MAX && Replace.getLength() == 1;
3688+
return Replace.getOffset() == std::numeric_limits<unsigned>::max() &&
3689+
Replace.getLength() == 1;
36833690
}
36843691

36853692
// FIXME: insert empty lines between newly created blocks.
@@ -3699,7 +3706,7 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
36993706
consumeError(HeaderInsertions.add(R));
37003707
} else if (isHeaderDeletion(R)) {
37013708
HeadersToDelete.insert(R.getReplacementText());
3702-
} else if (R.getOffset() == UINT_MAX) {
3709+
} else if (R.getOffset() == std::numeric_limits<unsigned>::max()) {
37033710
llvm::errs() << "Insertions other than header #include insertion are "
37043711
"not supported! "
37053712
<< R.getReplacementText() << "\n";

clang/lib/Lex/Lexer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <cstddef>
4242
#include <cstdint>
4343
#include <cstring>
44+
#include <limits>
4445
#include <optional>
4546
#include <string>
4647
#include <tuple>
@@ -3456,7 +3457,7 @@ std::optional<uint32_t> Lexer::tryReadNumericUCN(const char *&StartPtr,
34563457
}
34573458

34583459
unsigned Value = llvm::hexDigitValue(C);
3459-
if (Value == -1U) {
3460+
if (Value == std::numeric_limits<unsigned>::max()) {
34603461
if (!Delimited)
34613462
break;
34623463
if (Diagnose)

clang/lib/Sema/SemaExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "llvm/Support/SaveAndRestore.h"
6666
#include "llvm/Support/TimeProfiler.h"
6767
#include "llvm/Support/TypeSize.h"
68+
#include <limits>
6869
#include <optional>
6970

7071
using namespace clang;
@@ -1906,7 +1907,7 @@ ExprResult Sema::CreateGenericSelectionExpr(
19061907
}
19071908

19081909
SmallVector<unsigned, 1> CompatIndices;
1909-
unsigned DefaultIndex = -1U;
1910+
unsigned DefaultIndex = std::numeric_limits<unsigned>::max();
19101911
// Look at the canonical type of the controlling expression in case it was a
19111912
// deduced type like __auto_type. However, when issuing diagnostics, use the
19121913
// type the user wrote in source rather than the canonical one.
@@ -1961,7 +1962,8 @@ ExprResult Sema::CreateGenericSelectionExpr(
19611962
// C11 6.5.1.1p2 "If a generic selection has no default generic association,
19621963
// its controlling expression shall have type compatible with exactly one of
19631964
// the types named in its generic association list."
1964-
if (DefaultIndex == -1U && CompatIndices.size() == 0) {
1965+
if (DefaultIndex == std::numeric_limits<unsigned>::max() &&
1966+
CompatIndices.size() == 0) {
19651967
auto P = GetControllingRangeAndType(ControllingExpr, ControllingType);
19661968
SourceRange SR = P.first;
19671969
Diag(SR.getBegin(), diag::err_generic_sel_no_match) << SR << P.second;

0 commit comments

Comments
 (0)