[clang] d1ae844 - [-Wunsafe-buffer-usage] Do not emit fixits for C++ interfaces with C linkage
Rashmi Mudduluru via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 16 13:28:45 PDT 2023
Author: Rashmi Mudduluru
Date: 2023-06-16T13:28:34-07:00
New Revision: d1ae844dc2cc58bc762135d9500464c61d50f4f9
URL: https://github.com/llvm/llvm-project/commit/d1ae844dc2cc58bc762135d9500464c61d50f4f9
DIFF: https://github.com/llvm/llvm-project/commit/d1ae844dc2cc58bc762135d9500464c61d50f4f9.diff
LOG: [-Wunsafe-buffer-usage] Do not emit fixits for C++ interfaces with C linkage
Differential Revision: https://reviews.llvm.org/D153064
Added:
clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp
Modified:
clang/lib/Analysis/UnsafeBufferUsage.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 999b94a070368..49229a6b56c1c 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -2139,6 +2139,18 @@ void clang::checkUnsafeBufferUsage(const Decl *D,
UnsafeBufferUsageHandler &Handler,
bool EmitSuggestions) {
assert(D && D->getBody());
+
+ // Do not emit fixit suggestions for functions declared in an
+ // extern "C" block.
+ if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
+ for (FunctionDecl *FReDecl : FD->redecls()) {
+ if (FReDecl->isExternC()) {
+ EmitSuggestions = false;
+ break;
+ }
+ }
+ }
+
WarningGadgetSets UnsafeOps;
FixableGadgetSets FixablesForAllVars;
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp
new file mode 100644
index 0000000000000..6c134098f87df
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s
+
+extern "C" {
+void foo(int *ptr) {
+ ptr[5] = 10; // expected-warning{{unsafe buffer access}}
+}
+
+void bar(int *ptr);
+
+struct c_struct {
+ char *name;
+};
+}
+
+void bar(int *ptr) {
+ ptr[5] = 10; // expected-warning{{unsafe buffer access}}
+}
+
+void call_foo(int *p) {
+ foo(p);
+ struct c_struct str;
+ str.name[7] = 9; // expected-warning{{unsafe buffer access}}
+ bar(p);
+}
More information about the cfe-commits
mailing list