Browse Source

* don't print range check warnings when evaluating inlined explicit typecasts

git-svn-id: trunk@46904 -
Jonas Maebe 4 years ago
parent
commit
9f42931eeb
3 changed files with 28 additions and 1 deletions
  1. 1 0
      .gitattributes
  2. 7 1
      compiler/ncnv.pas
  3. 20 0
      tests/tbs/tb0676a.pp

+ 1 - 0
.gitattributes

@@ -13350,6 +13350,7 @@ tests/tbs/tb0673.pp svneol=native#text/pascal
 tests/tbs/tb0674.pp svneol=native#text/pascal
 tests/tbs/tb0675.pp svneol=native#text/pascal
 tests/tbs/tb0676.pp svneol=native#text/pascal
+tests/tbs/tb0676a.pp svneol=native#text/plain
 tests/tbs/ub0060.pp svneol=native#text/plain
 tests/tbs/ub0069.pp svneol=native#text/plain
 tests/tbs/ub0119.pp svneol=native#text/plain

+ 7 - 1
compiler/ncnv.pas

@@ -3175,7 +3175,13 @@ implementation
                        if (target_info.endian = endian_big) and (nf_absolute in flags) then
                          swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
                        if not(nf_generic_para in flags) then
-                         adaptrange(resultdef,tordconstnode(left).value,([nf_internal,nf_absolute]*flags)<>[],nf_explicit in flags,cs_check_range in localswitches);
+                          adaptrange(
+                            resultdef,tordconstnode(left).value,
+                            { when evaluating an explicit typecast during inlining, don't warn about
+                              lost bits; only warn if someone literally typed e.g. byte($1ff) }
+                            (([nf_internal,nf_absolute]*flags)<>[]) or (forinline and (nf_explicit in flags)),
+                            nf_explicit in flags,
+                            cs_check_range in localswitches);
                        { swap value back, but according to new type }
                        if (target_info.endian = endian_big) and (nf_absolute in flags) then
                          swap_const_value(tordconstnode(left).value,resultdef.size);

+ 20 - 0
tests/tbs/tb0676a.pp

@@ -0,0 +1,20 @@
+{ %opt=-vw -Sew }
+{ %norun }
+
+{$mode objfpc}
+
+function SwapEndian(const AValue: Word): Word;inline;
+  begin
+    Result := Word((AValue shr 8) or (AValue shl 8));
+  end;
+
+const
+  Value = 8008;
+
+var
+  v: Word;
+begin
+  writeln(sizeof(Value));
+  Writeln(HexStr(Value, 4));
+  v := swapendian(Value);
+  Writeln(HexStr(v, 4));