[llvm] r315406 - Silence MSVC warnings about unsigned wrapping without UB

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 10 18:40:39 PDT 2017


Author: rnk
Date: Tue Oct 10 18:40:38 2017
New Revision: 315406

URL: http://llvm.org/viewvc/llvm-project?rev=315406&view=rev
Log:
Silence MSVC warnings about unsigned wrapping without UB

Of course, casting an unsigned value too large for 'int' is UB. So,
write out the ternary. LLVM folds it to ADD anyway.

Fixes the warning from r303693 a different way.

Thanks to Erich Keane for pointing this out!

Modified:
    llvm/trunk/lib/IR/Attributes.cpp

Modified: llvm/trunk/lib/IR/Attributes.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Attributes.cpp?rev=315406&r1=315405&r2=315406&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Attributes.cpp (original)
+++ llvm/trunk/lib/IR/Attributes.cpp Tue Oct 10 18:40:38 2017
@@ -790,14 +790,12 @@ std::string AttributeSetNode::getAsStrin
 // AttributeListImpl Definition
 //===----------------------------------------------------------------------===//
 
-/// Map from AttributeList index to the internal array index. Adding one works:
-///   FunctionIndex: ~0U -> 0
-///   ReturnIndex:    0  -> 1
-///   FirstArgIndex: 1.. -> 2..
+/// Map from AttributeList index to the internal array index. Adding one happens
+/// to work, but it relies on unsigned integer wrapping. MSVC warns about
+/// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
+/// folds it to add anyway.
 static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
-  // MSVC warns about '~0U + 1' wrapping around when this is called on
-  // FunctionIndex, so cast to int first.
-  return static_cast<int>(Index) + 1;
+  return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
 }
 
 AttributeListImpl::AttributeListImpl(LLVMContext &C,




More information about the llvm-commits mailing list