[PATCH] D156616: [clang-tidy] Fix c_str() removal and cast addition when re-ordering arguments

Mike Crowe via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 30 07:15:15 PDT 2023


mikecrowe added a comment.

Fixing this was rather messier than I expected, but this solution does seem to work for the test cases. Here's my original commit message before I trimmed it, in case it provides any more insight.

  [clang-tidy] Fix c_str() removal and cast addition when re-ordering arguments
  
  The modernize-use-std-print check would get confused if it had to
  re-order field-width and precision arguments at the same time as adding
  casts or removing calls to c_str(). For example applying the check with
  StrictMode enabled to:
  
   printf("%*s=%*d\n", 4, s.c_str(), 3, ui);
  
  yields:
  
   std::println("{:>{}}={:{}}", s.c_str(), s4, ui, static_cast<int>(3));
  
  rather than the expected:
  
   std::println("{:>{}}={:{}}", s, 4, static_cast<int>(ui), 3);
  
  Fix this by:
  
  - storing the ArgIndex rather than the Expr pointer for any arguments
    that need casts to be added in ArgFixes so that the index can be
    modified when the arguments are re-ordered. Use a struct rather than a
    tuple for clarity.
  
  - Making applyFixes do argument re-ordering first, but also taking care
    of c_str() removal at the same time if necessary. Update the argument
    index of any ArgFixes too.
  
  - Apply the ArgFixes afterwards.
  
  - Finally apply and c_str() removals that remain.
  
  - Add lit check test cases for the combinations of argument reordering,
    casts and c_str() removal. This required moving the definition of
    struct iterator earlier in use-std-print.cpp.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156616



More information about the cfe-commits mailing list