Skip to content

Commit 23118b9

Browse files
authored
[NFC] Address compiler warnings: C4146 - A 'grab bag' of remaining instances (microsoft#7574)
## Fix C4146 warnings: unary minus on unsigned types Fixes several remaining MSVC C4146 warnings where unary minus was applied to unsigned integers. This should be the last PR containing MSVC C4146 warning fixes. I will remove the disablement of the warning as an error in a subsequent PR once the other pending PRs are completed. **Changes:** - Replace `-(unsigned_value)` with `~unsigned_value + 1` for offset calculations - Use `-1LL` instead of `-1ULL` where signed values are intended - Fix alignment padding calculation to avoid unsigned negation **Files changed:** - CoverageMappingGen.cpp, Lexer.cpp, Rewriter.cpp: Use two's complement for safe unsigned negation in offset calculations - ItaniumCXXABI.cpp: Use signed literal for ABI-compliant null member pointer (-1) - ExprConstant.cpp: Replace `-1ULL` with `~0ULL` for bitmasks - CodeGenMapTable.cpp: Fix sentinel value generation All changes are mathematically equivalent and preserve existing behavior while eliminating compiler warnings. Addresses microsoft#7573
1 parent 3e01e8b commit 23118b9

12 files changed

Lines changed: 42 additions & 36 deletions

File tree

lib/Support/APFloat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
446446
if (~parts[count])
447447
return ~(integerPart) 0; /* A lot. */
448448

449-
return -parts[0];
449+
return (~parts[0] + 1);
450450
}
451451

452452
return ~(integerPart) 0; /* A lot. */

lib/Support/StringRef.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/ADT/Hashing.h"
1313
#include "llvm/ADT/edit_distance.h"
1414
#include <bitset>
15+
#include <limits>
1516

1617
using namespace llvm;
1718

@@ -393,13 +394,16 @@ bool llvm::getAsSignedInteger(StringRef Str, unsigned Radix,
393394

394395
// Get the positive part of the value.
395396
if (getAsUnsignedInteger(Str.substr(1), Radix, ULLVal) ||
396-
// Reject values so large they'd overflow as negative signed, but allow
397-
// "-0". This negates the unsigned so that the negative isn't undefined
398-
// on signed overflow.
399-
(long long)-ULLVal > 0)
397+
// Reject values larger than what can be represented as negative signed.
398+
// The most negative long long is LLONG_MIN, which has magnitude
399+
// (LLONG_MAX + 1). Values larger than this magnitude cannot be negated
400+
// without overflow.
401+
ULLVal > static_cast<unsigned long long>(
402+
std::numeric_limits<long long>::max()) +
403+
1)
400404
return true;
401405

402-
Result = -ULLVal;
406+
Result = (~ULLVal + 1);
403407
return false;
404408
}
405409

lib/Support/TimeValue.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ using namespace sys;
1919

2020
const TimeValue::SecondsType
2121
TimeValue::PosixZeroTimeSeconds = -946684800;
22-
const TimeValue::SecondsType
23-
TimeValue::Win32ZeroTimeSeconds = -12591158400ULL;
22+
const TimeValue::SecondsType TimeValue::Win32ZeroTimeSeconds = -12591158400LL;
2423

2524
void
2625
TimeValue::normalize( void ) {

lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,8 +1395,11 @@ static bool isAMCompletelyFolded(const TargetTransformInfo &TTI,
13951395
// ICmpZero -1*ScaleReg + BaseOffset => ICmp ScaleReg, BaseOffset
13961396
// Offs is the ICmp immediate.
13971397
if (Scale == 0)
1398-
// The cast does the right thing with INT64_MIN.
1399-
BaseOffset = -(uint64_t)BaseOffset;
1398+
// Negate BaseOffset using two's complement (~x + 1) to avoid undefined
1399+
// behavior. Simple negation (-BaseOffset) would be undefined for
1400+
// INT64_MIN since -INT64_MIN cannot fit in int64_t. Two's complement
1401+
// gives the expected wraparound behavior: -INT64_MIN becomes INT64_MIN.
1402+
BaseOffset = ~BaseOffset + 1ULL;
14001403
return TTI.isLegalICmpImmediate(BaseOffset);
14011404
}
14021405

@@ -3000,7 +3003,7 @@ void LSRInstance::CollectFixupsAndInitialFormulae() {
30003003
// of -1) are now also interesting.
30013004
for (size_t i = 0, e = Factors.size(); i != e; ++i)
30023005
if (Factors[i] != -1)
3003-
Factors.insert(-(uint64_t)Factors[i]);
3006+
Factors.insert(~Factors[i] + 1ULL);
30043007
Factors.insert(-1);
30053008
}
30063009

@@ -3739,7 +3742,7 @@ void LSRInstance::GenerateCrossUseConstantOffsets() {
37393742
const SCEV *OrigReg = WI.OrigReg;
37403743

37413744
Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType());
3742-
const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm));
3745+
const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, ~Imm + 1ULL));
37433746
unsigned BitWidth = SE.getTypeSizeInBits(IntTy);
37443747

37453748
// TODO: Use a more targeted data structure.
@@ -3754,8 +3757,8 @@ void LSRInstance::GenerateCrossUseConstantOffsets() {
37543757
if (F.ScaledReg == OrigReg) {
37553758
int64_t Offset = (uint64_t)F.BaseOffset + Imm * (uint64_t)F.Scale;
37563759
// Don't create 50 + reg(-50).
3757-
if (F.referencesReg(SE.getSCEV(
3758-
ConstantInt::get(IntTy, -(uint64_t)Offset))))
3760+
if (F.referencesReg(
3761+
SE.getSCEV(ConstantInt::get(IntTy, ~Offset + 1ULL))))
37593762
continue;
37603763
Formula NewF = F;
37613764
NewF.BaseOffset = Offset;
@@ -4556,7 +4559,7 @@ Value *LSRInstance::Expand(const LSRFixup &LF, const Formula &F,
45564559
// The other interesting way of "folding" with an ICmpZero is to use a
45574560
// negated immediate.
45584561
if (!ICmpScaledV)
4559-
ICmpScaledV = ConstantInt::get(IntTy, -(uint64_t)Offset);
4562+
ICmpScaledV = ConstantInt::get(IntTy, ~Offset + 1ULL);
45604563
else {
45614564
Ops.push_back(SE.getUnknown(ICmpScaledV));
45624565
ICmpScaledV = ConstantInt::get(IntTy, Offset);
@@ -4608,8 +4611,8 @@ Value *LSRInstance::Expand(const LSRFixup &LF, const Formula &F,
46084611
assert((F.Scale == 0 || F.Scale == 1) &&
46094612
"ICmp does not support folding a global value and "
46104613
"a scale at the same time!");
4611-
Constant *C = ConstantInt::getSigned(SE.getEffectiveSCEVType(OpTy),
4612-
-(uint64_t)Offset);
4614+
Constant *C =
4615+
ConstantInt::getSigned(SE.getEffectiveSCEVType(OpTy), ~Offset + 1ULL);
46134616
if (C->getType() != OpTy)
46144617
C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
46154618
OpTy, false),

tools/clang/lib/AST/ExprConstant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6555,7 +6555,7 @@ bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
65556555
// handle all cases where the expression has side-effects.
65566556
if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
65576557
if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1)
6558-
return Success(-1ULL, E);
6558+
return Success(~0ULL, E);
65596559
return Success(0, E);
65606560
}
65616561

@@ -6570,7 +6570,7 @@ bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
65706570
return Error(E);
65716571
case EvalInfo::EM_ConstantExpressionUnevaluated:
65726572
case EvalInfo::EM_PotentialConstantExpressionUnevaluated:
6573-
return Success(-1ULL, E);
6573+
return Success(~0ULL, E);
65746574
}
65756575
llvm_unreachable("Invalid EvalMode!");
65766576
}

tools/clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) {
633633

634634
uint64_t Value = static_cast<uint64_t>(Number);
635635
if (Number < 0) {
636-
Value = -Value;
636+
Value = ~Value + 1ULL;
637637
Out << '?';
638638
}
639639

@@ -2308,7 +2308,7 @@ static void mangleThunkThisAdjustment(const CXXMethodDecl *MD,
23082308
Out << AccessSpec;
23092309
Mangler.mangleNumber(
23102310
static_cast<uint32_t>(Adjustment.Virtual.Microsoft.VtordispOffset));
2311-
Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
2311+
Mangler.mangleNumber(~static_cast<uint32_t>(Adjustment.NonVirtual) + 1);
23122312
}
23132313
} else if (Adjustment.NonVirtual != 0) {
23142314
switch (MD->getAccess()) {
@@ -2323,7 +2323,7 @@ static void mangleThunkThisAdjustment(const CXXMethodDecl *MD,
23232323
case AS_public:
23242324
Out << 'W';
23252325
}
2326-
Mangler.mangleNumber(-static_cast<uint32_t>(Adjustment.NonVirtual));
2326+
Mangler.mangleNumber(~static_cast<uint32_t>(Adjustment.NonVirtual) + 1);
23272327
} else {
23282328
switch (MD->getAccess()) {
23292329
case AS_none:

tools/clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class CoverageMappingBuilder {
116116
/// \brief Return the start location of an included file or expanded macro.
117117
SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
118118
if (Loc.isMacroID())
119-
return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
119+
return Loc.getLocWithOffset(~SM.getFileOffset(Loc) + 1);
120120
return SM.getLocForStartOfFile(SM.getFileID(Loc));
121121
}
122122

tools/clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,8 @@ llvm::Constant *
639639
ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
640640
// Itanium C++ ABI 2.3:
641641
// A NULL pointer is represented as -1.
642-
if (MPT->isMemberDataPointer())
643-
return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
642+
if (MPT->isMemberDataPointer())
643+
return llvm::ConstantInt::get(CGM.PtrDiffTy, -1LL, /*isSigned=*/true);
644644

645645
llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
646646
llvm::Constant *Values[2] = { Zero, Zero };
@@ -1023,7 +1023,7 @@ static CharUnits computeOffsetHint(ASTContext &Context,
10231023
// If Dst is not derived from Src we can skip the whole computation below and
10241024
// return that Src is not a public base of Dst. Record all inheritance paths.
10251025
if (!Dst->isDerivedFrom(Src, Paths))
1026-
return CharUnits::fromQuantity(-2ULL);
1026+
return CharUnits::fromQuantity(-2LL);
10271027

10281028
unsigned NumPublicPaths = 0;
10291029
CharUnits Offset;
@@ -1040,7 +1040,7 @@ static CharUnits computeOffsetHint(ASTContext &Context,
10401040
// If the path contains a virtual base class we can't give any hint.
10411041
// -1: no hint.
10421042
if (J->Base->isVirtual())
1043-
return CharUnits::fromQuantity(-1ULL);
1043+
return CharUnits::fromQuantity(-1LL);
10441044

10451045
if (NumPublicPaths > 1) // Won't use offsets, skip computation.
10461046
continue;
@@ -1053,11 +1053,11 @@ static CharUnits computeOffsetHint(ASTContext &Context,
10531053

10541054
// -2: Src is not a public base of Dst.
10551055
if (NumPublicPaths == 0)
1056-
return CharUnits::fromQuantity(-2ULL);
1056+
return CharUnits::fromQuantity(-2LL);
10571057

10581058
// -3: Src is a multiple public base type but never a virtual base type.
10591059
if (NumPublicPaths > 1)
1060-
return CharUnits::fromQuantity(-3ULL);
1060+
return CharUnits::fromQuantity(-3LL);
10611061

10621062
// Otherwise, the Src type is a unique public nonvirtual base type of Dst.
10631063
// Return the offset of Src from the origin of Dst.
@@ -1154,7 +1154,7 @@ llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF,
11541154

11551155
// Get the offset-to-top from the vtable.
11561156
llvm::Value *OffsetToTop =
1157-
CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1157+
CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2LL);
11581158
OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top");
11591159

11601160
// Finally, add the offset to the pointer.

tools/clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
12831283
Addr = CGF.Builder.CreateGEP(Addr, Offset);
12841284
llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
12851285
CGF.Int32Ty);
1286-
llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1286+
llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, ~Align + 1);
12871287
Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
12881288
Addr->getType(),
12891289
"ap.cur.aligned");
@@ -2849,7 +2849,7 @@ static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
28492849
overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
28502850
llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
28512851
CGF.Int64Ty);
2852-
llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
2852+
llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~Align + 1);
28532853
overflow_arg_area =
28542854
CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
28552855
overflow_arg_area->getType(),

tools/clang/lib/Format/Format.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ class FormatTokenLexer {
10491049
FormatTok = new (Allocator.Allocate()) FormatToken;
10501050
readRawToken(*FormatTok);
10511051
SourceLocation WhitespaceStart =
1052-
FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
1052+
FormatTok->Tok.getLocation().getLocWithOffset(~TrailingWhitespace + 1);
10531053
FormatTok->IsFirst = IsFirstToken;
10541054
IsFirstToken = false;
10551055

0 commit comments

Comments
 (0)