This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix modernize-use-std-print check when return value used
ClosedPublic

Authored by mikecrowe on Jun 27 2023, 5:24 AM.

Details

Summary

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.

Diff Detail

Event Timeline

mikecrowe created this revision.Jun 27 2023, 5:24 AM
Herald added a project: Restricted Project. · View Herald Transcript
mikecrowe requested review of this revision.Jun 27 2023, 5:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 5:24 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
PiotrZSL accepted this revision.Jun 27 2023, 7:59 AM

LGTM

clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
73–95
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.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp
63
NOTE: I don't think that those FIXES-NOT are needed.
This revision is now accepted and ready to land.Jun 27 2023, 7:59 AM
mikecrowe marked an inline comment as done.Jun 27 2023, 9:27 AM
mikecrowe added inline comments.
clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp
73–95

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<clang::ast_matchers::internal::HasParentMatcher, internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>, internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>>'
      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<T> &InnerMatcher) 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<T...> &InnerMatcher) const {

(Reducing the set of matchers inside the anyOf didn't help.)

clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp
63

OK. I'll leave only the ones that are for deficiencies that might one day be fixed.

dyung added a subscriber: dyung.Jun 27 2023, 11:49 AM

@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

PiotrZSL reopened this revision.Jun 27 2023, 12:26 PM
This revision is now accepted and ready to land.Jun 27 2023, 12:26 PM
mikecrowe marked an inline comment as done.Jun 27 2023, 12:57 PM

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

mikecrowe updated this revision to Diff 535106.Jun 27 2023, 1:12 PM

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
mikecrowe requested review of this revision.Jun 29 2023, 4:54 AM

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

This revision was not accepted when it landed; it landed in state Needs Review.Jun 29 2023, 7:00 AM
This revision was automatically updated to reflect the committed changes.