[PATCH] D156189: [-Wunsafe-buffer-usage] Refactor to let local variable fix-its and parameter fix-its share common code

Ziqing Luo via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 25 15:11:41 PDT 2023


ziqingluo-90 added inline comments.


================
Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:1824
+
+  if (!IdentText)
+    return {};
----------------
t-rasmud wrote:
> When will this condition be satisfied? I just want to understand if there are code examples where there is no identifier text or is it that `getVarDeclIdentifierText` fails.
`!IdentText` if `getVarDeclIdentifierText` fails.  

`getVarDeclIdentifierText` assumes that the identifier text is a single token.   It starts with the begin location of the identifier and tries to obtain the end location via `Lexer::getLocForEndOfToken`.
So it could fail if 1) the identifier text is not a single token or 2) any unexpected failure happens in obtaining valid source locations.

Case 2) could be rare I think since I don't know when it could happen.  We just need to assume that it is NOT always successful in manipulating source locations.

For case 1), I originally thought that if the identifier is expanded from a macro with parameters, e.g., 
```
#define MACRO(x)  name
int * MACRO(x);
```
, `MACRO(x)` is not a single token.  But it seems I was wrong---it is one token to the `Lexer`.  See my test at `warn-unsafe-buffer-usage-fixits-local-var-span.cpp:186--206`.

In conclusion, `getVarDeclIdentifierText` could fail but it should be rare.


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

https://reviews.llvm.org/D156189



More information about the cfe-commits mailing list