[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-08-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 160243.
0x8000- added a comment.

Rebased on curent master.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,199 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, \
+// RUN:   {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;1.0;101.0;0x1.2p3"}, \
+// RUN:   {key: readability-magic-numbers.IgnorePowersOf2IntegerValues, value: 1}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[15];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 15 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 22; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 22 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 66 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float SomeFloats[] = {0.5, 0x1.2p4};
+// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 0.5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: 0x1.2p4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in th

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-08-12 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In https://reviews.llvm.org/D49114#1196480, @aaron.ballman wrote:

> Alex has had plenty of time to respond, so I'm fine handling any concerns he 
> has post-commit. Do you need me to commit this on your behalf?


Yes, please! Thank you!

Author: "Florin Iucha "


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-19 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 3 inline comments as done.
0x8000- added inline comments.



Comment at: test/clang-tidy/readability-magic-numbers.cpp:16
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 
[readability-magic-numbers]

aaron.ballman wrote:
> 0x8000- wrote:
> > 0x8000- wrote:
> > > Quuxplusone wrote:
> > > > Please add test cases immediately following this one, for
> > > > 
> > > > const int BadLocalConstInt = 6;
> > > > constexpr int BadLocalConstexprInt = 6;
> > > > static const int BadLocalStaticConstInt = 6;
> > > > static constexpr int BadLocalStaticConstexprInt = 6;
> > > > 
> > > > (except of course changing "Bad" to "Good" in any cases where 6 is 
> > > > acceptable as an initializer).
> > > > 
> > > > Also
> > > > 
> > > > std::vector BadLocalVector(6);
> > > > const std::vector BadLocalConstVector(6);
> > > > 
> > > > etc. etc.
> > > Again... all the "const .* (\d)+" patterns should be acceptable. We're 
> > > initializing a constant. Would you prefer an explicit option?
> > I have  template and constructor arguments already in the test. I have 
> > tried including  but somehow it is not found and the std::vector is 
> > reported as an error in itself.
> Tests need to be hermetic and cannot rely on STL headers or other system 
> headers. Basically, you have to replicate the contents of  (or 
> whatever) within the test file for whatever you're trying to test.
Ok - we have that already and there's nothing magic about std::vector or 
std::array.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-19 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 156392.
0x8000- added a comment.

Small refactoring and documentation update.

Revert built-in acceptable integers to -1, 0 and 1 and document them.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,154 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.141592741 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yval) noexcept : x{xval}, y{yv

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-19 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 156422.
0x8000- added a comment.

Add a (presently failing) test for not tripping up on __LINE__ through several 
layers of macro expansion (as in GoogleTest library). This creates a lot of 
false positives in the unit tests and needs to be fixed.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,172 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.141592741 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+tem

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-19 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 156425.
0x8000- added a comment.

Filter out synthetic integers (such as _LINE_) from the report.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,172 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.141592741 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yval) noexcept : x{xval}, y{yval} {
+  }
+};
+
+template 
+struct Dimension {
+  

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-19 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

  ./tools/clang/tools/extra/clang-tidy/tool/run-clang-tidy.py 
-clang-tidy-binary ../llvm.rel/bin/clang-tidy 
-checks="-*,readability-magic-numbers" -j 12 -p ../llvm.rel -j 12 -quiet > 
/tmp/llvm.magic
  grep "warning:" /tmp/llvm.magic | cut -d: -f5 | cut -d" " -f2 | sort | uniq 
-c | sort -rn | head -40

With about 2000 files scanned, these are the popularity rankings:

  9559 2
  5041 4
  4359 8
  2811 3
  2555 16
  1748 32
  1213 64
  1170 10
  1155 128
   910 5
   853 6
   606 7
   537 256
   385 12
   382 15
   349 20
   322 1024
   288 255
   252 100
   238 9
   233 11
   181 40
   174 63
   170 24
   163 31
   161 512
   146 65535
   144 13
   136 14
   126 18
   120 1
   113 17
   103 1000
   100 4096
92 60
85 192
84 30
81 21
76 25
71 23

So it seems power of 2 are popular, as are 10, 100, 1000.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-19 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 156430.
0x8000- added a comment.

Avoid parsing and reformatting the input literal - just print the original 
source code.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,174 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angles[] = { 45.0f, 90.0f, 135.0f };
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yv

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-23 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 156950.
0x8000- added a comment.

Ignore literals implicitly added by the compiler, such as when using a 
range-for loop over a constant array.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,190 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angles[] = {45.0f, 90.0f, 135.0f};
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-24 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157000.
0x8000- added a comment.

Bail out early if a [grand-]parent node is a declaration, but not a constant 
declaration.
Search for enumerations and declarations in the same tree traversal.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,192 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angles[] = {45.0f, 90.0f, 135.0f};
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted =

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-24 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

@aaron.ballman , @JonasToth , @Eugene.Zelenko  - would you be so kind to do one 
more review pass and let me know if there are any blockers or if this is ready 
to merge? I do not have commit privileges, so if it is alright, feel free to 
merge it.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-24 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157186.
0x8000- added a comment.

Fix a few typos


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,192 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angles[] = {45.0f, 90.0f, 135.0f};
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yval) noexcept : x{xval}, y{yval} {
+ 

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-24 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157187.
0x8000- added a comment.

Update links in documentation


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,192 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angles[] = {45.0f, 90.0f, 135.0f};
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yval) noexcept : x{xval}

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-26 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 3 inline comments as done.
0x8000- added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:55
+
+By default only `0`, `1` and `-1` integer values are accepted without a 
warning.
+This can be overridden with the :option:`IgnoredIntegerValues` option.  In 
addition,

JonasToth wrote:
> -1 is not in the default list anymore.
I will update the comment. -1 is still accepted because it is (unary operator) 
- followed by accepted (integer literal) 1.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-26 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157615.
0x8000- marked an inline comment as done.
0x8000- added a comment.

Remove extra verbose namespaces and update documentation to indicate why -1 is 
accepted even when not explicitly called out.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,192 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angles[] = {45.0f, 90.0f, 135.0f};
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZero

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157863.
0x8000- added a comment.

Address review comments to improve documentation and readability


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,192 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angles[] = {45.0f, 90.0f, 135.0f};
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 7 inline comments as done.
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:53
+
+const char DefaultIgnoredIntegerValues[] = "0;1;";
+

aaron.ballman wrote:
> Is the trailing semicolon after the `1` necessary?
I have found so, experimentally.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:61-63
+configuration for accepted floating point values, primarily because most
+floating point comparisons are not exact, and some of the exact ones are not
+portable.

aaron.ballman wrote:
> I am curious to know how true this is. You got some data for integer values 
> and reported it, but I'm wondering if you've tried the same experiment with 
> floating-point numbers?
The problem with the floating point numbers as text is: they need to be parsed 
both from the configuration and from the source code _then_ compared. What is 
an acceptable epsilon? I don't know. Is the same epsilon acceptable on all 
source code? I don't know.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked an inline comment as done.
0x8000- added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:61-63
+configuration for accepted floating point values, primarily because most
+floating point comparisons are not exact, and some of the exact ones are not
+portable.

lebedev.ri wrote:
> aaron.ballman wrote:
> > 0x8000- wrote:
> > > aaron.ballman wrote:
> > > > I am curious to know how true this is. You got some data for integer 
> > > > values and reported it, but I'm wondering if you've tried the same 
> > > > experiment with floating-point numbers?
> > > The problem with the floating point numbers as text is: they need to be 
> > > parsed both from the configuration and from the source code _then_ 
> > > compared. What is an acceptable epsilon? I don't know. Is the same 
> > > epsilon acceptable on all source code? I don't know.
> > Yeah, I'm not too worried about the situations in which the epsilon 
> > matters. I'm more worried that we'll see a lot of 1.0, 2.0 floating-point 
> > literals where the floating-point value is a nice, round, easy-to-represent 
> > number but users have no way to disable this diagnostic short of `const 
> > float Two = 2.0f;`
> Random thought: the types that are ignored should/could be configurable, i.e. 
> there should be a switch
> whether or not to complain about floats.
Even though they might be nice and round... they should mean _something_ other 
than 'Two'.

The thing is... magic integers are used as buffer sizes, or to map things that 
are discrete in nature - number of legs of a typical mammal for instance. Not 
sure what magic numbers exist in nature besides pi and e and some fundamental 
physical constants )Avogadro's number, etc). But even there, it is better to 
use a symbolic constant.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:61-63
+configuration for accepted floating point values, primarily because most
+floating point comparisons are not exact, and some of the exact ones are not
+portable.

0x8000- wrote:
> lebedev.ri wrote:
> > aaron.ballman wrote:
> > > 0x8000- wrote:
> > > > aaron.ballman wrote:
> > > > > I am curious to know how true this is. You got some data for integer 
> > > > > values and reported it, but I'm wondering if you've tried the same 
> > > > > experiment with floating-point numbers?
> > > > The problem with the floating point numbers as text is: they need to be 
> > > > parsed both from the configuration and from the source code _then_ 
> > > > compared. What is an acceptable epsilon? I don't know. Is the same 
> > > > epsilon acceptable on all source code? I don't know.
> > > Yeah, I'm not too worried about the situations in which the epsilon 
> > > matters. I'm more worried that we'll see a lot of 1.0, 2.0 floating-point 
> > > literals where the floating-point value is a nice, round, 
> > > easy-to-represent number but users have no way to disable this diagnostic 
> > > short of `const float Two = 2.0f;`
> > Random thought: the types that are ignored should/could be configurable, 
> > i.e. there should be a switch
> > whether or not to complain about floats.
> Even though they might be nice and round... they should mean _something_ 
> other than 'Two'.
> 
> The thing is... magic integers are used as buffer sizes, or to map things 
> that are discrete in nature - number of legs of a typical mammal for 
> instance. Not sure what magic numbers exist in nature besides pi and e and 
> some fundamental physical constants )Avogadro's number, etc). But even there, 
> it is better to use a symbolic constant.
Actually that is a _great_ idea, thank you!


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

I will add one more option: IgnoreFloatingPointValues, to ignore all floats and 
doubles, because the FloatingLiteral does not distinguish between them (as 
implementation detail), and I don't see a good reason to be strict about 
doubles and lenient about floats, or viceversa. The default value for this 
option is false.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157871.
0x8000- added a comment.

Add option to ignore all floating point values.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,192 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angles[] = {45.0f, 90.0f, 135.0f};
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yval) 

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:61-63
+configuration for accepted floating point values, primarily because most
+floating point comparisons are not exact, and some of the exact ones are not
+portable.

lebedev.ri wrote:
> aaron.ballman wrote:
> > 0x8000- wrote:
> > > 0x8000- wrote:
> > > > lebedev.ri wrote:
> > > > > aaron.ballman wrote:
> > > > > > 0x8000- wrote:
> > > > > > > aaron.ballman wrote:
> > > > > > > > I am curious to know how true this is. You got some data for 
> > > > > > > > integer values and reported it, but I'm wondering if you've 
> > > > > > > > tried the same experiment with floating-point numbers?
> > > > > > > The problem with the floating point numbers as text is: they need 
> > > > > > > to be parsed both from the configuration and from the source code 
> > > > > > > _then_ compared. What is an acceptable epsilon? I don't know. Is 
> > > > > > > the same epsilon acceptable on all source code? I don't know.
> > > > > > Yeah, I'm not too worried about the situations in which the epsilon 
> > > > > > matters. I'm more worried that we'll see a lot of 1.0, 2.0 
> > > > > > floating-point literals where the floating-point value is a nice, 
> > > > > > round, easy-to-represent number but users have no way to disable 
> > > > > > this diagnostic short of `const float Two = 2.0f;`
> > > > > Random thought: the types that are ignored should/could be 
> > > > > configurable, i.e. there should be a switch
> > > > > whether or not to complain about floats.
> > > > Even though they might be nice and round... they should mean 
> > > > _something_ other than 'Two'.
> > > > 
> > > > The thing is... magic integers are used as buffer sizes, or to map 
> > > > things that are discrete in nature - number of legs of a typical mammal 
> > > > for instance. Not sure what magic numbers exist in nature besides pi 
> > > > and e and some fundamental physical constants )Avogadro's number, etc). 
> > > > But even there, it is better to use a symbolic constant.
> > > Actually that is a _great_ idea, thank you!
> > > The thing is... magic integers are used as buffer sizes, or to map things 
> > > that are discrete in nature - number of legs of a typical mammal for 
> > > instance. Not sure what magic numbers exist in nature besides pi and e 
> > > and some fundamental physical constants )Avogadro's number, etc). But 
> > > even there, it is better to use a symbolic constant.
> > 
> > That's my point -- I think there's a lot of uses of round floating-point 
> > values that are not magical numbers, they're sensible constants. Looking at 
> > LLVM's code base shows a *lot* of 1.0 and 2.0 values (hundreds of instances 
> > from a quick text-based search). No one should be forced to turn those into 
> > named constants. However, I've seen code using `1.02` and `.98` in places 
> > -- those seem like sensible things to make named constants because the 
> > values have semantically interesting meaning to the surrounding code.
> > 
> > > Random thought: the types that are ignored should/could be configurable, 
> > > i.e. there should be a switch
> > whether or not to complain about floats.
> > 
> > I think this would be a useful option, for sure (I used to work at a place 
> > that did a ton of floating-point math that would benefit from the integer 
> > side of this check but could never use the floating-point side of it). 
> > However, the presence of such an option doesn't give us a pass on coming up 
> > with a data-driven list of default values to ignore for the floating-point 
> > side. If we don't want to make that list configurable, I think that's 
> > something we can discuss (I think I'm fine with not making it a user-facing 
> > configuration option). But I think that `0.0` is insufficient.
> Yep. I didn't mean for that flag to be a replacement for the ignore-list for 
> fp constants.
This opens up an entire can of worms that requires quite a bit of thought and 
invites significant amount of bikesheding. Is "2.00" as acceptable as "2.0"? Do 
we compare textually, or with regular expressions? Is 2.0001 represented 
the same way on PowerPC and ARM as on Intel?

As this check is specified and implemented right now, people have the following 
options:
* not use the check at all
* use it to flag all literals
* use it to flag only integral literals
* use it to flag all integral literals not on a white list.

If somebody can come up with a more logical spec for how to filter out certain 
floating point values - they can definitely extend this check, without 
impacting users that are happy with the menu offered above.

Does this sound reasonable?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:61-63
+configuration for accepted floating point values, primarily because most
+floating point comparisons are not exact, and some of the exact ones are not
+portable.

aaron.ballman wrote:
> 0x8000- wrote:
> > lebedev.ri wrote:
> > > aaron.ballman wrote:
> > > > 0x8000- wrote:
> > > > > 0x8000- wrote:
> > > > > > lebedev.ri wrote:
> > > > > > > aaron.ballman wrote:
> > > > > > > > 0x8000- wrote:
> > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > I am curious to know how true this is. You got some data 
> > > > > > > > > > for integer values and reported it, but I'm wondering if 
> > > > > > > > > > you've tried the same experiment with floating-point 
> > > > > > > > > > numbers?
> > > > > > > > > The problem with the floating point numbers as text is: they 
> > > > > > > > > need to be parsed both from the configuration and from the 
> > > > > > > > > source code _then_ compared. What is an acceptable epsilon? I 
> > > > > > > > > don't know. Is the same epsilon acceptable on all source 
> > > > > > > > > code? I don't know.
> > > > > > > > Yeah, I'm not too worried about the situations in which the 
> > > > > > > > epsilon matters. I'm more worried that we'll see a lot of 1.0, 
> > > > > > > > 2.0 floating-point literals where the floating-point value is a 
> > > > > > > > nice, round, easy-to-represent number but users have no way to 
> > > > > > > > disable this diagnostic short of `const float Two = 2.0f;`
> > > > > > > Random thought: the types that are ignored should/could be 
> > > > > > > configurable, i.e. there should be a switch
> > > > > > > whether or not to complain about floats.
> > > > > > Even though they might be nice and round... they should mean 
> > > > > > _something_ other than 'Two'.
> > > > > > 
> > > > > > The thing is... magic integers are used as buffer sizes, or to map 
> > > > > > things that are discrete in nature - number of legs of a typical 
> > > > > > mammal for instance. Not sure what magic numbers exist in nature 
> > > > > > besides pi and e and some fundamental physical constants 
> > > > > > )Avogadro's number, etc). But even there, it is better to use a 
> > > > > > symbolic constant.
> > > > > Actually that is a _great_ idea, thank you!
> > > > > The thing is... magic integers are used as buffer sizes, or to map 
> > > > > things that are discrete in nature - number of legs of a typical 
> > > > > mammal for instance. Not sure what magic numbers exist in nature 
> > > > > besides pi and e and some fundamental physical constants )Avogadro's 
> > > > > number, etc). But even there, it is better to use a symbolic constant.
> > > > 
> > > > That's my point -- I think there's a lot of uses of round 
> > > > floating-point values that are not magical numbers, they're sensible 
> > > > constants. Looking at LLVM's code base shows a *lot* of 1.0 and 2.0 
> > > > values (hundreds of instances from a quick text-based search). No one 
> > > > should be forced to turn those into named constants. However, I've seen 
> > > > code using `1.02` and `.98` in places -- those seem like sensible 
> > > > things to make named constants because the values have semantically 
> > > > interesting meaning to the surrounding code.
> > > > 
> > > > > Random thought: the types that are ignored should/could be 
> > > > > configurable, i.e. there should be a switch
> > > > whether or not to complain about floats.
> > > > 
> > > > I think this would be a useful option, for sure (I used to work at a 
> > > > place that did a ton of floating-point math that would benefit from the 
> > > > integer side of this check but could never use the floating-point side 
> > > > of it). However, the presence of such an option doesn't give us a pass 
> > > > on coming up with a data-driven list of default values to ignore for 
> > > > the floating-point side. If we don't want to make that list 
> > > > configurable, I think that's something we can discuss (I think I'm fine 
> > > > with not making it a user-facing configuration option). But I think 
> > > > that `0.0` is insufficient.
> > > Yep. I didn't mean for that flag to be a replacement for the ignore-list 
> > > for fp constants.
> > This opens up an entire can of worms that requires quite a bit of thought 
> > and invites significant amount of bikesheding. Is "2.00" as acceptable as 
> > "2.0"? Do we compare textually, or with regular expressions? Is 2.0001 
> > represented the same way on PowerPC and ARM as on Intel?
> > 
> > As this check is specified and implemented right now, people have the 
> > following options:
> > * not use the check at all
> > * use it to flag all literals
> > * use it to flag only integral literals
> > * use it to flag all integral literals not on a white list.
> > 
> > If somebody can come up with a more logical spec for how to filter out 
> > certain floating 

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

Not trying to be difficult here - I have attempted to implement the 
straight-forward check.

Added this to the MagicNumbersCheck::MagicNumbersCheck constructor:

  +  // process set of ignored floating point values
  +  const std::vector IgnoredFloatingPointValuesInput =
  +  utils::options::parseStringList(Options.get(
  +  "IgnoredFloatingPointValues", DefaultIgnoredFloatingPointValues));
  +  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
  +  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
  +llvm::APFloat FloatValue(llvm::APFloat::IEEEdouble());
  +FloatValue.convertFromString(InputValue, DefaultRoundingMode);
  +const double Value = FloatValue.convertToDouble();
  +IgnoredFloatingPointValues.push_back(Value);
  +  }
  +  llvm::sort(IgnoredFloatingPointValues.begin(),
  + IgnoredFloatingPointValues.end());

and changed isIgnoredValue(const FloatingLiteral *Literal) like this:

   bool MagicNumbersCheck::isIgnoredValue(const FloatingLiteral *Literal) const 
{
  -  llvm::APFloat FloatValue = Literal->getValue();
  -  return FloatValue.isZero();
  +  const llvm::APFloat FloatValue = Literal->getValue();
  +  double Value;
  +  if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) {
  +Value = FloatValue.convertToDouble();
  +  } else if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) {
  +Value = static_cast(FloatValue.convertToFloat());
  +  } else
  +return false;
  +
  +  return std::binary_search(IgnoredFloatingPointValues.begin(),
  +IgnoredFloatingPointValues.end(), Value);
   }

When running under the debugger and printing various values, it seems that 3.14 
read as double and converted to double prints as 3.1397, while 
3.14f read as float and cast to double prints as 3.141049041748

I will parse the IgnoredFloatingPointValues twice and store the results in two 
arrays, one of doubles and one of floats.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-28 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157879.
0x8000- added a comment.

Add support for ignoring specific floating point numbers.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,194 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;1.0;101.0"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredIntegerValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+float GrandfatheredFloatValues[] = { 3.14f, 3.14, 2.71828, 2.71828f, -1.01E2, 1E4 };
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+const float Angle

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157886.
0x8000- added a comment.

Indicate that `0` and `0.0` are accepted unconditionally (because it makes 
sense in the source code, and speeds-up many checks as 0s are very common and 
we don't want to spend log2(n) to find them at the beginning of the vector).


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,194 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;1.0;101.0"}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredIntegerValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+float GrandfatheredFloatValues[] = { 3.14f, 3.14, 2.71828, 2.71828f, -1.01E2, 1E4 };
+
+/*
+ * no warnings for enums
+ */
+enum Sm

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 4 inline comments as done.
0x8000- added a comment.

See inline comments. Basically we need two arrays because APFloats of different 
semantics don't compare well, and even if we coerce them, they sometimes are 
not equal.




Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:57
+const char DefaultIgnoredIntegerValues[] = "0;1;";
+const char DefaultIgnoredFloatingPointValues[] = "0.0;";
+

aaron.ballman wrote:
> I would still like to see some data on common floating-point literal values 
> used in large open source project so that we can see what sensible values 
> should be in this list.
What value would that bring? The ideal target is that there are no magic values 
- no guideline that I have seen makes exception for 3.141 or 9.81. Each project 
is special based on how they evolved, and they need to decide for themselves 
what is worth cleaning vs what can be swept under the rug for now. Why would we 
lend authority to any particular floating point value?



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

aaron.ballman wrote:
> This is where I would construct an `APFloat` object from the string given. As 
> for the semantics to be used, I would recommend getting it from 
> `TargetInfo::getDoubleFormat()` on the belief that we aren't going to care 
> about precision (explained in the documentation).
Here is the problem I tried to explain last night but perhaps I wasn't clear 
enough.

When we parse the input list from strings, we have to commit to one floating 
point value "semantic" - in our case single or double precision.

When we encounter the value in the source code and it is captured by a matcher, 
it comes as either one of those values.

Floats with different semantics can't be directly compared - so we have to 
maintain two distinct arrays.

If we do that, rather than store APFloats and sort/compare them with awkward 
lambdas, we might as well just use the native float/double and be done with it 
more cleanly.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:128-139
+  if (FloatValue.isZero())
+return true;
+  else if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEsingle()) {
+const float Value = FloatValue.convertToFloat();
+return std::binary_search(IgnoredFloatingPointValues.begin(),
+  IgnoredFloatingPointValues.end(), Value);
+  } else if (&FloatValue.getSemantics() == &llvm::APFloat::IEEEdouble()) {

aaron.ballman wrote:
> Here is where I would compare `FloatValue` against everything in the 
> `IgnoredFloatingPointValues` array using `APFloat::compare()`. However, you 
> need the floats to have the same semantics for that to work, so you'd have to 
> convert `FloatValue` using `APFloat::convert()` to whatever the target 
> floating-point format is.
If you do that, 3.14f will not be equal to 3.14 . You need two arrays.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 2 inline comments as done.
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

aaron.ballman wrote:
> 0x8000- wrote:
> > aaron.ballman wrote:
> > > This is where I would construct an `APFloat` object from the string 
> > > given. As for the semantics to be used, I would recommend getting it from 
> > > `TargetInfo::getDoubleFormat()` on the belief that we aren't going to 
> > > care about precision (explained in the documentation).
> > Here is the problem I tried to explain last night but perhaps I wasn't 
> > clear enough.
> > 
> > When we parse the input list from strings, we have to commit to one 
> > floating point value "semantic" - in our case single or double precision.
> > 
> > When we encounter the value in the source code and it is captured by a 
> > matcher, it comes as either one of those values.
> > 
> > Floats with different semantics can't be directly compared - so we have to 
> > maintain two distinct arrays.
> > 
> > If we do that, rather than store APFloats and sort/compare them with 
> > awkward lambdas, we might as well just use the native float/double and be 
> > done with it more cleanly.
> >When we encounter the value in the source code and it is captured by a 
> >matcher, it comes as either one of those values.
> 
> It may also come in as long double or __float128, for instance, because there 
> are type suffixes for that.
> 
> > Floats with different semantics can't be directly compared - so we have to 
> > maintain two distinct arrays.
> 
> Yes, floats with different semantics cannot be directly compared. That's why 
> I said below that we should coerce the literal values.
> 
> > If we do that, rather than store APFloats and sort/compare them with 
> > awkward lambdas, we might as well just use the native float/double and be 
> > done with it more cleanly.
> 
> There are too many different floating-point semantics for this to be viable, 
> hence why coercion is a reasonable behavior.
Let me see if I understood it - your proposal is: store only doubles, and when 
a floating-point literal is encountered in code, do not use the FloatingLiteral 
instance, but parse it again into a double and compare exactly. If the 
comparison matches - ignore it.

In that case what is the value of storing APFloats with double semantics in the 
IgnoredValues array, instead of doubles?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:57
+const char DefaultIgnoredIntegerValues[] = "0;1;";
+const char DefaultIgnoredFloatingPointValues[] = "0.0;";
+

aaron.ballman wrote:
> 0x8000- wrote:
> > aaron.ballman wrote:
> > > I would still like to see some data on common floating-point literal 
> > > values used in large open source project so that we can see what sensible 
> > > values should be in this list.
> > What value would that bring? The ideal target is that there are no magic 
> > values - no guideline that I have seen makes exception for 3.141 or 9.81. 
> > Each project is special based on how they evolved, and they need to decide 
> > for themselves what is worth cleaning vs what can be swept under the rug 
> > for now. Why would we lend authority to any particular floating point value?
> Because that's too high of a high false positive rate for an acceptable 
> clang-tidy check. As mentioned before, there are literally hundreds of 
> unnameable floating-point literals in LLVM alone where the value is 1.0 or 
> 2.0. Having statistical data to pick sensible defaults for this list is 
> valuable in that it lowers the false positive rate. If the user dislikes the 
> default list for some reason (because for their project, maybe 2.0 is a 
> supremely nameable literal value), they can pick a different set of defaults.
> 
> Right now, I'm operating off an assumption that most floating-point literals 
> that should not be named are going to be whole numbers that are precisely 
> represented in all floating-point semantic models. This data will tell us if 
> that assumption is wrong, and if the assumption is wrong, we might want to go 
> with separate lists like you've done.
Here are the results with the check as-is, run on the llvm code base as of last 
night:

top-40
```
  10435 2
   5543 4
   4629 8
   3271 3
   2702 16
   1876 32
   1324 64
   1309 10
   1207 5
   1116 128
966 6
733 7
575 256
421 20
406 12
339 9
331 1024
311 100
281 42
253 11
226 15
189 40
172 24
169 0xff
168 13
168 0x80
166 512
137 1.0
133 14
132 31
129 0xDEADBEEF
120 18
120 17
120 1000
115 4096
100 30
 94 60
 94 0x1234
 89 0x20
 86 0xFF
```

1.0 is in position 28 with 137 occurrences
2.0 is in position 93 with 27 occurrences
100.0 is in position 96 with 26 occurences
1.0f is in position 182 with 11 occurences

we also have 2.0e0 four times :)

This data suggests that there would be value in a IgnorePowerOf2IntegerLiterals 
option.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

aaron.ballman wrote:
> 0x8000- wrote:
> > aaron.ballman wrote:
> > > 0x8000- wrote:
> > > > aaron.ballman wrote:
> > > > > This is where I would construct an `APFloat` object from the string 
> > > > > given. As for the semantics to be used, I would recommend getting it 
> > > > > from `TargetInfo::getDoubleFormat()` on the belief that we aren't 
> > > > > going to care about precision (explained in the documentation).
> > > > Here is the problem I tried to explain last night but perhaps I wasn't 
> > > > clear enough.
> > > > 
> > > > When we parse the input list from strings, we have to commit to one 
> > > > floating point value "semantic" - in our case single or double 
> > > > precision.
> > > > 
> > > > When we encounter the value in the source code and it is captured by a 
> > > > matcher, it comes as either one of those values.
> > > > 
> > > > Floats with different semantics can't be directly compared - so we have 
> > > > to maintain two distinct arrays.
> > > > 
> > > > If we do that, rather than store APFloats and sort/compare them with 
> > > > awkward lambdas, we might as well just use the native float/double and 
> > > > be done with it more cleanly.
> > > >When we encounter the value in the source code and it is captured by a 
> > > >matcher, it comes as either one of those values.
> > > 
> > > It may also come in as long double or __float128, for instance, because 
> > > there are type suffixes for that.
> > > 
> > > > Floats with different semantics can't be directly compared - so we have 
> > > > to maintain two distinct arrays.
> > > 
> > > Yes, floats with different semantics cannot be directly compared. That's 
> > > why I said below that we should coerce the literal values.
> > > 
> > > > If we do that, rather than store APFloats and sort/compare them 

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:57
+const char DefaultIgnoredIntegerValues[] = "0;1;";
+const char DefaultIgnoredFloatingPointValues[] = "0.0;";
+

aaron.ballman wrote:
> 0x8000- wrote:
> > aaron.ballman wrote:
> > > 0x8000- wrote:
> > > > aaron.ballman wrote:
> > > > > I would still like to see some data on common floating-point literal 
> > > > > values used in large open source project so that we can see what 
> > > > > sensible values should be in this list.
> > > > What value would that bring? The ideal target is that there are no 
> > > > magic values - no guideline that I have seen makes exception for 3.141 
> > > > or 9.81. Each project is special based on how they evolved, and they 
> > > > need to decide for themselves what is worth cleaning vs what can be 
> > > > swept under the rug for now. Why would we lend authority to any 
> > > > particular floating point value?
> > > Because that's too high of a high false positive rate for an acceptable 
> > > clang-tidy check. As mentioned before, there are literally hundreds of 
> > > unnameable floating-point literals in LLVM alone where the value is 1.0 
> > > or 2.0. Having statistical data to pick sensible defaults for this list 
> > > is valuable in that it lowers the false positive rate. If the user 
> > > dislikes the default list for some reason (because for their project, 
> > > maybe 2.0 is a supremely nameable literal value), they can pick a 
> > > different set of defaults.
> > > 
> > > Right now, I'm operating off an assumption that most floating-point 
> > > literals that should not be named are going to be whole numbers that are 
> > > precisely represented in all floating-point semantic models. This data 
> > > will tell us if that assumption is wrong, and if the assumption is wrong, 
> > > we might want to go with separate lists like you've done.
> > Here are the results with the check as-is, run on the llvm code base as of 
> > last night:
> > 
> > top-40
> > ```
> >   10435 2
> >5543 4
> >4629 8
> >3271 3
> >2702 16
> >1876 32
> >1324 64
> >1309 10
> >1207 5
> >1116 128
> > 966 6
> > 733 7
> > 575 256
> > 421 20
> > 406 12
> > 339 9
> > 331 1024
> > 311 100
> > 281 42
> > 253 11
> > 226 15
> > 189 40
> > 172 24
> > 169 0xff
> > 168 13
> > 168 0x80
> > 166 512
> > 137 1.0
> > 133 14
> > 132 31
> > 129 0xDEADBEEF
> > 120 18
> > 120 17
> > 120 1000
> > 115 4096
> > 100 30
> >  94 60
> >  94 0x1234
> >  89 0x20
> >  86 0xFF
> > ```
> > 
> > 1.0 is in position 28 with 137 occurrences
> > 2.0 is in position 93 with 27 occurrences
> > 100.0 is in position 96 with 26 occurences
> > 1.0f is in position 182 with 11 occurences
> > 
> > we also have 2.0e0 four times :)
> > 
> > This data suggests that there would be value in a 
> > IgnorePowerOf2IntegerLiterals option.
> Any chance you can hack the check run on LLVM where it doesn't report any 
> integer values (I'm curious about the top ten or so for floating-point 
> values)? Additionally, it'd be great to get similar numbers from some other 
> projects, like https://github.com/qt just to see how it compares (I've used 
> `bear` to get compile_commands.json file out of Qt before, so I would guess 
> that would work here).
No need for the hack, I just grep for dot in my report:

```
137 1.0
 27 2.0
 26 100.0
 20 0.5
 11 1.0f
  8 1.02
  7 3.0
  7 1.98
  7 1.5
  6 .5
  6 1.1
  5 0.01
  4 89.0f
  4 4294967296.f
  4 3.14159
  4 2.0e0
  4 10.0
  3 88.0f
  3 255.0
  3 127.0f
  3 12345.0f
  3 123.45
  3 0.2f
  3 0.25
  3 0.1f
  3 0.1
  2 80.0
  2 710.0f
  2 710.0
  2 709.0f
  2 6.0
  2 3.14f
  2 3.14
  2 2.5
  2 2349214918.58107
  2 149.0f
  2 14.5f
  2 1.17549435e-38F
  2 11357.0f
  2 11356.0f
  2 103.0f
  2 0.99
  2 0.9
  2 0.01f
  1 745.0f
  1 745.0
  1 7.1653228759765625e2f
  1 709.0
  1 7.08687663657301358331704585496e-268
  1 6.62E-34
  1 64.0f
  1 6.02E23
  1 4950.0f
  1 4932.0f
  1 45.0f
  1 42.42
  1 4.2
  1 4.0
  1 38.0f
  1 3.7
  1 323.0f
  1 32.0f
  1 32.0
  1 3.1415
  1 308.0f
  1 2.718
  1 2.7
  1 225.0f
  1 21.67
  1 2.0f
  1 2.088
  1 .17532f
  1 16445.0f
  1 1.616f
  1 128.0f
  1 12346.0f
  1 12.0f
  1 1.2
  1 1.1f
  1 11399.0f
  1 11383.0f
  1 1.0L
  1 1.0E+9
  1 1.0E+6
  1 1.0e-5f
  1 1.0E+12
  1 1074.0f
  1 1074.0
  1 1023.0f
  1 1023.0
  1 1.01F
  1 1.01
  1 10.0f
  1 100.
  1 0.99
  1 0.8f
  1 .08215f
  1 0.7
  1 0.6
  1 0.5F
   

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

0x8000- wrote:
> aaron.ballman wrote:
> > 0x8000- wrote:
> > > aaron.ballman wrote:
> > > > 0x8000- wrote:
> > > > > aaron.ballman wrote:
> > > > > > This is where I would construct an `APFloat` object from the string 
> > > > > > given. As for the semantics to be used, I would recommend getting 
> > > > > > it from `TargetInfo::getDoubleFormat()` on the belief that we 
> > > > > > aren't going to care about precision (explained in the 
> > > > > > documentation).
> > > > > Here is the problem I tried to explain last night but perhaps I 
> > > > > wasn't clear enough.
> > > > > 
> > > > > When we parse the input list from strings, we have to commit to one 
> > > > > floating point value "semantic" - in our case single or double 
> > > > > precision.
> > > > > 
> > > > > When we encounter the value in the source code and it is captured by 
> > > > > a matcher, it comes as either one of those values.
> > > > > 
> > > > > Floats with different semantics can't be directly compared - so we 
> > > > > have to maintain two distinct arrays.
> > > > > 
> > > > > If we do that, rather than store APFloats and sort/compare them with 
> > > > > awkward lambdas, we might as well just use the native float/double 
> > > > > and be done with it more cleanly.
> > > > >When we encounter the value in the source code and it is captured by a 
> > > > >matcher, it comes as either one of those values.
> > > > 
> > > > It may also come in as long double or __float128, for instance, because 
> > > > there are type suffixes for that.
> > > > 
> > > > > Floats with different semantics can't be directly compared - so we 
> > > > > have to maintain two distinct arrays.
> > > > 
> > > > Yes, floats with different semantics cannot be directly compared. 
> > > > That's why I said below that we should coerce the literal values.
> > > > 
> > > > > If we do that, rather than store APFloats and sort/compare them with 
> > > > > awkward lambdas, we might as well just use the native float/double 
> > > > > and be done with it more cleanly.
> > > > 
> > > > There are too many different floating-point semantics for this to be 
> > > > viable, hence why coercion is a reasonable behavior.
> > > Let me see if I understood it - your proposal is: store only doubles, and 
> > > when a floating-point literal is encountered in code, do not use the 
> > > FloatingLiteral instance, but parse it again into a double and compare 
> > > exactly. If the comparison matches - ignore it.
> > > 
> > > In that case what is the value of storing APFloats with double semantics 
> > > in the IgnoredValues array, instead of doubles?
> > > Let me see if I understood it - your proposal is: store only doubles, and 
> > > when a floating-point literal is encountered in code, do not use the 
> > > FloatingLiteral instance, but parse it again into a double and compare 
> > > exactly. If the comparison matches - ignore it.
> > 
> > My proposal is to use `APFloat` as the storage and comparison medium. Read 
> > in strings from the configuration and convert them to an `APFloat` that has 
> > double semantics. Read in literals and call `FloatLiteral::getValue()` to 
> > get the `APFloat` from it, convert it to one that has double semantics as 
> > needed, then perform the comparison between those two `APFloat` objects.
> > 
> > > In that case what is the value of storing APFloats with double semantics 
> > > in the IgnoredValues array, instead of doubles?
> > 
> > Mostly that it allows us to modify or extend the check for more complicated 
> > semantics in the future. Also, it's good practice to use something with 
> > consistent semantic behavior across hosts and targets (comparisons between 
> > numbers that cannot be precisely represented will at least be consistently 
> > compared across hosts when compiling for the same target).
> > 
> ok - coming right up!
> My proposal is to use APFloat as the storage and comparison medium. Read in 
> strings from the configuration and convert them to an APFloat that has double 
> semantics.

This is easy.

> Read in literals and call FloatLiteral::getValue() to get the APFloat from 
> it, 

this is easy as well,

> convert it to one that has double semantics as needed, then perform the 
> comparison between those two APFloat objects.

The conversion methods in `APFloat` only produce plain-old-data-types: 
```
  double convertToDouble() const; 

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

0x8000- wrote:
> 0x8000- wrote:
> > 0x8000- wrote:
> > > aaron.ballman wrote:
> > > > 0x8000- wrote:
> > > > > aaron.ballman wrote:
> > > > > > 0x8000- wrote:
> > > > > > > aaron.ballman wrote:
> > > > > > > > This is where I would construct an `APFloat` object from the 
> > > > > > > > string given. As for the semantics to be used, I would 
> > > > > > > > recommend getting it from `TargetInfo::getDoubleFormat()` on 
> > > > > > > > the belief that we aren't going to care about precision 
> > > > > > > > (explained in the documentation).
> > > > > > > Here is the problem I tried to explain last night but perhaps I 
> > > > > > > wasn't clear enough.
> > > > > > > 
> > > > > > > When we parse the input list from strings, we have to commit to 
> > > > > > > one floating point value "semantic" - in our case single or 
> > > > > > > double precision.
> > > > > > > 
> > > > > > > When we encounter the value in the source code and it is captured 
> > > > > > > by a matcher, it comes as either one of those values.
> > > > > > > 
> > > > > > > Floats with different semantics can't be directly compared - so 
> > > > > > > we have to maintain two distinct arrays.
> > > > > > > 
> > > > > > > If we do that, rather than store APFloats and sort/compare them 
> > > > > > > with awkward lambdas, we might as well just use the native 
> > > > > > > float/double and be done with it more cleanly.
> > > > > > >When we encounter the value in the source code and it is captured 
> > > > > > >by a matcher, it comes as either one of those values.
> > > > > > 
> > > > > > It may also come in as long double or __float128, for instance, 
> > > > > > because there are type suffixes for that.
> > > > > > 
> > > > > > > Floats with different semantics can't be directly compared - so 
> > > > > > > we have to maintain two distinct arrays.
> > > > > > 
> > > > > > Yes, floats with different semantics cannot be directly compared. 
> > > > > > That's why I said below that we should coerce the literal values.
> > > > > > 
> > > > > > > If we do that, rather than store APFloats and sort/compare them 
> > > > > > > with awkward lambdas, we might as well just use the native 
> > > > > > > float/double and be done with it more cleanly.
> > > > > > 
> > > > > > There are too many different floating-point semantics for this to 
> > > > > > be viable, hence why coercion is a reasonable behavior.
> > > > > Let me see if I understood it - your proposal is: store only doubles, 
> > > > > and when a floating-point literal is encountered in code, do not use 
> > > > > the FloatingLiteral instance, but parse it again into a double and 
> > > > > compare exactly. If the comparison matches - ignore it.
> > > > > 
> > > > > In that case what is the value of storing APFloats with double 
> > > > > semantics in the IgnoredValues array, instead of doubles?
> > > > > Let me see if I understood it - your proposal is: store only doubles, 
> > > > > and when a floating-point literal is encountered in code, do not use 
> > > > > the FloatingLiteral instance, but parse it again into a double and 
> > > > > compare exactly. If the comparison matches - ignore it.
> > > > 
> > > > My proposal is to use `APFloat` as the storage and comparison medium. 
> > > > Read in strings from the configuration and convert them to an `APFloat` 
> > > > that has double semantics. Read in literals and call 
> > > > `FloatLiteral::getValue()` to get the `APFloat` from it, convert it to 
> > > > one that has double semantics as needed, then perform the comparison 
> > > > between those two `APFloat` objects.
> > > > 
> > > > > In that case what is the value of storing APFloats with double 
> > > > > semantics in the IgnoredValues array, instead of doubles?
> > > > 
> > > > Mostly that it allows us to modify or extend the check for more 
> > > > complicated semantics in the future. Also, it's good practice to use 
> > > > something with consistent semantic behavior across hosts and targets 
> > > > (comparisons between numbers that cannot be precisely represented will 
> > > > at least be consistently compared across hosts when compiling for the 
> > > > same target).
> > > > 
> > > ok - coming right up!
> > > My proposal is to use APFloat as the storage and comparison medium. Read 
> > > in strings from the configuration and convert them to an APFloat that has 
> > > double semantics.
> > 
> > This is easy.
> > 
> > >

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

0x8000- wrote:
> 0x8000- wrote:
> > 0x8000- wrote:
> > > 0x8000- wrote:
> > > > aaron.ballman wrote:
> > > > > 0x8000- wrote:
> > > > > > aaron.ballman wrote:
> > > > > > > 0x8000- wrote:
> > > > > > > > aaron.ballman wrote:
> > > > > > > > > This is where I would construct an `APFloat` object from the 
> > > > > > > > > string given. As for the semantics to be used, I would 
> > > > > > > > > recommend getting it from `TargetInfo::getDoubleFormat()` on 
> > > > > > > > > the belief that we aren't going to care about precision 
> > > > > > > > > (explained in the documentation).
> > > > > > > > Here is the problem I tried to explain last night but perhaps I 
> > > > > > > > wasn't clear enough.
> > > > > > > > 
> > > > > > > > When we parse the input list from strings, we have to commit to 
> > > > > > > > one floating point value "semantic" - in our case single or 
> > > > > > > > double precision.
> > > > > > > > 
> > > > > > > > When we encounter the value in the source code and it is 
> > > > > > > > captured by a matcher, it comes as either one of those values.
> > > > > > > > 
> > > > > > > > Floats with different semantics can't be directly compared - so 
> > > > > > > > we have to maintain two distinct arrays.
> > > > > > > > 
> > > > > > > > If we do that, rather than store APFloats and sort/compare them 
> > > > > > > > with awkward lambdas, we might as well just use the native 
> > > > > > > > float/double and be done with it more cleanly.
> > > > > > > >When we encounter the value in the source code and it is 
> > > > > > > >captured by a matcher, it comes as either one of those values.
> > > > > > > 
> > > > > > > It may also come in as long double or __float128, for instance, 
> > > > > > > because there are type suffixes for that.
> > > > > > > 
> > > > > > > > Floats with different semantics can't be directly compared - so 
> > > > > > > > we have to maintain two distinct arrays.
> > > > > > > 
> > > > > > > Yes, floats with different semantics cannot be directly compared. 
> > > > > > > That's why I said below that we should coerce the literal values.
> > > > > > > 
> > > > > > > > If we do that, rather than store APFloats and sort/compare them 
> > > > > > > > with awkward lambdas, we might as well just use the native 
> > > > > > > > float/double and be done with it more cleanly.
> > > > > > > 
> > > > > > > There are too many different floating-point semantics for this to 
> > > > > > > be viable, hence why coercion is a reasonable behavior.
> > > > > > Let me see if I understood it - your proposal is: store only 
> > > > > > doubles, and when a floating-point literal is encountered in code, 
> > > > > > do not use the FloatingLiteral instance, but parse it again into a 
> > > > > > double and compare exactly. If the comparison matches - ignore it.
> > > > > > 
> > > > > > In that case what is the value of storing APFloats with double 
> > > > > > semantics in the IgnoredValues array, instead of doubles?
> > > > > > Let me see if I understood it - your proposal is: store only 
> > > > > > doubles, and when a floating-point literal is encountered in code, 
> > > > > > do not use the FloatingLiteral instance, but parse it again into a 
> > > > > > double and compare exactly. If the comparison matches - ignore it.
> > > > > 
> > > > > My proposal is to use `APFloat` as the storage and comparison medium. 
> > > > > Read in strings from the configuration and convert them to an 
> > > > > `APFloat` that has double semantics. Read in literals and call 
> > > > > `FloatLiteral::getValue()` to get the `APFloat` from it, convert it 
> > > > > to one that has double semantics as needed, then perform the 
> > > > > comparison between those two `APFloat` objects.
> > > > > 
> > > > > > In that case what is the value of storing APFloats with double 
> > > > > > semantics in the IgnoredValues array, instead of doubles?
> > > > > 
> > > > > Mostly that it allows us to modify or extend the check for more 
> > > > > complicated semantics in the future. Also, it's good practice to use 
> > > > > something with consistent semantic behavior across hosts and targets 
> > > > > (comparisons between numbers that cannot be precisely represented 
> > > > > will at least be consistently compared across hosts when compiling 
> > > > > for the same target).
> > > > > 
> > > > ok - coming right up!
> > > > My proposal is to use APFloat as the 

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157889.
0x8000- added a comment.

Add a flag to ignore all powers of two integral values.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,195 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, \
+// RUN:   {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;1.0;101.0"}, \
+// RUN:   {key: readability-magic-numbers.IgnorePowersOf2IntegerValues, value: 1}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[15];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 15 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 22; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 22 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 66 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredIntegerValues[] = {0, 1, 2, 10, 100, -1, -10, -100, 65536};
+
+float GrandfatheredFloatValues[] = {3.14f, 3.14, 2.71828, 2.71828f, -1.01E2, 1E4};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+cons

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

Top 40 magic numbers in https://github.com/qt/qtbase

  4859 2
  2901 3
  1855 4
   985 5
   968 8
   605 6
   600 7
   439 16
   432 10
   363 
   356 32
   241 1.0f
   217 12
   209 255
   207 100
   205 9
   205 20
   204 50
   177 0.5
   174 15
   162 0x2
   144 24
   140 0x80
   135 11
   127 256
   113 14
   110 0xff
   101 1.0
99 64
99 200
96 13
86 30
84 1000
68 18
66 150
62 127
62 0xFF
58 19
58 0.05f
57 128

Top 40 floating point magic numbers in https://github.com/qt/qtbase

  241 1.0f
  177 0.5
  101 1.0
   58 0.05f
   44 2.0
   42 0.5f
   31 10.0
   28 30.0
   24 20.0
   22 60.0
   20 100.0
   19 0.8
   19 0.25
   17 0.2
   16 1000.0
   14 1.224744871
   14 100.
   13 25.0
   13 0.1
   12 90.0
   12 40.0
   12 0.707106781
   12 0.30
   12 0.20
   11 80.0
   11 6.0
   11 50.0
   11 2.0f
   11 0.75
   11 0.66f
   11 0.1f
   10 6.28
   10 5.0
   10 4.0
   10 1.414213562
9 360.0
9 25.4
9 2.54
8 70.0
8 55.0

Top 40 magic numbers in https://github.com/facebook/rocksdb

  2131 2
   896 3
   859 4
   858 10
   685 100
   678 1024
   600 8
   445 5
   323 1000
   244 20
   231 301
   227 200
   223 6
   209 16
   189 7
   154 1
   131 100
   119 10
   111 30
   105 256
   104 32
   103 5U
   103 50
94 128
91 64
89 60
88 3U
85 2U
84 500
72 4U
67 9
65 300
63 13
59 0xff
57 6U
52 4096
52 24
52 12
51 600
50 10U

Top 40 floating point numbers in rocksdb:

  37 100.0
  30 1.0
  27 0.5
  24 0.001
  12 1048576.0
  12 0.25
  11 1.1
   8 50.0
   8 1.5
   8 1.0
   5 .3
   5 .1
   5 0.8
   4 99.99
   4 99.9
   4 2.0
   4 1.048576
   4 100.0f
   4 0.9
   4 0.75
   4 0.69
   4 0.02
   4 0.1
   3 100.0
   3 0.4
   3 0.1
   2 0.7
   2 0.6
   2 0.45
   1 8.0
   1 5.6
   1 40.2
   1 40.1
   1 3.25
   1 2.0
   1 2.
   1 116.2
   1 116.1
   1 110.5e-4
   1 1024.0


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

aaron.ballman wrote:
> aaron.ballman wrote:
> > 0x8000- wrote:
> > > 0x8000- wrote:
> > > > 0x8000- wrote:
> > > > > 0x8000- wrote:
> > > > > > 0x8000- wrote:
> > > > > > > aaron.ballman wrote:
> > > > > > > > 0x8000- wrote:
> > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > 0x8000- wrote:
> > > > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > > > This is where I would construct an `APFloat` object 
> > > > > > > > > > > > from the string given. As for the semantics to be used, 
> > > > > > > > > > > > I would recommend getting it from 
> > > > > > > > > > > > `TargetInfo::getDoubleFormat()` on the belief that we 
> > > > > > > > > > > > aren't going to care about precision (explained in the 
> > > > > > > > > > > > documentation).
> > > > > > > > > > > Here is the problem I tried to explain last night but 
> > > > > > > > > > > perhaps I wasn't clear enough.
> > > > > > > > > > > 
> > > > > > > > > > > When we parse the input list from strings, we have to 
> > > > > > > > > > > commit to one floating point value "semantic" - in our 
> > > > > > > > > > > case single or double precision.
> > > > > > > > > > > 
> > > > > > > > > > > When we encounter the value in the source code and it is 
> > > > > > > > > > > captured by a matcher, it comes as either one of those 
> > > > > > > > > > > values.
> > > > > > > > > > > 
> > > > > > > > > > > Floats with different semantics can't be directly 
> > > > > > > > > > > compared - so we have to maintain two distinct arrays.
> > > > > > > > > > > 
> > > > > > > > > > > If we do that, rather than store APFloats and 
> > > > > > > > > > > sort/compare them with awkward lambdas, we might as well 
> > > > > > > > > > > just use the native float/double and be done with it more 
> > > > > > > > > > > cleanly.
> > > > > > > > > > >When we encounter the value in the source code and it is 
> > > > > > > > > > >captured by a matcher, it comes as either one of those 
> > > > > > > > > > >values.
> > > > > > > > > > 
> > > > > > > > > > It may also come in as long double or __float128, for 
> > > > > > > > > > instance, because there are type suffixes for that.
> > > > > > > > > > 
> > > > > > > > > > > Floats with different semantics can't be directly 
> > > > > > > > > > > compared - so we have to maintain two distinct arrays.
> > > > > > > > > > 
> > > > > > > > > > Yes, floats with different semantics cannot be directly 
> > > > > > > > > > compared. That's why I said below that we should coerce the 
> > > > > > > > > > literal values.
> > > > > > > > > > 
> > > > > > > > > > > If we do that, rather than store APFloats and 
> > > > > > > > > > > sort/compare them with awkward lambdas, we might as well 
> > > > > > > > > > > just use the native float/double and be done with it more 
> > > > > > > > > > > cleanly.
> > > > > > > > > > 
> > > > > > > > > > There are too many different floating-point semantics for 
> > > > > > > > > > this to be viable, hence why coercion is a reasonable 
> > > > > > > > > > behavior.
> > > > > > > > > Let me see if I understood it - your proposal is: store only 
> > > > > > > > > doubles, and when a floating-point literal is encountered in 
> > > > > > > > > code, do not use the FloatingLiteral instance, but parse it 
> > > > > > > > > again into a double and compare exactly. If the comparison 
> > > > > > > > > matches - ignore it.
> > > > > > > > > 
> > > > > > > > > In that case what is the value of storing APFloats with 
> > > > > > > > > double semantics in the IgnoredValues array, instead of 
> > > > > > > > > doubles?
> > > > > > > > > Let me see if I understood it - your proposal is: store only 
> > > > > > > > > doubles, and when a floating-point literal is encountered in 
> > > > > > > > > code, do not use the FloatingLiteral instance, but parse it 
> > > > > > > > > again into a double and compare exactly. If the comparison 
> > > > > > > > > matches - ignore it.
> > > > > > > > 
> > > > > > > > My proposal is to use `APFloat` as the storage and comparison 
> > > > > > > > medium. Read in strings from the configuration and convert them 
> > > > > > > > to an `APFloat` that has double semantics. Read in literals and 
> > > > > > > > call `FloatLiteral::getValue()` to get the `APFloat` from it, 
> > > > > > > > convert it to one that has double semantics as needed, then 
> > > > > > > > perform the comparison between those two `APFloat` objects

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

0x8000- wrote:
> 0x8000- wrote:
> > aaron.ballman wrote:
> > > aaron.ballman wrote:
> > > > 0x8000- wrote:
> > > > > 0x8000- wrote:
> > > > > > 0x8000- wrote:
> > > > > > > 0x8000- wrote:
> > > > > > > > 0x8000- wrote:
> > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > 0x8000- wrote:
> > > > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > > > 0x8000- wrote:
> > > > > > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > > > > > This is where I would construct an `APFloat` object 
> > > > > > > > > > > > > > from the string given. As for the semantics to be 
> > > > > > > > > > > > > > used, I would recommend getting it from 
> > > > > > > > > > > > > > `TargetInfo::getDoubleFormat()` on the belief that 
> > > > > > > > > > > > > > we aren't going to care about precision (explained 
> > > > > > > > > > > > > > in the documentation).
> > > > > > > > > > > > > Here is the problem I tried to explain last night but 
> > > > > > > > > > > > > perhaps I wasn't clear enough.
> > > > > > > > > > > > > 
> > > > > > > > > > > > > When we parse the input list from strings, we have to 
> > > > > > > > > > > > > commit to one floating point value "semantic" - in 
> > > > > > > > > > > > > our case single or double precision.
> > > > > > > > > > > > > 
> > > > > > > > > > > > > When we encounter the value in the source code and it 
> > > > > > > > > > > > > is captured by a matcher, it comes as either one of 
> > > > > > > > > > > > > those values.
> > > > > > > > > > > > > 
> > > > > > > > > > > > > Floats with different semantics can't be directly 
> > > > > > > > > > > > > compared - so we have to maintain two distinct arrays.
> > > > > > > > > > > > > 
> > > > > > > > > > > > > If we do that, rather than store APFloats and 
> > > > > > > > > > > > > sort/compare them with awkward lambdas, we might as 
> > > > > > > > > > > > > well just use the native float/double and be done 
> > > > > > > > > > > > > with it more cleanly.
> > > > > > > > > > > > >When we encounter the value in the source code and it 
> > > > > > > > > > > > >is captured by a matcher, it comes as either one of 
> > > > > > > > > > > > >those values.
> > > > > > > > > > > > 
> > > > > > > > > > > > It may also come in as long double or __float128, for 
> > > > > > > > > > > > instance, because there are type suffixes for that.
> > > > > > > > > > > > 
> > > > > > > > > > > > > Floats with different semantics can't be directly 
> > > > > > > > > > > > > compared - so we have to maintain two distinct arrays.
> > > > > > > > > > > > 
> > > > > > > > > > > > Yes, floats with different semantics cannot be directly 
> > > > > > > > > > > > compared. That's why I said below that we should coerce 
> > > > > > > > > > > > the literal values.
> > > > > > > > > > > > 
> > > > > > > > > > > > > If we do that, rather than store APFloats and 
> > > > > > > > > > > > > sort/compare them with awkward lambdas, we might as 
> > > > > > > > > > > > > well just use the native float/double and be done 
> > > > > > > > > > > > > with it more cleanly.
> > > > > > > > > > > > 
> > > > > > > > > > > > There are too many different floating-point semantics 
> > > > > > > > > > > > for this to be viable, hence why coercion is a 
> > > > > > > > > > > > reasonable behavior.
> > > > > > > > > > > Let me see if I understood it - your proposal is: store 
> > > > > > > > > > > only doubles, and when a floating-point literal is 
> > > > > > > > > > > encountered in code, do not use the FloatingLiteral 
> > > > > > > > > > > instance, but parse it again into a double and compare 
> > > > > > > > > > > exactly. If the comparison matches - ignore it.
> > > > > > > > > > > 
> > > > > > > > > > > In that case what is the value of storing APFloats with 
> > > > > > > > > > > double semantics in the IgnoredValues array, instead of 
> > > > > > > > > > > doubles?
> > > > > > > > > > > Let me see if I understood it - your proposal is: store 
> > > > > > > > > > > only doubles, and when a floating-point literal is 
> > > > > > > > > > > encountered in code, do not use the FloatingLiteral 
> > > > > > > > > > > instance, but parse it again into a double and compare 
> > > > > > > > > > > exactly. If the comparison matches - ignore it.
> > > > > > > > > > 
> > > > > > > > > > My proposal is to use `APFloat` as the storage and 
> > > > > > > > > > comparison medium. Read in strings from the con

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

> Based on this, I think the integer list should also include 2, 3, and 4 as 
> defaults -- those show up a lot more than I'd have expected. As for 
> floating-point values, 1.0 certainly jumps out at me, but none of the rest 
> seem particularly common. What do you think?

I'm good with 0, 1, 2, 3, 4 for integers and 0.0, 1.0 and also 100.0 (used to 
compute percentages) for floating point values.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
+llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
+FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
+

aaron.ballman wrote:
> 0x8000- wrote:
> > 0x8000- wrote:
> > > 0x8000- wrote:
> > > > aaron.ballman wrote:
> > > > > aaron.ballman wrote:
> > > > > > 0x8000- wrote:
> > > > > > > 0x8000- wrote:
> > > > > > > > 0x8000- wrote:
> > > > > > > > > 0x8000- wrote:
> > > > > > > > > > 0x8000- wrote:
> > > > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > > > 0x8000- wrote:
> > > > > > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > > > > > 0x8000- wrote:
> > > > > > > > > > > > > > > aaron.ballman wrote:
> > > > > > > > > > > > > > > > This is where I would construct an `APFloat` 
> > > > > > > > > > > > > > > > object from the string given. As for the 
> > > > > > > > > > > > > > > > semantics to be used, I would recommend getting 
> > > > > > > > > > > > > > > > it from `TargetInfo::getDoubleFormat()` on the 
> > > > > > > > > > > > > > > > belief that we aren't going to care about 
> > > > > > > > > > > > > > > > precision (explained in the documentation).
> > > > > > > > > > > > > > > Here is the problem I tried to explain last night 
> > > > > > > > > > > > > > > but perhaps I wasn't clear enough.
> > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > When we parse the input list from strings, we 
> > > > > > > > > > > > > > > have to commit to one floating point value 
> > > > > > > > > > > > > > > "semantic" - in our case single or double 
> > > > > > > > > > > > > > > precision.
> > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > When we encounter the value in the source code 
> > > > > > > > > > > > > > > and it is captured by a matcher, it comes as 
> > > > > > > > > > > > > > > either one of those values.
> > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > Floats with different semantics can't be directly 
> > > > > > > > > > > > > > > compared - so we have to maintain two distinct 
> > > > > > > > > > > > > > > arrays.
> > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > If we do that, rather than store APFloats and 
> > > > > > > > > > > > > > > sort/compare them with awkward lambdas, we might 
> > > > > > > > > > > > > > > as well just use the native float/double and be 
> > > > > > > > > > > > > > > done with it more cleanly.
> > > > > > > > > > > > > > >When we encounter the value in the source code and 
> > > > > > > > > > > > > > >it is captured by a matcher, it comes as either 
> > > > > > > > > > > > > > >one of those values.
> > > > > > > > > > > > > > 
> > > > > > > > > > > > > > It may also come in as long double or __float128, 
> > > > > > > > > > > > > > for instance, because there are type suffixes for 
> > > > > > > > > > > > > > that.
> > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > Floats with different semantics can't be directly 
> > > > > > > > > > > > > > > compared - so we have to maintain two distinct 
> > > > > > > > > > > > > > > arrays.
> > > > > > > > > > > > > > 
> > > > > > > > > > > > > > Yes, floats with different semantics cannot be 
> > > > > > > > > > > > > > directly compared. That's why I said below that we 
> > > > > > > > > > > > > > should coerce the literal values.
> > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > If we do that, rather than store APFloats and 
> > > > > > > > > > > > > > > sort/compare them with awkward lambdas, we might 
> > > > > > > > > > > > > > > as well just use the native float/double and be 
> > > > > > > > > > > > > > > done with it more cleanly.
> > > > > > > > > > > > > > 
> > > > > > > > > > > > > > There are too many different floating-point 
> > > > > > > > > > > > > > semantics for this to be viable, hence why coercion 
> > > > > > > > > > > > > > is a reasonable behavior.
> > > > > > > > > > > > > Let me see if I understood it - your proposal is: 
> > > > > > > > > > > > > store only doubles, and when a floating-point literal 
> > > > > > > > > > > > > is encountered in code, do not use the 
> > > > > > > > > > > > > FloatingLiteral instance, but parse it again into a 
> > > > > > > > > > > > > double and compare exactly. If the comparison matches 
> > > > > > > > > > > > > - ignore it.
> > > > > > > > > > > > > 
> > > > > > > > > > > > > In that case what is the value of storing APFloats 
> > > > > > > > > > > > > with double semantics in the IgnoredValues array, 
> > > > > > > > > > > > > instead of doubles?
> > > > > > > > > > > > > Let me see if I understood it - your propos

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-29 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 157899.
0x8000- added a comment.

Update the list of magic values ignored by default.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,195 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, \
+// RUN:   {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;1.0;101.0"}, \
+// RUN:   {key: readability-magic-numbers.IgnorePowersOf2IntegerValues, value: 1}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[15];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 15 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 22; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 22 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 66 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for ignored values (specified in the configuration above)
+ */
+int GrandfatheredIntegerValues[] = {0, 1, 2, 10, 100, -1, -10, -100, 65536};
+
+float GrandfatheredFloatValues[] = {3.14f, 3.14, 2.71828, 2.71828f, -1.01E2, 1E4};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const fl

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-31 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

The state of this patch:

- user interface is as agreed-upon, giving end-users the capability to filter 
out floats and ints as it makes sense for their code base
- code is clean
- documentation is up to date
- default ignore lists are sensible

There is one outstanding improvement request - to combine the two float/double 
lists into a single APFloat but that requires more refactoring outside this 
check and the benefit lies mainly in a future extensibility and the saving of a 
couple hundred bytes.

@aaron.ballman - can we get this merged as-is and improve it later? I'd like to 
see this check available in Clang7.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-31 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 158425.
0x8000- added a comment.

Add tests to show that we can detect, report and ignore floating point numbers 
represented using hexadecimal notation


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,199 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, \
+// RUN:   {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;1.0;101.0;0x1.2p3"}, \
+// RUN:   {key: readability-magic-numbers.IgnorePowersOf2IntegerValues, value: 1}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[15];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 15 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 22; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 22 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 66 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float SomeFloats[] = {0.5, 0x1.2p4};
+// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 0.5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: 0x1.2p4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValue

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-31 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 158450.
0x8000- added a comment.

Add reference to 5.1.1 Use symbolic names instead of literal values in code 

 in the documentation.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,199 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, \
+// RUN:   {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;1.0;101.0;0x1.2p3"}, \
+// RUN:   {key: readability-magic-numbers.IgnorePowersOf2IntegerValues, value: 1}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[15];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 15 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 22; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 22 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 66 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float SomeFloats[] = {0.5, 0x1.2p4};
+// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 0.5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: 0x1.2p4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBuck

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-08-05 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 159222.
0x8000- added a comment.

Address several style comments. Rebase to current trunk (8.0).


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,199 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, \
+// RUN:   {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;1.0;101.0;0x1.2p3"}, \
+// RUN:   {key: readability-magic-numbers.IgnorePowersOf2IntegerValues, value: 1}]}' \
+// RUN: --
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[15];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 15 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 22; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 22 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 66 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float SomeFloats[] = {0.5, 0x1.2p4};
+// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 0.5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: 0x1.2p4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+int InitializedByMacro = INT_MACRO;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warning

[PATCH] D49114: Add a clang-tidy check for "magic numbers"

2018-07-09 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- created this revision.
0x8000- added reviewers: Wizard, aaron.ballman, alexfh, hokein.
Herald added subscribers: cfe-commits, mgorny.

Add a clang-tidy check for "magic numbers", integers and floating point values 
embedded in the code instead of using symbols or constants.

Bad example:

  double circleArea = 3.1415926535 * radius * radius;

Good example:

  double circleArea = M_PI * radius * radius;

This version detects and report integers only. If there is interest of merging 
the tool I can add the functionality for floats as well.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,80 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 5 [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 7 [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: magic number integer literal 8 [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number integer literal 8 [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: magic number integer literal 3 [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 4 [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(2), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 2 [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: magic number integer literal 9 [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: magic number integer literal 5 [readability-magic-numbers]
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntContant = 42;
+
+constexpr int AlsoGoodGlobalIntContant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntContant = 43;
+
+  (void)IntSquarer(GoodLocalIntContant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
Index: docs/clang-tidy/checks/readability-magic-numbers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-magic-numbers.rst
@@ -0,0 +1,15 @@
+.. title:: clang-tidy - readability-magic-numbers
+
+readability-magic-numbers
+==
+
+Detects magic numbers, integer or floating point literal that are embedded in
+code and not introduced via constants or symbols.
+
+Bad example:
+
+   double circleArea = 3.1415926535 * radius * radius;
+
+Good example:
+
+   double circleArea = M_PI * radius * radius;
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -217,6 +217,7 @@
readability-identifier-naming
readability-implicit-bool-conversion
readability-inconsistent-declaration-parameter-name
+   readability-magic-numbers
readability-misleading-indentation
readability-misplaced-array-index
readability-named-parameter
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,6 +57,12 @@
 Improvements to clang-tidy
 --
 
+- New `readability-magic-numbers
+  `_ check
+
+  Detect usage of magic numbers, numbers that are used as literals instead of
+  introduced via constants or symbols.
+

[PATCH] D49114: Add a clang-tidy check for "magic numbers"

2018-07-09 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

Perhaps M_PI wasn't the best example, as its value won't change soon, but other 
numbers should be defined in relation to constants.

Also I have seen coding guidelines suggesting "100" is grandfathered due to 
100% calculations. 2 and 10 due to logarithms, etc. Not sure where to draw the 
line there.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: Add a clang-tidy check for "magic numbers"

2018-07-09 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In https://reviews.llvm.org/D49114#1156835, @hfinkel wrote:

> I suspect that the check will be very noisy for powers of 2 and 10 that are 
> used as multiplicands. You might wish to exclude those.


Good point.

> Also, what happens for enums? Especially when initialized using expressions 
> such as 1 << 5? Some tests might be useful in this regard.

Even better point!

I will implement those suggestions and update the review packet.

Thank you,
florin

In https://reviews.llvm.org/D49114#1156835, @hfinkel wrote:

> > This version detects and report integers only. If there is interest of 
> > merging the tool I can add the functionality for floats as well.
>
> FWIW: I think that the FP check would be interesting.
>
> > Also I have seen coding guidelines suggesting "100" is grandfathered due to 
> > 100% calculations. 2 and 10 due to logarithms, etc. Not sure where to draw 
> > the line there.
>
> I suspect that the check will be very noisy for powers of 2 and 10 that are 
> used as multiplicands. You might wish to exclude those.
>
> Also, what happens for enums? Especially when initialized using expressions 
> such as 1 << 5? Some tests might be useful in this regard.





Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: Add a clang-tidy check for "magic numbers"

2018-07-09 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In https://reviews.llvm.org/D49114#1156878, @Quuxplusone wrote:

> The cult of "no magic numbers" is horrible and we should be trying to 
> //deprogram// its adherents, not create a whole new generation of them. I 
> would be happy if this clang-tidy patch were quickly abandoned. //But//, it's 
> just a clang-tidy check — it's easy for people who don't want it to ignore 
> its existence — so I'll just plan to be in that group of people.


There are several problems with magic numbers but the worst one is when you 
have a few of them sprinkled through the code (4, 5, 24) and you make one 
change - let's say 4 needs to become a 5. Now: how many other values do you 
need to change? Do you change the 5 because it is 4 + 1? Do you change 24 
because is it 4 * 6? Do the 4s in these sub-expressions relate to the same 
concept/constant/value or not?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: Add a clang-tidy check for "magic numbers"

2018-07-09 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: test/clang-tidy/readability-magic-numbers.cpp:38
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(2), 
anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 2 
[readability-magic-numbers]

Quuxplusone wrote:
> How come you diagnose `2 * x` but not `x + x` or `x << 1`? Do you have a 
> rationale for why the former is worse than the latter?
No tool is perfect. The intent is not to unpack any craftily obfuscated code, 
but to gently guide the design and thinking and reviews..



Comment at: test/clang-tidy/readability-magic-numbers.cpp:56
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 
[readability-magic-numbers]

Quuxplusone wrote:
> Given that you're trying to diagnose certain kinds of static data arrays and 
> not others, I'd be interested to see what you think ought to happen if you 
> put `{3, 5}` in a context where it creates an `initializer_list` of static 
> lifetime.
The principle I've followed until now is - if you're ultimately 
defining/constructing a constant object - then it should be acceptable. If 
you're just seeding the object and those values will change over the program's 
lifetime, it is better to seed them with named constants.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-10 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In https://reviews.llvm.org/D49114#1156987, @JonasToth wrote:

> I think some of the logic you have in your check code could be done via 
> matchers. That is usually better for performance, because you analyze less 
> code.


I have considered that approach, but had to abandon it because the logic in 
this check is substractive, not additive: I would not use matchers to find more 
loci to diagnose, but to exculpate loci found by more general rules.

In other words: I want to report as diagnostic _all_ integer literals, except 
some, based on the larger construct they are part of.

In my understanding matchers are effective if you need to search for "X 
followed by Y", "X followed by Z", "X followed by Y". The union of the results 
is the set I want.

In my case, I am searching for "all of X", "X unless preceded by A", "X unless 
preceded by B".  The "unless preceded by" rules don't compose well, as their 
union tends to be the whole set "all of X".


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-10 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:42
+
+const auto &Parents = Result.Context->getParents(*MatchedDecl);
+for (const auto &Parent : Parents) {

Eugene.Zelenko wrote:
> Please don't use auto where type is not easily deducible. Same in other 
> places.
This is the same code pattern as bugprone/MultipleStatementMacroCheck.cpp and 
readability/RedundantDeclarationCheck.cpp. What type shall I use for Parent 
instead? DynTypedNodeList::DynTypedNode ?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-10 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 7 inline comments as done.
0x8000- added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:11
+
+   double circleArea = 3.1415926535 * radius * radius;
+

Eugene.Zelenko wrote:
> JonasToth wrote:
> > This example is good, but right now the code only checks for integer 
> > literals. Maybe an integer example would be better?
> Please use .. code-block:: c++. Same for good example.
@JonasToth I raked my brain but I just can't come up with a short and effective 
example. I haven't given up yet.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-10 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 154921.
0x8000- added a comment.
Herald added a subscriber: mgrang.

Incorporate review comments. Add support for floating point detection.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,107 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 5 [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 7 [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: magic number integer literal 8 [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number integer literal 8 [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: magic number integer literal 3 [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 4 [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: magic number integer literal 9 [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: magic number integer literal 5 [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number float literal 3.141592741 [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: magic number float literal 6.283185307 [readability-magic-numbers]
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntContant = 42;
+
+constexpr int AlsoGoodGlobalIntContant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntContant = 43;
+
+  (void)IntSquarer(GoodLocalIntContant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for grandfathered values
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
Index: docs/clang-tidy/checks/readability-magic-numbers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-magic-numbers.rst
@@ -0,0 +1,19 @@
+.. title:: clang-tidy - readability-magic-numbers
+
+readability-magic-numbers
+==
+
+Detects magic numbers, integer or floating point literal that are embedded in
+code and not introduced via constants or symbols.
+
+Bad example:
+
+.. code-block:: c++
+
+   double circleArea = 3.1415926535 * radius * radius;
+
+Good example:
+
+.. code-block:: c++
+
+   double circleArea = M_PI * radius * radius;
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -217,6 +217,7 @@
readability-identifier-naming
readability-implicit-bool-conversion
readability-inconsistent-declaration-parameter-name
+   readability-magic-numbers
readability-misleading-indentation
readability-misplaced-array-index
readability-named-parameter
Index: docs/ReleaseNotes.rst
===

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In https://reviews.llvm.org/D49114#1159211, @Eugene.Zelenko wrote:

> C++ Core Guidelines contains ES.45: Avoid "magic constants"; use symbolic 
> constants 
> ,
>  so I think check should be moved into cppcoreguidelines module.


The same guidelines existed in medical and aerospace environments for many 
years before C++ Core Guidelines. In fact the same checker should run just fine 
on C code (I'm not sure about Objective-C).

I am not opposed to moving it, if this is where the consensus leads - I know 
where to find it :)


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:11
+
+   double circleArea = 3.1415926535 * radius * radius;
+

Quuxplusone wrote:
> 0x8000- wrote:
> > Eugene.Zelenko wrote:
> > > JonasToth wrote:
> > > > This example is good, but right now the code only checks for integer 
> > > > literals. Maybe an integer example would be better?
> > > Please use .. code-block:: c++. Same for good example.
> > @JonasToth I raked my brain but I just can't come up with a short and 
> > effective example. I haven't given up yet.
> A common "magic number" in real code would be something like
> ```
> int get_answer() {
> if (cond) return x;
> return -1;  // FILENOTFOUND
> }
> ```
Thank you. May I use this as an example?

The other cases I could find involved either "constants" coming from customer 
requirements that became variable with age, or examples where the same 
underlying value was buried inside other pre-calculated values that needed to 
be kept in sync when the base value changed but that does not make for a 
pithy example.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 155078.
0x8000- edited the summary of this revision.
0x8000- added a comment.

Updated examples code and added several references


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,115 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 5 [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 7 [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: magic number integer literal 8 [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number integer literal 8 [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: magic number integer literal 3 [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 4 [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: magic number integer literal 9 [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: magic number integer literal 5 [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number float literal 3.141592741 [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: magic number float literal 6.283185307 [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: magic number integer literal 3 [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntContant = 42;
+
+constexpr int AlsoGoodGlobalIntContant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntContant = 43;
+
+  (void)IntSquarer(GoodLocalIntContant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for grandfathered values
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
Index: docs/clang-tidy/checks/readability-magic-numbers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-magic-numbers.rst
@@ -0,0 +1,53 @@
+.. title:: clang-tidy - readability-magic-numbers
+
+readability-magic-numbers
+==
+
+Detects magic numbers, integer or floating point literal that are embedded in
+code and not introduced via constants or symbols.
+
+Many coding guidelines advise replacing the magic values with symbolic
+constants to improve readability. Here are a few references:
+
+   * Item 17 in "C++ Coding Standards: 101 Rules, Guidelines and Best
+ Practices" by Herb Sutter and Andrei Alexandrescu
+   * Chapter 17 in "Clean Code - A handbook of agile software craftsmanship."
+ by Robert C. Martin
+   * Rule 20701 in "TRAIN REAL TIME DATA PROTOCOL Coding Rules" by Armin-Hagen
+ Weiss, Bombardier
+   * 

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 155084.
0x8000- added a comment.

Update documentation to clean up formatting


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,115 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 5 [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 7 [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: magic number integer literal 8 [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number integer literal 8 [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: magic number integer literal 3 [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 4 [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: magic number integer literal 9 [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: magic number integer literal 5 [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number float literal 3.141592741 [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: magic number float literal 6.283185307 [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: magic number integer literal 3 [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntContant = 42;
+
+constexpr int AlsoGoodGlobalIntContant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntContant = 43;
+
+  (void)IntSquarer(GoodLocalIntContant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for grandfathered values
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
Index: docs/clang-tidy/checks/readability-magic-numbers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-magic-numbers.rst
@@ -0,0 +1,51 @@
+.. title:: clang-tidy - readability-magic-numbers
+
+readability-magic-numbers
+==
+
+Detects magic numbers, integer or floating point literal that are embedded in
+code and not introduced via constants or symbols.
+
+Many coding guidelines advise replacing the magic values with symbolic
+constants to improve readability. Here are a few references:
+
+   * Item 17 in "C++ Coding Standards: 101 Rules, Guidelines and Best
+ Practices" by Herb Sutter and Andrei Alexandrescu
+   * Chapter 17 in "Clean Code - A handbook of agile software craftsmanship."
+ by Robert C. Martin
+   * Rule 20701 in "TRAIN REAL TIME DATA PROTOCOL Coding Rules" by Armin-Hagen
+ Weiss, Bombardier
+   * http://wiki.c2.com/?MagicNumber
+
+
+Examples of magic v

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 3 inline comments as done.
0x8000- added a comment.

Thank you for your review @Eugene.Zelenko


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked an inline comment as done.
0x8000- added a comment.

Remove extraneous .html. Sorry for not seeing it earlier.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 155089.
0x8000- added a comment.

Fix typo


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,115 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 5 [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 7 [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: magic number integer literal 8 [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number integer literal 8 [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: magic number integer literal 3 [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 4 [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: magic number integer literal 9 [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: magic number integer literal 5 [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number float literal 3.141592741 [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: magic number float literal 6.283185307 [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: magic number integer literal 3 [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntContant = 42;
+
+constexpr int AlsoGoodGlobalIntContant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntContant = 43;
+
+  (void)IntSquarer(GoodLocalIntContant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for grandfathered values
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
Index: docs/clang-tidy/checks/readability-magic-numbers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-magic-numbers.rst
@@ -0,0 +1,51 @@
+.. title:: clang-tidy - readability-magic-numbers
+
+readability-magic-numbers
+==
+
+Detects magic numbers, integer or floating point literal that are embedded in
+code and not introduced via constants or symbols.
+
+Many coding guidelines advise replacing the magic values with symbolic
+constants to improve readability. Here are a few references:
+
+   * Item 17 in "C++ Coding Standards: 101 Rules, Guidelines and Best
+ Practices" by Herb Sutter and Andrei Alexandrescu
+   * Chapter 17 in "Clean Code - A handbook of agile software craftsmanship."
+ by Robert C. Martin
+   * Rule 20701 in "TRAIN REAL TIME DATA PROTOCOL Coding Rules" by Armin-Hagen
+ Weiss, Bombardier
+   * http://wiki.c2.com/?MagicNumber
+
+
+Examples of magic values:
+
+.. code-block:: c++
+
+  

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-13 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 155549.
0x8000- added a comment.

Accept magic values arbitrarily deep in a constant initialization


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,151 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 5 [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 7 [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: magic number integer literal 8 [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number integer literal 8 [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: magic number integer literal 3 [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 4 [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: magic number integer literal 9 [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: magic number integer literal 5 [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number float literal 3.141592741 [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: magic number float literal 6.283185307 [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: magic number integer literal 3 [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntContant = 42;
+
+constexpr int AlsoGoodGlobalIntContant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntContant = 43;
+
+  (void)IntSquarer(GoodLocalIntContant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for grandfathered values
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yval) noexcept : x{xval}, y{yval} {
+  }
+};
+
+template 
+struct Dimension {
+  T x;
+  T y;
+
+  explicit Dimension(T xval, T yval) noexcept : x{xval}, y{yval} {
+  }
+};
+
+template 
+struct Rectangle {
+  Point origin;
+  Dimension size;
+  T rotation; // angle of rotation around origin
+
+  Rectangle(Point origin_, Dimension size_, T rotation_ = 0) noexcept : origin{origin_}, size{size_}, rotation{rotation_} {
+  }
+
+  bool contains(Point point) const;
+};
+
+} // namespace geometry
+
+const geometry::Rectangle mandelbrotCanvas{geometry::Point{-2.5, -1}, geometry::Dimension{3.5, 2}};
Index: docs/clang-tidy/checks/readability-magic-numbers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-magic-numbers.rst
@@ -0,0 +1,51 @@
+.. title:: clang-tidy - readability-magic-numbers
+
+re

[PATCH] D45444: [clang-tidy] implement new check for const-correctness

2018-07-14 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In https://reviews.llvm.org/D45444#1162715, @JonasToth wrote:

> Time :/


Is this available as a public Git branch somewhere, or is downloading the diff 
the preferred way to interact with this code?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D45444



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D45444: [clang-tidy] implement new check for const-correctness

2018-07-17 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In https://reviews.llvm.org/D45444#1164671, @JonasToth wrote:

> @0x8000- are you interested in working on the check? If i recall 
> correctly the branch for 7.0 will happen on 1. august. That would be the 
> timewindow in which it makes sense to give you the patch. I will have time in 
> september following, but not right now.
>  I try to commit 2 bug fixes in this time, because they are almost done 
> already.


@JonasToth 
I am interested in helping - is there any area you'd like me to look at? I have 
reviewed the test cases and they seem fine. The code is a bit more 
intimidating, given that I just started looking at clang/llvm internals. But I 
have pulled the patch into my personal build at home and at ${BIG_CO} and I am 
using the check "in production". I have detected no crashes and no false 
positives yet.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D45444



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-17 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

@aaron.ballman , @JonasToth , @Eugene.Zelenko Is there anything missing from 
this patch? What do I need to do to get it merged? This is my first 
contribution to LLVM so I'm not quite sure. Thank you!


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-17 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 4 inline comments as done.
0x8000- added a comment.

@Eugene.Zelenko  thank you for suggesting the alias - I didn't know it is that 
easy, but a grep on "alias" led me to the right place.




Comment at: clang-tidy/readability/MagicNumbersCheck.h:55
+  const std::vector IngnoredValuesInput;
+  std::vector IngnoredValues;
+};

JonasToth wrote:
> Maybe these vectors could be `llvm::SmallVector` that has a better 
> performance.
The IgnoredValuesInput is parsed via utils::options::parseStringList which 
returns a std::vector. I am using that instance directly - copying 
it into a llvm::SmallVector would be more expensive.



Comment at: test/clang-tidy/readability-magic-numbers.cpp:124
+
+  explicit Point(T xval, T yval) noexcept : x{xval}, y{yval} {
+  }

Eugene.Zelenko wrote:
> Please run Clang-format over test case.
I do run it constantly. In this case there is no change. I am using the 
clang-6.0 formatter though.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-17 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 155994.
0x8000- added a comment.

Address several review comments. Create alias for 
cppcoreguidelines-avoid-magic-numbers .


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,151 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 5 [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 7 [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: magic number integer literal 8 [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number integer literal 8 [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: magic number integer literal 3 [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: magic number integer literal 4 [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 6 [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: magic number integer literal 9 [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: magic number integer literal 5 [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: magic number float literal 3.141592741 [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: magic number float literal 6.283185307 [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: magic number integer literal 3 [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntContant = 42;
+
+constexpr int AlsoGoodGlobalIntContant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntContant = 43;
+
+  (void)IntSquarer(GoodLocalIntContant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for grandfathered values
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yval) noexcept : x{xval}, y{yval} {
+  }
+};
+
+template 
+struct Dimension {
+  T x;
+  T y;
+
+  explicit Dimension(T xval, T yval) noexcept : x{xval}, y{yval} {
+  }
+};
+
+template 
+struct Rectangle {
+  Point origin;
+  Dimension size;
+  T rotation; // angle of rotation around origin
+
+  Rectangle(Point origin_, Dimension size_, T rotation_ = 0) noexcept : origin{origin_}, size{size_}, rotation{rotation_} {
+  }
+
+  bool contains(Point point) const;
+};
+
+} // namespace geometry
+
+const geometry::Rectangle mandelbrotCanvas{geometry::Point{-2.5, -1}, geometry::Dimension{3.5, 2}};
Index: docs/clang-tidy/checks/readability-magic-numbers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-magic-numbers.rst
@@ -0,0 +1,51 @@
+.. title:: clang-tidy - readab

[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-17 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked an inline comment as done.
0x8000- added inline comments.



Comment at: test/clang-tidy/readability-magic-numbers.cpp:124
+
+  explicit Point(T xval, T yval) noexcept : x{xval}, y{yval} {
+  }

Eugene.Zelenko wrote:
> 0x8000- wrote:
> > Eugene.Zelenko wrote:
> > > Please run Clang-format over test case.
> > I do run it constantly. In this case there is no change. I am using the 
> > clang-6.0 formatter though.
> It's weird, since closing curly bracket should be in same line as opening. 
> Also Rectangle constructor is longer then 80 symbols.
If you download the code and run clang-format-6.0 do you get something 
different?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-17 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked an inline comment as done.
0x8000- added a comment.

florin@helios:~/tools/llvm$ find . -name .clang-format | sort
./.clang-format
./projects/compiler-rt/lib/asan/.clang-format
./projects/compiler-rt/lib/dfsan/.clang-format
./projects/compiler-rt/lib/hwasan/.clang-format
./projects/compiler-rt/lib/interception/.clang-format
./projects/compiler-rt/lib/lsan/.clang-format
./projects/compiler-rt/lib/msan/.clang-format
./projects/compiler-rt/lib/safestack/.clang-format
./projects/compiler-rt/lib/sanitizer_common/.clang-format
./projects/compiler-rt/lib/tsan/.clang-format
./projects/libcxxabi/.clang-format
./projects/libcxx/.clang-format
./projects/libunwind/.clang-format
./projects/lld/.clang-format
./projects/openmp/runtime/.clang-format
./test/.clang-format
./tools/clang/.clang-format
./tools/clang/test/.clang-format
./tools/clang/tools/extra/test/.clang-format
florin@helios:~/tools/llvm$ cat ./tools/clang/tools/extra/test/.clang-format
BasedOnStyle: LLVM
**ColumnLimit: 0**

Doesn't seem that weird ;)


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-18 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:23
+
+const char DefaultIgnoredValues[] = "0;1;2;10;100;";
+

aaron.ballman wrote:
> lebedev.ri wrote:
> > aaron.ballman wrote:
> > > Why 2, 10, and 100?
> > These really should be a config option.
> These are the default values for the config option. I think 0 and 1 make a 
> lot of sense to have in the default list given how prevalent they are in real 
> world code. But the other three seem to be used far less often.
> 
> I think if we wanted to have any values other than 0 and 1 (and perhaps -1) 
> as defaults, I'd want to see some data on large amounts of code to justify 
> anything else.
No need for -1, as it is covered by 1, plus unary operator.

I know 100 is used often for converting percentages.

10 is used for number parsing (yes, many people still do that by hand instead 
of libraries).

But this is the configuration - and I'm ok with reverting to 0 and 1 as I had 
originally.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-18 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 17 inline comments as done.
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:30-32
+  for (const std::string &IgnoredValue : IngnoredValuesInput) {
+IngnoredValues.push_back(std::stoll(IgnoredValue));
+  }

aaron.ballman wrote:
> This can be replaced with `llvm::transform(IgnoredValuesInput, 
> IgnoredValues.begin(), std::stoll);`
/home/florin/tools/llvm/tools/clang/tools/extra/clang-tidy/readability/MagicNumbersCheck.cpp:30:3:
 error: no matching function for call to 'transform'

  llvm::transform(IgnoredValuesInput, IgnoredIntegerValues.begin(), std::stoll);
  ^~~
/home/florin/tools/llvm/include/llvm/ADT/STLExtras.h:990:10: note: candidate 
template ignored: couldn't infer template argument 'UnaryPredicate' 
 
OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P) {
 ^
1 error generated.

Shall I wrap it in a lambda?



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:41-42
+void MagicNumbersCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(integerLiteral().bind("ii"), this);
+  Finder->addMatcher(floatLiteral().bind("ff"), this);
+}

aaron.ballman wrote:
> The names `ii` and `ff` could be a bit more user-friendly. Also, this can be 
> written using a single matcher, I think.
> `anyOf(integerLiteral().bind("integer"), floatLiteral().bind("float"))`
addMatcher(anyOf(...), this) is ambiguous. Is there any benefit over the two 
statements?



Comment at: clang-tidy/readability/ReadabilityTidyModule.cpp:69-70
 "readability-inconsistent-declaration-parameter-name");
+CheckFactories.registerCheck(
+"readability-magic-numbers");
 CheckFactories.registerCheck(

aaron.ballman wrote:
> I think this should also be registered as a C++ Core Guideline checker, as I 
> believe it covers at least the integer and floating-point parts of 
> https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-magic
I have it as an alias in my branch.  I will check why it was not exported in 
the patch.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-18 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked 8 inline comments as done.
0x8000- added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:33
+
+  return -3; // FILENOTFOUND
+   }

Quuxplusone wrote:
> I notice you changed `-1` to `-3`. Is `return -1` not an example of the "no 
> magic numbers" rule? If not, why not — and can you put something in the 
> documentation about it?
> At least add a unit test proving that `return -1;` is accepted without any 
> diagnostic, if that's the intended behavior.
-1 is parsed as "UnaryOperator" - "IntegerLiteral" 1. I think -1 should be 
acceptable by default.



Comment at: test/clang-tidy/readability-magic-numbers.cpp:16
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 
[readability-magic-numbers]

Quuxplusone wrote:
> Please add test cases immediately following this one, for
> 
> const int BadLocalConstInt = 6;
> constexpr int BadLocalConstexprInt = 6;
> static const int BadLocalStaticConstInt = 6;
> static constexpr int BadLocalStaticConstexprInt = 6;
> 
> (except of course changing "Bad" to "Good" in any cases where 6 is acceptable 
> as an initializer).
> 
> Also
> 
> std::vector BadLocalVector(6);
> const std::vector BadLocalConstVector(6);
> 
> etc. etc.
Again... all the "const .* (\d)+" patterns should be acceptable. We're 
initializing a constant. Would you prefer an explicit option?



Comment at: test/clang-tidy/readability-magic-numbers.cpp:151
+
+const geometry::Rectangle 
mandelbrotCanvas{geometry::Point{-2.5, -1}, 
geometry::Dimension{3.5, 2}};

Quuxplusone wrote:
> Surely you should expect some diagnostics on this line!
I am using those values to initialize a constant, as described earlier.

We can disable this - but this will be a terribly noisy checker.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-18 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

I think this requires a separate discussion - do we accept magic values only 
when they are used directly to initialize a constant of the same type? Or do we 
allow them generically when they are used to initialize any constant? Or do we 
only accept several layers of nesting, but not too much?

a) const int Foo = 42;
b) #define FROB 66
c) cont int Bar[] = { 43, 44 };
d) const geometry::Rectangle 
mandelbrotCanvas{geometry::Point{-2.5, -1}, 
geometry::Dimension{3.5, 2}};

Which of these should produce a warning?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-18 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- updated this revision to Diff 156203.
0x8000- added a comment.
Herald added subscribers: kbarton, nemanjai.

Significant refactoring to address review comments - mainly to reduce 
duplication and implement in functional style.

cppcoreguidelines-avoid-magic-numbers is documented as an alias to 
readability-magic-numbers check.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114

Files:
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tidy/readability/MagicNumbersCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cppcoreguidelines-avoid-magic-numbers.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-magic-numbers.rst
  test/clang-tidy/readability-magic-numbers.cpp

Index: test/clang-tidy/readability-magic-numbers.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-magic-numbers.cpp
@@ -0,0 +1,151 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+
+template 
+struct ValueBucket {
+  T value[V];
+};
+
+int BadGlobalInt = 5;
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int IntSquarer(int param) {
+  return param * param;
+}
+
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  (void)IntSquarer(7);
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int LocalArray[8];
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  for (int ii = 0; ii < 8; ++ii)
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  {
+LocalArray[ii] = 3 * ii;
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+  }
+
+  ValueBucket Bucket;
+  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+class TwoIntContainer {
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int getValue() const;
+
+private:
+  int oneMember = 9;
+  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+  int anotherMember;
+
+  int yetAnotherMember;
+
+  const int oneConstant = 2;
+
+  const int anotherConstant;
+};
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+float FloatPiVariable = 3.1415926535f;
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.141592741 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+double DoublePiVariable = 6.283185307;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+
+int getAnswer() {
+  if (ValueArray[0] < ValueArray[1])
+return ValueArray[1];
+
+  return -3; // FILENOTFOUND
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
+
+/*
+ * Clean code
+ */
+
+#define INT_MACRO 5
+
+const int GoodGlobalIntConstant = 42;
+
+constexpr int AlsoGoodGlobalIntConstant = 42;
+
+void SolidFunction() {
+  const int GoodLocalIntConstant = 43;
+
+  (void)IntSquarer(GoodLocalIntConstant);
+
+  int LocalArray[INT_MACRO];
+
+  ValueBucket Bucket;
+}
+
+const int ConstValueArray[] = {7, 9};
+
+const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
+
+/*
+ * no warnings for grandfathered values
+ */
+int GrandfatheredValues[] = {0, 1, 2, 10, 100, -1, -10, -100};
+
+/*
+ * no warnings for enums
+ */
+enum Smorgasbord {
+  STARTER,
+  ALPHA = 3,
+  BETA = 1 << 5,
+};
+
+const float FloatPiConstant = 3.1415926535f;
+const double DoublePiConstant = 6.283185307;
+
+double DoubleZeroIsAccepted = 0.0;
+float FloatZeroIsAccepted = 0.0f;
+
+namespace geometry {
+
+template 
+struct Point {
+  T x;
+  T y;
+
+  explicit Point(T xval, T yval) noexcept : x{xval}, y{yval} {
+  }
+};
+
+template

[PATCH] D45444: [clang-tidy] implement new check for const-correctness

2018-07-18 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

Any plans for fix-its that will add the suggested 'const' qualifiers?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D45444



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49114: [clang-tidy] Add a check for "magic numbers"

2018-07-18 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked an inline comment as done.
0x8000- added inline comments.



Comment at: test/clang-tidy/readability-magic-numbers.cpp:16
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 
[readability-magic-numbers]

0x8000- wrote:
> Quuxplusone wrote:
> > Please add test cases immediately following this one, for
> > 
> > const int BadLocalConstInt = 6;
> > constexpr int BadLocalConstexprInt = 6;
> > static const int BadLocalStaticConstInt = 6;
> > static constexpr int BadLocalStaticConstexprInt = 6;
> > 
> > (except of course changing "Bad" to "Good" in any cases where 6 is 
> > acceptable as an initializer).
> > 
> > Also
> > 
> > std::vector BadLocalVector(6);
> > const std::vector BadLocalConstVector(6);
> > 
> > etc. etc.
> Again... all the "const .* (\d)+" patterns should be acceptable. We're 
> initializing a constant. Would you prefer an explicit option?
I have  template and constructor arguments already in the test. I have tried 
including  but somehow it is not found and the std::vector is reported 
as an error in itself.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D69764: [clang-format] Add East/West Const fixer capability

2020-05-27 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

@MyDeveloperDay +1 from the trenches - I am in that same position and it took a 
lot of work to get the organization to _align_ on a consistent style, then 
enforce that.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69764/new/

https://reviews.llvm.org/D69764



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2022-02-02 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

@aaron.ballman - can this land for Clang14, or does it have wait for 15?

Are there any other reviewers that can approve this?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D67265: [clang-tidy] Magic number checker shouldn't warn on user defined string literals

2019-09-06 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- accepted this revision.
0x8000- added a comment.
This revision is now accepted and ready to land.

Looks good to me. Thank you for the fix.


Repository:
  rCTE Clang Tools Extra

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D67265/new/

https://reviews.llvm.org/D67265



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2019-12-31 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

F11163406: 0001-Add-extra-tests.patch  
shows a couple of false positives.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-03 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In D54943#1803448 , @JonasToth wrote:

> In D54943#1800182 , @0x8000- 
> wrote:
>
> > F11163406: 0001-Add-extra-tests.patch  
> > shows a couple of false positives.
>
>
> I added your tests, but some did not show false positives anymore. I think i 
> will remove some, that i find redundant.
>  But could you please recheck with the current version first, if this is 
> actually correct and the original false positives are gone?


I can confirm that the false positives in the real code have been cleaned up. 
Thank you!

However I would be loath to discard tests - unless we can prove they are truly 
redundant.

Also, here is a new test for you - it trips now on "unsigned someValue = 0;" 
line:

  struct IntWrapper {
 unsigned low;
 unsigned high;
  
 IntWrapper& operator=(unsigned value) {
low = value & 0x;
high = (value >> 16) & 0x;
 }
  
 template
 friend Istream& operator>>(Istream& is, IntWrapper& rhs) {
unsigned someValue = 0;
if (is >> someValue) {
   rhs = someValue;
}
return is;
 }
  };
  
  
  unsigned TestHiddenFriend(IntMaker& im) {
 IntWrapper iw;
  
 im >> iw;
  
 return iw.low;
  }


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-03 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

Indicated where the new test code should go.




Comment at: 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-values.cpp:608
+}
+
+template 

Please insert the this test code here:

```
struct IntWrapper {

   unsigned low;
   unsigned high;

   IntWrapper& operator=(unsigned value) {
  low = value & 0x;
  high = (value >> 16) & 0x;
   }

   template
   friend Istream& operator>>(Istream& is, IntWrapper& rhs) {
  unsigned someValue = 0;   // false positive now
  if (is >> someValue) {
 rhs = someValue;
  }
  return is;
   }
};

unsigned TestHiddenFriend(IntMaker& im) {
   IntWrapper iw;

   im >> iw;

   return iw.low;
}
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-04 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-values.cpp:608
+}
+
+template 

JonasToth wrote:
> JonasToth wrote:
> > 0x8000- wrote:
> > > Please insert the this test code here:
> > > 
> > > ```
> > > struct IntWrapper {
> > > 
> > >unsigned low;
> > >unsigned high;
> > > 
> > >IntWrapper& operator=(unsigned value) {
> > >   low = value & 0x;
> > >   high = (value >> 16) & 0x;
> > >}
> > > 
> > >template
> > >friend Istream& operator>>(Istream& is, IntWrapper& rhs) {
> > >   unsigned someValue = 0;   // false positive now
> > >   if (is >> someValue) {
> > >  rhs = someValue;
> > >   }
> > >   return is;
> > >}
> > > };
> > > 
> > > unsigned TestHiddenFriend(IntMaker& im) {
> > >IntWrapper iw;
> > > 
> > >im >> iw;
> > > 
> > >return iw.low;
> > > }
> > > ```
> > clang gives me no error when I add the const there. sure it does reproduce 
> > properly?
> Here for reference: https://godbolt.org/z/DXKMYh
Probably I wasn't clear - I suggested adding my test code at line 608, because 
it needs the "IntMaker" definition at line 594.

The false positive from this const check is reported on the "unsigned someValue 
= 0;" line inside the template extraction operator.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-04 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-values.cpp:608
+}
+
+template 

0x8000- wrote:
> JonasToth wrote:
> > JonasToth wrote:
> > > 0x8000- wrote:
> > > > Please insert the this test code here:
> > > > 
> > > > ```
> > > > struct IntWrapper {
> > > > 
> > > >unsigned low;
> > > >unsigned high;
> > > > 
> > > >IntWrapper& operator=(unsigned value) {
> > > >   low = value & 0x;
> > > >   high = (value >> 16) & 0x;
> > > >}
> > > > 
> > > >template
> > > >friend Istream& operator>>(Istream& is, IntWrapper& rhs) {
> > > >   unsigned someValue = 0;   // false positive now
> > > >   if (is >> someValue) {
> > > >  rhs = someValue;
> > > >   }
> > > >   return is;
> > > >}
> > > > };
> > > > 
> > > > unsigned TestHiddenFriend(IntMaker& im) {
> > > >IntWrapper iw;
> > > > 
> > > >im >> iw;
> > > > 
> > > >return iw.low;
> > > > }
> > > > ```
> > > clang gives me no error when I add the const there. sure it does 
> > > reproduce properly?
> > Here for reference: https://godbolt.org/z/DXKMYh
> Probably I wasn't clear - I suggested adding my test code at line 608, 
> because it needs the "IntMaker" definition at line 594.
> 
> The false positive from this const check is reported on the "unsigned 
> someValue = 0;" line inside the template extraction operator.
Oh, I got it - don't think "shift" think "overloaded extraction operator".

In my code above, you don't know what ">>" does, and it clearly takes a mutable 
reference as the right side.

```
const int foo;
std::cin >> foo;
```

should not compile, either :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-04 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

This patch does not build on top of current tree:

  /usr/bin/ld: 
lib/libclangTidyCppCoreGuidelinesModule.a(ConstCorrectnessCheck.cpp.o): in 
function 
`clang::tidy::cppcoreguidelines::ConstCorrectnessCheck::registerMatchers(clang::ast_matchers::MatchFinder*)':
  
/home/florin/tools/llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/ConstCorrectnessCheck.cpp:65:
 undefined reference to `clang::ast_matchers::decompositionDecl'
  clang: error: linker command failed with exit code 1 (use -v to see 
invocation)
  [2771/4997] Building CXX object 
tools/clang/tools/extra/clang-include-fixer/find-all-symbols/CMakeFiles/obj.findAllSymbols.dir/PragmaCommentHandler.cpp.o
  ninja: build stopped: subcommand failed.

This is my top of the tree right now:

  b7ecf1c1c373c53183ef6ef66efbe4237ff7b96d (origin/master, origin/HEAD, master) 
NFC: Fix trivial typos in comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-04 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In D54943#1804291 , @JonasToth wrote:

> In D54943#1804183 , @0x8000- 
> wrote:
>
> > This patch does not build on top of current tree:
> >
> >   /usr/bin/ld: 
> > lib/libclangTidyCppCoreGuidelinesModule.a(ConstCorrectnessCheck.cpp.o): in 
> > function 
> > `clang::tidy::cppcoreguidelines::ConstCorrectnessCheck::registerMatchers(clang::ast_matchers::MatchFinder*)':
> >   
> > /home/florin/tools/llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/ConstCorrectnessCheck.cpp:65:
> >  undefined reference to `clang::ast_matchers::decompositionDecl'
> >   clang: error: linker command failed with exit code 1 (use -v to see 
> > invocation)
> >   [2771/4997] Building CXX object 
> > tools/clang/tools/extra/clang-include-fixer/find-all-symbols/CMakeFiles/obj.findAllSymbols.dir/PragmaCommentHandler.cpp.o
> >   ninja: build stopped: subcommand failed.
> >
> >
> > This is my top of the tree right now:
> >
> >   b7ecf1c1c373c53183ef6ef66efbe4237ff7b96d (origin/master, origin/HEAD, 
> > master) NFC: Fix trivial typos in comments
> >
>
>
> did the changes to `clang/include/clang/ASTMatchers/ASTMatchers.h` and 
> `clang/lib/ASTMatchers/Dynamic/Registry.cpp` apply? They provide the 
> `decompositionDecl` matcher. It seems odd, they are not available :/


This is the result of applying the patch as e-mailed:

  ~/tools/llvm-project$ patch -p0 < /tmp/D54943.236101.patch


  patching file clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp   


  patching file clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp


  patching file clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp


  patching file clang/lib/Analysis/ExprMutationAnalyzer.cpp 


  patching file clang/lib/ASTMatchers/Dynamic/Registry.cpp  


  patching file clang/include/clang/ASTMatchers/ASTMatchers.h   


  patching file 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-values.cpp
   
  patching file 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-transform-values.cpp
 
  patching file 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-transform-pointer-as-values.cpp
  
  patching file 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-pointer-as-values.cpp

  patching file 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-cxx17.cpp

  patching file 
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-const-correctness.rst

  patching file clang-tools-extra/docs/ReleaseNotes.rst 


  patching file 
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp  

  patching file 
clang-tools-extra/clang-tidy/cppcoreguidelines/ConstCorrectnessCheck.h  

  patching file 
clang-tools-extra/clang-tidy/cppcoreguidelines/ConstCorrectnessCheck.cpp

  patching file clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt   


  patching file clang-tools-extra/CMakeLists.txt

No rejection that I can see.

  ~/tools/llvm-project$ ag decompositionDecl
  clang-tools-extra/clang-tidy/cppcoreguidelines/ConstCorrectnessCheck.cpp
  67: unless(has(decompositionDecl()

[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-04 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-values.cpp:608
+}
+
+template 

JonasToth wrote:
> 0x8000- wrote:
> > 0x8000- wrote:
> > > JonasToth wrote:
> > > > JonasToth wrote:
> > > > > 0x8000- wrote:
> > > > > > Please insert the this test code here:
> > > > > > 
> > > > > > ```
> > > > > > struct IntWrapper {
> > > > > > 
> > > > > >unsigned low;
> > > > > >unsigned high;
> > > > > > 
> > > > > >IntWrapper& operator=(unsigned value) {
> > > > > >   low = value & 0x;
> > > > > >   high = (value >> 16) & 0x;
> > > > > >}
> > > > > > 
> > > > > >template
> > > > > >friend Istream& operator>>(Istream& is, IntWrapper& rhs) {
> > > > > >   unsigned someValue = 0;   // false positive now
> > > > > >   if (is >> someValue) {
> > > > > >  rhs = someValue;
> > > > > >   }
> > > > > >   return is;
> > > > > >}
> > > > > > };
> > > > > > 
> > > > > > unsigned TestHiddenFriend(IntMaker& im) {
> > > > > >IntWrapper iw;
> > > > > > 
> > > > > >im >> iw;
> > > > > > 
> > > > > >return iw.low;
> > > > > > }
> > > > > > ```
> > > > > clang gives me no error when I add the const there. sure it does 
> > > > > reproduce properly?
> > > > Here for reference: https://godbolt.org/z/DXKMYh
> > > Probably I wasn't clear - I suggested adding my test code at line 608, 
> > > because it needs the "IntMaker" definition at line 594.
> > > 
> > > The false positive from this const check is reported on the "unsigned 
> > > someValue = 0;" line inside the template extraction operator.
> > Oh, I got it - don't think "shift" think "overloaded extraction operator".
> > 
> > In my code above, you don't know what ">>" does, and it clearly takes a 
> > mutable reference as the right side.
> > 
> > ```
> > const int foo;
> > std::cin >> foo;
> > ```
> > 
> > should not compile, either :)
> no. something else is going on.
> https://godbolt.org/z/xm8eVC
> ```
> | |   |-CXXOperatorCallExpr  ''
> | |   | |-UnresolvedLookupExpr  '' lvalue 
> (ADL) = 'operator>>' 0x55a26b885938 0x55a26b857238
> | |   | |-DeclRefExpr  'Istream' lvalue ParmVar 0x55a26b885748 'is' 
> 'Istream &'
> | |   | `-DeclRefExpr  'const unsigned int' lvalue Var 0x55a26b885a38 
> 'someValue' 'const unsigned int'
> ```
> This code is only a false positive in the sense, that the "we can not know if 
> something bad happens" is not detected. The operator>> is not resolved. That 
> is because the template is not instantiated and the expressions can therefore 
> not be resolved yet.
> There seems to be no instantiation of this template function.
> 
> Somehow the `im >> iw;` does not instantiate the `friend operator<<`. I 
> reduced it to something i think is minimal and that shows the same behaviour. 
> (https://godbolt.org/z/MMG_4q)
https://godbolt.org/z/7QEdHJ

```
struct IntMaker {
  operator bool() const;

  friend IntMaker &operator>>(IntMaker &, unsigned &);
  //friend IntMaker &operator>>(IntMaker &, const unsigned &) = delete;
};
```

If you uncomment the deleted overload then

```
template 
Istream& operator>>(Istream& is, IntWrapper& rhs)  {
unsigned const someValue = 0;
if (is >> someValue) {
rhs = someValue;
}
return is;
}
```

Fails to compile.

Depending on what else is around, it seems that somehow the compiler would try 
to use the (bool) conversion to obtain an integral then use it as an argument 
to the built-in integral left shift.

https://godbolt.org/z/-JFL5o - this does not compile, as expected:

```
#include 

int readInt() {
const int foo = 0;
std::cin >> foo;
return foo;
}
```

so this check should not recommend making foo constant.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-05 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- marked an inline comment as done.
0x8000- added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-values.cpp:608
+}
+
+template 

JonasToth wrote:
> JonasToth wrote:
> > 0x8000- wrote:
> > > JonasToth wrote:
> > > > 0x8000- wrote:
> > > > > 0x8000- wrote:
> > > > > > JonasToth wrote:
> > > > > > > JonasToth wrote:
> > > > > > > > 0x8000- wrote:
> > > > > > > > > Please insert the this test code here:
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > struct IntWrapper {
> > > > > > > > > 
> > > > > > > > >unsigned low;
> > > > > > > > >unsigned high;
> > > > > > > > > 
> > > > > > > > >IntWrapper& operator=(unsigned value) {
> > > > > > > > >   low = value & 0x;
> > > > > > > > >   high = (value >> 16) & 0x;
> > > > > > > > >}
> > > > > > > > > 
> > > > > > > > >template
> > > > > > > > >friend Istream& operator>>(Istream& is, IntWrapper& rhs) {
> > > > > > > > >   unsigned someValue = 0;   // false positive now
> > > > > > > > >   if (is >> someValue) {
> > > > > > > > >  rhs = someValue;
> > > > > > > > >   }
> > > > > > > > >   return is;
> > > > > > > > >}
> > > > > > > > > };
> > > > > > > > > 
> > > > > > > > > unsigned TestHiddenFriend(IntMaker& im) {
> > > > > > > > >IntWrapper iw;
> > > > > > > > > 
> > > > > > > > >im >> iw;
> > > > > > > > > 
> > > > > > > > >return iw.low;
> > > > > > > > > }
> > > > > > > > > ```
> > > > > > > > clang gives me no error when I add the const there. sure it 
> > > > > > > > does reproduce properly?
> > > > > > > Here for reference: https://godbolt.org/z/DXKMYh
> > > > > > Probably I wasn't clear - I suggested adding my test code at line 
> > > > > > 608, because it needs the "IntMaker" definition at line 594.
> > > > > > 
> > > > > > The false positive from this const check is reported on the 
> > > > > > "unsigned someValue = 0;" line inside the template extraction 
> > > > > > operator.
> > > > > Oh, I got it - don't think "shift" think "overloaded extraction 
> > > > > operator".
> > > > > 
> > > > > In my code above, you don't know what ">>" does, and it clearly takes 
> > > > > a mutable reference as the right side.
> > > > > 
> > > > > ```
> > > > > const int foo;
> > > > > std::cin >> foo;
> > > > > ```
> > > > > 
> > > > > should not compile, either :)
> > > > no. something else is going on.
> > > > https://godbolt.org/z/xm8eVC
> > > > ```
> > > > | |   |-CXXOperatorCallExpr  ''
> > > > | |   | |-UnresolvedLookupExpr  '' 
> > > > lvalue (ADL) = 'operator>>' 0x55a26b885938 0x55a26b857238
> > > > | |   | |-DeclRefExpr  'Istream' lvalue ParmVar 0x55a26b885748 
> > > > 'is' 'Istream &'
> > > > | |   | `-DeclRefExpr  'const unsigned int' lvalue Var 
> > > > 0x55a26b885a38 'someValue' 'const unsigned int'
> > > > ```
> > > > This code is only a false positive in the sense, that the "we can not 
> > > > know if something bad happens" is not detected. The operator>> is not 
> > > > resolved. That is because the template is not instantiated and the 
> > > > expressions can therefore not be resolved yet.
> > > > There seems to be no instantiation of this template function.
> > > > 
> > > > Somehow the `im >> iw;` does not instantiate the `friend operator<<`. I 
> > > > reduced it to something i think is minimal and that shows the same 
> > > > behaviour. (https://godbolt.org/z/MMG_4q)
> > > https://godbolt.org/z/7QEdHJ
> > > 
> > > ```
> > > struct IntMaker {
> > >   operator bool() const;
> > > 
> > >   friend IntMaker &operator>>(IntMaker &, unsigned &);
> > >   //friend IntMaker &operator>>(IntMaker &, const unsigned &) = delete;
> > > };
> > > ```
> > > 
> > > If you uncomment the deleted overload then
> > > 
> > > ```
> > > template 
> > > Istream& operator>>(Istream& is, IntWrapper& rhs)  {
> > > unsigned const someValue = 0;
> > > if (is >> someValue) {
> > > rhs = someValue;
> > > }
> > > return is;
> > > }
> > > ```
> > > 
> > > Fails to compile.
> > > 
> > > Depending on what else is around, it seems that somehow the compiler 
> > > would try to use the (bool) conversion to obtain an integral then use it 
> > > as an argument to the built-in integral left shift.
> > > 
> > > https://godbolt.org/z/-JFL5o - this does not compile, as expected:
> > > 
> > > ```
> > > #include 
> > > 
> > > int readInt() {
> > > const int foo = 0;
> > > std::cin >> foo;
> > > return foo;
> > > }
> > > ```
> > > 
> > > so this check should not recommend making foo constant.
> > I see. Implicit conversions are great... :)
> > 
> > I will recheck that. And the `std::cin` example is analyzed correctly. I 
> > added a test for that, too.
> > Thank you for researching the issue!
> https://godbolt.org/z/VerWce
> Minimal example that shows the issue.
As much as I would have liked to contribute a good test to your new checker, 
I'm happ

[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-05 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

I can't build this on top of current llvm-project tree 
(6a6e6f04ec2cd2f4f07ec4943036c5c2d47ce0c7 
):

  /usr/bin/ld: 
lib/libclangTidyCppCoreGuidelinesModule.a(ConstCorrectnessCheck.cpp.o): in 
function 
`clang::tidy::cppcoreguidelines::ConstCorrectnessCheck::registerMatchers(clang::ast_matchers::MatchFinder*)':
  
/home/florin/tools/llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/ConstCorrectnessCheck.cpp:65:
 undefined reference to `clang::ast_matchers::decompositionDecl'

I am building using clang++-9 from Debian, and diff7 applied cleanly.

Jonas, is it possible to rebase this on top of llvm-project and push it to a 
branch on https://github.com/JonasToth/llvm-project ?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-05 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In D54943#1804904 , @JonasToth wrote:

> In D54943#1804890 , @0x8000- 
> wrote:
>
> > I can't build this on top of current llvm-project tree 
> > (6a6e6f04ec2cd2f4f07ec4943036c5c2d47ce0c7 
> > ):
> >
> >   /usr/bin/ld: 
> > lib/libclangTidyCppCoreGuidelinesModule.a(ConstCorrectnessCheck.cpp.o): in 
> > function 
> > `clang::tidy::cppcoreguidelines::ConstCorrectnessCheck::registerMatchers(clang::ast_matchers::MatchFinder*)':
> >   
> > /home/florin/tools/llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/ConstCorrectnessCheck.cpp:65:
> >  undefined reference to `clang::ast_matchers::decompositionDecl'
> >
> >
> > I am building using clang++-9 from Debian, and diff7 applied cleanly.
> >
> > Jonas, is it possible to rebase this on top of llvm-project and push it to 
> > a branch on https://github.com/JonasToth/llvm-project ?
>
>
> I just merged master into the branch and pushed to 
> `feature_transform_const.patch` branch in my fork. Maybe that works?
>  If you want you can join IRC, its faster to get this fixed there :)


Strange - I have checked out your branch, and the patch is correct (no 
difference between when I apply the phabricator diff7 on top of upstream 
llvm-project and when i look at feature_transform_const.patch)

I am using the following cmake configuration:

  cmake ../llvm \
 -DCMAKE_INSTALL_PREFIX=/opt/clang10 \
 -DCMAKE_C_COMPILER=/usr/bin/clang-9 \
 -DCMAKE_CXX_COMPILER=/usr/bin/clang++-9 \
 -DCMAKE_BUILD_TYPE=Debug \
 -DLLVM_TARGETS_TO_BUILD=X86 \
 -DLLVM_ENABLE_THREADS=On \
 -DLLVM_INSTALL_TOOLCHAIN_ONLY=On \
 -DLLVM_USE_SPLIT_DWARF=On \
 -DLLVM_BUILD_TESTS=ON \
 
-DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi;libunwind;lldb;compiler-rt;lld;clang-tools-extra;"
 \
 -GNinja

What cmake configuration are you using?

(Sorry - I need to go, no time for IRC today)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-05 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In D54943#1804943 , @JonasToth wrote:

> - Merge branch 'master' into feature_transform_const.patch
> - link clangAnalysis to the cppcoreguidelines-module
> - fix InitListExpr as well


This last version too builds with RelWithDebInfo and fails with Debug 
configuration (both clean builds).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-06 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In D54943#1805439 , @JonasToth wrote:

> > This last version too builds with RelWithDebInfo and fails with Debug 
> > configuration (both clean builds).
>
> So it only fails in debug-build?


Correct; I can build 'Release' or 'RelWithDebInfo' but not 'Debug'.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-10 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

Applied the fix-it on one of our smaller (but C++ code bases) and it builds and 
passes the tests. Comments:

1. I'm still seeing some false positives, where some locals that are const are 
flagged as could be made const - I'm working to create a minimal reproduction 
recipe.
2. We're a west const shop :) so adding a bunch of east-consts will not fly 
well. Is there a way to configure where 'const' is placed by the fixit?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-10 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In D54943#1815121 , @JonasToth wrote:

> In D54943#1815073 , @0x8000- 
> wrote:
>
> > Applied the fix-it on one of our smaller (but C++ code bases) and it builds 
> > and passes the tests. Comments:
> >
> > 1. I'm still seeing some false positives, where some locals that are const 
> > are flagged as could be made const - I'm working to create a minimal 
> > reproduction recipe.
> > 2. We're a west const shop :) so adding a bunch of east-consts will not fly 
> > well. Is there a way to configure where 'const' is placed by the fixit? 
> > (Specifically 'auto const', we prefer it 'const auto').
>
>
> Does it build now? I couldn't find a way to reproduce that and gave up, tbh.
>
> 1. Template context? Auto involved? I saw some double-analysis for `auto&`, 
> because clang-tidy didn't ignore those properly. And are you using 
> `run-clang-tidy`? It deduplicates fixits, maybe that is involved?
> 2. YesNo, The utility for adding const is able to do both, west const has 
> problems with `typedef int * MyType;` scenarios, where the `const` will apply 
> to the wrong thing. Doing that right requires special care. BUT: 
> `clang-format` has a east-const/west-const feature now (i think new with this 
> release). So I am now somewhat considering to let clang-format do that for me.
>
>   Thanks again for taking a look at it. But if the issues you found are new, 
> i think we should maybe not commit this weekend.


I haven't tried Debug build, Release only.

I'm good with having clang-format do the west-const transformation.

It's not a template, but something like "const Foo foo{x, y, z};" where x and y 
were references and z was a pointer. I'll try to reduce a test case.

I have also noticed a check failure, but not sure if in this tool or not.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-10 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

Here is a minimal example that shows the problem:

  #include 
  
  template
  struct DoGooder
  {
  DoGooder(void* accessor, SomeValue value)
  {
  }
  
  };
  
  struct Bingus
  {
  static constexpr auto someRandomConstant = 99;
  };
  
  template
  struct HardWorker
  {
  HardWorker()
  {
  const DoGooder anInstanceOf{nullptr, Foo::someRandomConstant};
  }
  };
  
  struct TheContainer
  {
  std::unique_ptr> m_theOtherInstance;
  };

Example run:

  $ /opt/clang10/bin/clang-tidy 
-checks="-*,cppcoreguidelines-const-correctness" -header-filter=".*" 
reproconst.cpp -- -std=c++17 -Wall
  54 warnings generated.
  reproconst.cpp:22:9: warning: variable 'anInstanceOf' of type '' can be declared 'const' [cppcoreguidelines-const-correctness]
  const DoGooder anInstanceOf{nullptr, Foo::someRandomConstant};
  ^
 const
  Suppressed 53 warnings (53 in non-user code).
  Use -header-filter=.* to display errors from all non-system headers. Use 
-system-headers to display errors from system headers as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In D54943#1815473 , @JonasToth wrote:

> In D54943#1815358 , @0x8000- 
> wrote:
>
> > Here is a minimal example that shows the problem:
>
>
> I can not reproduce that case :(
>  It gives me a compilation error in `const DoGooder anInstanceOf...`, because 
> `DoGooder` requires an template parameter. I then add `DoGooder` and the 
> variable gets no transformation.
>  Did you reproduce the error with exactly that code?
>
> And which version did you run? Maybe that was a previous false positive, that 
> might be fixed right now?


The way I am testing is that I am fetching the current master branch of 
https://github.com/llvm/llvm-project.git, then downloading the latest patch 
from here and applying on top, then building.

This is the state of the tree right as of last night:

  commit 1c8ab48028cc39429a392691ef2e66968460d782 (HEAD -> D54943.diff12)
  Author: Florin Iucha 
  Date:   Fri Jan 10 18:52:54 2020 -0500
  
  D54943.diff12
  
  commit 1b8c84b8dd5a4a294943a6a6f0631d2d3a1f9f27 (origin/master, origin/HEAD, 
master)
  Author: Richard Smith 
  Date:   Fri Jan 10 15:47:29 2020 -0800
  
  Improve precision of documentation comment.

CTAD takes care of the missing template parameter, that's why I'm passing 
"-std=c++17" to clang-tidy.

  /tmp$ /opt/clang10/bin/clang-tidy 
-checks="-*,cppcoreguidelines-const-correctness" -header-filter=".*" test.cpp 
-- -std=c++17 -Wall
  54 warnings generated.
  /tmp/test.cpp:22:9: warning: variable 'anInstanceOf' of type '' can be declared 'const' [cppcoreguidelines-const-correctness]
  const DoGooder anInstanceOf{nullptr, Foo::someRandomConstant};
  ^
 const 
  Suppressed 53 warnings (53 in non-user code).
  Use -header-filter=.* to display errors from all non-system headers. Use 
-system-headers to display errors from system headers as well.
  /tmp$ /opt/clang10/bin/clang++ -c test.cpp
  test.cpp:22:15: error: use of class template 'DoGooder' requires template 
arguments
  const DoGooder anInstanceOf{nullptr, Foo::someRandomConstant};
^
  test.cpp:4:8: note: template is declared here
  struct DoGooder
 ^
  1 error generated.
  /tmp$ /opt/clang10/bin/clang++ -std=c++17 -c test.cpp
  -rw-r--r-- 1 florin florin 432 Jan 11 09:48 test.cpp
  -rw-r--r-- 1 florin florin 744 Jan 11 09:52 test.o


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

In D54943#1815568 , @JonasToth wrote:

> @0x8000- i did remove `dependentTypes` from matching. Locally that works 
> with your reported test-case (i was running clang-tidy wrong :().
>
> I do understand the `auto &` issues now (i think).
>  If the type is deduced to a template-type or something that depends the 
> standard ways of detecting that do not work, because in each template 
> instantiation this information is not available.
>
> Having a `auto & local = TemplatedVariable` does not have the 
> `isInstantiationDependentType()`-bits set and for each instantiation of the 
> template the const-analysis is run.
>  The false positives happened in different template instantiations, i think 
> because the overloaded operators were sometimes `const` and sometimes not. I 
> tried many ways to detect, if the type comes from a template, but had no 
> success.
>  This is probably an issue in `Sema` or requires adjustments there.
>  I added tests in clang-tidy, but `#if 0` them out because are not working 
> right now.


Thank you for looking into this; I'll start building diff14 to test locally.

I'm good with merging this checker as-is, as long as it is not enabled by 
default. I want to be able to use it even partially, in environments where I 
might not be able to inject a home-built compiler ;)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

I have built diff14 and tried to apply it on an internal code base with ~7500 
translation units.

  $ python3 /opt/clang10/share/clang/run-clang-tidy.py -clang-tidy-binary  
/opt/clang10/bin/clang-tidy -clang-apply-replacements 
/opt/clang10/bin/clang-apply-replacements -p ../build.clang10.dbg/ 
"-checks=-*,cppcoreguidelines-const-correctness"  -fix -j 24 -quiet 
-deduplicate $(find lib -name \*.cpp) > const_fix.log 2&> const_err.log

The diffstat is

  1608 files changed, 19927 insertions(+), 19927 deletions(-)

This generated 56 "const const" declarations, of which 25 were triple const!

I have tried -deduplicate argument as in Jonas' script, but ...

  run-clang-tidy.py: error: unrecognized arguments: -deduplicate

Now onto editing those files by hand to remove the duplications and re-running 
the build.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

Summary of "misses"

One:

  uint32_t counter = 0;
  BOOST_FOREACH(const Foo* foo, *theFoos)
  {
  if (foo->getType() == someType)
  {
  ++counter;
  }
  }

The -fix makes the counter const :), so obviously it breaks compilation.

Two:

It marks a lot of std::stringstream / std::ostringstream instances as const. So 
we can't << into it, breaking compilation.

Three:

It marked an array in the header as const. There were some pointer variables 
(in a translation unit which included that header) that were declared and 
initialized to 0 (meaning nullptr), then later in a case block of a switch they 
were assigned &array[0] value. It did not change the pointer variables, but 
they used to be mutable pointer to mutable, so now the assignment from a const 
array failed to build.

The changes required to get the build complete (7500 translation units);

  15 files changed, 59 insertions(+), 59 deletions(-)

I can live with the breakage in the 3rd case - since the fix is one time and 
straightforward (and makes the code clearly better). But I prefer not to have 
to annotate all std::sotringstream instances with NOLINT. Also we have a fair 
number of BOOST_FOREACH macros from way back then. They should be ripped out of 
course, but it's in code that wasn't touched in a while, so having the 
clang-tidy keep reporting / misfixing that area is bothersome.

I really want this clang-tidy tool, but I don't want to put off users via false 
positives. Many don't want to deal with it anyway, and the smallest excuse will 
be brought up against enabling the use of the tool.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-11 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

One more mis-constification that I can't explain, nor reduce to a small test 
case (when I extract the code, the check behaves as expected / desired)

  namespace util {
struct StringIgnoreInitialHash : public std::unary_function
{
   size_t operator()(const std::string& rhs) const
   {
  std::size_t hash = 0;
  std::string::const_iterator str = rhs.begin();
  std::string::const_iterator end = rhs.end();
  if (str != end)
  {
 boost::hash_combine(hash, std::toupper(*str, std::locale()));
 ++str;
  }
  for (; str != end; ++str)
  {
 boost::hash_combine(hash, *str);
  }
  return hash;
   }
};
  }

This is in a header included 29 times in other headers and 106 times directly 
in translation units.

The const-checker reported 31 times that the variable 'hash' can be made 
constant.

Also it reported 5 times that the variable 'str' can be made constant (which it 
can't) and 194 times that variable 'end' can be made constant (which it can, 
and should).

Running in fix mode, made 'hash', 'str' and 'end' all constants.

Extracting this into its own translation unit and adding the requisite 
\#includes and paths to the boost files does not reproduce the error. Only 
'end' is, correctly, reported as needing to be const.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2020-01-12 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added a comment.

As an aside, once this is merged in, I dream of a "fix-it" for old style C code:

  int foo;
  
  ...
  /* page of code which does not use either foo or bar */
  ...
  foo = 5;

Moves the foo declaration to the line where it is actually first initialized. 
Then run the constify tool on top.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   >