Issue 175183
Summary clang-tidy: modernize-use-std-format mangles the code inside a macro
Labels clang-tidy
Assignees
Reporter vadz
    Here is the minimum example reproducing the problem I could make:

```cpp
#include <string>
#include <stdexcept>

namespace String
{

extern std::string Printf(const char *format, ...);

}

enum SomeEnum
{
    SomeEnum_Good,
    SomeEnum_Bad
};

#define EXCEPTION_MSG(msg) \
    std::runtime_error(msg)

void Check(SomeEnum n) {
    throw EXCEPTION_MSG(String::Printf("Invalid (%d)", n));
}
```

Running clang-tidy-21 with the following config:

```yaml
Checks: '-*,modernize-use-std-format'
CheckOptions:
 modernize-use-std-format.StrFormatLikeFunctions: 'String::Printf'
 modernize-use-std-format.ReplacementFormatFunction: 'fmt::format'
 modernize-use-std-format.FormatHeader: '<fmt/format.h>'
```

produces the following broken "fix":

```
% clang-tidy-21 t.cpp --  
1 warning generated.
t.cpp:21:25: warning: use 'fmt::format' instead of 'Printf' [modernize-use-std-format]
   21 |     throw EXCEPTION_MSG(String::Printf("Invalid (%d)", n));
      | ^~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
      | fmt::format    )"Invalid ({})" static_cast<int>(
t.cpp:18:24: note: expanded from macro 'EXCEPTION_MSG'
   18 |     std::runtime_error(msg)
 |                        ^
```

Unfortunately I have plenty of `EXCEPTION_MSG()` macro occurrences in my real code (where this macro does something less trivial than above...), so running clang-tidy on the full code base breaks a lot of code.

As an aside (not sure if it's worth opening a separate issue for this?), please note that clang-tidy didn't generate the code adding `#include <fmt/format.h>` here, which is almost certainly also related to the fact that the function being replaced is inside a macro, as it did add the header to other files in the project.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to