[clang] [clang][Sema] Don't warn for implicit uses of builtins in system headers (PR #138205)

via cfe-commits cfe-commits at lists.llvm.org
Fri May 2 07:25:08 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Nick Sarnie (sarnex)

<details>
<summary>Changes</summary>

When trying to remove the usage of `__has_builtin` on MSVC CUDA ARM for some builtins, the recommended direction was to universally declare the MSVC builtins on all platforms and require the header providing declarations to be included. This was done [here](https://github.com/llvm/llvm-project/pull/128222).

However, some MSVC headers already use the MSVC builtins without including the header, so we introduce a warning for anyone compiling with MSVC for this target, so the above change had to be reverted.

As a workaround, don't warn for implicit uses of library functions if it's inside a system header, unless system header warnings are enabled.

If this PR is accepted I will re-apply the original commit reworking the builtins.

---
Full diff: https://github.com/llvm/llvm-project/pull/138205.diff


3 Files Affected:

- (modified) clang/lib/Sema/SemaDecl.cpp (+6-1) 
- (added) clang/test/Sema/Inputs/builtin-system-header.h (+1) 
- (added) clang/test/Sema/builtin-system-header.c (+8) 


``````````diff
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a3285e8f6f5a2..37008f9eb3235 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -2376,9 +2376,14 @@ NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID,
     return nullptr;
   }
 
+  // Warn for implicit uses of header dependent libraries,
+  // except in system headers.
   if (!ForRedeclaration &&
       (Context.BuiltinInfo.isPredefinedLibFunction(ID) ||
-       Context.BuiltinInfo.isHeaderDependentFunction(ID))) {
+       Context.BuiltinInfo.isHeaderDependentFunction(ID)) &&
+      (!getDiagnostics().getSuppressSystemWarnings() ||
+       !Context.getSourceManager().isInSystemHeader(
+           Context.getSourceManager().getSpellingLoc(Loc)))) {
     Diag(Loc, LangOpts.C99 ? diag::ext_implicit_lib_function_decl_c99
                            : diag::ext_implicit_lib_function_decl)
         << Context.BuiltinInfo.getName(ID) << R;
diff --git a/clang/test/Sema/Inputs/builtin-system-header.h b/clang/test/Sema/Inputs/builtin-system-header.h
new file mode 100644
index 0000000000000..ebd5655e6f8ef
--- /dev/null
+++ b/clang/test/Sema/Inputs/builtin-system-header.h
@@ -0,0 +1 @@
+#define MACRO(x,y) _InterlockedOr64(x,y);
diff --git a/clang/test/Sema/builtin-system-header.c b/clang/test/Sema/builtin-system-header.c
new file mode 100644
index 0000000000000..83c3c15e314a7
--- /dev/null
+++ b/clang/test/Sema/builtin-system-header.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s
+
+// expected-no-diagnostics
+#include <builtin-system-header.h>
+
+void foo() {
+  MACRO(0,0);
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/138205


More information about the cfe-commits mailing list