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

2018-08-12 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman closed this revision.
aaron.ballman added a comment.

Thank you for the patch and great discussion! I've commit in r339516.


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-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-08-12 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

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?


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-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 

[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 

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

2018-08-05 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

I think this is close. If @alexfh doesn't chime in on the open question in the 
next few days, I would say the check is ready to go in and we can address the 
open question in follow-up commits.




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

0x8000- wrote:
> 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 

[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];
+
+  

[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 

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

2018-07-31 Thread Jonas Toth via Phabricator via cfe-commits
JonasToth added a comment.

In https://reviews.llvm.org/D49114#1182071, @0x8000- wrote:

> 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.


In my opinion this check is in good shape. 
One thing that came to my mind: hexadecimal floating point literals. They 
should work out of the box, but could you add a test that they are recognized 
correctly?


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- 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-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 

[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  : 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 proposal is: 
> 

[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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

In https://reviews.llvm.org/D49114#1179652, @0x8000- wrote:

> 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
>   


Awesome, thank you for this! 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?




Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto  : 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:
> > > > 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 
> > 

[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  : 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 configuration 

[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  : 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 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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



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

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.
> > > > > > 
> > > > > > > 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 
> 

[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,
+};
+

[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  : 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 storage 

[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  : 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.
> > 
> > > Read in 

[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  : 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: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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



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

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).


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  : 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 with 
> > 

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

2018-07-29 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:76-86
+  IgnoredFloatingPointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
+  for (const auto  : 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:
> > > > 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).



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  : 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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



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

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.



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

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.



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

0x8000- wrote:
> 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.
The point to a named constants check is to give names to literal values that 
should have 

[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  : 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 (() == ::APFloat::IEEEsingle()) {
+const float Value = FloatValue.convertToFloat();
+return std::binary_search(IgnoredFloatingPointValues.begin(),
+  IgnoredFloatingPointValues.end(), Value);
+  } else if (() == ::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- 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 

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

2018-07-29 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

Thank you for working on this, I think it's getting closer! I'd use a slightly 
different approach to handling floating-point values, but if that turns out to 
be a clean implementation we may want to think about whether there are 
improvements from modelling integers similarly (only using `APInt` instead of 
`APFloat`).




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

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.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:63
+  Options.get("IgnoreAllFloatingPointValues", false)) {
+  // process set of ignored integer values
+  const std::vector IgnoredIntegerValuesInput =

Comments should be grammatically correct, including capitalization and 
punctuation (elsewhere in this function as well).



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

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).



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

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.



Comment at: clang-tidy/readability/MagicNumbersCheck.h:85-88
+  llvm::SmallVector
+  IgnoredFloatingPointValues;
+  llvm::SmallVector
+  IgnoredDoublePointValues;

The model I was thinking of would have this be: `llvm::SmallVector IgnoredFloatingPointValues;` so that you 
don't need to distinguish between floats and doubles (which also nicely 
supports long doubles, __float128, and _Float16).



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:63-67
+be configured using the :option:`IgnoredFloatingPointValues` option.  For each
+value in that set, bot the single-precision form and double-precision
+form are accepted (for example, if 3.14 is in the set, neither 3.14f nor 3.14
+will produce a warning). Scientific notation is supported for both source code
+input and option.

This isn't far off the mark, but I'd go with something like:
```
For each value in that set, the given string value is converted to a 
floating-point value representation used by the target architecture . If a 
floating-point literal value compares equal to one of the converted values, 
then that literal is not diagnosed by this check. Because floating-point 
equality is used to determine whether to diagnose or not, the user needs to be 
aware of the details of floating-point representations for any values that 
cannot be precisely represented for their target architecture.
```
The  bit is where we can stick details we haven't worked out yet, like 
what rounding mode is used to perform the conversion and whether lossy 
conversions are allowed or ignored.


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 

[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  : 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 (() == ::APFloat::IEEEdouble()) {
  +Value = FloatValue.convertToDouble();
  +  } else if (() == ::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- 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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman 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:
> > > > 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?
> This opens up an entire can of worms that 

[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- 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 Roman Lebedev via Phabricator via cfe-commits
lebedev.ri 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:
> > 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.


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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman 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:
> 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.


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- 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- 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 Roman Lebedev via Phabricator via cfe-commits
lebedev.ri 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:
> > 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.


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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman 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:
> 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;`


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 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- 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 

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

2018-07-28 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



Comment at: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp:45-46
 "cppcoreguidelines-interfaces-global-init");
+CheckFactories.registerCheck(
+"cppcoreguidelines-avoid-magic-numbers");
 CheckFactories.registerCheck(

Please keep this alphabetized in the list.



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

Is the trailing semicolon after the `1` necessary?



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:88
+   // where the template is defined, not where it is instantiated.
+   (Parent.get() != nullptr);
+  });

No need to use `!= nullptr`, or having the parens for that expression.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:109
+  SourceManager->getDecomposedLoc(Literal->getLocation());
+  if (FileOffset.first.isInvalid()) {
+return false;

Can elide the braces.



Comment at: clang-tidy/readability/MagicNumbersCheck.h:49
+  void checkBoundMatch(const ast_matchers::MatchFinder::MatchResult ,
+   const char *boundName) {
+const L *MatchedLiteral = Result.Nodes.getNodeAs(boundName);

boundName -> BoundName per naming conventions.



Comment at: clang-tidy/readability/MagicNumbersCheck.h:51
+const L *MatchedLiteral = Result.Nodes.getNodeAs(boundName);
+if (MatchedLiteral == nullptr)
+  return;

`!MatchedLiteral`



Comment at: docs/clang-tidy/checks/list.rst:82
cppcoreguidelines-avoid-goto
+   cppcoreguidelines-avoid-magic-numbers
cppcoreguidelines-c-copy-assignment-signature (redirects to 
misc-unconventional-assign-operator) 


This should have a `(redirects to blah)` blurb after it.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:12
+
+   * Item 17 in "C++ Coding Standards: 101 Rules, Guidelines and Best
+ Practices" by Herb Sutter and Andrei Alexandrescu

Given that the C++ Core Guideline check redirects here, it would be good to 
link to the correct place in those guidelines from here.



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.

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?


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 

[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 Jonas Toth via Phabricator via cfe-commits
JonasToth added a comment.

I did not find any major issue :)




Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:20
+bool isUsedToInitializeAConstant(
+const clang::ast_matchers::MatchFinder::MatchResult ,
+const clang::ast_type_traits::DynTypedNode ) {

You move the `using namespace clang::ast_matchers;` up to shorten your 
signature.
Adding a using for `ast_type_traits` is possible, too .



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:79
+bool MagicNumbersCheck::isConstant(
+const clang::ast_matchers::MatchFinder::MatchResult ,
+const clang::Expr ) const {

is the `clang::` necessary? The code should be in that namespace already. I 
think shortening the really long type qualifier helps with readability. Similar 
on other places.



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,

-1 is not in the default list anymore.


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 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 : 

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

2018-07-24 Thread Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added inline comments.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:56
+By default only `0`, `1` and `-1` integer values are accepted without a 
warning.
+This can be overridden with the 'IgnoredIntegerValues' option.  In addition,
+the `0.0` floating point value is accepted. There is no configuration for

Sorry, almost forgot. Please prefix IgnoredIntegerValues with :option:. You 
still need to use ` for 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 Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.h:42
+return false;
+  };
+  bool isSyntheticValue(const clang::SourceManager *SourceManager,

Unnecessary semicolon. Please also add empty line after.



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 overriden with the 'IgnoredIntegerValues' option.  In addition,

Please enclose numbers in `. Same for 0.0.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:56
+By default only 0, 1 and -1 integer values are accepted without a warning.
+This can be overriden with the 'IgnoredIntegerValues' option.  In addition,
+the 0.0 floating point value is accepted. There is no configuration for

Please use `.


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- 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 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-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;
+
+  

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

2018-07-20 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 

[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 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- 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 {
+

[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}, 

[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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



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

0x8000- wrote:
> 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.
Good point on -1. I'd say let's revert until we have statistics to justify any 
other default values. That said, I'd still be curious to know how chatty this 
is over large code bases. How many times does this trigger in LLVM, for 
instance?



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

0x8000- wrote:
> 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 &, OutputIt d_first, UnaryPredicate P) {
>  ^
> 1 error generated.
> 
> Shall I wrap it in a lambda?
Yeah, I'd wrap in a lambda then.



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);
+}

0x8000- wrote:
> 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?
Better memoization, which may not really be critical given how trivial the 
matchers are.



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:
> 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.


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 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] 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} {
+  }
+};
+

[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- 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- marked 17 inline comments as done.
0x8000- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:30-32
+  for (const std::string  : 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 &, 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 Arthur O'Dwyer via Phabricator via cfe-commits
Quuxplusone added inline comments.



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

`Ignored`. Please spellcheck throughout.



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

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.



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]

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.



Comment at: test/clang-tidy/readability-magic-numbers.cpp:84
+void SolidFunction() {
+  const int GoodLocalIntContant = 43;
+

`Constant`. Please spellcheck throughout.



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

Surely you should expect some diagnostics on this line!


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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



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

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.


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 Roman Lebedev via Phabricator via cfe-commits
lebedev.ri added inline comments.



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

aaron.ballman wrote:
> Why 2, 10, and 100?
These really should be a config option.


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 Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



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

Why 2, 10, and 100?



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

This can be replaced with `llvm::transform(IgnoredValuesInput, 
IgnoredValues.begin(), std::stoll);`



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:33
+  }
+  std::sort(IngnoredValues.begin(), IngnoredValues.end());
+}

Please use `llvm::sort()` instead to help find non-determinism bugs. (I'm a bit 
surprised we don't have a range-based sort though.)



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:37
+void MagicNumbersCheck::storeOptions(ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "IgnoredValues", DefaultIgnoredValues);
+}

`IgnoredIntegerValues` as the public facing option as well, unless you want to 
allow users to specify ignored floating-point values too (which could be useful 
as well).



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);
+}

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"))`



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:54
+  Result.Nodes.getNodeAs("ii");
+  if (MatchedInteger) {
+

Rather than indent everything for the typical case, I'd prefer this to be an 
early return.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:72
+
+diag(MatchedInteger->getLocation(), "magic number integer literal %0")
+<< Str.data();

This diagnostic text doesn't really tell the user what's wrong with their code 
or how to fix it. Also, this function is largely duplicated in 
`checkFloatMatch()`. I think they should be combined where possible and the 
diagnostic should read: `'%0' is a magic number; consider replacing with a 
named constant` or something along those lines.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:118
+const ast_type_traits::DynTypedNode ) const {
+  const VarDecl *AsVarDecl = Node.get();
+  if (AsVarDecl) {

Can use `const auto *` here as the type is spelled out in the initialization.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:119-123
+  if (AsVarDecl) {
+if (AsVarDecl->getType().isConstQualified()) {
+  return true;
+}
+  }

The `if` statements can be combined into a single statement and the braces can 
be elided.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:124-126
+  const FieldDecl *AsFieldDecl = Node.get();
+  if (AsFieldDecl) {
+if (AsFieldDecl->getType().isConstQualified()) {

Same here for `auto` and single statement.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:135-140
+  const SubstNonTypeTemplateParmExpr *AsTemplateArgument =
+  Node.get();
+  if (AsTemplateArgument) {
+return true;
+  }
+  return false;

`return Node.get != nullptr;` However, that makes 
me wonder why this should be a function at all.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:147
+
+  if (isConstantDefinition(Node)) {
+return true;

Elide braces



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:151-158
+  for (const ast_type_traits::DynTypedNode  :
+   Result.Context->getParents(Node)) {
+if (isEventuallyConstant(Result, Parent)) {
+  return true;
+}
+  }
+

I think this could be replaced by a call to `llvm::any_of()`



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:164
+const ast_type_traits::DynTypedNode ) const {
+  const EnumConstantDecl *AsEnumConstantDecl = Node.get();
+  if (AsEnumConstantDecl)

No need for this local variable.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:168-174
+  for (const ast_type_traits::DynTypedNode  :
+   Result.Context->getParents(Node)) {
+if (isEnumerationDefinition(Result, Parent))
+  return true;
+  }
+
+  return false;

`llvm::any_of()`



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:183-186
+/*
+ * Ignore this instance, because this is the report where the template is
+ * defined, not where it is instantiated.
+ */

[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-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- 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 - 

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

2018-07-17 Thread Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added inline comments.



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

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.


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 Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added a comment.

Adding C++ Core Guidelines alias is definitely low-hanging fruit which could be 
implemented within this patch.




Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:14
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include 

Please remove empty line.



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

auto could be used here because it's range loop over container.



Comment at: clang-tidy/readability/MagicNumbersCheck.h:14
+#include "../ClangTidy.h"
+
+#include 

Please remove empty line and also include .



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

Please run Clang-format over test case.


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 Jonas Toth via Phabricator via cfe-commits
JonasToth added a comment.

In https://reviews.llvm.org/D49114#1164788, @0x8000- wrote:

> @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!


Usually the review process takes a while ;) If you have currently time and are 
motivated you can have multiple patches in parallel and add aaron, hokein, 
alexfh as reviewers.

For the general procedure:

- propose the patch
- Review happens, discuss everything and fix problems
- review gets accepted with a LGTM (looks good to me) + a green symbol in the 
review (alexfh or aaron.ballman are usually the reviewers that accept. You 
should usually wait for their go)
- patch gets commited. You most likely don't have the commit access yet. But if 
you plan to contribute more often you can request it.

For more information you can take a look at: 
https://llvm.org/docs/Contributing.html and 
https://llvm.org/docs/Phabricator.html and 
https://llvm.org/docs/DeveloperPolicy.html#obtaining-commit-access




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

0x8000- wrote:
> 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 ?
Use the type the function returns (what auto would deduce to).



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

Maybe these vectors could be `llvm::SmallVector` that has a better performance.



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

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.
There is an example in the cpp coreguidelines: 
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es45-avoid-magic-constants-use-symbolic-constants

```
for (int m = 1; m <= 12; ++m)   // don't: magic constant 12
cout << month[m] << '\n';
```

Maybe this could work?


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- 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-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
+

[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-11 Thread Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added inline comments.



Comment at: docs/ReleaseNotes.rst:158
+- New :doc:`readability-magic-numbers
+  ` check.
+

.html is not needed.


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 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- 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 

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

2018-07-11 Thread Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added inline comments.



Comment at: docs/ReleaseNotes.rst:158
+- New :doc:`readability-magic-numbers
+  
` 
check.
+

Please make link short. See other links as examples.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:29
+
+... code-block:: c
+

This is not needed and also wrongly formatted.



Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:46
+
+... code-block:: c
+

This is not needed and also wrongly formatted.


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 Arthur O'Dwyer via Phabricator via cfe-commits
Quuxplusone added inline comments.



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

0x8000- wrote:
> 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.
> Thank you. May I use this as an example?

Of course! Just make sure to add it as a unit test also ;)


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 Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added a comment.

In https://reviews.llvm.org/D49114#1159327, @0x8000- wrote:

> 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 :)


You could create aliases for other guidelines.


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 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 Arthur O'Dwyer via Phabricator via cfe-commits
Quuxplusone added inline comments.



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

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
}
```


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 Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added a comment.

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


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-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- added inline comments.



Comment at: clang-tidy/readability/MagicNumbersCheck.cpp:42
+
+const auto  = Result.Context->getParents(*MatchedDecl);
+for (const auto  : 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 Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added a comment.

It's highly likely that this part of coding guidelines.




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

Please don't use auto where type is not easily deducible. Same in other places.



Comment at: docs/ReleaseNotes.rst:60
 
+- New `readability-magic-numbers
+  
`_
 check

Please move into new checks list in alphabetical order.



Comment at: docs/ReleaseNotes.rst:63
+
+  Detect usage of magic numbers, numbers that are used as literals instead of
+  introduced via constants or symbols.

Please use //Detects//.



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

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.


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


  1   2   >