www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Some things from GCC 5

reply "bearophile" <bearophileHUGS lycos.com> writes:
Some interesting changes in GCC 5.0:
https://gcc.gnu.org/gcc-5/changes.html

-----------------

A new command-line option -Wlogical-not-parentheses has been 
added for the C and C++ compilers, which warns about "logical 
not" used on the left hand side operand of a comparison.

bool not_equal(int x, int y) {
   return !x == y;  // warn here
}

return !(x == y);  // first fix-it, to negate comparison.

return (!x) == y;  // second fix-it, to silence warning.


Its usefulness:

 Close to 100% true positive.  That is hundreds of bugs found in 
 millions of
 lines of code.  For most cases, evaluating the comparison first 
 was the
 correct change.  For some others, the '!' was a typo and 
 removed.  A few
 cases involved macros.
If it's close to 100% positive then perhaps it's worth turning it into an error in D. ----------------- There are now intrinsic functions to perform arithmetics with overflow guards, __builtin_add_overflow, __builtin_sub_overflow and __builtin_mul_overflow: void * calloc (size_t x, size_t y) { size_t sz; if (__builtin_mul_overflow (x, y, &sz)) return NULL; void *ret = malloc (sz); if (ret) memset (res, 0, sz); return ret; } They are the same in GCC and LLVM-Clang, so is it worth changing the API of core.checkedint to make is the same of those functions, for efficiency and GDC/LDC implementation simplicity? ----------------- New warnings -Wsuggest-final-types and -Wsuggest-final-methods helps developers to annotate programs by final specifiers (or anonymous namespaces) in the cases where code generation improves. These warnings can be used at compile time, but they are more useful in combination with link-time optimization. Some info: https://gcc.gnu.org/ml/gcc-patches/2014-08/msg00119.html For D this can become a "tip" from the compiler. ----------------- C++ Dynamic Arrays, I miss them in D: http://www.open-std.org/JTC1/sc22/WG21/docs/papers/2013/n3662.html ----------------- Bye, bearophile
Nov 30 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 A new command-line option -Wlogical-not-parentheses has been 
 added for the C and C++ compilers, which warns about "logical 
 not" used on the left hand side operand of a comparison.

 bool not_equal(int x, int y) {
   return !x == y;  // warn here
 }

 return !(x == y);  // first fix-it, to negate comparison.

 return (!x) == y;  // second fix-it, to silence warning.
I'll wait for your opinions before asking for this error in D.
 New warnings -Wsuggest-final-types and -Wsuggest-final-methods 
 helps developers to annotate programs by final specifiers (or 
 anonymous namespaces) in the cases where code generation 
 improves. These warnings can be used at compile time, but they 
 are more useful in combination with link-time optimization.

 Some info:
 https://gcc.gnu.org/ml/gcc-patches/2014-08/msg00119.html
I have added this to Bugzilla, it can please the "final on default" crowd: https://issues.dlang.org/show_bug.cgi?id=13798 Bye, bearophile
Nov 30 2014
parent "Meta" <jared771 gmail.com> writes:
On Sunday, 30 November 2014 at 10:58:01 UTC, bearophile wrote:
 I'll wait for your opinions before asking for this error in D.

 I have added this to Bugzilla, it can please the "final on 
 default" crowd:
 https://issues.dlang.org/show_bug.cgi?id=13798

 Bye,
 bearophile
It seems that both of these could be added as a rule for DScanner (maybe not the latter, I don't know). It's not ideal, but anyone that cares enough to want these to be warnings or errors is using DScanner anyway.
Nov 30 2014