diff options
author | jkorous-apple <32549412+jkorous-apple@users.noreply.github.com> | 2024-02-12 15:52:20 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-12 15:52:20 -0800 |
commit | 644ac2a018c9bf83c9ba256074e552ad7f1fe941 (patch) | |
tree | 2e156b3f438a02968c707fd16da0d809ad9b9bce /clang/lib/Sema/AnalysisBasedWarnings.cpp | |
parent | fac6d3d98ba1bac24acc5b19c84d07af25c1b755 (diff) | |
download | llvm-644ac2a018c9bf83c9ba256074e552ad7f1fe941.zip llvm-644ac2a018c9bf83c9ba256074e552ad7f1fe941.tar.gz llvm-644ac2a018c9bf83c9ba256074e552ad7f1fe941.tar.bz2 |
[-Wunsafe-buffer-usage] Introduce std::array fixits (#80084)
Array subscript on a const size array is not bounds-checked. The idiomatic
replacement is std::array which is bounds-safe in hardened mode of libc++.
This commit extends the fixit-producing machine to consider std::array as a
transformation target type and teaches it to handle the array subscript on const
size arrays with a trivial (empty) fixit.
Diffstat (limited to 'clang/lib/Sema/AnalysisBasedWarnings.cpp')
-rw-r--r-- | clang/lib/Sema/AnalysisBasedWarnings.cpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp index 78b9f32..8239ba4 100644 --- a/clang/lib/Sema/AnalysisBasedWarnings.cpp +++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp @@ -2297,7 +2297,8 @@ public: void handleUnsafeVariableGroup(const VarDecl *Variable, const VariableGroupsManager &VarGrpMgr, - FixItList &&Fixes, const Decl *D) override { + FixItList &&Fixes, const Decl *D, + const FixitStrategy &VarTargetTypes) override { assert(!SuggestSuggestions && "Unsafe buffer usage fixits displayed without suggestions!"); S.Diag(Variable->getLocation(), diag::warn_unsafe_buffer_variable) @@ -2312,7 +2313,18 @@ public: // NOT explain how the variables are grouped as the reason is non-trivial // and irrelavant to users' experience: const auto VarGroupForVD = VarGrpMgr.getGroupOfVar(Variable, &BriefMsg); - unsigned FixItStrategy = 0; // For now we only have 'std::span' strategy + unsigned FixItStrategy = 0; + switch (VarTargetTypes.lookup(Variable)) { + case clang::FixitStrategy::Kind::Span: + FixItStrategy = 0; + break; + case clang::FixitStrategy::Kind::Array: + FixItStrategy = 1; + break; + default: + assert(false && "We support only std::span and std::array"); + }; + const auto &FD = S.Diag(Variable->getLocation(), BriefMsg ? diag::note_unsafe_buffer_variable_fixit_together |