aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/AnalysisBasedWarnings.cpp
diff options
context:
space:
mode:
authorArtem Dergachev <adergachev@apple.com>2022-12-02 12:58:56 -0800
committerArtem Dergachev <adergachev@apple.com>2022-12-05 15:13:42 -0800
commit200007ec85f81122fd260a4e68308e54607ca37a (patch)
treeeed7b009e719757d3f01708528a6ba6002b7c88e /clang/lib/Sema/AnalysisBasedWarnings.cpp
parentfe3103fa485f67b15efffd11d592a3341eb7bd57 (diff)
downloadllvm-200007ec85f81122fd260a4e68308e54607ca37a.zip
llvm-200007ec85f81122fd260a4e68308e54607ca37a.tar.gz
llvm-200007ec85f81122fd260a4e68308e54607ca37a.tar.bz2
[-Wunsafe-buffer-usage] Initial commit - Transition away from raw buffers.
This is the initial commit for -Wunsafe-buffer-usage, a warning that helps codebases (especially modern C++ codebases) transition away from raw buffer pointers. The warning is implemented in libAnalysis as it's going to become a non-trivial analysis, mostly the fixit part where we try to figure out if we understand a variable's use pattern well enough to suggest a safe container/view as a replacement. Some parts of this analsysis may eventually prove useful for any similar fixit machine that tries to change types of variables. The warning is disabled by default. RFC/discussion in https://discourse.llvm.org/t/rfc-c-buffer-hardening/65734 Differential Revision: https://reviews.llvm.org/D137346
Diffstat (limited to 'clang/lib/Sema/AnalysisBasedWarnings.cpp')
-rw-r--r--clang/lib/Sema/AnalysisBasedWarnings.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index 9780a0a..70b81c0 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -29,6 +29,7 @@
#include "clang/Analysis/Analyses/ReachableCode.h"
#include "clang/Analysis/Analyses/ThreadSafety.h"
#include "clang/Analysis/Analyses/UninitializedValues.h"
+#include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
#include "clang/Analysis/AnalysisDeclContext.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/CFGStmtMap.h"
@@ -2139,6 +2140,23 @@ public:
} // namespace clang
//===----------------------------------------------------------------------===//
+// Unsafe buffer usage analysis.
+//===----------------------------------------------------------------------===//
+
+class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
+ Sema &S;
+
+public:
+ UnsafeBufferUsageReporter(Sema &S) : S(S) {}
+
+ void handleUnsafeOperation(const Stmt *Operation) override {
+ S.Diag(Operation->getBeginLoc(), diag::warn_unsafe_buffer_usage)
+ << Operation->getSourceRange();
+ }
+};
+
+
+//===----------------------------------------------------------------------===//
// AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based
// warnings on a function, method, or block.
//===----------------------------------------------------------------------===//
@@ -2430,6 +2448,12 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
if (S.getLangOpts().CPlusPlus && isNoexcept(FD))
checkThrowInNonThrowingFunc(S, FD, AC);
+ // Emit unsafe buffer usage warnings and fixits.
+ if (!Diags.isIgnored(diag::warn_unsafe_buffer_usage, D->getBeginLoc())) {
+ UnsafeBufferUsageReporter R(S);
+ checkUnsafeBufferUsage(D, R);
+ }
+
// If none of the previous checks caused a CFG build, trigger one here
// for the logical error handler.
if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) {