[clang-tools-extra] [clang-tidy] Only expand <inttypes.h> macros in modernize-use-std-format/print (PR #97911)

Mike Crowe via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 21 08:15:37 PDT 2024


mikecrowe wrote:

@5chmidti wrote:
> It should be possible to detect this case comparing the FileID of the call's location and the format expression's location. If they differ, then they are different arguments -> different macros. Although, I haven't checked that yet.

I had thought so too but I was unable to find any `SourceLocation` or `Lexer` functions that returned what I needed. I've pushed an extra commit to the branch for this PR that contains an extra LIT test and some debug output. The test contains:
```C++
SURROUND_ALL(printf(SURROUND_FORMAT_PARTIAL("%" FMT) "\n", "42"));
```
with the hope that I can detect that `SURROUND_ALL` is applied to both the `printf` and the `"\n"` but different macros are applied to the other tokens that make up the format string.

The debug output (with the filenames shortened for readability) is:
```
getImmediateExpansionRange for 'BeginCallLoc': <use-std-print-macro.cpp.tmp.cpp:16:3 <Spelling=line:10:25>>
getExpansionRange for 'BeginCallLoc': <use-std-print-macro.cpp.tmp.cpp:16:3, col:67>
getTopMacroCaller for 'BeginCallLoc': use-std-print-macro.cpp.tmp.cpp:16:16
getImmediateMacroCallerLoc for 'BeginCallLoc': use-std-print-macro.cpp.tmp.cpp:16:16
getImmediateSpellingLoc for 'BeginCallLoc': use-std-print-macro.cpp.tmp.cpp:16:16
getFileLoc for 'BeginCallLoc': use-std-print-macro.cpp.tmp.cpp:16:16

getImmediateExpansionRange for 'SURROUND_FORMAT_PARTIAL': <use-std-print-macro.cpp.tmp.cpp:16:3 <Spelling=line:10:25>>
getExpansionRange for 'SURROUND_FORMAT_PARTIAL': <use-std-print-macro.cpp.tmp.cpp:16:3, col:67>
getTopMacroCaller for 'SURROUND_FORMAT_PARTIAL': use-std-print-macro.cpp.tmp.cpp:16:47
getImmediateMacroCallerLoc for 'SURROUND_FORMAT_PARTIAL': use-std-print-macro.cpp.tmp.cpp:16:23 <Spelling=use-std-print-macro.cpp.tmp.cpp:16:47>
getImmediateSpellingLoc for 'SURROUND_FORMAT_PARTIAL': use-std-print-macro.cpp.tmp.cpp:16:23 <Spelling=use-std-print-macro.cpp.tmp.cpp:16:47>
getFileLoc for 'SURROUND_FORMAT_PARTIAL': use-std-print-macro.cpp.tmp.cpp:16:47

getImmediateExpansionRange for 'FMT': <use-std-print-macro.cpp.tmp.cpp:16:3 <Spelling=line:10:25>>
getExpansionRange for 'FMT': <use-std-print-macro.cpp.tmp.cpp:16:3, col:67>
getTopMacroCaller for 'FMT': use-std-print-macro.cpp.tmp.cpp:16:51 <Spelling=use-std-print-macro.cpp.tmp.cpp:12:13>
getImmediateMacroCallerLoc for 'FMT': use-std-print-macro.cpp.tmp.cpp:16:23 <Spelling=use-std-print-macro.cpp.tmp.cpp:12:13>
getImmediateSpellingLoc for 'FMT': use-std-print-macro.cpp.tmp.cpp:16:23 <Spelling=use-std-print-macro.cpp.tmp.cpp:12:13>
getFileLoc for 'FMT': use-std-print-macro.cpp.tmp.cpp:16:51

getImmediateExpansionRange for 'SURROUND_ALL': <use-std-print-macro.cpp.tmp.cpp:16:3 <Spelling=line:10:25>>
getExpansionRange for 'SURROUND_ALL': <use-std-print-macro.cpp.tmp.cpp:16:3, col:67>
getTopMacroCaller for 'SURROUND_ALL': use-std-print-macro.cpp.tmp.cpp:16:56
getImmediateMacroCallerLoc for 'SURROUND_ALL': use-std-print-macro.cpp.tmp.cpp:16:56
getImmediateSpellingLoc for 'SURROUND_ALL': use-std-print-macro.cpp.tmp.cpp:16:56
getFileLoc for 'SURROUND_ALL': use-std-print-macro.cpp.tmp.cpp:16:56
```
Although the last token inside `SURROUND_ALL` has a few functions which return the same location as for `BeginCallLoc`, these functions also return that location for the tokens inside other macros too. :(

The change also captures the FileID for `BeginCallLoc` and compares it against each `TokenLoc` but never gets a match.

Or perhaps I've misinterpreted what you meant. If so, please can you provide more details?


https://github.com/llvm/llvm-project/pull/97911


More information about the cfe-commits mailing list