c++ - Why does the only signed integer type causes overflow warning in clang -


The following simple code example causes a warning:
  Include # lt   

; Iostream & gt; #include & lt; Climits & gt; Int b = INT_MAX + 1; // Warning: Overflow in Expression; The result is -2147483648 // type 'int' [-wigger-overflow] unsigned int a = INT_MAX + 1; // OK main () {std :: cout & lt; & Lt; B & L; & Lt; Std :: endl & lt; & Lt; a; }

Why is there no warning in the second line with overflow? Is this a bug or do not understand anything?

This is a bug, though the result is "right thing". And it seems that the operation has actually been unsigned prior to the operation, as far as I can tell.

The warning is generated by this code, which clearly checks for unsigned and skips overflow check (clearly intended and included in the comment):

  /// The given integer operation, which is known for the needs of most of the bitwidth /// bits, and check the overflow in the original type (if that type was not a /// unsigned type ). Template & lt; Typename operation & gt; Static APSInt CheckedIntArithmetic (EvalInfo and information, constant Expr * E, constant APSInt & LHS, constant APSInt & RHS, unsigned BitWidth, operation op) {if (LHS.isUnsigned ()) returned (LHS, RHS); APSIT value (OP (LHS extension (bitwidth), RHS extension (bitdream)), incorrect; APSInt result = Value.trunc (LHS.getBitWidth ()); If (Result.extend (BitWidth) = Value!) {If (Info.checkingForOverflow ()) Info.Ctx.getDiagnostics () Report (E & gt; getExprLoc (), diagnostic :: warn_integer_constant_overflow) & Lt; & Lt; Results (10) & lt; & Lt; E & gt; GetType (); And handle overflow (type in notice, e, value, e- gt) (); } Return results; }   

We see, unsigned warnings can not be given:

  if returned (LHS.isUnsigned ()) Op (LHS, RHS);   

delete the program (, #includes from a relevant header to copy the code to #define INT_MAX ... ), cut it down and If the ring is running with jig ++ -Xclang -ast-dump ... , then the following output returns:; & Lt; Invalid SLOC & gt; & Gt

  TranslationUnitDecl 0x4ca2830 & lt; & Lt; Invalid sloc & gt; | -Captive DECL 0x4ca2d70 & lt; & Lt; Invalid sloc & gt; & Gt; & Lt; Invalid sloc & gt; Built-in __int128_t '__int128' | -speedfexx0x4ca2dd0  Col: 14 An 'unsigned int' cinit | `-IplicitCastExpr 0x4ce2340 & lt; Line: 1: 19, line: 6: 28> 'Unsigned' int & lieutenant; Integral Cast & gt; | `-Binary operator 0x4ce2318 & lt; Line: 1: 19, Line: 6: 28> 'Int' '+' | | -tegaiyar literal 0x4ce22d8 & lt; Line: 1: 1 9 & gt; 'Int' 2147483647 | `-techier literal 0x4ce22f8 & lt; Row: 6: 28 & gt; 'Int' 1 `function decl 0x4ce23b0 & lt; Line: 7: 1, Line: 10: 1> Line: 7: 5 Main 'int' (`Empty)` - CompStSTMT 0x4ce24a8 & lt; Line: 8: 1, Line: 10: 1>   

We can clearly see that this is an integer:

  | `-Binary operator 0x4ce2318 & lt; Line: 1: 19, Line: 6: 28> 'Int' '+' | | -tegaiyar literal 0x4ce22d8 & lt; Line: 1: 1 9 & gt; 'Int' 2147483647 | `-techier literal 0x4ce22f8 & lt; Row: 6: 28 & gt; 'Int' 1   

Therefore, the compiler must first cast an unsigned file, then it should implement + . Casting means:

  | `-IplicitCastExpr 0x4ce2340 & lt; Line: 1: 19, line: 6: 28> 'Unsigned' int & lieutenant; Integral Cast & gt;   

I'll see another one to see if I can know why it's really wrong. But he has to wait a bit ...

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -