[clang] [Clang] Don't use crtbegin/crtend when building for musl. (PR #85089)

2024-03-13 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/85089

>From 8bdd6627e21eaddedfff208eebaa46f1eeb81674 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Thu, 22 Feb 2024 11:36:57 +
Subject: [PATCH 1/2] [Clang] Don't use crtbegin/crtend when building for musl.

musl doesn't supply crtbegin/crtend objects, so we don't want to
try to link them there.

rdar://123436174
---
 clang/lib/Driver/ToolChains/Gnu.cpp | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index a9c9d2475809d7..7b7d9194a773ec 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -371,13 +371,15 @@ void tools::gnutools::Linker::ConstructJob(Compilation 
&C, const JobAction &JA,
   const llvm::Triple::ArchType Arch = ToolChain.getArch();
   const bool isOHOSFamily = ToolChain.getTriple().isOHOSFamily();
   const bool isAndroid = ToolChain.getTriple().isAndroid();
+  const bool isMusl = ToolChain.getTriple().isMusl();
   const bool IsIAMCU = ToolChain.getTriple().isOSIAMCU();
   const bool IsVE = ToolChain.getTriple().isVE();
   const bool IsStaticPIE = getStaticPIE(Args, ToolChain);
   const bool IsStatic = getStatic(Args);
   const bool HasCRTBeginEndFiles =
-  ToolChain.getTriple().hasEnvironment() ||
-  (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies);
+!isMusl && (ToolChain.getTriple().hasEnvironment() ||
+(ToolChain.getTriple().getVendor()
+ != llvm::Triple::MipsTechnologies));
 
   ArgStringList CmdArgs;
 

>From cb13d69df1ff750d9653a76186a8ac4626695470 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 13 Mar 2024 16:05:09 +
Subject: [PATCH 2/2] [Clang] Tweak code formatting.

Tweak code formatting slightly to make clang-format happy.

rdar://123436174
---
 clang/lib/Driver/ToolChains/Gnu.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index 7b7d9194a773ec..740a160cfa5c22 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -377,9 +377,9 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   const bool IsStaticPIE = getStaticPIE(Args, ToolChain);
   const bool IsStatic = getStatic(Args);
   const bool HasCRTBeginEndFiles =
-!isMusl && (ToolChain.getTriple().hasEnvironment() ||
-(ToolChain.getTriple().getVendor()
- != llvm::Triple::MipsTechnologies));
+  !isMusl &&
+  (ToolChain.getTriple().hasEnvironment() ||
+   (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies));
 
   ArgStringList CmdArgs;
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [libunwind] Tweak tests for musl support. (PR #85097)

2024-03-13 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/85097

>From 6808d20b198bba4f2e062094b89cfc13eac49f85 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Thu, 22 Feb 2024 11:59:24 +
Subject: [PATCH 1/2] [libunwind] Tweak tests for musl support.

We can't use `dladdr()` in the tests, because when we're statically
linking with musl that function is a no-op.

Additionally, because musl disables emission of unwind information
in its build, and because its signal trampolines don't include
unwind information, tests that involve unwinding through a signal
handler won't work and need to be disabled for musl.

rdar://123436891
---
 libunwind/test/floatregister.pass.cpp   | 18 +-
 libunwind/test/forceunwind.pass.cpp | 19 +--
 libunwind/test/signal_unwind.pass.cpp   | 26 +++--
 libunwind/test/unwind_leaffunction.pass.cpp | 24 ++-
 4 files changed, 61 insertions(+), 26 deletions(-)

diff --git a/libunwind/test/floatregister.pass.cpp 
b/libunwind/test/floatregister.pass.cpp
index 64107e6d490b70..e4657c63fd1adf 100644
--- a/libunwind/test/floatregister.pass.cpp
+++ b/libunwind/test/floatregister.pass.cpp
@@ -11,20 +11,26 @@
 
 // Basic test for float registers number are accepted.
 
-#include 
 #include 
 #include 
 #include 
 
+// Using __attribute__((section("main_func"))) is Linux specific, but then
+// this entire test is marked as requiring Linux, so we should be good.
+//
+// We don't use dladdr() because on musl it's a no-op when statically linked.
+extern char __start_main_func;
+extern char __stop_main_func;
+
 _Unwind_Reason_Code frame_handler(struct _Unwind_Context *ctx, void *arg) {
   (void)arg;
-  Dl_info info = {0, 0, 0, 0};
 
-  // Unwind util the main is reached, above frames depend on the platform and
+  // Unwind until the main is reached, above frames depend on the platform and
   // architecture.
-  if (dladdr(reinterpret_cast(_Unwind_GetIP(ctx)), &info) &&
-  info.dli_sname && !strcmp("main", info.dli_sname))
+  uintptr_t ip = _Unwind_GetIP(ctx);
+  if (ip >= (uintptr_t)&__start_main_func && ip < 
(uintptr_t)&__stop_main_func) {
 _Exit(0);
+  }
 
   return _URC_NO_REASON;
 }
@@ -45,7 +51,7 @@ __attribute__((noinline)) void foo() {
   _Unwind_Backtrace(frame_handler, NULL);
 }
 
-int main() {
+__attribute__((section("main_func"))) int main() {
   foo();
   return -2;
 }
diff --git a/libunwind/test/forceunwind.pass.cpp 
b/libunwind/test/forceunwind.pass.cpp
index db499d8bc30894..feb71fb769980c 100644
--- a/libunwind/test/forceunwind.pass.cpp
+++ b/libunwind/test/forceunwind.pass.cpp
@@ -17,7 +17,6 @@
 
 #undef NDEBUG
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -27,6 +26,13 @@
 #include 
 #include 
 
+// Using __attribute__((section("main_func"))) is Linux specific, but then
+// this entire test is marked as requiring Linux, so we should be good.
+//
+// We don't use dladdr() because on musl it's a no-op when statically linked.
+extern char __start_main_func;
+extern char __stop_main_func;
+
 void foo();
 _Unwind_Exception ex;
 
@@ -41,14 +47,13 @@ _Unwind_Reason_Code stop(int version, _Unwind_Action 
actions,
   assert(exceptionObject == &ex);
   assert(stop_parameter == &foo);
 
-  Dl_info info = {0, 0, 0, 0};
-
-  // Unwind util the main is reached, above frames depend on the platform and
+  // Unwind until the main is reached, above frames depend on the platform and
   // architecture.
-  if (dladdr(reinterpret_cast(_Unwind_GetIP(context)), &info) &&
-  info.dli_sname && !strcmp("main", info.dli_sname)) {
+  uintptr_t ip = _Unwind_GetIP(context);
+  if (ip >= (uintptr_t)&__start_main_func && ip < 
(uintptr_t)&__stop_main_func) {
 _Exit(0);
   }
+
   return _URC_NO_REASON;
 }
 
@@ -66,7 +71,7 @@ __attribute__((noinline)) void foo() {
   _Unwind_ForcedUnwind(e, stop, (void *)&foo);
 }
 
-int main() {
+__attribute__((section("main_func"))) int main() {
   foo();
   return -2;
 }
diff --git a/libunwind/test/signal_unwind.pass.cpp 
b/libunwind/test/signal_unwind.pass.cpp
index 954a5d4ba3db10..715299e7496efe 100644
--- a/libunwind/test/signal_unwind.pass.cpp
+++ b/libunwind/test/signal_unwind.pass.cpp
@@ -8,14 +8,13 @@
 
//===--===//
 
 // Ensure that the unwinder can cope with the signal handler.
-// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
+// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+}}linux-gnu
 
 // TODO: Figure out why this fails with Memory Sanitizer.
 // XFAIL: msan
 
 #undef NDEBUG
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -24,16 +23,29 @@
 #include 
 #include 
 
+// Note: this test fails on musl because:
+//
+//  (a) musl disables emission of unwind information for its build, and
+//  (b) musl's signal trampolines don't include unwind information
+//
+
+// Using __attribute__((section("main_func"))) is Linux specific, but then
+// this entir

[clang] [Clang] Don't use crtbegin/crtend when building for musl. (PR #85089)

2024-03-13 Thread Alastair Houghton via cfe-commits

al45tair wrote:

> The compiler provides crtbegin/crtend files, not musl.

Interesting. Looking at the LLVM repo, it seems compiler-rt might provide them, 
but I don't believe I have them in spite of building compiler-rt. I'll look at 
this again and see if maybe I need to tweak my compiler-rt build to get it to 
generate or install them in the right place.

https://github.com/llvm/llvm-project/pull/85089
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Don't use crtbegin/crtend when building for musl. (PR #85089)

2024-03-13 Thread Alastair Houghton via cfe-commits

al45tair wrote:

(I had assumed that they came from the C library, since that's where `crt1.o` 
comes from, but if they don't then it sounds like I just need to build them.)

https://github.com/llvm/llvm-project/pull/85089
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Don't use crtbegin/crtend when building for musl. (PR #85089)

2024-03-14 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair closed 
https://github.com/llvm/llvm-project/pull/85089
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Don't use crtbegin/crtend when building for musl. (PR #85089)

2024-03-14 Thread Alastair Houghton via cfe-commits

al45tair wrote:

Regardless of who provides them, if someone is relying on this then this is the 
wrong change. Closing.

https://github.com/llvm/llvm-project/pull/85089
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Prioritise built-in headers, even on musl. (PR #85092)

2024-03-14 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/85092

>From 527aa4616dc53f8f7ca212472fa40c23f40b6dc1 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Thu, 22 Feb 2024 11:33:32 +
Subject: [PATCH 1/2] [Clang] Prioritise built-in headers, even on musl.

Clang was putting its built-in headers at the end of the search path
if running on musl; this was a mistake, because it breaks libc++, as
the latter tries to include the built-in header and then the
`#include_next` in the built-in header fails.

The right solution here is to have the built-in headers remain in
their usual location in the search path, and then if it's desirable
to override them for musl, have them explicitly include the musl
header with `#include_next`.  This is the solution that is already
in use for other platforms.

rdar://118881637
---
 clang/lib/Basic/Targets/OSTargets.h   |  2 ++
 clang/lib/Driver/ToolChains/Linux.cpp | 22 +-
 clang/lib/Headers/float.h | 10 +++---
 clang/lib/Headers/stddef.h| 10 ++
 4 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.h 
b/clang/lib/Basic/Targets/OSTargets.h
index 4366c1149e4053..7c5ef420757ef3 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -331,6 +331,8 @@ class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public 
OSTargetInfo {
 } else {
 Builder.defineMacro("__gnu_linux__");
 }
+if (Triple.isMusl())
+  Builder.defineMacro("__musl__", "1");
 if (Opts.POSIXThreads)
   Builder.defineMacro("_REENTRANT");
 if (Opts.CPlusPlus)
diff --git a/clang/lib/Driver/ToolChains/Linux.cpp 
b/clang/lib/Driver/ToolChains/Linux.cpp
index 6c2f23e57bce05..857cbb73240cf6 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -629,13 +629,20 @@ void Linux::AddClangSystemIncludeArgs(const ArgList 
&DriverArgs,
 
   // Add 'include' in the resource directory, which is similar to
   // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
-  // contains some files conflicting with system /usr/include. musl systems
-  // prefer the /usr/include copies which are more relevant.
-  SmallString<128> ResourceDirInclude(D.ResourceDir);
-  llvm::sys::path::append(ResourceDirInclude, "include");
-  if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
-  (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
+  // contains some files conflicting with system /usr/include.
+  //
+  // Note: the include order used to be different on musl systems because
+  // of the expectation that that users of that C library would use the
+  // C library's copies of the "built-in" headers.  This was a mistake;
+  // it's better to adapt the built-in headers to fall through in that case
+  // (using #include_next), since otherwise if they get pulled in through
+  // some other mechanism, __has_include_next() will fail and then
+  // they'll try to redefine things, which causes errors.
+  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
+SmallString<128> ResourceDirInclude(D.ResourceDir);
+llvm::sys::path::append(ResourceDirInclude, "include");
 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
+  }
 
   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
 return;
@@ -676,9 +683,6 @@ void Linux::AddClangSystemIncludeArgs(const ArgList 
&DriverArgs,
   addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include"));
 
   addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, 
"/usr/include"));
-
-  if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
-addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
 }
 
 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
diff --git a/clang/lib/Headers/float.h b/clang/lib/Headers/float.h
index 0e73bca0a2d6e4..715a4eb54b54a4 100644
--- a/clang/lib/Headers/float.h
+++ b/clang/lib/Headers/float.h
@@ -10,15 +10,11 @@
 #ifndef __CLANG_FLOAT_H
 #define __CLANG_FLOAT_H
 
-/* If we're on MinGW, fall back to the system's float.h, which might have
- * additional definitions provided for Windows.
- * For more details see http://msdn.microsoft.com/en-us/library/y0ybw9fy.aspx
- *
- * Also fall back on Darwin and AIX to allow additional definitions and
- * implementation-defined values.
+/* On various platforms, fall back to the system's float.h, which might have
+ * additional definitions and/or implementation-defined values.
  */
 #if (defined(__APPLE__) || defined(__MINGW32__) || defined(_MSC_VER) ||
\
- defined(_AIX)) && 
\
+ defined(_AIX) || defined(__musl__)) &&
\
 __STDC_HOSTED__ && __has_include_next()
 
 /* Prior to Apple's 10.7 SDK, float.h SDK header used to apply an extra level
diff --git a/clang/lib/Headers/stddef.h 

[libunwind] [libunwind] Tweak tests for musl support. (PR #85097)

2024-03-22 Thread Alastair Houghton via cfe-commits


@@ -11,20 +11,27 @@
 
 // Basic test for float registers number are accepted.
 
-#include 
 #include 
 #include 
 #include 
 
+// Using __attribute__((section("main_func"))) is Linux specific, but then

al45tair wrote:

Fair point.

https://github.com/llvm/llvm-project/pull/85097
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Don't use crtbegin/crtend when building for musl. (PR #85089)

2024-03-13 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair created 
https://github.com/llvm/llvm-project/pull/85089

musl doesn't supply crtbegin/crtend objects, so we don't want to try to link 
them there.

rdar://123436174

>From 8bdd6627e21eaddedfff208eebaa46f1eeb81674 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Thu, 22 Feb 2024 11:36:57 +
Subject: [PATCH] [Clang] Don't use crtbegin/crtend when building for musl.

musl doesn't supply crtbegin/crtend objects, so we don't want to
try to link them there.

rdar://123436174
---
 clang/lib/Driver/ToolChains/Gnu.cpp | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index a9c9d2475809d7..7b7d9194a773ec 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -371,13 +371,15 @@ void tools::gnutools::Linker::ConstructJob(Compilation 
&C, const JobAction &JA,
   const llvm::Triple::ArchType Arch = ToolChain.getArch();
   const bool isOHOSFamily = ToolChain.getTriple().isOHOSFamily();
   const bool isAndroid = ToolChain.getTriple().isAndroid();
+  const bool isMusl = ToolChain.getTriple().isMusl();
   const bool IsIAMCU = ToolChain.getTriple().isOSIAMCU();
   const bool IsVE = ToolChain.getTriple().isVE();
   const bool IsStaticPIE = getStaticPIE(Args, ToolChain);
   const bool IsStatic = getStatic(Args);
   const bool HasCRTBeginEndFiles =
-  ToolChain.getTriple().hasEnvironment() ||
-  (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies);
+!isMusl && (ToolChain.getTriple().hasEnvironment() ||
+(ToolChain.getTriple().getVendor()
+ != llvm::Triple::MipsTechnologies));
 
   ArgStringList CmdArgs;
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Prioritise built-in headers, even on musl. (PR #85092)

2024-03-13 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair created 
https://github.com/llvm/llvm-project/pull/85092

Clang was putting its built-in headers at the end of the search path if running 
on musl; this was a mistake, because it breaks libc++, as the latter tries to 
include the built-in header and then the `#include_next` in the built-in header 
fails.

The right solution here is to have the built-in headers remain in their usual 
location in the search path, and then if it's desirable to override them for 
musl, have them explicitly include the musl header with `#include_next`.  This 
is the solution that is already in use for other platforms.

rdar://118881637

>From 527aa4616dc53f8f7ca212472fa40c23f40b6dc1 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Thu, 22 Feb 2024 11:33:32 +
Subject: [PATCH] [Clang] Prioritise built-in headers, even on musl.

Clang was putting its built-in headers at the end of the search path
if running on musl; this was a mistake, because it breaks libc++, as
the latter tries to include the built-in header and then the
`#include_next` in the built-in header fails.

The right solution here is to have the built-in headers remain in
their usual location in the search path, and then if it's desirable
to override them for musl, have them explicitly include the musl
header with `#include_next`.  This is the solution that is already
in use for other platforms.

rdar://118881637
---
 clang/lib/Basic/Targets/OSTargets.h   |  2 ++
 clang/lib/Driver/ToolChains/Linux.cpp | 22 +-
 clang/lib/Headers/float.h | 10 +++---
 clang/lib/Headers/stddef.h| 10 ++
 4 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.h 
b/clang/lib/Basic/Targets/OSTargets.h
index 4366c1149e4053..7c5ef420757ef3 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -331,6 +331,8 @@ class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public 
OSTargetInfo {
 } else {
 Builder.defineMacro("__gnu_linux__");
 }
+if (Triple.isMusl())
+  Builder.defineMacro("__musl__", "1");
 if (Opts.POSIXThreads)
   Builder.defineMacro("_REENTRANT");
 if (Opts.CPlusPlus)
diff --git a/clang/lib/Driver/ToolChains/Linux.cpp 
b/clang/lib/Driver/ToolChains/Linux.cpp
index 6c2f23e57bce05..857cbb73240cf6 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -629,13 +629,20 @@ void Linux::AddClangSystemIncludeArgs(const ArgList 
&DriverArgs,
 
   // Add 'include' in the resource directory, which is similar to
   // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
-  // contains some files conflicting with system /usr/include. musl systems
-  // prefer the /usr/include copies which are more relevant.
-  SmallString<128> ResourceDirInclude(D.ResourceDir);
-  llvm::sys::path::append(ResourceDirInclude, "include");
-  if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
-  (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
+  // contains some files conflicting with system /usr/include.
+  //
+  // Note: the include order used to be different on musl systems because
+  // of the expectation that that users of that C library would use the
+  // C library's copies of the "built-in" headers.  This was a mistake;
+  // it's better to adapt the built-in headers to fall through in that case
+  // (using #include_next), since otherwise if they get pulled in through
+  // some other mechanism, __has_include_next() will fail and then
+  // they'll try to redefine things, which causes errors.
+  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
+SmallString<128> ResourceDirInclude(D.ResourceDir);
+llvm::sys::path::append(ResourceDirInclude, "include");
 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
+  }
 
   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
 return;
@@ -676,9 +683,6 @@ void Linux::AddClangSystemIncludeArgs(const ArgList 
&DriverArgs,
   addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include"));
 
   addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, 
"/usr/include"));
-
-  if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
-addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
 }
 
 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
diff --git a/clang/lib/Headers/float.h b/clang/lib/Headers/float.h
index 0e73bca0a2d6e4..715a4eb54b54a4 100644
--- a/clang/lib/Headers/float.h
+++ b/clang/lib/Headers/float.h
@@ -10,15 +10,11 @@
 #ifndef __CLANG_FLOAT_H
 #define __CLANG_FLOAT_H
 
-/* If we're on MinGW, fall back to the system's float.h, which might have
- * additional definitions provided for Windows.
- * For more details see http://msdn.microsoft.com/en-us/library/y0ybw9fy.aspx
- *
- * Also fall back on Darwin and AIX to allow additional definitions and
- * implementation-defined values.
+/

[libunwind] [libunwind] Tweak tests for musl support. (PR #85097)

2024-03-13 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair created 
https://github.com/llvm/llvm-project/pull/85097

We can't use `dladdr()` in the tests, because when we're statically linking 
with musl that function is a no-op.

Additionally, because musl disables emission of unwind information in its 
build, and because its signal trampolines don't include unwind information, 
tests that involve unwinding through a signal handler won't work and need to be 
disabled for musl.

rdar://123436891

>From 6808d20b198bba4f2e062094b89cfc13eac49f85 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Thu, 22 Feb 2024 11:59:24 +
Subject: [PATCH] [libunwind] Tweak tests for musl support.

We can't use `dladdr()` in the tests, because when we're statically
linking with musl that function is a no-op.

Additionally, because musl disables emission of unwind information
in its build, and because its signal trampolines don't include
unwind information, tests that involve unwinding through a signal
handler won't work and need to be disabled for musl.

rdar://123436891
---
 libunwind/test/floatregister.pass.cpp   | 18 +-
 libunwind/test/forceunwind.pass.cpp | 19 +--
 libunwind/test/signal_unwind.pass.cpp   | 26 +++--
 libunwind/test/unwind_leaffunction.pass.cpp | 24 ++-
 4 files changed, 61 insertions(+), 26 deletions(-)

diff --git a/libunwind/test/floatregister.pass.cpp 
b/libunwind/test/floatregister.pass.cpp
index 64107e6d490b70b..e4657c63fd1adf3 100644
--- a/libunwind/test/floatregister.pass.cpp
+++ b/libunwind/test/floatregister.pass.cpp
@@ -11,20 +11,26 @@
 
 // Basic test for float registers number are accepted.
 
-#include 
 #include 
 #include 
 #include 
 
+// Using __attribute__((section("main_func"))) is Linux specific, but then
+// this entire test is marked as requiring Linux, so we should be good.
+//
+// We don't use dladdr() because on musl it's a no-op when statically linked.
+extern char __start_main_func;
+extern char __stop_main_func;
+
 _Unwind_Reason_Code frame_handler(struct _Unwind_Context *ctx, void *arg) {
   (void)arg;
-  Dl_info info = {0, 0, 0, 0};
 
-  // Unwind util the main is reached, above frames depend on the platform and
+  // Unwind until the main is reached, above frames depend on the platform and
   // architecture.
-  if (dladdr(reinterpret_cast(_Unwind_GetIP(ctx)), &info) &&
-  info.dli_sname && !strcmp("main", info.dli_sname))
+  uintptr_t ip = _Unwind_GetIP(ctx);
+  if (ip >= (uintptr_t)&__start_main_func && ip < 
(uintptr_t)&__stop_main_func) {
 _Exit(0);
+  }
 
   return _URC_NO_REASON;
 }
@@ -45,7 +51,7 @@ __attribute__((noinline)) void foo() {
   _Unwind_Backtrace(frame_handler, NULL);
 }
 
-int main() {
+__attribute__((section("main_func"))) int main() {
   foo();
   return -2;
 }
diff --git a/libunwind/test/forceunwind.pass.cpp 
b/libunwind/test/forceunwind.pass.cpp
index db499d8bc30894e..feb71fb769980c8 100644
--- a/libunwind/test/forceunwind.pass.cpp
+++ b/libunwind/test/forceunwind.pass.cpp
@@ -17,7 +17,6 @@
 
 #undef NDEBUG
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -27,6 +26,13 @@
 #include 
 #include 
 
+// Using __attribute__((section("main_func"))) is Linux specific, but then
+// this entire test is marked as requiring Linux, so we should be good.
+//
+// We don't use dladdr() because on musl it's a no-op when statically linked.
+extern char __start_main_func;
+extern char __stop_main_func;
+
 void foo();
 _Unwind_Exception ex;
 
@@ -41,14 +47,13 @@ _Unwind_Reason_Code stop(int version, _Unwind_Action 
actions,
   assert(exceptionObject == &ex);
   assert(stop_parameter == &foo);
 
-  Dl_info info = {0, 0, 0, 0};
-
-  // Unwind util the main is reached, above frames depend on the platform and
+  // Unwind until the main is reached, above frames depend on the platform and
   // architecture.
-  if (dladdr(reinterpret_cast(_Unwind_GetIP(context)), &info) &&
-  info.dli_sname && !strcmp("main", info.dli_sname)) {
+  uintptr_t ip = _Unwind_GetIP(context);
+  if (ip >= (uintptr_t)&__start_main_func && ip < 
(uintptr_t)&__stop_main_func) {
 _Exit(0);
   }
+
   return _URC_NO_REASON;
 }
 
@@ -66,7 +71,7 @@ __attribute__((noinline)) void foo() {
   _Unwind_ForcedUnwind(e, stop, (void *)&foo);
 }
 
-int main() {
+__attribute__((section("main_func"))) int main() {
   foo();
   return -2;
 }
diff --git a/libunwind/test/signal_unwind.pass.cpp 
b/libunwind/test/signal_unwind.pass.cpp
index 954a5d4ba3db10d..715299e7496efe7 100644
--- a/libunwind/test/signal_unwind.pass.cpp
+++ b/libunwind/test/signal_unwind.pass.cpp
@@ -8,14 +8,13 @@
 
//===--===//
 
 // Ensure that the unwinder can cope with the signal handler.
-// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
+// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+}}linux-gnu
 
 // TODO: Figure out why this fails with Memory Sanitizer.
 // XFAIL: msan
 
 #undef NDE

[libunwind] [libunwind] Tweak tests for musl support. (PR #85097)

2024-03-13 Thread Alastair Houghton via cfe-commits

al45tair wrote:

This is a cherry pick of https://github.com/apple/llvm-project/pull/8260.

https://github.com/llvm/llvm-project/pull/85097
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][ObjC] Add optionality to property attribute strings. (PR #66507)

2023-09-26 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair edited 
https://github.com/llvm/llvm-project/pull/66507
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][ObjC] Add optionality to property attribute strings. (PR #66507)

2023-09-15 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair created 
https://github.com/llvm/llvm-project/pull/66507

Add a new attribute, "?", to the property attribute string for properties of 
protocols that are declared @optional.

(Previously https://reviews.llvm.org/D135273)

rdar://100463524

>From 3514013089dcf9fa798e9ba5812b34df497a493b Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Fri, 15 Sep 2023 13:46:19 +0100
Subject: [PATCH] [Clang][ObjC] Add optionality to property attribute strings.

Add a new attribute, "?", to the property attribute string for
properties of protocols that are declared @optional.

(Previously https://reviews.llvm.org/D135273)

rdar://100463524
---
 clang/lib/AST/ASTContext.cpp |  4 
 clang/test/CodeGenObjC/objc-asm-attribute-test.m | 10 +-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 4b1d9e86797b778..57aaa05b1d81ddb 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -7890,6 +7890,7 @@ ASTContext::getObjCPropertyImplDeclForPropertyDecl(
 /// kPropertyWeak = 'W'  // 'weak' property
 /// kPropertyStrong = 'P'// property GC'able
 /// kPropertyNonAtomic = 'N' // property non-atomic
+/// kPropertyOptional = '?'  // property optional
 /// };
 /// @endcode
 std::string
@@ -7915,6 +7916,9 @@ ASTContext::getObjCEncodingForPropertyDecl(const 
ObjCPropertyDecl *PD,
   // closely resembles encoding of ivars.
   getObjCEncodingForPropertyType(PD->getType(), S);
 
+  if (PD->isOptional())
+S += ",?";
+
   if (PD->isReadOnly()) {
 S += ",R";
 if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy)
diff --git a/clang/test/CodeGenObjC/objc-asm-attribute-test.m 
b/clang/test/CodeGenObjC/objc-asm-attribute-test.m
index 876370115bfc424..e57e42535f67274 100644
--- a/clang/test/CodeGenObjC/objc-asm-attribute-test.m
+++ b/clang/test/CodeGenObjC/objc-asm-attribute-test.m
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -emit-llvm -triple x86_64-apple-darwin %s -o - | FileCheck 
%s
+// RUN: %clang_cc1 -Wno-objc-root-class -emit-llvm -triple x86_64-apple-darwin 
%s -o - | FileCheck %s
 
 __attribute__((objc_runtime_name("MySecretNamespace.Protocol")))
 @protocol Protocol
@@ -10,6 +10,10 @@ + (void) ClsMethodP;
 @protocol Protocol2
 - (void) MethodP2;
 + (void) ClsMethodP2;
+
+@optional
+@property(retain) id optionalProp;
+
 @end
 
 __attribute__((objc_runtime_name("MySecretNamespace.Protocol3")))
@@ -57,6 +61,10 @@ id Test16877359(void) {
 // CHECK: @"OBJC_CLASS_$_MySecretNamespace.Message" ={{.*}} global 
%struct._class_t
 // CHECK: @"OBJC_METACLASS_$_MySecretNamespace.Message" ={{.*}} global 
%struct._class_t
 
+// CHECK: @OBJC_PROP_NAME_ATTR_ = private unnamed_addr constant [13 x i8] 
c"optionalProp\00"
+// CHECK-NEXT: @OBJC_PROP_NAME_ATTR_.11 = private unnamed_addr constant [7 x 
i8] c"T@,?,&\00"
+// CHECK: @"_OBJC_$_PROP_LIST_MySecretNamespace.Protocol2" ={{.*}} 
[%struct._prop_t { ptr @OBJC_PROP_NAME_ATTR_, ptr @OBJC_PROP_NAME_ATTR_.11 }]
+
 // CHECK: private unnamed_addr constant [42 x i8] 
c"T@\22MySecretNamespace.Message\22,&,V_msgProp\00"
 // CHECK: private unnamed_addr constant [76 x i8] 
c"T@\22MySecretNamespace.Message\22,&,V_msgProtoProp\00"
 // CHECK: private unnamed_addr constant [50 x i8] 
c"T@\22\22,&,V_idProtoProp\00"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-03 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair created 
https://github.com/llvm/llvm-project/pull/97597

This lets clang generate warnings automatically if it sees Objective-C names 
that don't have the correct prefix length.

rdar://131055157

>From 02f7c5ef71b382866e02037ce82c85fa6fcbb394 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9431eea1f6be2..0d944434cda38 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -598,6 +598,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 491759e2fcdbb..9df19b01f0b22 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -351,6 +351,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 1ede75d3782cd..829efafd03a2c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3418,6 +3418,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MySt

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-03 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From 02f7c5ef71b382866e02037ce82c85fa6fcbb394 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/2] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9431eea1f6be2..0d944434cda38 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -598,6 +598,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 491759e2fcdbb..9df19b01f0b22 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -351,6 +351,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 1ede75d3782cd..829efafd03a2c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3418,6 +3418,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is set
+//

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-04 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From 02f7c5ef71b382866e02037ce82c85fa6fcbb394 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/2] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9431eea1f6be2..0d944434cda38 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -598,6 +598,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 491759e2fcdbb..9df19b01f0b22 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -351,6 +351,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 1ede75d3782cd..829efafd03a2c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3418,6 +3418,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is set
+//

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-04 Thread Alastair Houghton via cfe-commits


@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 %s -Wobjc-prefixes=NS,NSCF,NSURL -fsyntax-only -verify

al45tair wrote:

Hmmm, maybe, yes. Not hard to add.

https://github.com/llvm/llvm-project/pull/97597
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-04 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From 02f7c5ef71b382866e02037ce82c85fa6fcbb394 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/3] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9431eea1f6be2..0d944434cda38 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -598,6 +598,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 491759e2fcdbb..9df19b01f0b22 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -351,6 +351,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 1ede75d3782cd..829efafd03a2c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3418,6 +3418,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is set
+//

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-04 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From 02f7c5ef71b382866e02037ce82c85fa6fcbb394 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/4] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9431eea1f6be2..0d944434cda38 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -598,6 +598,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 491759e2fcdbb..9df19b01f0b22 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -351,6 +351,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 1ede75d3782cd..829efafd03a2c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3418,6 +3418,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is set
+//

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-04 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From 02f7c5ef71b382866e02037ce82c85fa6fcbb394 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/5] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9431eea1f6be2..0d944434cda38 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -598,6 +598,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 491759e2fcdbb..9df19b01f0b22 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -351,6 +351,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 1ede75d3782cd..829efafd03a2c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3418,6 +3418,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is set
+//

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-04 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From 02f7c5ef71b382866e02037ce82c85fa6fcbb394 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/6] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9431eea1f6be22..0d944434cda387 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -598,6 +598,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f285567..7e26bd13f9c7bc 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 491759e2fcdbb9..9df19b01f0b22d 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -351,6 +351,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 1ede75d3782cdc..829efafd03a2c0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3418,6 +3418,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is

[libunwind] [libunwind] Tweak tests for musl support. (PR #85097)

2024-06-06 Thread Alastair Houghton via cfe-commits


@@ -24,16 +23,30 @@
 #include 
 #include 
 
+// Note: this test fails on musl because:
+//
+//  (a) musl disables emission of unwind information for its build, and
+//  (b) musl's signal trampolines don't include unwind information
+//

al45tair wrote:

Let's rearrange it to use `XFAIL`, since that's clearer.

https://github.com/llvm/llvm-project/pull/85097
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [libunwind] Tweak tests for musl support. (PR #85097)

2024-06-06 Thread Alastair Houghton via cfe-commits


@@ -24,16 +23,30 @@
 #include 
 #include 
 
+// Note: this test fails on musl because:
+//
+//  (a) musl disables emission of unwind information for its build, and
+//  (b) musl's signal trampolines don't include unwind information
+//

al45tair wrote:

We'll use `XFAIL` here too.

https://github.com/llvm/llvm-project/pull/85097
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [libunwind] Tweak tests for musl support. (PR #85097)

2024-06-06 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/85097

>From 6808d20b198bba4f2e062094b89cfc13eac49f85 Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Thu, 22 Feb 2024 11:59:24 +
Subject: [PATCH 1/3] [libunwind] Tweak tests for musl support.

We can't use `dladdr()` in the tests, because when we're statically
linking with musl that function is a no-op.

Additionally, because musl disables emission of unwind information
in its build, and because its signal trampolines don't include
unwind information, tests that involve unwinding through a signal
handler won't work and need to be disabled for musl.

rdar://123436891
---
 libunwind/test/floatregister.pass.cpp   | 18 +-
 libunwind/test/forceunwind.pass.cpp | 19 +--
 libunwind/test/signal_unwind.pass.cpp   | 26 +++--
 libunwind/test/unwind_leaffunction.pass.cpp | 24 ++-
 4 files changed, 61 insertions(+), 26 deletions(-)

diff --git a/libunwind/test/floatregister.pass.cpp 
b/libunwind/test/floatregister.pass.cpp
index 64107e6d490b7..e4657c63fd1ad 100644
--- a/libunwind/test/floatregister.pass.cpp
+++ b/libunwind/test/floatregister.pass.cpp
@@ -11,20 +11,26 @@
 
 // Basic test for float registers number are accepted.
 
-#include 
 #include 
 #include 
 #include 
 
+// Using __attribute__((section("main_func"))) is Linux specific, but then
+// this entire test is marked as requiring Linux, so we should be good.
+//
+// We don't use dladdr() because on musl it's a no-op when statically linked.
+extern char __start_main_func;
+extern char __stop_main_func;
+
 _Unwind_Reason_Code frame_handler(struct _Unwind_Context *ctx, void *arg) {
   (void)arg;
-  Dl_info info = {0, 0, 0, 0};
 
-  // Unwind util the main is reached, above frames depend on the platform and
+  // Unwind until the main is reached, above frames depend on the platform and
   // architecture.
-  if (dladdr(reinterpret_cast(_Unwind_GetIP(ctx)), &info) &&
-  info.dli_sname && !strcmp("main", info.dli_sname))
+  uintptr_t ip = _Unwind_GetIP(ctx);
+  if (ip >= (uintptr_t)&__start_main_func && ip < 
(uintptr_t)&__stop_main_func) {
 _Exit(0);
+  }
 
   return _URC_NO_REASON;
 }
@@ -45,7 +51,7 @@ __attribute__((noinline)) void foo() {
   _Unwind_Backtrace(frame_handler, NULL);
 }
 
-int main() {
+__attribute__((section("main_func"))) int main() {
   foo();
   return -2;
 }
diff --git a/libunwind/test/forceunwind.pass.cpp 
b/libunwind/test/forceunwind.pass.cpp
index db499d8bc3089..feb71fb769980 100644
--- a/libunwind/test/forceunwind.pass.cpp
+++ b/libunwind/test/forceunwind.pass.cpp
@@ -17,7 +17,6 @@
 
 #undef NDEBUG
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -27,6 +26,13 @@
 #include 
 #include 
 
+// Using __attribute__((section("main_func"))) is Linux specific, but then
+// this entire test is marked as requiring Linux, so we should be good.
+//
+// We don't use dladdr() because on musl it's a no-op when statically linked.
+extern char __start_main_func;
+extern char __stop_main_func;
+
 void foo();
 _Unwind_Exception ex;
 
@@ -41,14 +47,13 @@ _Unwind_Reason_Code stop(int version, _Unwind_Action 
actions,
   assert(exceptionObject == &ex);
   assert(stop_parameter == &foo);
 
-  Dl_info info = {0, 0, 0, 0};
-
-  // Unwind util the main is reached, above frames depend on the platform and
+  // Unwind until the main is reached, above frames depend on the platform and
   // architecture.
-  if (dladdr(reinterpret_cast(_Unwind_GetIP(context)), &info) &&
-  info.dli_sname && !strcmp("main", info.dli_sname)) {
+  uintptr_t ip = _Unwind_GetIP(context);
+  if (ip >= (uintptr_t)&__start_main_func && ip < 
(uintptr_t)&__stop_main_func) {
 _Exit(0);
   }
+
   return _URC_NO_REASON;
 }
 
@@ -66,7 +71,7 @@ __attribute__((noinline)) void foo() {
   _Unwind_ForcedUnwind(e, stop, (void *)&foo);
 }
 
-int main() {
+__attribute__((section("main_func"))) int main() {
   foo();
   return -2;
 }
diff --git a/libunwind/test/signal_unwind.pass.cpp 
b/libunwind/test/signal_unwind.pass.cpp
index 954a5d4ba3db1..715299e7496ef 100644
--- a/libunwind/test/signal_unwind.pass.cpp
+++ b/libunwind/test/signal_unwind.pass.cpp
@@ -8,14 +8,13 @@
 
//===--===//
 
 // Ensure that the unwinder can cope with the signal handler.
-// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
+// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+}}linux-gnu
 
 // TODO: Figure out why this fails with Memory Sanitizer.
 // XFAIL: msan
 
 #undef NDEBUG
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -24,16 +23,29 @@
 #include 
 #include 
 
+// Note: this test fails on musl because:
+//
+//  (a) musl disables emission of unwind information for its build, and
+//  (b) musl's signal trampolines don't include unwind information
+//
+
+// Using __attribute__((section("main_func"))) is Linux specific, but then
+// this entire test

[libunwind] [libunwind] Tweak tests for musl support. (PR #85097)

2024-06-07 Thread Alastair Houghton via cfe-commits

al45tair wrote:

I don't have merge rights here, so someone else will need to hit the Merge 
button.

https://github.com/llvm/llvm-project/pull/85097
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema][ObjC] Add warning options to detect bad name prefixes. (PR #97597)

2024-07-29 Thread Alastair Houghton via cfe-commits

al45tair wrote:

Failure doesn't look like anything to do with this PR.

https://github.com/llvm/llvm-project/pull/97597
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema][ObjC] Add warning options to detect bad name prefixes. (PR #97597)

2024-07-31 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair converted_to_draft 
https://github.com/llvm/llvm-project/pull/97597
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema][ObjC] Add warning options to detect bad name prefixes. (PR #97597)

2024-07-31 Thread Alastair Houghton via cfe-commits

al45tair wrote:

Converting back to draft as I'm still tinkering slightly with the warning rules.

https://github.com/llvm/llvm-project/pull/97597
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-16 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From c11624370bf5615c7902993052435b92ae50b41b Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/7] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..fc09603bf3e1e 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -612,6 +612,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 5ad9c1f24b7c5..7debdc0cdf75a 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -353,6 +353,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 2400b193d4d38..57344b75cebda 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3419,6 +3419,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is set
+//

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-17 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From c11624370bf5615c7902993052435b92ae50b41b Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/8] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..fc09603bf3e1e 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -612,6 +612,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 5ad9c1f24b7c5..7debdc0cdf75a 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -353,6 +353,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 2400b193d4d38..57344b75cebda 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3419,6 +3419,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is set
+//

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-18 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From c11624370bf5615c7902993052435b92ae50b41b Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH 1/8] [Parser][ObjC] Add -Wobjc-prefix-length warning option.

This lets clang generate warnings automatically if it sees Objective-C
names that don't have the correct prefix length.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |  1 +
 .../clang/Basic/DiagnosticParseKinds.td   |  7 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Driver/Options.td |  7 ++
 clang/include/clang/Parse/Parser.h|  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  1 +
 clang/lib/Parse/ParseObjc.cpp | 65 +++
 clang/test/Parser/objc-prefixes.m | 62 ++
 8 files changed, 146 insertions(+)
 create mode 100644 clang/test/Parser/objc-prefixes.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..fc09603bf3e1e 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -612,6 +612,7 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 12aab09f28556..7e26bd13f9c7b 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -563,6 +563,13 @@ def err_declaration_does_not_declare_param : Error<
   "declaration does not declare a parameter">;
 def err_no_matching_param : Error<"parameter named %0 is missing">;
 
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+
 /// Objective-C++ parser diagnostics
 def err_expected_token_instead_of_objcxx_keyword : Error<
   "expected %0; %1 is a keyword in Objective-C++">;
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 5ad9c1f24b7c5..7debdc0cdf75a 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -353,6 +353,8 @@ LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated 
reference counting")
 LANGOPT(ObjCWeakRuntime , 1, 0, "__weak support in the ARC runtime")
 LANGOPT(ObjCWeak, 1, 0, "Objective-C __weak in ARC and MRC files")
 LANGOPT(ObjCSubscriptingLegacyRuntime , 1, 0, "Subscripting support in 
legacy ObjectiveC runtime")
+BENIGN_LANGOPT(ObjCPrefixLength, 32, 0,
+   "if non-zero, warn about Objective-C classes or protocols with 
names that do not have a prefix of the specified length.")
 BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
"compatibility mode for type checking block parameters "
"involving qualified id types")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 2400b193d4d38..57344b75cebda 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3419,6 +3419,13 @@ defm objc_exceptions : BoolFOption<"objc-exceptions",
   PosFlag,
   NegFlag>;
+def Wobjc_prefix_length_EQ:
+  Joined<["-"], "Wobjc-prefix-length=">,
+  Visibility<[ClangOption, CC1Option]>,
+  MetaVarName<"">,
+  HelpText<"Warn if Objective-C class or protocol names do not start with a 
prefix of the specified length">,
+  MarshallingInfoInt>;
+
 defm application_extension : BoolFOption<"application-extension",
   LangOpts<"AppExt">, DefaultFalse,
   PosFlaggetBeginLoc(), diag::note_objc_container_start) << (int)ock;
 }
 
+/// An Objective-C public name (a class name or protocol name) is
+/// expected to have a prefix as all names are in a single global
+/// namespace.
+///
+/// If the -Wobjc-prefix-length is set to N, the name must start
+/// with N+1 capital letters, which must be followed by a character
+/// that is not a capital letter.
+///
+/// For instance, for N set to 2, the following are valid:
+///
+///  NSString
+///  NSArray
+///
+///  but these are not:
+///
+///  MyString
+///  NSKString
+///  NSnotAString
+///
+/// We make a special exception for NSCF things when the prefix is set
+//

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-18 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair updated 
https://github.com/llvm/llvm-project/pull/97597

>From 6dfbc7b61b137fdfd9d39b23bce62e451dca3e1c Mon Sep 17 00:00:00 2001
From: Alastair Houghton 
Date: Wed, 3 Jul 2024 17:00:51 +0100
Subject: [PATCH] [Sema][ObjC] Add warning options to detect bad name prefixes.

On Darwin, two-character prefixes are reserved for Apple, while three-
character prefixes are reserved for use by third party developers.
Breaking these rules in framework code can lead to symbol clashes,
which can result in unexpected behaviour.

This changeset adds three warning options, all turned off by default,
that cause Clang to warn the user if they breach the policy specified
by the warning options.  They are:

* `-Wobjc-prefix-length=`, which tests for a certain prefix length
  on every ObjC class or protocol
* `-Wobjc-prefixes=[,...]`, which requires that the prefix
  on every ObjC class or protocol be one from a specified list, and
* `-Wobjc-forbidden-prefixes=[,...]`, which lists prefixes
  that are explicitly disallowed.

These are also used to determine whether or not a prefix should be added
to a category method; if the class on which the category is being declared
does not match the permitted prefix settings, then the above warning
options will *also* require a prefix on any methods declared in that
category.

All of these warnings are disabled for system headers.

rdar://131055157
---
 clang/include/clang/Basic/DiagnosticGroups.td |   3 +
 .../clang/Basic/DiagnosticSemaKinds.td|  31 +++
 clang/include/clang/Basic/LangOptions.def |   2 +
 clang/include/clang/Basic/LangOptions.h   |   6 +
 clang/include/clang/Driver/Options.td |  19 ++
 clang/include/clang/Sema/SemaObjC.h   |   9 +
 clang/lib/Driver/ToolChains/Clang.cpp |   4 +
 clang/lib/Parse/ParseObjc.cpp |   1 +
 clang/lib/Sema/SemaDeclObjC.cpp   | 254 ++
 clang/lib/Sema/SemaObjCProperty.cpp   |  45 +++-
 clang/test/SemaObjC/prefixes.m| 129 +
 clang/test/SemaObjC/prefixes2.m   |  70 +
 12 files changed, 572 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaObjC/prefixes.m
 create mode 100644 clang/test/SemaObjC/prefixes2.m

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..7f3d4b463a8df 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -612,6 +612,9 @@ def ObjCPointerIntrospect : 
DiagGroup<"deprecated-objc-pointer-introspection", [
 def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">;
 def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">;
 def ObjCBoxing : DiagGroup<"objc-boxing">;
+def ObjCPrefixes : DiagGroup<"objc-prefixes">;
+def ObjCForbiddenPrefixes : DiagGroup<"objc-forbidden-prefixes">;
+def ObjCPrefixLength : DiagGroup<"objc-prefix-length">;
 def CompletionHandler : DiagGroup<"completion-handler">;
 def CalledOnceParameter : DiagGroup<"called-once-parameter", 
[CompletionHandler]>;
 def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 52ff4b026a60e..4e0c9f0fa2d06 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1419,6 +1419,37 @@ def warn_objc_property_retain_of_block : Warning<
 def warn_objc_readonly_property_has_setter : Warning<
 "setter cannot be specified for a readonly property">,
 InGroup;
+
+def warn_objc_unprefixed_class_name : Warning<
+  "un-prefixed Objective-C class name">,
+  InGroup;
+def warn_objc_unprefixed_protocol_name : Warning<
+  "un-prefixed Objective-C protocol name">,
+  InGroup;
+def warn_objc_unprefixed_category_method_name : Warning<
+  "un-prefixed Objective-C method name on category">,
+  InGroup;
+
+def warn_objc_bad_class_name_prefix : Warning<
+  "Objective-C class name prefix not in permitted list">,
+  InGroup;
+def warn_objc_bad_protocol_name_prefix : Warning<
+  "Objective-C protocol name prefix not in permitted list">,
+  InGroup;
+def warn_objc_bad_category_method_name_prefix : Warning<
+  "Objective-C category method name prefix not in permitted list">,
+  InGroup;
+
+def warn_objc_forbidden_class_name_prefix : Warning<
+  "Objective-C class name prefix in forbidden list">,
+  InGroup;
+def warn_objc_forbidden_protocol_name_prefix : Warning<
+  "Objective-C protocol name prefix in forbidden list">,
+  InGroup;
+def warn_objc_forbidden_category_method_name_prefix : Warning<
+  "Objective-C category method name prefix in forbidden list">,
+  InGroup;
+
 def warn_atomic_property_rule : Warning<
   "writable atomic property %0 cannot pair a synthesized 
%select{getter|setter}1 "
   "with a user defined %select{getter|setter}2">,
diff --git a/clang/include/clang/Basic/LangOptions.def

[clang] [Parser][ObjC] Add -Wobjc-prefix-length warning option. (PR #97597)

2024-07-18 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair edited 
https://github.com/llvm/llvm-project/pull/97597
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Sema][ObjC] Add warning options to detect bad name prefixes. (PR #97597)

2024-07-18 Thread Alastair Houghton via cfe-commits

https://github.com/al45tair edited 
https://github.com/llvm/llvm-project/pull/97597
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits