[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-29 Thread Piotr Zegar via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rG09ed2102eab5: [clang-tidy] Fix modernize-use-std-print check 
when return value used (authored by mikecrowe, committed by PiotrZSL).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153860

Files:
  clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
  clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
@@ -1,11 +1,11 @@
 // RUN: %check_clang_tidy -check-suffixes=,STRICT \
 // RUN:   -std=c++23 %s modernize-use-std-print %t -- \
 // RUN:   -config="{CheckOptions: [{key: StrictMode, value: true}]}" \
-// RUN:   -- -isystem %clang_tidy_headers
+// RUN:   -- -isystem %clang_tidy_headers -fexceptions
 // RUN: %check_clang_tidy -check-suffixes=,NOTSTRICT \
 // RUN:   -std=c++23 %s modernize-use-std-print %t -- \
 // RUN:   -config="{CheckOptions: [{key: StrictMode, value: false}]}" \
-// RUN:   -- -isystem %clang_tidy_headers
+// RUN:   -- -isystem %clang_tidy_headers -fexceptions
 #include 
 #include 
 #include 
@@ -44,6 +44,239 @@
   // CHECK-FIXES: std::println("Hello");
 }
 
+// std::print returns nothing, so any callers that use the return
+// value cannot be automatically translated.
+int printf_uses_return_value(int choice) {
+  const int i = printf("Return value assigned to variable %d\n", 42);
+
+  extern void accepts_int(int);
+  accepts_int(printf("Return value passed to function %d\n", 42));
+
+  if (choice == 0)
+printf("if body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("if body {}", i);
+  else if (choice == 1)
+printf("else if body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("else if body {}", i);
+  else
+printf("else body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("else body {}", i);
+
+  if (printf("Return value used as boolean in if statement"))
+if (printf("Return value used in expression if statement") == 44)
+  if (const int j = printf("Return value used in assignment in if statement"))
+if (const int k = printf("Return value used with initializer in if statement"); k == 44)
+  ;
+
+  int d = 0;
+  while (printf("%d", d) < 2)
+++d;
+
+  while (true)
+printf("while body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("while body {}", i);
+
+  do
+printf("do body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("do body {}", i);
+  while (true);
+
+  for (;;)
+printf("for body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for body {}", i);
+
+  for (printf("for init statement %d\n", i);;)
+// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for init statement {}", i);
+;;
+
+  for (int j = printf("for init statement %d\n", i);;)
+;;
+
+  for (; printf("for condition %d\n", i);)
+;;
+
+  for (;; printf("for expression %d\n", i))
+// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for expression {}", i)
+;;
+
+  for (auto C : "foo")
+printf("ranged-for body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("ranged-for body {}", i);
+
+  switch (1) {
+  case 1:
+printf("switch case body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("switch case body {}", i);
+break;
+  default:
+printf("switch default body %d\n", i);
+// CHECK-MESSAGES: 

[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-29 Thread Mike Crowe via Phabricator via cfe-commits
mikecrowe requested review of this revision.
mikecrowe added a comment.

I believe that the problems that caused this to be reverted have been fixed.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153860

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


[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-27 Thread Mike Crowe via Phabricator via cfe-commits
mikecrowe updated this revision to Diff 535106.
mikecrowe added a comment.

Fix test failures on PS4 and PS5 and improve tests

- Ensure that exceptions are available so the test can use try/catch
- Remove unwanted -NOT checks
- Add more return value tests


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153860

Files:
  clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
  clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
@@ -1,11 +1,11 @@
 // RUN: %check_clang_tidy -check-suffixes=,STRICT \
 // RUN:   -std=c++23 %s modernize-use-std-print %t -- \
 // RUN:   -config="{CheckOptions: [{key: StrictMode, value: true}]}" \
-// RUN:   -- -isystem %clang_tidy_headers
+// RUN:   -- -isystem %clang_tidy_headers -fexceptions
 // RUN: %check_clang_tidy -check-suffixes=,NOTSTRICT \
 // RUN:   -std=c++23 %s modernize-use-std-print %t -- \
 // RUN:   -config="{CheckOptions: [{key: StrictMode, value: false}]}" \
-// RUN:   -- -isystem %clang_tidy_headers
+// RUN:   -- -isystem %clang_tidy_headers -fexceptions
 #include 
 #include 
 #include 
@@ -44,6 +44,239 @@
   // CHECK-FIXES: std::println("Hello");
 }
 
+// std::print returns nothing, so any callers that use the return
+// value cannot be automatically translated.
+int printf_uses_return_value(int choice) {
+  const int i = printf("Return value assigned to variable %d\n", 42);
+
+  extern void accepts_int(int);
+  accepts_int(printf("Return value passed to function %d\n", 42));
+
+  if (choice == 0)
+printf("if body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("if body {}", i);
+  else if (choice == 1)
+printf("else if body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("else if body {}", i);
+  else
+printf("else body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("else body {}", i);
+
+  if (printf("Return value used as boolean in if statement"))
+if (printf("Return value used in expression if statement") == 44)
+  if (const int j = printf("Return value used in assignment in if statement"))
+if (const int k = printf("Return value used with initializer in if statement"); k == 44)
+  ;
+
+  int d = 0;
+  while (printf("%d", d) < 2)
+++d;
+
+  while (true)
+printf("while body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("while body {}", i);
+
+  do
+printf("do body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("do body {}", i);
+  while (true);
+
+  for (;;)
+printf("for body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for body {}", i);
+
+  for (printf("for init statement %d\n", i);;)
+// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for init statement {}", i);
+;;
+
+  for (int j = printf("for init statement %d\n", i);;)
+;;
+
+  for (; printf("for condition %d\n", i);)
+;;
+
+  for (;; printf("for expression %d\n", i))
+// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for expression {}", i)
+;;
+
+  for (auto C : "foo")
+printf("ranged-for body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("ranged-for body {}", i);
+
+  switch (1) {
+  case 1:
+printf("switch case body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("switch case body {}", i);
+break;
+  default:
+printf("switch default body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 

[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-27 Thread Mike Crowe via Phabricator via cfe-commits
mikecrowe marked an inline comment as done.
mikecrowe added a comment.

In D153860#4453572 , @dyung wrote:

> @mikecrowe Your change is causing a test failure on the PS4 linux and PS5 
> Windows build bots. Can you take a look and fix or revert if you need time to 
> investigate?
>
> https://lab.llvm.org/buildbot/#/builders/139/builds/43856
> https://lab.llvm.org/buildbot/#/builders/216/builds/23017

The failure is due to:

  use-std-print.cpp.tmp.cpp:125:3: error: cannot use 'try' with exceptions 
disabled [clang-diagnostic-error]

I wasn't expecting that!

It looks like I failed to notice that the `bugprone-unused-return-value` check 
explicitly added `-fexceptions` to the `clang-tidy` command line to avoid this 
problem. I can add that to hopefully fix the problem, and I had some other 
changes that I hadn't pushed for review before this landed the first time too.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153860

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


[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-27 Thread Douglas Yung via Phabricator via cfe-commits
dyung added a comment.

@mikecrowe Your change is causing a test failure on the PS4 linux and PS5 
Windows build bots. Can you take a look and fix or revert if you need time to 
investigate?

https://lab.llvm.org/buildbot/#/builders/139/builds/43856
https://lab.llvm.org/buildbot/#/builders/216/builds/23017


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153860

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


[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-27 Thread Piotr Zegar via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG3e12b2e207cf: [clang-tidy] Fix modernize-use-std-print check 
when return value used (authored by mikecrowe, committed by PiotrZSL).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153860

Files:
  clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
  clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
@@ -44,6 +44,233 @@
   // CHECK-FIXES: std::println("Hello");
 }
 
+// std::print returns nothing, so any callers that use the return
+// value cannot be automatically translated.
+int printf_uses_return_value(int choice) {
+  const int i = printf("Return value assigned to variable %d\n", 42);
+
+  if (choice == 0)
+printf("if body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("if body {}", i);
+  else if (choice == 1)
+printf("else if body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("else if body {}", i);
+  else
+printf("else body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("else body {}", i);
+
+  if (printf("Return value used as boolean in if statement"))
+if (printf("Return value used in expression if statement") == 44)
+  if (const int j = printf("Return value used in assignment in if statement"))
+if (const int k = printf("Return value used with initializer in if statement"); k == 44)
+  ;
+
+  int d = 0;
+  while (printf("%d", d) < 2)
+++d;
+
+  while (true)
+printf("while body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("while body {}", i);
+
+  do
+printf("do body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("do body {}", i);
+  while (true);
+
+  for (;;)
+printf("for body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for body {}", i);
+
+  for (printf("for init statement %d\n", i);;)
+// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for init statement {}", i);
+;;
+
+  for (int j = printf("for init statement %d\n", i);;)
+;;
+
+  for (; printf("for condition %d\n", i);)
+;;
+
+  for (;; printf("for expression %d\n", i))
+// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for expression {}", i)
+;;
+
+  for (auto C : "foo")
+printf("ranged-for body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("ranged-for body {}", i);
+
+  switch (1) {
+  case 1:
+printf("switch case body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("switch case body {}", i);
+break;
+  default:
+printf("switch default body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("switch default body {}", i);
+break;
+  }
+
+  try {
+printf("try body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("try body {}", i);
+  } catch (int) {
+printf("catch body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("catch body {}", i);
+  }
+
+  (printf("Parenthesised expression %d\n", i));
+  // CHECK-MESSAGES: [[@LINE-1]]:4: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+  // CHECK-FIXES: 

[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-27 Thread Mike Crowe via Phabricator via cfe-commits
mikecrowe marked an inline comment as done.
mikecrowe added inline comments.



Comment at: clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp:73-95
+static clang::ast_matchers::StatementMatcher
+unusedReturnValue(clang::ast_matchers::StatementMatcher MatchedCallExpr) {
+  auto UnusedInCompoundStmt =
+  compoundStmt(forEach(MatchedCallExpr),
+   // The checker can't currently differentiate between the
+   // return statement and other statements inside GNU 
statement
+   // expressions, so disable the checker inside them to avoid

PiotrZSL wrote:
> NOTE: Personally I do not thing that this is right way. Instead of using 
> "inclusion" matcher, better would be to use elimination.
> like:
> ```callExpr(unless(hasParent(anyOf(varDecl(), callExpr(), ifStmt(), ...```.
> 
> But if it's working fine, then it could be for now, so lets leave it. Simply 
> with this it may not find all cases.
I'm happy to try doing it a different way. I just took this code from the 
`bugprone-unused-return-value` check. I had a go at using:
```C++
Finder->addMatcher(
  callExpr(unless(hasParent(anyOf(varDecl(), callExpr(), ifStmt(), 
whileStmt(), doStmt(), forStmt(), cxxForRangeStmt(), switchCase(, 
argumentCountAtLeast(1),
```
but there's no overload of `hasParent` that will accept the return type of 
`anyOf`:
```
/home/mac/git/llvm-project/clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp:100:23:
 error: no matching function for call to object of type 'const 
internal::ArgumentAdaptingMatcherFunc, 
internal::TypeList>'
  callExpr(unless(hasParent(anyOf(varDecl(), callExpr(), ifStmt(), 
whileStmt(), doStmt(), forStmt(), cxxForRangeStmt(), switchCase(, 
argumentCountAtLeast(1),
  ^
/home/mac/git/llvm-project/clang/include/clang/ASTMatchers/ASTMatchersInternal.h:1491:3:
 note: candidate template ignored: could not match 'Matcher' against 
'VariadicOperatorMatcher'
  operator()(const Matcher ) const {
  ^
/home/mac/git/llvm-project/clang/include/clang/ASTMatchers/ASTMatchersInternal.h:1498:3:
 note: candidate template ignored: could not match 'MapAnyOfHelper' against 
'VariadicOperatorMatcher'
  operator()(const MapAnyOfHelper ) const {
```
(Reducing the set of matchers inside the `anyOf` didn't help.)



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp:63
+  // CHECK-MESSAGES-NOT: [[@LINE-1]]:10: warning: use 'std::println' instead 
of 'PrintF' [modernize-use-std-print]
+  // CHECK-FIXES-NOT: std::println("return value {}", i);
+}

PiotrZSL wrote:
> NOTE: I don't think that those FIXES-NOT are needed.
OK. I'll leave only the ones that are for deficiencies that might one day be 
fixed.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153860

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


[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-27 Thread Piotr Zegar via Phabricator via cfe-commits
PiotrZSL accepted this revision.
PiotrZSL added a comment.
This revision is now accepted and ready to land.

LGTM




Comment at: clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp:73-95
+static clang::ast_matchers::StatementMatcher
+unusedReturnValue(clang::ast_matchers::StatementMatcher MatchedCallExpr) {
+  auto UnusedInCompoundStmt =
+  compoundStmt(forEach(MatchedCallExpr),
+   // The checker can't currently differentiate between the
+   // return statement and other statements inside GNU 
statement
+   // expressions, so disable the checker inside them to avoid

NOTE: Personally I do not thing that this is right way. Instead of using 
"inclusion" matcher, better would be to use elimination.
like:
```callExpr(unless(hasParent(anyOf(varDecl(), callExpr(), ifStmt(), ...```.

But if it's working fine, then it could be for now, so lets leave it. Simply 
with this it may not find all cases.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp:63
+  // CHECK-MESSAGES-NOT: [[@LINE-1]]:10: warning: use 'std::println' instead 
of 'PrintF' [modernize-use-std-print]
+  // CHECK-FIXES-NOT: std::println("return value {}", i);
+}

NOTE: I don't think that those FIXES-NOT are needed.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153860

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


[PATCH] D153860: [clang-tidy] Fix modernize-use-std-print check when return value used

2023-06-27 Thread Mike Crowe via Phabricator via cfe-commits
mikecrowe created this revision.
mikecrowe added a reviewer: PiotrZSL.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
mikecrowe requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

The initial implementation of the modernize-use-std-print check was
capable of converting calls to printf (etc.) which used the return value
to calls to std::print which has no return value, thus breaking the
code.

Use code inspired by the implementation of bugprone-unused-return-value
check to ignore cases where the return value is used. Add appropriate
lit test cases and documentation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153860

Files:
  clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
  clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp
@@ -44,6 +44,233 @@
   // CHECK-FIXES: std::println("Hello");
 }
 
+// std::print returns nothing, so any callers that use the return
+// value cannot be automatically translated.
+int printf_uses_return_value(int choice) {
+  const int i = printf("Return value assigned to variable %d\n", 42);
+
+  if (choice == 0)
+printf("if body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("if body {}", i);
+  else if (choice == 1)
+printf("else if body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("else if body {}", i);
+  else
+printf("else body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("else body {}", i);
+
+  if (printf("Return value used as boolean in if statement"))
+if (printf("Return value used in expression if statement") == 44)
+  if (const int j = printf("Return value used in assignment in if statement"))
+if (const int k = printf("Return value used with initializer in if statement"); k == 44)
+  ;
+
+  int d = 0;
+  while (printf("%d", d) < 2)
+++d;
+
+  while (true)
+printf("while body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("while body {}", i);
+
+  do
+printf("do body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("do body {}", i);
+  while (true);
+
+  for (;;)
+printf("for body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for body {}", i);
+
+  for (printf("for init statement %d\n", i);;)
+// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for init statement {}", i);
+;;
+
+  for (int j = printf("for init statement %d\n", i);;)
+;;
+
+  for (; printf("for condition %d\n", i);)
+;;
+
+  for (;; printf("for expression %d\n", i))
+// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("for expression {}", i)
+;;
+
+  for (auto C : "foo")
+printf("ranged-for body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("ranged-for body {}", i);
+
+  switch (1) {
+  case 1:
+printf("switch case body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("switch case body {}", i);
+break;
+  default:
+printf("switch default body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("switch default body {}", i);
+break;
+  }
+
+  try {
+printf("try body %d\n", i);
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]
+// CHECK-FIXES: std::println("try body {}", i);
+  } catch