Refactor the code for local variable fix-its so that it reuses the code for parameter fix-its, which is in general better. For example, cv-qualifiers are supported.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-span.cpp | ||
---|---|---|
242 | We no longer generate fix-its for a variable declaration where the type specifier is just auto. This is to avoid that fix-its may become incorrect when the adopting codebase changes. For example, int x; auto p = &x; If we fix the declaration of p to std::span<int> p{&x, 1}, and later int x is changed to long x, the program becomes incorrect and the programmer may not know that the type of p should simply follow the type of x. |
clang/lib/Analysis/UnsafeBufferUsage.cpp | ||
---|---|---|
1860 | 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. |
clang/lib/Analysis/UnsafeBufferUsage.cpp | ||
---|---|---|
1860 | !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. 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. |
LGTM! I have minor suggestions for comments.
clang/lib/Analysis/UnsafeBufferUsage.cpp | ||
---|---|---|
1829–1833 | ||
1861 | Should we start adding debug notes to all these early-returns? Or are they already covered by a catch-all debug note down the line? | |
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-span.cpp | ||
20–23 | ||
42–45 |