[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-03-09 Thread via cfe-commits

hoyhoy wrote:

cmake is broken as well.  It includes zlib internally.

https://gitlab.kitware.com/cmake/cmake/-/issues/25755

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-01-25 Thread Zixu Wang via cfe-commits

zixu-w wrote:

> Is the motivation for this change in behavior (on macOS) documented 
> somewhere? Besides zlib as already discussed above, pre-defining 
> `TARGET_OS_MAC` apparently at least also affects some libpng builds (and I 
> ultimately had to globally set `-fno-define-target-os-macros` for now to make 
> a test build of LibreOffice and all its included external libraries work).

I should be able to put up documentation/release note regarding the change.

IMO, we've also noticed the issue and upstreamed a fix for libpng 
(https://github.com/pnggroup/libpng/pull/529)

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-01-24 Thread via cfe-commits

stbergmann wrote:

Is the motivation for this change in behavior (on macOS) documented somewhere?  
Besides zlib as already discussed above, pre-defining `TARGET_OS_MAC` 
apparently at least also affects some libpng builds (and I ultimately had to 
globally set `-fno-define-target-os-macros` for now to make a test build of 
LibreOffice and all its included external libraries work).

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-01-19 Thread Mark Adler via cfe-commits

madler wrote:

> > We have provided a fix for zlib 
> > ([madler/zlib#895](https://github.com/madler/zlib/pull/895)) which was 
> > accepted by @madler.
> 
> But nowhere released, not even in the repository :(

This should be fixed in the zlib develop branch. Have you tried it?

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-01-19 Thread Florian Hahn via cfe-commits

fhahn wrote:

It looks like this may also break both Blender and Povray. Worked around build 
failures in SPEC2017 in llvm-test-suite in 
9b14a92d585657b87f2ca2b135ba9044685393be, but the projects will likely need to 
be fixed at the source.

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-01-17 Thread Zixu Wang via cfe-commits

zixu-w wrote:

> > We have provided a fix for zlib 
> > ([madler/zlib#895](https://github.com/madler/zlib/pull/895)) which was 
> > accepted by @madler.
> 
> But nowhere released, not even in the repository :(

Yeah... Not entirely familiar with the zlib development and contribution 
process, but looks like most of the PRs were just closed and changes committed 
separately some time after (presumably with a release?).

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-01-17 Thread Mike Hommey via cfe-commits

glandium wrote:

> We have provided a fix for zlib 
> ([madler/zlib#895](https://github.com/madler/zlib/pull/895)) which was 
> accepted by @madler.

But nowhere released, not even in the repository :(

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-01-17 Thread Zixu Wang via cfe-commits

zixu-w wrote:

> > It looks like this breaks building at least `MultiSource` from 
> > https://github.com/llvm/llvm-test-suite/. The first failure I see is when 
> > building `llvm-test-suite/MultiSource/Applications/ClamAV/zlib_zutil.c`
> > ```
> > In file included from 
> > /llvm-test-suite/MultiSource/Applications/ClamAV/zlib_zutil.c:10:
> > In file included from 
> > test-suites/llvm-test-suite/MultiSource/Applications/ClamAV/zlib/gzguts.h:21:
> > ../usr/include/stdio.h:220:7: error: expected identifier or '('
> >   220 | FILE*fdopen(int, const char *) 
> > __DARWIN_ALIAS_STARTING(__MAC_10_6, __IPHONE_2_0, __DARWIN_ALIAS(fdopen));
> >   |  ^
> > llvm-test-suite/MultiSource/Applications/ClamAV/zlib/zutil.h:140:33: note: 
> > expanded from macro 'fdopen'
> >   140 | #define fdopen(fd,mode) NULL /* No fdopen() */
> >   | ^
> > llvm-project/builds/release-with-assertions/ccache-stage1/lib/clang/18/include/__stddef_null.h:26:16:
> >  note: expanded from macro 'NULL'
> >26 | #define NULL ((void*)0)
> >   |^
> > ```
> 
> These are actually part of zlib, so apart from llvm test suite having been 
> broken by this (but was fixed), plain zlib has been broken too (although 
> arguably, zlib is where the real issue is).

We have provided a fix for zlib (https://github.com/madler/zlib/pull/895) which 
was accepted by @madler.

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2024-01-16 Thread Mike Hommey via cfe-commits

glandium wrote:

> It looks like this breaks building at least `MultiSource` from 
> https://github.com/llvm/llvm-test-suite/. The first failure I see is when 
> building `llvm-test-suite/MultiSource/Applications/ClamAV/zlib_zutil.c`
> 
> ```
> In file included from 
> /llvm-test-suite/MultiSource/Applications/ClamAV/zlib_zutil.c:10:
> In file included from 
> test-suites/llvm-test-suite/MultiSource/Applications/ClamAV/zlib/gzguts.h:21:
> ../usr/include/stdio.h:220:7: error: expected identifier or '('
>   220 | FILE*fdopen(int, const char *) 
> __DARWIN_ALIAS_STARTING(__MAC_10_6, __IPHONE_2_0, __DARWIN_ALIAS(fdopen));
>   |  ^
> llvm-test-suite/MultiSource/Applications/ClamAV/zlib/zutil.h:140:33: note: 
> expanded from macro 'fdopen'
>   140 | #define fdopen(fd,mode) NULL /* No fdopen() */
>   | ^
> llvm-project/builds/release-with-assertions/ccache-stage1/lib/clang/18/include/__stddef_null.h:26:16:
>  note: expanded from macro 'NULL'
>26 | #define NULL ((void*)0)
>   |^
> ```

These are actually part of zlib, so apart from llvm test suite having been 
broken by this (but was fixed), plain zlib has been broken too (although 
arguably, zlib is where the real issue is).

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-08 Thread Florian Hahn via cfe-commits

fhahn wrote:

Please also see the failures on GreenDragon: 
https://green.lab.llvm.org/green/job/lnt-ctmark-aarch64-Os/15893/console

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-08 Thread Florian Hahn via cfe-commits

fhahn wrote:

It looks like this breaks building at least `MultiSource` from 
https://github.com/llvm/llvm-test-suite/. The first failure I see is when 
building `llvm-test-suite/MultiSource/Applications/ClamAV/zlib_zutil.c`

```
In file included from 
/llvm-test-suite/MultiSource/Applications/ClamAV/zlib_zutil.c:10:
In file included from 
test-suites/llvm-test-suite/MultiSource/Applications/ClamAV/zlib/gzguts.h:21:
../usr/include/stdio.h:220:7: error: expected identifier or '('
  220 | FILE*fdopen(int, const char *) __DARWIN_ALIAS_STARTING(__MAC_10_6, 
__IPHONE_2_0, __DARWIN_ALIAS(fdopen));
  |  ^
llvm-test-suite/MultiSource/Applications/ClamAV/zlib/zutil.h:140:33: note: 
expanded from macro 'fdopen'
  140 | #define fdopen(fd,mode) NULL /* No fdopen() */
  | ^
llvm-project/builds/release-with-assertions/ccache-stage1/lib/clang/18/include/__stddef_null.h:26:16:
 note: expanded from macro 'NULL'
   26 | #define NULL ((void*)0)
  |^
```

I think it also breaks building PovRay from SPEC2017.

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-07 Thread Zixu Wang via cfe-commits

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-07 Thread Ian Anderson via cfe-commits

https://github.com/ian-twilightcoder approved this pull request.


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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-07 Thread Jon Roelofs via cfe-commits

https://github.com/jroelofs approved this pull request.


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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-07 Thread Zixu Wang via cfe-commits

https://github.com/zixu-w updated 
https://github.com/llvm/llvm-project/pull/74676

>From f02d0c7323fa8fb357bd0228f6746b7f878eaa59 Mon Sep 17 00:00:00 2001
From: Zixu Wang 
Date: Thu, 14 Sep 2023 17:06:24 -0700
Subject: [PATCH] [clang][PP] Add extension to predefine target OS macros

Add an extension feature `define-target-os-macros` that enables clang
to provide definitions of common TARGET_OS_* conditional macros.
The extension is enabled in the Darwin toolchain driver.
---
 clang/include/clang/Basic/Features.def|   2 +
 clang/include/clang/Basic/TargetOSMacros.def  |  55 
 clang/include/clang/Driver/Options.td |   3 +
 clang/include/clang/Lex/PreprocessorOptions.h |   3 +
 clang/lib/Driver/ToolChains/Clang.cpp |   3 +
 clang/lib/Driver/ToolChains/Darwin.cpp|   4 +
 clang/lib/Frontend/CompilerInvocation.cpp |   7 +
 clang/lib/Frontend/InitPreprocessor.cpp   |   9 +
 clang/test/Driver/fdefine-target-os-macros.c  | 241 ++
 9 files changed, 327 insertions(+)
 create mode 100644 clang/include/clang/Basic/TargetOSMacros.def
 create mode 100644 clang/test/Driver/fdefine-target-os-macros.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index adaf2e413f2f6d..df1eff8cbcc9f0 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -89,6 +89,8 @@ FEATURE(blocks, LangOpts.Blocks)
 FEATURE(c_thread_safety_attributes, true)
 FEATURE(cxx_exceptions, LangOpts.CXXExceptions)
 FEATURE(cxx_rtti, LangOpts.RTTI &)
+EXTENSION(define_target_os_macros,
+  PP.getPreprocessorOpts().DefineTargetOSMacros)
 FEATURE(enumerator_attributes, true)
 FEATURE(nullability, true)
 FEATURE(nullability_on_arrays, true)
diff --git a/clang/include/clang/Basic/TargetOSMacros.def 
b/clang/include/clang/Basic/TargetOSMacros.def
new file mode 100644
index 00..dfc2e033f6fd0d
--- /dev/null
+++ b/clang/include/clang/Basic/TargetOSMacros.def
@@ -0,0 +1,55 @@
+//===--- TargetOSMacros.def - Target OS macros --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file specifies the predefined TARGET_OS_* conditional macros.
+// A target macro `Name` should be defined if `Predicate` evaluates to true.
+// The macro expects `const llvm::Triple ` and the class `llvm::Triple`
+// to be available for the predicate.
+//
+//===--===//
+
+#ifndef TARGET_OS
+#define TARGET_OS(Name, Predicate)
+#endif
+
+// Windows targets.
+TARGET_OS(TARGET_OS_WIN32, Triple.isOSWindows())
+TARGET_OS(TARGET_OS_WINDOWS, Triple.isOSWindows())
+
+// Linux target.
+TARGET_OS(TARGET_OS_LINUX, Triple.isOSLinux())
+
+// Unix target.
+TARGET_OS(TARGET_OS_UNIX, Triple.isOSNetBSD() ||
+  Triple.isOSFreeBSD() ||
+  Triple.isOSOpenBSD() ||
+  Triple.isOSSolaris())
+
+// Apple (Mac) targets.
+TARGET_OS(TARGET_OS_MAC, Triple.isOSDarwin())
+TARGET_OS(TARGET_OS_OSX, Triple.isMacOSX())
+TARGET_OS(TARGET_OS_IPHONE, Triple.isiOS() || Triple.isTvOS() ||
+Triple.isWatchOS())
+// Triple::isiOS() also includes tvOS
+TARGET_OS(TARGET_OS_IOS, Triple.getOS() == llvm::Triple::IOS)
+TARGET_OS(TARGET_OS_TV, Triple.isTvOS())
+TARGET_OS(TARGET_OS_WATCH, Triple.isWatchOS())
+TARGET_OS(TARGET_OS_DRIVERKIT, Triple.isDriverKit())
+TARGET_OS(TARGET_OS_MACCATALYST, Triple.isMacCatalystEnvironment())
+TARGET_OS(TARGET_OS_SIMULATOR, Triple.isSimulatorEnvironment())
+
+// Deprecated Apple target conditionals.
+TARGET_OS(TARGET_OS_EMBEDDED, (Triple.isiOS() || Triple.isTvOS() \
+   || Triple.isWatchOS()) \
+   && !Triple.isMacCatalystEnvironment() \
+   && !Triple.isSimulatorEnvironment())
+TARGET_OS(TARGET_OS_NANO, Triple.isWatchOS())
+TARGET_OS(TARGET_IPHONE_SIMULATOR, Triple.isSimulatorEnvironment())
+TARGET_OS(TARGET_OS_UIKITFORMAC, Triple.isMacCatalystEnvironment())
+
+#undef TARGET_OS
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 0eec2b35263762..b959fd20fe413d 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1818,6 +1818,9 @@ def fcomment_block_commands : CommaJoined<["-"], 
"fcomment-block-commands=">, Gr
   Visibility<[ClangOption, CC1Option]>,
   HelpText<"Treat each comma separated argument in  as a documentation 
comment block command">,
   MetaVarName<"">, 
MarshallingInfoStringVector>;
+defm define_target_os_macros : OptInCC1FFlag<"define-target-os-macros",
+  "Enable", "Disable", " predefined target OS macros",

[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-07 Thread Jon Roelofs via cfe-commits


@@ -0,0 +1,131 @@
+// RUN: %clang -### --target=arm64-apple-darwin %s 2>&1 | FileCheck %s 
--check-prefix=DEFAULT-OPTION
+
+// RUN: %clang -dM -E --target=arm64-apple-macos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=1 \
+// RUN:-DIPHONE=0  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios-macabi %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=1 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-tvos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=1  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-tvos-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=1  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-watchos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=1   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-watchos-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=1   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-driverkit %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=0  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=1   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0
+
+// DEFAULT-OPTION: "-fdefine-target-os-macros"
+
+// CHECK-DAG: #define TARGET_OS_MAC [[MAC]]
+// CHECK-DAG: #define TARGET_OS_OSX [[OSX]]
+// CHECK-DAG: #define TARGET_OS_IPHONE [[IPHONE]]
+// CHECK-DAG: #define TARGET_OS_IOS [[IOS]]
+// CHECK-DAG: #define TARGET_OS_TV [[TV]]
+// CHECK-DAG: #define TARGET_OS_WATCH [[WATCH]]
+// CHECK-DAG: #define TARGET_OS_DRIVERKIT [[DRIVERKIT]]
+// CHECK-DAG: #define TARGET_OS_MACCATALYST [[MACCATALYST]]
+// CHECK-DAG: #define TARGET_OS_SIMULATOR [[SIMULATOR]]
+// Deprecated

[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-07 Thread Jon Roelofs via cfe-commits


@@ -0,0 +1,131 @@
+// RUN: %clang -### --target=arm64-apple-darwin %s 2>&1 | FileCheck %s 
--check-prefix=DEFAULT-OPTION
+
+// RUN: %clang -dM -E --target=arm64-apple-macos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=1 \
+// RUN:-DIPHONE=0  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios-macabi %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=1 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-tvos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=1  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-tvos-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=1  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-watchos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=1   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-watchos-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=1   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-driverkit %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=0  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=1   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0

jroelofs wrote:

TIL FileCheck can do `-D`... neat!

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-07 Thread Jon Roelofs via cfe-commits


@@ -1344,6 +1344,15 @@ static void InitializePredefinedMacros(const TargetInfo 
,
   if (TI.getTriple().isOSBinFormatELF())
 Builder.defineMacro("__ELF__");
 
+  // Target OS macro definitions.
+  if (PPOpts.DefineTargetOSMacros) {
+const llvm::Triple  = TI.getTriple();
+#define TARGET_OS(Name, Predicate) 
\
+  Builder.defineMacro(#Name, Predicate ? "1" : "0");

jroelofs wrote:

`Predicate` should be in parens to make the precedence explicit in the face of 
preprocessor expansion.

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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-06 Thread Ian Anderson via cfe-commits

https://github.com/ian-twilightcoder approved this pull request.


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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-06 Thread Ian Anderson via cfe-commits


@@ -0,0 +1,131 @@
+// RUN: %clang -### --target=arm64-apple-darwin %s 2>&1 | FileCheck %s 
--check-prefix=DEFAULT-OPTION
+
+// RUN: %clang -dM -E --target=arm64-apple-macos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=1 \
+// RUN:-DIPHONE=0  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios-macabi %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=1 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-ios-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=1 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-tvos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=1  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-tvos-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=1  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-watchos %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=1   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=1\
+// RUN:-DSIMULATOR=0
+
+// RUN: %clang -dM -E --target=arm64-apple-watchos-simulator %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=1  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=1   \
+// RUN:-DDRIVERKIT=0   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=1
+
+// RUN: %clang -dM -E --target=arm64-apple-driverkit %s 2>&1 \
+// RUN: | FileCheck %s -DMAC=1 \
+// RUN:-DOSX=0 \
+// RUN:-DIPHONE=0  \
+// RUN:-DIOS=0 \
+// RUN:-DTV=0  \
+// RUN:-DWATCH=0   \
+// RUN:-DDRIVERKIT=1   \
+// RUN:-DMACCATALYST=0 \
+// RUN:-DEMBEDDED=0\
+// RUN:-DSIMULATOR=0
+
+// DEFAULT-OPTION: "-fdefine-target-os-macros"
+
+// CHECK-DAG: #define TARGET_OS_MAC [[MAC]]
+// CHECK-DAG: #define TARGET_OS_OSX [[OSX]]
+// CHECK-DAG: #define TARGET_OS_IPHONE [[IPHONE]]
+// CHECK-DAG: #define TARGET_OS_IOS [[IOS]]
+// CHECK-DAG: #define TARGET_OS_TV [[TV]]
+// CHECK-DAG: #define TARGET_OS_WATCH [[WATCH]]
+// CHECK-DAG: #define TARGET_OS_DRIVERKIT [[DRIVERKIT]]
+// CHECK-DAG: #define TARGET_OS_MACCATALYST [[MACCATALYST]]
+// CHECK-DAG: #define TARGET_OS_SIMULATOR [[SIMULATOR]]
+// Deprecated

[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-06 Thread Zixu Wang via cfe-commits

https://github.com/zixu-w updated 
https://github.com/llvm/llvm-project/pull/74676

>From cdc7cf721a7d4a6bf318181ea3f37adf7c571ed1 Mon Sep 17 00:00:00 2001
From: Zixu Wang 
Date: Thu, 14 Sep 2023 17:06:24 -0700
Subject: [PATCH] [clang][PP] Add extension to predefine target OS macros

Add an extension feature `define-target-os-macros` that enables clang
to provide definitions of common TARGET_OS_* conditional macros.
The extension is enabled in the Darwin toolchain driver.
---
 clang/include/clang/Basic/Features.def|   2 +
 clang/include/clang/Basic/TargetOSMacros.def  |  55 
 clang/include/clang/Driver/Options.td |   3 +
 clang/include/clang/Lex/PreprocessorOptions.h |   3 +
 clang/lib/Driver/ToolChains/Darwin.cpp|   4 +
 clang/lib/Frontend/CompilerInvocation.cpp |   7 +
 clang/lib/Frontend/InitPreprocessor.cpp   |   9 ++
 .../Driver/darwin-fdefine-target-os-macros.c  | 131 ++
 8 files changed, 214 insertions(+)
 create mode 100644 clang/include/clang/Basic/TargetOSMacros.def
 create mode 100644 clang/test/Driver/darwin-fdefine-target-os-macros.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index adaf2e413f2f6d..df1eff8cbcc9f0 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -89,6 +89,8 @@ FEATURE(blocks, LangOpts.Blocks)
 FEATURE(c_thread_safety_attributes, true)
 FEATURE(cxx_exceptions, LangOpts.CXXExceptions)
 FEATURE(cxx_rtti, LangOpts.RTTI &)
+EXTENSION(define_target_os_macros,
+  PP.getPreprocessorOpts().DefineTargetOSMacros)
 FEATURE(enumerator_attributes, true)
 FEATURE(nullability, true)
 FEATURE(nullability_on_arrays, true)
diff --git a/clang/include/clang/Basic/TargetOSMacros.def 
b/clang/include/clang/Basic/TargetOSMacros.def
new file mode 100644
index 00..dfc2e033f6fd0d
--- /dev/null
+++ b/clang/include/clang/Basic/TargetOSMacros.def
@@ -0,0 +1,55 @@
+//===--- TargetOSMacros.def - Target OS macros --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file specifies the predefined TARGET_OS_* conditional macros.
+// A target macro `Name` should be defined if `Predicate` evaluates to true.
+// The macro expects `const llvm::Triple ` and the class `llvm::Triple`
+// to be available for the predicate.
+//
+//===--===//
+
+#ifndef TARGET_OS
+#define TARGET_OS(Name, Predicate)
+#endif
+
+// Windows targets.
+TARGET_OS(TARGET_OS_WIN32, Triple.isOSWindows())
+TARGET_OS(TARGET_OS_WINDOWS, Triple.isOSWindows())
+
+// Linux target.
+TARGET_OS(TARGET_OS_LINUX, Triple.isOSLinux())
+
+// Unix target.
+TARGET_OS(TARGET_OS_UNIX, Triple.isOSNetBSD() ||
+  Triple.isOSFreeBSD() ||
+  Triple.isOSOpenBSD() ||
+  Triple.isOSSolaris())
+
+// Apple (Mac) targets.
+TARGET_OS(TARGET_OS_MAC, Triple.isOSDarwin())
+TARGET_OS(TARGET_OS_OSX, Triple.isMacOSX())
+TARGET_OS(TARGET_OS_IPHONE, Triple.isiOS() || Triple.isTvOS() ||
+Triple.isWatchOS())
+// Triple::isiOS() also includes tvOS
+TARGET_OS(TARGET_OS_IOS, Triple.getOS() == llvm::Triple::IOS)
+TARGET_OS(TARGET_OS_TV, Triple.isTvOS())
+TARGET_OS(TARGET_OS_WATCH, Triple.isWatchOS())
+TARGET_OS(TARGET_OS_DRIVERKIT, Triple.isDriverKit())
+TARGET_OS(TARGET_OS_MACCATALYST, Triple.isMacCatalystEnvironment())
+TARGET_OS(TARGET_OS_SIMULATOR, Triple.isSimulatorEnvironment())
+
+// Deprecated Apple target conditionals.
+TARGET_OS(TARGET_OS_EMBEDDED, (Triple.isiOS() || Triple.isTvOS() \
+   || Triple.isWatchOS()) \
+   && !Triple.isMacCatalystEnvironment() \
+   && !Triple.isSimulatorEnvironment())
+TARGET_OS(TARGET_OS_NANO, Triple.isWatchOS())
+TARGET_OS(TARGET_IPHONE_SIMULATOR, Triple.isSimulatorEnvironment())
+TARGET_OS(TARGET_OS_UIKITFORMAC, Triple.isMacCatalystEnvironment())
+
+#undef TARGET_OS
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 0eec2b35263762..b959fd20fe413d 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1818,6 +1818,9 @@ def fcomment_block_commands : CommaJoined<["-"], 
"fcomment-block-commands=">, Gr
   Visibility<[ClangOption, CC1Option]>,
   HelpText<"Treat each comma separated argument in  as a documentation 
comment block command">,
   MetaVarName<"">, 
MarshallingInfoStringVector>;
+defm define_target_os_macros : OptInCC1FFlag<"define-target-os-macros",
+  "Enable", "Disable", " predefined target OS macros",
+  [ClangOption, CC1Option]>;
 def 

[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-06 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 23d402e5b705d98463150302a55623951284b5f2 
2812f69940451b810b8aa9a580e98a4a22a89967 -- 
clang/test/Driver/darwin-fdefine-target-os-macros.c 
clang/include/clang/Lex/PreprocessorOptions.h 
clang/lib/Driver/ToolChains/Darwin.cpp 
clang/lib/Frontend/CompilerInvocation.cpp 
clang/lib/Frontend/InitPreprocessor.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp 
b/clang/lib/Frontend/InitPreprocessor.cpp
index 320894f33f..cb82844808 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1348,7 +1348,7 @@ static void InitializePredefinedMacros(const TargetInfo 
,
   if (PPOpts.DefineTargetOSMacros) {
 const llvm::Triple  = TI.getTriple();
 #define TARGET_OS(Name, Predicate) 
\
-Builder.defineMacro(#Name, Predicate ? "1" : "0");
+  Builder.defineMacro(#Name, Predicate ? "1" : "0");
 #include "clang/Basic/TargetOSMacros.def"
 #undef TARGET_OS
   }

``




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


[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-06 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-driver

Author: Zixu Wang (zixu-w)


Changes

Add an extension feature `define-target-os-macros` that enables clang to 
provide definitions of common TARGET_OS_* conditional macros. The extension is 
enabled in the Darwin toolchain driver.

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


8 Files Affected:

- (modified) clang/include/clang/Basic/Features.def (+2) 
- (added) clang/include/clang/Basic/TargetOSMacros.def (+55) 
- (modified) clang/include/clang/Driver/Options.td (+3) 
- (modified) clang/include/clang/Lex/PreprocessorOptions.h (+3) 
- (modified) clang/lib/Driver/ToolChains/Darwin.cpp (+4) 
- (modified) clang/lib/Frontend/CompilerInvocation.cpp (+7) 
- (modified) clang/lib/Frontend/InitPreprocessor.cpp (+9) 
- (added) clang/test/Driver/darwin-fdefine-target-os-macros.c (+131) 


``diff
diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index adaf2e413f2f6..df1eff8cbcc9f 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -89,6 +89,8 @@ FEATURE(blocks, LangOpts.Blocks)
 FEATURE(c_thread_safety_attributes, true)
 FEATURE(cxx_exceptions, LangOpts.CXXExceptions)
 FEATURE(cxx_rtti, LangOpts.RTTI &)
+EXTENSION(define_target_os_macros,
+  PP.getPreprocessorOpts().DefineTargetOSMacros)
 FEATURE(enumerator_attributes, true)
 FEATURE(nullability, true)
 FEATURE(nullability_on_arrays, true)
diff --git a/clang/include/clang/Basic/TargetOSMacros.def 
b/clang/include/clang/Basic/TargetOSMacros.def
new file mode 100644
index 0..dfc2e033f6fd0
--- /dev/null
+++ b/clang/include/clang/Basic/TargetOSMacros.def
@@ -0,0 +1,55 @@
+//===--- TargetOSMacros.def - Target OS macros --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file specifies the predefined TARGET_OS_* conditional macros.
+// A target macro `Name` should be defined if `Predicate` evaluates to true.
+// The macro expects `const llvm::Triple ` and the class `llvm::Triple`
+// to be available for the predicate.
+//
+//===--===//
+
+#ifndef TARGET_OS
+#define TARGET_OS(Name, Predicate)
+#endif
+
+// Windows targets.
+TARGET_OS(TARGET_OS_WIN32, Triple.isOSWindows())
+TARGET_OS(TARGET_OS_WINDOWS, Triple.isOSWindows())
+
+// Linux target.
+TARGET_OS(TARGET_OS_LINUX, Triple.isOSLinux())
+
+// Unix target.
+TARGET_OS(TARGET_OS_UNIX, Triple.isOSNetBSD() ||
+  Triple.isOSFreeBSD() ||
+  Triple.isOSOpenBSD() ||
+  Triple.isOSSolaris())
+
+// Apple (Mac) targets.
+TARGET_OS(TARGET_OS_MAC, Triple.isOSDarwin())
+TARGET_OS(TARGET_OS_OSX, Triple.isMacOSX())
+TARGET_OS(TARGET_OS_IPHONE, Triple.isiOS() || Triple.isTvOS() ||
+Triple.isWatchOS())
+// Triple::isiOS() also includes tvOS
+TARGET_OS(TARGET_OS_IOS, Triple.getOS() == llvm::Triple::IOS)
+TARGET_OS(TARGET_OS_TV, Triple.isTvOS())
+TARGET_OS(TARGET_OS_WATCH, Triple.isWatchOS())
+TARGET_OS(TARGET_OS_DRIVERKIT, Triple.isDriverKit())
+TARGET_OS(TARGET_OS_MACCATALYST, Triple.isMacCatalystEnvironment())
+TARGET_OS(TARGET_OS_SIMULATOR, Triple.isSimulatorEnvironment())
+
+// Deprecated Apple target conditionals.
+TARGET_OS(TARGET_OS_EMBEDDED, (Triple.isiOS() || Triple.isTvOS() \
+   || Triple.isWatchOS()) \
+   && !Triple.isMacCatalystEnvironment() \
+   && !Triple.isSimulatorEnvironment())
+TARGET_OS(TARGET_OS_NANO, Triple.isWatchOS())
+TARGET_OS(TARGET_IPHONE_SIMULATOR, Triple.isSimulatorEnvironment())
+TARGET_OS(TARGET_OS_UIKITFORMAC, Triple.isMacCatalystEnvironment())
+
+#undef TARGET_OS
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 0eec2b3526376..b959fd20fe413 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1818,6 +1818,9 @@ def fcomment_block_commands : CommaJoined<["-"], 
"fcomment-block-commands=">, Gr
   Visibility<[ClangOption, CC1Option]>,
   HelpText<"Treat each comma separated argument in  as a documentation 
comment block command">,
   MetaVarName<"">, 
MarshallingInfoStringVector>;
+defm define_target_os_macros : OptInCC1FFlag<"define-target-os-macros",
+  "Enable", "Disable", " predefined target OS macros",
+  [ClangOption, CC1Option]>;
 def fparse_all_comments : Flag<["-"], "fparse-all-comments">, 
Group,
   Visibility<[ClangOption, CC1Option]>,
   MarshallingInfoFlag>;
diff --git a/clang/include/clang/Lex/PreprocessorOptions.h 
b/clang/include/clang/Lex/PreprocessorOptions.h
index 

[clang] [clang][PP] Add extension to predefine target OS macros (PR #74676)

2023-12-06 Thread Zixu Wang via cfe-commits

https://github.com/zixu-w created 
https://github.com/llvm/llvm-project/pull/74676

Add an extension feature `define-target-os-macros` that enables clang to 
provide definitions of common TARGET_OS_* conditional macros. The extension is 
enabled in the Darwin toolchain driver.

>From 2812f69940451b810b8aa9a580e98a4a22a89967 Mon Sep 17 00:00:00 2001
From: Zixu Wang 
Date: Thu, 14 Sep 2023 17:06:24 -0700
Subject: [PATCH] [clang][PP] Add extension to predefine target OS macros

Add an extension feature `define-target-os-macros` that enables clang
to provide definitions of common TARGET_OS_* conditional macros.
The extension is enabled in the Darwin toolchain driver.
---
 clang/include/clang/Basic/Features.def|   2 +
 clang/include/clang/Basic/TargetOSMacros.def  |  55 
 clang/include/clang/Driver/Options.td |   3 +
 clang/include/clang/Lex/PreprocessorOptions.h |   3 +
 clang/lib/Driver/ToolChains/Darwin.cpp|   4 +
 clang/lib/Frontend/CompilerInvocation.cpp |   7 +
 clang/lib/Frontend/InitPreprocessor.cpp   |   9 ++
 .../Driver/darwin-fdefine-target-os-macros.c  | 131 ++
 8 files changed, 214 insertions(+)
 create mode 100644 clang/include/clang/Basic/TargetOSMacros.def
 create mode 100644 clang/test/Driver/darwin-fdefine-target-os-macros.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index adaf2e413f2f6..df1eff8cbcc9f 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -89,6 +89,8 @@ FEATURE(blocks, LangOpts.Blocks)
 FEATURE(c_thread_safety_attributes, true)
 FEATURE(cxx_exceptions, LangOpts.CXXExceptions)
 FEATURE(cxx_rtti, LangOpts.RTTI &)
+EXTENSION(define_target_os_macros,
+  PP.getPreprocessorOpts().DefineTargetOSMacros)
 FEATURE(enumerator_attributes, true)
 FEATURE(nullability, true)
 FEATURE(nullability_on_arrays, true)
diff --git a/clang/include/clang/Basic/TargetOSMacros.def 
b/clang/include/clang/Basic/TargetOSMacros.def
new file mode 100644
index 0..dfc2e033f6fd0
--- /dev/null
+++ b/clang/include/clang/Basic/TargetOSMacros.def
@@ -0,0 +1,55 @@
+//===--- TargetOSMacros.def - Target OS macros --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file specifies the predefined TARGET_OS_* conditional macros.
+// A target macro `Name` should be defined if `Predicate` evaluates to true.
+// The macro expects `const llvm::Triple ` and the class `llvm::Triple`
+// to be available for the predicate.
+//
+//===--===//
+
+#ifndef TARGET_OS
+#define TARGET_OS(Name, Predicate)
+#endif
+
+// Windows targets.
+TARGET_OS(TARGET_OS_WIN32, Triple.isOSWindows())
+TARGET_OS(TARGET_OS_WINDOWS, Triple.isOSWindows())
+
+// Linux target.
+TARGET_OS(TARGET_OS_LINUX, Triple.isOSLinux())
+
+// Unix target.
+TARGET_OS(TARGET_OS_UNIX, Triple.isOSNetBSD() ||
+  Triple.isOSFreeBSD() ||
+  Triple.isOSOpenBSD() ||
+  Triple.isOSSolaris())
+
+// Apple (Mac) targets.
+TARGET_OS(TARGET_OS_MAC, Triple.isOSDarwin())
+TARGET_OS(TARGET_OS_OSX, Triple.isMacOSX())
+TARGET_OS(TARGET_OS_IPHONE, Triple.isiOS() || Triple.isTvOS() ||
+Triple.isWatchOS())
+// Triple::isiOS() also includes tvOS
+TARGET_OS(TARGET_OS_IOS, Triple.getOS() == llvm::Triple::IOS)
+TARGET_OS(TARGET_OS_TV, Triple.isTvOS())
+TARGET_OS(TARGET_OS_WATCH, Triple.isWatchOS())
+TARGET_OS(TARGET_OS_DRIVERKIT, Triple.isDriverKit())
+TARGET_OS(TARGET_OS_MACCATALYST, Triple.isMacCatalystEnvironment())
+TARGET_OS(TARGET_OS_SIMULATOR, Triple.isSimulatorEnvironment())
+
+// Deprecated Apple target conditionals.
+TARGET_OS(TARGET_OS_EMBEDDED, (Triple.isiOS() || Triple.isTvOS() \
+   || Triple.isWatchOS()) \
+   && !Triple.isMacCatalystEnvironment() \
+   && !Triple.isSimulatorEnvironment())
+TARGET_OS(TARGET_OS_NANO, Triple.isWatchOS())
+TARGET_OS(TARGET_IPHONE_SIMULATOR, Triple.isSimulatorEnvironment())
+TARGET_OS(TARGET_OS_UIKITFORMAC, Triple.isMacCatalystEnvironment())
+
+#undef TARGET_OS
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 0eec2b3526376..b959fd20fe413 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1818,6 +1818,9 @@ def fcomment_block_commands : CommaJoined<["-"], 
"fcomment-block-commands=">, Gr
   Visibility<[ClangOption, CC1Option]>,
   HelpText<"Treat each comma separated argument in  as a documentation 
comment block command">,
   MetaVarName<"">,