Skip to content

Commit d19a2c3

Browse files
committed
[-Wunsafe-buffer-usage] Fix whitespace handling in array types
Specifically applies to text with array element type and array size for constant size arrays. Trimming there strings on both sides and using llvm::StringRef::trim().
1 parent e3c2483 commit d19a2c3

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/Lex/Preprocessor.h"
1919
#include "llvm/ADT/APSInt.h"
2020
#include "llvm/ADT/SmallVector.h"
21+
#include "llvm/ADT/StringRef.h"
2122
#include <memory>
2223
#include <optional>
2324
#include <queue>
@@ -2500,16 +2501,7 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
25002501
auto MaybeElemTypeTxt = getRangeText({D->getBeginLoc(), IdentifierLoc}, Ctx.getSourceManager(), Ctx.getLangOpts());
25012502
if (!MaybeElemTypeTxt)
25022503
return {};
2503-
std::string ElemTypeTxt = MaybeElemTypeTxt->str();
2504-
// Trim whitespace from the type spelling.
2505-
unsigned TrailingWhitespace = 0;
2506-
for (auto It = ElemTypeTxt.rbegin(); It < ElemTypeTxt.rend(); ++It) {
2507-
if (!isWhitespace(*It))
2508-
break;
2509-
++TrailingWhitespace;
2510-
}
2511-
if (TrailingWhitespace > 0)
2512-
ElemTypeTxt.erase(ElemTypeTxt.length() - TrailingWhitespace);
2504+
const llvm::StringRef ElemTypeTxt = MaybeElemTypeTxt->trim();
25132505

25142506
// Find the '[' token.
25152507
std::optional<Token> NextTok = Lexer::findNextToken(IdentifierLoc, Ctx.getSourceManager(), Ctx.getLangOpts());
@@ -2523,8 +2515,7 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
25232515
auto MaybeArraySizeTxt = getRangeText({LSqBracketLoc.getLocWithOffset(1), D->getTypeSpecEndLoc()}, Ctx.getSourceManager(), Ctx.getLangOpts());
25242516
if (!MaybeArraySizeTxt)
25252517
return {};
2526-
2527-
const std::string ArraySizeTxt = MaybeArraySizeTxt->str();
2518+
const llvm::StringRef ArraySizeTxt = MaybeArraySizeTxt->trim();
25282519

25292520
std::optional<StringRef> IdentText =
25302521
getVarDeclIdentifierText(D, Ctx.getSourceManager(), Ctx.getLangOpts());

clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-array.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ void local_array(unsigned idx) {
1010
buffer[idx] = 0;
1111
}
1212

13-
void weird_whitespace_in_declaration(unsigned idx) {
14-
int buffer_w [ 10 ] ;
15-
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:35}:"std::array<int, 10 > buffer_w"
13+
void whitespace_in_declaration(unsigned idx) {
14+
int buffer_w [ 10 ];
15+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:35}:"std::array<int, 10> buffer_w"
1616
buffer_w[idx] = 0;
1717
}
1818

19-
void weird_comments_in_declaration(unsigned idx) {
20-
int /* [ ] */ buffer_w /* [ ] */ [ /* [ ] */ 10 /* [ ] */ ] ;
21-
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:67}:"std::array<int /* [ ] */, /* [ ] */ 10 /* [ ] */ > buffer_w"
19+
void comments_in_declaration(unsigned idx) {
20+
int /* [A] */ buffer_w /* [B] */ [ /* [C] */ 10 /* [D] */ ] ;
21+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:69}:"std::array<int /* [A] */, /* [C] */ 10 /* [D] */> buffer_w"
2222
buffer_w[idx] = 0;
2323
}
2424

0 commit comments

Comments
 (0)