diff options
author | Alex Sepkowski <alexsepkowski@gmail.com> | 2025-07-09 16:13:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-09 16:13:28 -0700 |
commit | 7c16a31aa593b9cc750e61b260c27ade74edb1dd (patch) | |
tree | 6628e4804b3e91a3d0a63100ba1a565f51c9c12b /clang/lib/Format/Format.cpp | |
parent | ddf9b91f9fed8654c4649881f7db51f1e474701f (diff) | |
download | llvm-7c16a31aa593b9cc750e61b260c27ade74edb1dd.zip llvm-7c16a31aa593b9cc750e61b260c27ade74edb1dd.tar.gz llvm-7c16a31aa593b9cc750e61b260c27ade74edb1dd.tar.bz2 |
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
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r-- | clang/lib/Format/Format.cpp | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index f0412cd..8b217bc 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -22,7 +22,9 @@ #include "UnwrappedLineFormatter.h" #include "UsingDeclarationsSorter.h" #include "clang/Tooling/Inclusions/HeaderIncludes.h" + #include "llvm/ADT/Sequence.h" +#include <limits> #define DEBUG_TYPE "format-formatter" @@ -777,7 +779,7 @@ template <> struct MappingTraits<FormatStyle::SpacesInLineComment> { IO.mapOptional("Maximum", signedMaximum); Space.Maximum = static_cast<unsigned>(signedMaximum); - if (Space.Maximum != -1u) + if (Space.Maximum < std::numeric_limits<unsigned>::max()) Space.Minimum = std::min(Space.Minimum, Space.Maximum); } }; @@ -1672,7 +1674,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.SpacesBeforeTrailingComments = 1; LLVMStyle.SpacesInAngles = FormatStyle::SIAS_Never; LLVMStyle.SpacesInContainerLiterals = true; - LLVMStyle.SpacesInLineCommentPrefix = {/*Minimum=*/1, /*Maximum=*/-1u}; + LLVMStyle.SpacesInLineCommentPrefix = { + /*Minimum=*/1, /*Maximum=*/std::numeric_limits<unsigned>::max()}; LLVMStyle.SpacesInParens = FormatStyle::SIPO_Never; LLVMStyle.SpacesInSquareBrackets = false; LLVMStyle.Standard = FormatStyle::LS_Latest; @@ -3168,11 +3171,12 @@ static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start, // the index of the first of the duplicates as the others are going to be // removed. OffsetToEOL describes the cursor's position relative to the end of // its current line. -// If `Cursor` is not on any #include, `Index` will be UINT_MAX. +// If `Cursor` is not on any #include, `Index` will be +// std::numeric_limits<unsigned>::max(). static std::pair<unsigned, unsigned> FindCursorIndex(const ArrayRef<IncludeDirective> &Includes, const ArrayRef<unsigned> &Indices, unsigned Cursor) { - unsigned CursorIndex = UINT_MAX; + unsigned CursorIndex = std::numeric_limits<unsigned>::max(); unsigned OffsetToEOL = 0; for (int i = 0, e = Includes.size(); i != e; ++i) { unsigned Start = Includes[Indices[i]].Offset; @@ -3440,11 +3444,12 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code, return Replaces; } -// Returns group number to use as a first order sort on imports. Gives UINT_MAX -// if the import does not match any given groups. +// Returns group number to use as a first order sort on imports. Gives +// std::numeric_limits<unsigned>::max() if the import does not match any given +// groups. static unsigned findJavaImportGroup(const FormatStyle &Style, StringRef ImportIdentifier) { - unsigned LongestMatchIndex = UINT_MAX; + unsigned LongestMatchIndex = std::numeric_limits<unsigned>::max(); unsigned LongestMatchLength = 0; for (unsigned I = 0; I < Style.JavaImportGroups.size(); I++) { const std::string &GroupPrefix = Style.JavaImportGroups[I]; @@ -3673,13 +3678,15 @@ formatReplacements(StringRef Code, const tooling::Replacements &Replaces, namespace { inline bool isHeaderInsertion(const tooling::Replacement &Replace) { - return Replace.getOffset() == UINT_MAX && Replace.getLength() == 0 && + return Replace.getOffset() == std::numeric_limits<unsigned>::max() && + Replace.getLength() == 0 && tooling::HeaderIncludes::IncludeRegex.match( Replace.getReplacementText()); } inline bool isHeaderDeletion(const tooling::Replacement &Replace) { - return Replace.getOffset() == UINT_MAX && Replace.getLength() == 1; + return Replace.getOffset() == std::numeric_limits<unsigned>::max() && + Replace.getLength() == 1; } // FIXME: insert empty lines between newly created blocks. @@ -3699,7 +3706,7 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces, consumeError(HeaderInsertions.add(R)); } else if (isHeaderDeletion(R)) { HeadersToDelete.insert(R.getReplacementText()); - } else if (R.getOffset() == UINT_MAX) { + } else if (R.getOffset() == std::numeric_limits<unsigned>::max()) { llvm::errs() << "Insertions other than header #include insertion are " "not supported! " << R.getReplacementText() << "\n"; |