[PATCH] D33825: [clang-tidy] signal handler must be plain old function check

2017-06-02 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti created this revision.
NorenaLeonetti added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, mgorny.

Based on CERT-MSC54-CPP


Repository:
  rL LLVM

https://reviews.llvm.org/D33825

Files:
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/CMakeLists.txt
  clang-tidy/cert/SignalHandlerMustBePlainOldFunctionCheck.cpp
  clang-tidy/cert/SignalHandlerMustBePlainOldFunctionCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cert-msc54-cpp.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp

Index: test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp
===
--- /dev/null
+++ test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp
@@ -0,0 +1,134 @@
+// RUN: %check_clang_tidy %s cert-msc54-cpp %t -- -- -std=c++11
+
+namespace std {
+extern "C" using signalhandler = void(int);
+int signal(int sig, signalhandler *func);
+}
+
+#define SIG_ERR -1
+#define SIGTERM 15
+
+static void sig_handler(int sig) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use 'external C' prefix for signal handlers [cert-msc54-cpp]
+// CHECK-MESSAGES: :[[@LINE+3]]:39: note: given as a signal parameter here
+
+void install_signal_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, sig_handler))
+return;
+}
+
+extern "C" void cpp_signal_handler(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not use C++ representations in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE+1]]:3: note: C++ representation used here
+  throw "error message";
+}
+
+void install_cpp_signal_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, cpp_signal_handler))
+return;
+}
+
+void indirect_recursion();
+
+void cpp_like(){
+  try {
+cpp_signal_handler(SIG_ERR);
+  } catch(...) {
+// handle error
+  }
+}
+
+void no_body();
+
+void recursive_function() {
+  indirect_recursion();
+  cpp_like();
+  no_body();
+  recursive_function();
+}
+
+void indirect_recursion() {
+  recursive_function();
+}
+
+extern "C" void signal_handler_with_cpp_function_call(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not call functions with C++ representations in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE-11]]:3: note: function called here
+  // CHECK-MESSAGES: :[[@LINE-23]]:3: note: C++ representation used here
+  recursive_function();
+}
+
+void install_signal_handler_with_cpp_function_call() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_cpp_function_call))
+return;
+}
+
+class TestClass {
+public:
+  static void static_function() {}
+};
+
+extern "C" void signal_handler_with_cpp_static_method_call(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not call functions with C++ representations in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE+1]]:3: note: function called here
+  TestClass::static_function();
+}
+
+void install_signal_handler_with_cpp_static_method_call() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_cpp_static_method_call))
+return;
+}
+
+
+// Something that does not trigger the check:
+
+extern "C" void c_sig_handler(int sig) {
+#define cbrt(X) _Generic((X), long double \
+ : cbrtl, default \
+ : cbrt)(X)
+  auto char c = '1';
+  extern _Thread_local double _Complex d;
+  static const unsigned long int i = sizeof(float);
+  register short s;
+  void f();
+  volatile struct c_struct {
+enum e {};
+union u;
+  };
+  typedef bool boolean;
+label:
+  switch (c) {
+  case ('1'):
+break;
+  default:
+d = 1.2;
+  }
+  goto label;
+  for (; i < 42;) {
+if (d == 1.2)
+  continue;
+else
+  return;
+  }
+  do {
+_Atomic int v = _Alignof(char);
+_Static_assert(42 == 42, "True");
+  } while (c == 42);
+}
+
+void install_c_sig_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, c_sig_handler)) {
+// Handle error
+  }
+}
+
+extern "C" void signal_handler_with_function_pointer(int sig) {
+  void (*funp) (void);
+  funp = &cpp_like;
+  funp();
+}
+
+void install_signal_handler_with_function_pointer() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_function_pointer))
+return;
+}
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -22,6 +22,7 @@
cert-flp30-c
cert-msc30-c (redirects to cert-msc50-cpp) 
cert-msc50-cpp
+   cert-msc54-cpp
cert-oop11-cpp (redirects to misc-move-constructor-init) 
cppcoreguidelines-interfaces-global-init
cppcoreguidelines-no-malloc
Index: docs/clang-tidy/checks/cert-msc54-cpp.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/cert-msc54-cpp.rst
@@ -0,0 +1,36 @@
+.. title:: clang-tidy - cert-msc54-cpp
+
+cert-msc54-cpp
+

[PATCH] D33826: [clang-tidy] avoid pointer cast to more strict alignment check

2017-06-02 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti created this revision.
NorenaLeonetti added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, mgorny.

Based on CERT-EXP36-C


Repository:
  rL LLVM

https://reviews.llvm.org/D33826

Files:
  clang-tidy/cert/AvoidPointerCastToMoreStrictAlignmentCheck.cpp
  clang-tidy/cert/AvoidPointerCastToMoreStrictAlignmentCheck.h
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/CMakeLists.txt
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cert-exp36-c.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c

Index: test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
===
--- /dev/null
+++ test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
@@ -0,0 +1,45 @@
+// RUN: %check_clang_tidy %s cert-exp36-c %t -- -- -std=c11
+
+void function(void) {
+  char c = 'x';
+  int *ip = (int *)&c;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
+
+struct foo_header {
+  int len;
+};
+ 
+void function2(char *data, unsigned offset) {
+  struct foo_header *tmp;
+  struct foo_header header;
+ 
+  tmp = (struct foo_header *)(data + offset);
+  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
+
+
+// Something that does not trigger the check:
+
+struct w;
+
+void function3(struct w *v) {
+  int *ip = (int *)v;
+  struct w *u = (struct w *)ip;
+}
+
+struct x {
+   _Alignas(int) char c;
+};
+
+void function4(void) {
+  struct x c = {'x'};
+  int *ip = (int *)&c;
+}
+
+// FIXME: we do not want a warning for this
+void function5(void) {
+  _Alignas(int) char c = 'x';
+  int *ip = (int *)&c;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -5,6 +5,7 @@
 
 .. toctree::
boost-use-to-string
+   cert-exp36-c
cert-dcl03-c (redirects to misc-static-assert) 
cert-dcl21-cpp
cert-dcl50-cpp
Index: docs/clang-tidy/checks/cert-exp36-c.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/cert-exp36-c.rst
@@ -0,0 +1,43 @@
+.. title:: clang-tidy - cert-exp36-c
+
+cert-exp36-c
+
+
+This check will give a warning if a pointer value is converted to
+a pointer type that is more strictly aligned than the referenced type.
+ 
+ Here's an example:
+ 
+ .. code-block:: c
+ 
+char c = 'x';
+int *ip = (int *)&c;
+// warning: do not cast pointers into more strictly aligned pointer types
+ 
+ This check does not completely include warnings for types with explicitly
+ specified alignment, this remains a possible future extension.
+
+ See the example:
+
+  .. code-block:: c
+
+// Works fine:
+struct x {
+  _Alignas(int) char c;
+};
+
+void function3(void) {
+  struct x c = {'x'};
+  int *ip = (int *)&c;
+}
+
+// Won't work:
+void function4(void) {
+  _Alignas(int) char c = 'x';
+  int *ip = (int *)&c;
+  // the check will give a warning for this
+}
+
+ This check corresponds to the CERT C Coding Standard rule
+ `EXP36-C. Do not cast pointers into more strictly aligned pointer types
+`_.
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,6 +57,11 @@
 Improvements to clang-tidy
 --
 
+- New `cert-exp36-c
+  `_ check
+
+  Checks if a pointer value is casted to a more stricter alignment.
+
 - New `cert-dcl21-cpp
   `_ check
 
Index: clang-tidy/cert/CMakeLists.txt
===
--- clang-tidy/cert/CMakeLists.txt
+++ clang-tidy/cert/CMakeLists.txt
@@ -1,6 +1,7 @@
 set(LLVM_LINK_COMPONENTS support)
 
 add_clang_library(clangTidyCERTModule
+  AvoidPointerCastToMoreStrictAlignmentCheck.cpp
   CERTTidyModule.cpp
   CommandProcessorCheck.cpp
   DontModifyStdNamespaceCheck.cpp
Index: clang-tidy/cert/CERTTidyModule.cpp
===
--- clang-tidy/cert/CERTTidyModule.cpp
+++ clang-tidy/cert/CERTTidyModule.cpp
@@ -16,6 +16,7 @@
 #include "../misc/NonCopyableObjects.h"
 #include "../misc/StaticAssertCheck.h"
 #include "../misc/ThrowByValueCatchByReferenceCheck.h"
+#include "AvoidPointerCastToMoreStrictAlignmentCheck.h"
 #include "CommandProcessorCheck.h"
 #includ

[PATCH] D33826: [clang-tidy] avoid pointer cast to more strict alignment check

2017-12-19 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti added a comment.

In https://reviews.llvm.org/D33826#935394, @lebedev.ri wrote:

> Ping?


Hi, I'm sorry I got a bit busy, I'll get back to it next year.


Repository:
  rL LLVM

https://reviews.llvm.org/D33826



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


[PATCH] D33825: [clang-tidy] signal handler must be plain old function check

2017-09-02 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti marked 16 inline comments as done.
NorenaLeonetti added a comment.

In https://reviews.llvm.org/D33825#772688, @aaron.ballman wrote:

> This check is an interesting one. The rules around what is signal safe are 
> changing for C++17 to be a bit more lenient than what the rules are for 
> C++14. CERT's rule is written against C++14, and so the current behavior 
> matches the rule wording. However, the *intent* of the rule is to ensure that 
> only signal-safe functionality is used from a signal handler, and so from 
> that perspective, I can imagine a user compiling for C++17 to want the 
> relaxed rules to still comply with CERT's wording. What do you think?


I think your concerns are right. I've checked the upcoming changes in the C++17 
standard draft and I'll add some logic to the code to have those relaxed rules 
at least partly fulfilled.
For now, I uploaded the changes required for this check.


Repository:
  rL LLVM

https://reviews.llvm.org/D33825



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


[PATCH] D33825: [clang-tidy] signal handler must be plain old function check

2017-09-02 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti updated this revision to Diff 113651.
Herald added a subscriber: JDevlieghere.

Repository:
  rL LLVM

https://reviews.llvm.org/D33825

Files:
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/CMakeLists.txt
  clang-tidy/cert/SignalHandlerMustBePlainOldFunctionCheck.cpp
  clang-tidy/cert/SignalHandlerMustBePlainOldFunctionCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cert-msc54-cpp.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp

Index: test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp
===
--- /dev/null
+++ test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp
@@ -0,0 +1,134 @@
+// RUN: %check_clang_tidy %s cert-msc54-cpp %t -- -- -std=c++11
+
+namespace std {
+extern "C" using signalhandler = void(int);
+int signal(int sig, signalhandler *func);
+}
+
+#define SIG_ERR -1
+#define SIGTERM 15
+
+static void sig_handler(int sig) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: signal handlers must be 'extern "C"' [cert-msc54-cpp]
+// CHECK-MESSAGES: :[[@LINE+3]]:39: note: given as a signal parameter here
+
+void install_signal_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, sig_handler))
+return;
+}
+
+extern "C" void cpp_signal_handler(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not use C++ constructs in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE+1]]:3: note: C++ construct used here
+  throw "error message";
+}
+
+void install_cpp_signal_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, cpp_signal_handler))
+return;
+}
+
+void indirect_recursion();
+
+void cpp_like(){
+  try {
+cpp_signal_handler(SIG_ERR);
+  } catch(...) {
+// handle error
+  }
+}
+
+void no_body();
+
+void recursive_function() {
+  indirect_recursion();
+  cpp_like();
+  no_body();
+  recursive_function();
+}
+
+void indirect_recursion() {
+  recursive_function();
+}
+
+extern "C" void signal_handler_with_cpp_function_call(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not call functions with C++ constructs in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE-11]]:3: note: function called here
+  // CHECK-MESSAGES: :[[@LINE-23]]:3: note: C++ construct used here
+  recursive_function();
+}
+
+void install_signal_handler_with_cpp_function_call() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_cpp_function_call))
+return;
+}
+
+class TestClass {
+public:
+  static void static_function() {}
+};
+
+extern "C" void signal_handler_with_cpp_static_method_call(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not call functions with C++ constructs in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE+1]]:3: note: function called here
+  TestClass::static_function();
+}
+
+void install_signal_handler_with_cpp_static_method_call() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_cpp_static_method_call))
+return;
+}
+
+
+// Something that does not trigger the check:
+
+extern "C" void c_sig_handler(int sig) {
+#define cbrt(X) _Generic((X), long double \
+ : cbrtl, default \
+ : cbrt)(X)
+  auto char c = '1';
+  extern _Thread_local double _Complex d;
+  static const unsigned long int i = sizeof(float);
+  register short s;
+  void f();
+  volatile struct c_struct {
+enum e {};
+union u;
+  };
+  typedef bool boolean;
+label:
+  switch (c) {
+  case ('1'):
+break;
+  default:
+d = 1.2;
+  }
+  goto label;
+  for (; i < 42;) {
+if (d == 1.2)
+  continue;
+else
+  return;
+  }
+  do {
+_Atomic int v = _Alignof(char);
+_Static_assert(42 == 42, "True");
+  } while (c == 42);
+}
+
+void install_c_sig_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, c_sig_handler)) {
+// Handle error
+  }
+}
+
+extern "C" void signal_handler_with_function_pointer(int sig) {
+  void (*funp) (void);
+  funp = &cpp_like;
+  funp();
+}
+
+void install_signal_handler_with_function_pointer() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_function_pointer))
+return;
+}
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -37,6 +37,7 @@
cert-flp30-c
cert-msc30-c (redirects to cert-msc50-cpp) 
cert-msc50-cpp
+   cert-msc54-cpp
cert-oop11-cpp (redirects to misc-move-constructor-init) 
cppcoreguidelines-c-copy-assignment-signature
cppcoreguidelines-interfaces-global-init
Index: docs/clang-tidy/checks/cert-msc54-cpp.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/cert-msc54-cpp.rst
@@ -0,0 +1,34 @@
+.. title:: clang-tidy - cert-msc54-cpp
+
+cert-msc54-cpp
+==
+
+This check will give a warning if a signal handler is not defined

[PATCH] D33825: [clang-tidy] signal handler must be plain old function check

2017-09-02 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti updated this revision to Diff 113652.

Repository:
  rL LLVM

https://reviews.llvm.org/D33825

Files:
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/CMakeLists.txt
  clang-tidy/cert/SignalHandlerMustBePlainOldFunctionCheck.cpp
  clang-tidy/cert/SignalHandlerMustBePlainOldFunctionCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cert-msc54-cpp.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp

Index: test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp
===
--- /dev/null
+++ test/clang-tidy/cert-signal-handler-must-be-plain-old-function.cpp
@@ -0,0 +1,134 @@
+// RUN: %check_clang_tidy %s cert-msc54-cpp %t -- -- -std=c++11
+
+namespace std {
+extern "C" using signalhandler = void(int);
+int signal(int sig, signalhandler *func);
+}
+
+#define SIG_ERR -1
+#define SIGTERM 15
+
+static void sig_handler(int sig) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: signal handlers must be 'extern "C"' [cert-msc54-cpp]
+// CHECK-MESSAGES: :[[@LINE+3]]:39: note: given as a signal parameter here
+
+void install_signal_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, sig_handler))
+return;
+}
+
+extern "C" void cpp_signal_handler(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not use C++ constructs in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE+1]]:3: note: C++ construct used here
+  throw "error message";
+}
+
+void install_cpp_signal_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, cpp_signal_handler))
+return;
+}
+
+void indirect_recursion();
+
+void cpp_like(){
+  try {
+cpp_signal_handler(SIG_ERR);
+  } catch(...) {
+// handle error
+  }
+}
+
+void no_body();
+
+void recursive_function() {
+  indirect_recursion();
+  cpp_like();
+  no_body();
+  recursive_function();
+}
+
+void indirect_recursion() {
+  recursive_function();
+}
+
+extern "C" void signal_handler_with_cpp_function_call(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not call functions with C++ constructs in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE-11]]:3: note: function called here
+  // CHECK-MESSAGES: :[[@LINE-23]]:3: note: C++ construct used here
+  recursive_function();
+}
+
+void install_signal_handler_with_cpp_function_call() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_cpp_function_call))
+return;
+}
+
+class TestClass {
+public:
+  static void static_function() {}
+};
+
+extern "C" void signal_handler_with_cpp_static_method_call(int sig) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not call functions with C++ constructs in signal handlers [cert-msc54-cpp]
+  // CHECK-MESSAGES: :[[@LINE+1]]:3: note: function called here
+  TestClass::static_function();
+}
+
+void install_signal_handler_with_cpp_static_method_call() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_cpp_static_method_call))
+return;
+}
+
+
+// Something that does not trigger the check:
+
+extern "C" void c_sig_handler(int sig) {
+#define cbrt(X) _Generic((X), long double \
+ : cbrtl, default \
+ : cbrt)(X)
+  auto char c = '1';
+  extern _Thread_local double _Complex d;
+  static const unsigned long int i = sizeof(float);
+  register short s;
+  void f();
+  volatile struct c_struct {
+enum e {};
+union u;
+  };
+  typedef bool boolean;
+label:
+  switch (c) {
+  case ('1'):
+break;
+  default:
+d = 1.2;
+  }
+  goto label;
+  for (; i < 42;) {
+if (d == 1.2)
+  continue;
+else
+  return;
+  }
+  do {
+_Atomic int v = _Alignof(char);
+_Static_assert(42 == 42, "True");
+  } while (c == 42);
+}
+
+void install_c_sig_handler() {
+  if (SIG_ERR == std::signal(SIGTERM, c_sig_handler)) {
+// Handle error
+  }
+}
+
+extern "C" void signal_handler_with_function_pointer(int sig) {
+  void (*funp) (void);
+  funp = &cpp_like;
+  funp();
+}
+
+void install_signal_handler_with_function_pointer() {
+  if (SIG_ERR == std::signal(SIGTERM, signal_handler_with_function_pointer))
+return;
+}
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -37,6 +37,7 @@
cert-flp30-c
cert-msc30-c (redirects to cert-msc50-cpp) 
cert-msc50-cpp
+   cert-msc54-cpp
cert-oop11-cpp (redirects to misc-move-constructor-init) 
cppcoreguidelines-c-copy-assignment-signature
cppcoreguidelines-interfaces-global-init
Index: docs/clang-tidy/checks/cert-msc54-cpp.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/cert-msc54-cpp.rst
@@ -0,0 +1,34 @@
+.. title:: clang-tidy - cert-msc54-cpp
+
+cert-msc54-cpp
+==
+
+This check will give a warning if a signal handler is not defined
+as an `extern "C"` function or if the de

[PATCH] D33826: [clang-tidy] avoid pointer cast to more strict alignment check

2017-09-02 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti updated this revision to Diff 113654.
NorenaLeonetti added a comment.
Herald added a subscriber: JDevlieghere.

Added a test for C++ style cast and modified the docs.


Repository:
  rL LLVM

https://reviews.llvm.org/D33826

Files:
  clang-tidy/cert/AvoidPointerCastToMoreStrictAlignmentCheck.cpp
  clang-tidy/cert/AvoidPointerCastToMoreStrictAlignmentCheck.h
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/CMakeLists.txt
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cert-exp36-c.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
  test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp

Index: test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp
===
--- /dev/null
+++ test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp
@@ -0,0 +1,7 @@
+// RUN: %check_clang_tidy %s cert-exp36-c %t
+
+void function(void) {
+  char c = 'x';
+  int *ip = reinterpret_cast(&c);
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
Index: test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
===
--- /dev/null
+++ test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
@@ -0,0 +1,45 @@
+// RUN: %check_clang_tidy %s cert-exp36-c %t -- -- -std=c11
+
+void function(void) {
+  char c = 'x';
+  int *ip = (int *)&c;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
+
+struct foo_header {
+  int len;
+};
+ 
+void function2(char *data, unsigned offset) {
+  struct foo_header *tmp;
+  struct foo_header header;
+ 
+  tmp = (struct foo_header *)(data + offset);
+  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
+
+
+// Something that does not trigger the check:
+
+struct w;
+
+void function3(struct w *v) {
+  int *ip = (int *)v;
+  struct w *u = (struct w *)ip;
+}
+
+struct x {
+   _Alignas(int) char c;
+};
+
+void function4(void) {
+  struct x c = {'x'};
+  int *ip = (int *)&c;
+}
+
+// FIXME: we do not want a warning for this
+void function5(void) {
+  _Alignas(int) char c = 'x';
+  int *ip = (int *)&c;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -20,6 +20,7 @@
bugprone-integer-division
bugprone-suspicious-memset-usage
bugprone-undefined-memory-manipulation
+   cert-exp36-c
cert-dcl03-c (redirects to misc-static-assert) 
cert-dcl21-cpp
cert-dcl50-cpp
Index: docs/clang-tidy/checks/cert-exp36-c.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/cert-exp36-c.rst
@@ -0,0 +1,45 @@
+.. title:: clang-tidy - cert-exp36-c
+
+cert-exp36-c
+
+
+This check will give a warning if a pointer value is converted to
+a pointer type that is more strictly aligned than the referenced type.
+ 
+This check is the same as `-Wcast-align`, except it checks for `reinterpret_cast` as well.
+
+ Here's an example:
+ 
+ .. code-block:: c
+ 
+char c = 'x';
+int *ip = (int *)&c;
+// warning: do not cast pointers into more strictly aligned pointer types
+ 
+ This check does not completely include warnings for types with explicitly
+ specified alignment, this remains a possible future extension.
+
+ See the example:
+
+  .. code-block:: c
+
+// Works fine:
+struct x {
+  _Alignas(int) char c;
+};
+
+void function3(void) {
+  struct x c = {'x'};
+  int *ip = (int *)&c;
+}
+
+// Won't work:
+void function4(void) {
+  _Alignas(int) char c = 'x';
+  int *ip = (int *)&c;
+  // the check will give a warning for this
+}
+
+ This check corresponds to the CERT C Coding Standard rule
+ `EXP36-C. Do not cast pointers into more strictly aligned pointer types
+`_.
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -56,6 +56,10 @@
 
 Improvements to clang-tidy
 --
+- New `cert-exp36-c
+  `_ check
+
+  Checks if a pointer value is casted to a more stricter alignment.
 
 - Renamed checks to use correct term "implicit conversion" instead of "implicit
   cast" and modified messages and option names accordingly:
@@ -143,7 +147,7 @@
   option.
 
 - Added alias `

[PATCH] D33826: [clang-tidy] avoid pointer cast to more strict alignment check

2017-09-11 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti marked an inline comment as done.
NorenaLeonetti added a comment.

In https://reviews.llvm.org/D33826#861226, @aaron.ballman wrote:

> Have you run this check over any large code bases to see what the quality of 
> the diagnostics are?


`-checks=-*,cert-exp36-c,modernize-unary-static-assert`

the result:

real52m6.886s
user298m8.824s
sys 7m29.292s

`-checks=-*,modernize-unary-static-assert`

the result:

real52m5.305s
user302m38.508s
sys 8m53.008s


Repository:
  rL LLVM

https://reviews.llvm.org/D33826



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


[PATCH] D33826: [clang-tidy] avoid pointer cast to more strict alignment check

2017-09-11 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti updated this revision to Diff 114544.

Repository:
  rL LLVM

https://reviews.llvm.org/D33826

Files:
  clang-tidy/cert/AvoidPointerCastToMoreStrictAlignmentCheck.cpp
  clang-tidy/cert/AvoidPointerCastToMoreStrictAlignmentCheck.h
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/CMakeLists.txt
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cert-exp36-c.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
  test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp

Index: test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp
===
--- /dev/null
+++ test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp
@@ -0,0 +1,7 @@
+// RUN: %check_clang_tidy %s cert-exp36-c %t
+
+void function(void) {
+  char c = 'x';
+  int *ip = reinterpret_cast(&c);
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
Index: test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
===
--- /dev/null
+++ test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
@@ -0,0 +1,122 @@
+// RUN: %check_clang_tidy %s cert-exp36-c %t -- -- -std=c11
+
+void function(void) {
+  char c = 'x';
+  int *ip = (int *)&c;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
+
+struct foo_header {
+  int len;
+};
+ 
+void function2(char *data, unsigned offset) {
+  struct foo_header *tmp;
+  struct foo_header header;
+ 
+  tmp = (struct foo_header *)(data + offset);
+  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
+
+// Test from -Wcast-align check:
+
+// Simple casts.
+void test0(char *P) {
+  char *a  = (char*)  P;
+  short *b = (short*) P;
+  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+  int *c   = (int*)   P;
+  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
+
+// Casts from void* are a special case.
+void test1(void *P) {
+  char *a  = (char*)  P;
+  short *b = (short*) P;
+  int *c   = (int*)   P;
+
+  const volatile void *P2 = P;
+  char *d  = (char*)  P2;
+  short *e = (short*) P2;
+  int *f   = (int*)   P2;
+
+  const char *g  = (const char*)  P2;
+  const short *h = (const short*) P2;
+  const int *i   = (const int*)   P2;
+
+  const volatile char *j  = (const volatile char*)  P2;
+  const volatile short *k = (const volatile short*) P2;
+  const volatile int *l   = (const volatile int*)   P2;
+}
+
+// Aligned struct.
+struct __attribute__((aligned(16))) A {
+  char buffer[16];
+};
+void test2(char *P) {
+  struct A *a = (struct A*) P;
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
+
+// Incomplete type.
+void test3(char *P) {
+  struct B *b = (struct B*) P;
+}
+
+// Do not issue a warning. The aligned attribute changes the alignment of the
+// variables and fields.
+char __attribute__((aligned(4))) a[16];
+
+struct S0 {
+  char a[16];
+};
+
+struct S {
+  char __attribute__((aligned(4))) a[16];
+  struct S0 __attribute__((aligned(4))) s0;
+};
+
+// same FIXME as in line 120
+void test4() {
+  struct S s;
+  int *i = (int *)s.a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+  i = (int *)&s.s0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+  i = (int *)a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
+
+// No warnings.
+typedef int (*FnTy)(void);
+unsigned int func5(void);
+
+FnTy test5(void) {
+  return (FnTy)&func5;
+}
+
+
+// Something that does not trigger the check:
+
+struct w;
+
+void function3(struct w *v) {
+  int *ip = (int *)v;
+  struct w *u = (struct w *)ip;
+}
+
+struct x {
+   _Alignas(int) char c;
+};
+
+void function4(void) {
+  struct x c = {'x'};
+  int *ip = (int *)&c;
+}
+
+// FIXME: we do not want a warning for this
+void function5(void) {
+  _Alignas(int) char c = 'x';
+  int *ip = (int *)&c;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -20,6 +20,7 @@
bugprone-integer-division
bugprone-suspicious-memset-usage
bugprone-undefined-memory-manipulation
+   cert-exp36-c
cert-dcl03-c (redirects to misc-sta

[PATCH] D33826: [clang-tidy] avoid pointer cast to more strict alignment check

2017-09-25 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti added a comment.

In https://reviews.llvm.org/D33826#866145, @lebedev.ri wrote:

> In https://reviews.llvm.org/D33826#866133, @NorenaLeonetti wrote:
>
> > In https://reviews.llvm.org/D33826#861226, @aaron.ballman wrote:
> >
> > > Have you run this check over any large code bases to see what the quality 
> > > of the diagnostics are?
> >
> >
> > `-checks=-*,cert-exp36-c,modernize-unary-static-assert`
> >
> > the result:
> >
> > real52m6.886s
> >  user298m8.824s
> >  sys 7m29.292s
> >
> > `-checks=-*,modernize-unary-static-assert`
> >
> > the result:
> >
> > real52m5.305s
> >  user302m38.508s
> >  sys 8m53.008s
>
>
> What was meant is, run the check over some large codebase (e.g. all of the 
> LLVM itself), and upload the results somewhere.
>  Then analyze the results. Are there any obvious false-positives?
>  If yes, then minimize the testcase, add it into 
> `test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp` and 
> fix it's handling.
>  Are there clear bugs in the analyzed code?


I've run the check on the LLVM codebase and got these warnings in the file.
F5385001: test_alignment_check.txt 


Repository:
  rL LLVM

https://reviews.llvm.org/D33826



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


[PATCH] D33826: [clang-tidy] avoid pointer cast to more strict alignment check

2017-09-25 Thread Eniko Donatella Toth via Phabricator via cfe-commits
NorenaLeonetti updated this revision to Diff 116510.

Repository:
  rL LLVM

https://reviews.llvm.org/D33826

Files:
  clang-tidy/cert/AvoidPointerCastToMoreStrictAlignmentCheck.cpp
  clang-tidy/cert/AvoidPointerCastToMoreStrictAlignmentCheck.h
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/CMakeLists.txt
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/cert-exp36-c.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
  test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp

Index: test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp
===
--- /dev/null
+++ test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.cpp
@@ -0,0 +1,7 @@
+// RUN: %check_clang_tidy %s cert-exp36-c %t
+
+void function(void) {
+  char c = 'x';
+  int *ip = reinterpret_cast(&c);
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
Index: test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
===
--- /dev/null
+++ test/clang-tidy/cert-avoid-pointer-cast-to-more-strict-alignment.c
@@ -0,0 +1,122 @@
+// RUN: %check_clang_tidy %s cert-exp36-c %t -- -- -std=c11
+
+void function(void) {
+  char c = 'x';
+  int *ip = (int *)&c;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
+
+struct foo_header {
+  int len;
+};
+ 
+void function2(char *data, unsigned offset) {
+  struct foo_header *tmp;
+  struct foo_header header;
+ 
+  tmp = (struct foo_header *)(data + offset);
+  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c] 
+}
+
+// Test from -Wcast-align check:
+
+// Simple casts.
+void test0(char *P) {
+  char *a  = (char*)  P;
+  short *b = (short*) P;
+  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+  int *c   = (int*)   P;
+  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
+
+// Casts from void* are a special case.
+void test1(void *P) {
+  char *a  = (char*)  P;
+  short *b = (short*) P;
+  int *c   = (int*)   P;
+
+  const volatile void *P2 = P;
+  char *d  = (char*)  P2;
+  short *e = (short*) P2;
+  int *f   = (int*)   P2;
+
+  const char *g  = (const char*)  P2;
+  const short *h = (const short*) P2;
+  const int *i   = (const int*)   P2;
+
+  const volatile char *j  = (const volatile char*)  P2;
+  const volatile short *k = (const volatile short*) P2;
+  const volatile int *l   = (const volatile int*)   P2;
+}
+
+// Aligned struct.
+struct __attribute__((aligned(16))) A {
+  char buffer[16];
+};
+void test2(char *P) {
+  struct A *a = (struct A*) P;
+  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
+
+// Incomplete type.
+void test3(char *P) {
+  struct B *b = (struct B*) P;
+}
+
+// Do not issue a warning. The aligned attribute changes the alignment of the
+// variables and fields.
+char __attribute__((aligned(4))) a[16];
+
+struct S0 {
+  char a[16];
+};
+
+struct S {
+  char __attribute__((aligned(4))) a[16];
+  struct S0 __attribute__((aligned(4))) s0;
+};
+
+// same FIXME as in line 120
+void test4() {
+  struct S s;
+  int *i = (int *)s.a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+  i = (int *)&s.s0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+  i = (int *)a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
+
+// No warnings.
+typedef int (*FnTy)(void);
+unsigned int func5(void);
+
+FnTy test5(void) {
+  return (FnTy)&func5;
+}
+
+
+// Something that does not trigger the check:
+
+struct w;
+
+void function3(struct w *v) {
+  int *ip = (int *)v;
+  struct w *u = (struct w *)ip;
+}
+
+struct x {
+   _Alignas(int) char c;
+};
+
+void function4(void) {
+  struct x c = {'x'};
+  int *ip = (int *)&c;
+}
+
+// FIXME: we do not want a warning for this
+void function5(void) {
+  _Alignas(int) char c = 'x';
+  int *ip = (int *)&c;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not cast pointers into more strictly aligned pointer types [cert-exp36-c]
+}
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -20,6 +20,7 @@
bugprone-integer-division
bugprone-suspicious-memset-usage
bugprone-undefined-memory-manipulation
+   cert-exp36-c
cert-dcl03-c (redirects to misc-sta