[PATCH] D45468: [clang-tidy] Adding Fuchsia checker for human-readable logging

2018-04-10 Thread Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added inline comments.



Comment at: docs/clang-tidy/checks/zircon-human-readable-status.rst:6
+
+Suggests fix for printf-family functions with a zx_status_t argument to convert
+status argument to a human-readable string (zx_status_t is a Zircon kernel

Please highlight zx_status_t and other identifiers with ``.


https://reviews.llvm.org/D45468



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


[PATCH] D45468: [clang-tidy] Adding Fuchsia checker for human-readable logging

2018-04-10 Thread Jonas Toth via Phabricator via cfe-commits
JonasToth added inline comments.



Comment at: clang-tidy/zircon/HumanReadableStatusCheck.cpp:68
+SourceRange FmtStringRange;
+for (const auto *Arg : D->arguments()) {
+  if (const auto *ICE = dyn_cast(Arg)) {

Did you consider using `forEachArgumentWithParam` matcher?

I think you could match any `zx_status_t` argument for the formatter functions 
and bind on them.



Comment at: clang-tidy/zircon/HumanReadableStatusCheck.cpp:103
+
+std::string HumanReadableStatusCheck::ReplaceFmtString(int Pos,
+   StringRef FmtString) {

Is this required as member function? You can make that `static` (or move into 
an unnamed namespace, not 100% sure about the ruling in llvm)



Comment at: clang-tidy/zircon/HumanReadableStatusCheck.cpp:108
+  while (Current <= Pos && Loc < FmtString.size() - 1) {
+Loc = FmtString.find('%', Loc);
+++Current;

That does not consider `\%` and would be buggy for such fmt strings



Comment at: clang-tidy/zircon/HumanReadableStatusCheck.cpp:112
+  }
+  std::string ReplacementString = FmtString;
+  if (Loc != StringRef::npos && ReplacementString[Loc] == 'd')

I think this replacement strategy is not general enough.

http://www.cplusplus.com/reference/cstdio/printf/

`%[flags][width][.precision][length]specifier`

If we decide to implement an actual `printf`-fmt string replacer that could 
maybe land in `utility`.


I assume that `%d` is the format specification used for 99,99% of all relevant 
calls?! Maybe it is enough anyway, because the check matches on very specific 
`%d` arguments.
What about the other integer formats? Any chance they are used?



Comment at: docs/ReleaseNotes.rst:202
+
+  FIXME: add release notes.
+

Please add a short describing sentence.



Comment at: docs/ReleaseNotes.rst:204
+
+- New `zircon-temporary-objects
+  
`_ 
check

This seems to be slipped through.



Comment at: test/clang-tidy/zircon-human-readable-status.cpp:68
+  WRAP("%d, %d", notstatus, status);
+  wrapper("%d, %d", notstatus, status);
+

Please add test with escaped `%` and other crazy format strings.

How are advanced format strings treated? I think it is necessary to do better 
parsing.


https://reviews.llvm.org/D45468



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


[PATCH] D45468: [clang-tidy] Adding Fuchsia checker for human-readable logging

2018-04-09 Thread Julie Hockett via Phabricator via cfe-commits
juliehockett created this revision.
juliehockett added reviewers: aaron.ballman, hokein, ilya-biryukov.
juliehockett added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, mgorny.

Adds a checker to the Fuchsia module to flag instances of attempting to log the 
system's numerical representation of the status, instead of the more 
human-friendly string representation. Matches formatting functions (the printf 
family and a set of user-specified formatting-like ones) that have a 
zx_status_t parameter and suggests a fixit to replace the zx_status_t parameter 
with a call to zx_status_get_string(param).


https://reviews.llvm.org/D45468

Files:
  clang-tidy/zircon/CMakeLists.txt
  clang-tidy/zircon/HumanReadableStatusCheck.cpp
  clang-tidy/zircon/HumanReadableStatusCheck.h
  clang-tidy/zircon/ZirconTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/zircon-human-readable-status.rst
  test/clang-tidy/zircon-human-readable-status.cpp

Index: test/clang-tidy/zircon-human-readable-status.cpp
===
--- /dev/null
+++ test/clang-tidy/zircon-human-readable-status.cpp
@@ -0,0 +1,70 @@
+// RUN: %check_clang_tidy %s zircon-human-readable-status %t -- \
+// RUN:   -config="{CheckOptions: [{key: zircon-human-readable-status.FormatLikeFunctions, value: 'my_printer'}]}" \
+// RUN:   -header-filter=.* \
+// RUN: -- -std=c++11
+
+#include 
+
+#define PRINT(fmt...) printf(fmt);
+
+void my_printer(const char *fmt, ...) {
+  va_list args;
+  va_start(args, fmt);
+  printf(fmt, args);
+  va_end(args);
+}
+
+#define WRAP(fmt...) wrapper(fmt);
+void wrapper(const char *fmt, ...) {
+  va_list args;
+  va_start(args, fmt);
+  printf(fmt, args);
+  va_end(args);
+}
+
+typedef int zx_status_t;
+
+const char *zx_status_get_string(zx_status_t status) { return "status"; }
+
+int main() {
+  zx_status_t status = 0;
+  printf("%d", status);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: non-human-readable log message
+  // CHECK-FIXES: printf("%s", zx_status_get_string(status));
+  printf("%s", zx_status_get_string(status));
+  my_printer("%d", status);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: non-human-readable log message
+  // CHECK-FIXES: my_printer("%s", zx_status_get_string(status));
+  PRINT("%d", status);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: non-human-readable log message
+  // CHECK-FIXES: PRINT("%s", zx_status_get_string(status));
+  
+  WRAP("%d", status);
+  wrapper("%d", status);
+  
+  unsigned notstatus = 0;
+  printf("%d", notstatus);
+  printf("%s", zx_status_get_string(status));
+  my_printer("%d", notstatus);
+  PRINT("%d", notstatus);
+  WRAP("%d", notstatus);
+  wrapper("%d", notstatus);
+  
+  printf("%d, %d", status, notstatus);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: non-human-readable log message
+  // CHECK-FIXES: printf("%s, %d", zx_status_get_string(status), notstatus);
+  printf("%s, %d", zx_status_get_string(status), notstatus);
+  my_printer("%d, %d", status, notstatus);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: non-human-readable log message
+  // CHECK-FIXES: my_printer("%s, %d", zx_status_get_string(status), notstatus);
+  printf("%d, %d", notstatus, status);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: non-human-readable log message
+  // CHECK-FIXES: printf("%d, %s", notstatus, zx_status_get_string(status));
+  PRINT("%d, %d", status, notstatus);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: non-human-readable log message
+  // CHECK-FIXES: PRINT("%s, %d", zx_status_get_string(status), notstatus);
+  
+  WRAP("%d, %d", notstatus, status);
+  wrapper("%d, %d", notstatus, status);
+
+}
Index: docs/clang-tidy/checks/zircon-human-readable-status.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/zircon-human-readable-status.rst
@@ -0,0 +1,30 @@
+.. title:: clang-tidy - zircon-human-readable-status
+
+zircon-human-readable-status
+
+
+Suggests fix for printf-family functions with a zx_status_t argument to convert
+status argument to a human-readable string (zx_status_t is a Zircon kernel
+status message, and zx_status_get_string() is a function that retrieves the
+associated string value.
+
+For example:
+
+.. code-block:: c++
+
+  zx_status_t status;
+  printf("%d", status);   // Suggests printf("%s", zx_status_get_string(status))
+  
+  unsigned num;
+  printf("%d", num)   // No warning
+
+
+Options
+---
+
+.. option:: Names
+
+   A semi-colon-separated list of fully-qualified names of functions that 
+   are printf-like (i.e. are variadic, with the first argument being a
+   formatting string and subsequent argments being the printf replacements.).
+   Default is empty.
\ No newline at end of file
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/