[clang] 15a3ae1 - [Clang] Add __STDCPP_THREADS__ to standard predefine macros
Zequan Wu via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 22 16:06:17 PST 2020
Author: Zequan Wu
Date: 2020-11-22T16:05:53-08:00
New Revision: 15a3ae1ab1a64cc62041c32ba54914a9dd7b8361
URL: https://github.com/llvm/llvm-project/commit/15a3ae1ab1a64cc62041c32ba54914a9dd7b8361
DIFF: https://github.com/llvm/llvm-project/commit/15a3ae1ab1a64cc62041c32ba54914a9dd7b8361.diff
LOG: [Clang] Add __STDCPP_THREADS__ to standard predefine macros
According to https://eel.is/c++draft/cpp.predefined#2.6, `__STDCPP_THREADS__` is a predefined macro.
Differential Revision: https://reviews.llvm.org/D91747
Added:
clang/test/CXX/cpp/cpp.predefined/p2.cpp
Modified:
clang/include/clang/Basic/CodeGenOptions.h
clang/include/clang/Basic/LangOptions.def
clang/include/clang/Basic/LangOptions.h
clang/lib/CodeGen/BackendUtil.cpp
clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/lib/Frontend/InitPreprocessor.cpp
clang/test/Preprocessor/init-aarch64.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h
index 6452d2bb25f6..e710c5792d76 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -211,9 +211,6 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// The name of the relocation model to use.
llvm::Reloc::Model RelocationModel;
- /// The thread model to use
- std::string ThreadModel;
-
/// If not an empty string, trap intrinsics are lowered to calls to this
/// function instead of to trap instructions.
std::string TrapFuncName;
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index 3788ae87f6b9..9c573b43049c 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -330,6 +330,7 @@ VALUE_LANGOPT(TrivialAutoVarInitStopAfter, 32, 0,
"stop trivial automatic variable initialization after the specified number of instances. Must be greater than 0.")
ENUM_LANGOPT(SignedOverflowBehavior, SignedOverflowBehaviorTy, 2, SOB_Undefined,
"signed integer overflow handling")
+ENUM_LANGOPT(ThreadModel , ThreadModelKind, 2, ThreadModelKind::POSIX, "Thread Model")
BENIGN_LANGOPT(ArrowDepth, 32, 256,
"maximum number of operator->s to follow")
diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index dea9d217cf0c..7806483ec5b5 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -232,6 +232,13 @@ class LangOptions : public LangOptionsBase {
BKey
};
+ enum class ThreadModelKind {
+ /// POSIX Threads.
+ POSIX,
+ /// Single Threaded Environment.
+ Single
+ };
+
public:
/// Set of enabled sanitizers.
SanitizerSet Sanitize;
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 816eaf3bf27e..243468598928 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -453,10 +453,14 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
const clang::TargetOptions &TargetOpts,
const LangOptions &LangOpts,
const HeaderSearchOptions &HSOpts) {
- Options.ThreadModel =
- llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
- .Case("posix", llvm::ThreadModel::POSIX)
- .Case("single", llvm::ThreadModel::Single);
+ switch (LangOpts.getThreadModel()) {
+ case LangOptions::ThreadModelKind::POSIX:
+ Options.ThreadModel = llvm::ThreadModel::POSIX;
+ break;
+ case LangOptions::ThreadModelKind::Single:
+ Options.ThreadModel = llvm::ThreadModel::Single;
+ break;
+ }
// Set float ABI type.
assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
diff --git a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp b/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
index 04bd6680e31c..de5c1a4c8f02 100644
--- a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
+++ b/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
@@ -49,7 +49,7 @@ class PCHContainerGenerator : public ASTConsumer {
const PreprocessorOptions &PreprocessorOpts;
CodeGenOptions CodeGenOpts;
const TargetOptions TargetOpts;
- const LangOptions LangOpts;
+ LangOptions LangOpts;
std::unique_ptr<llvm::LLVMContext> VMContext;
std::unique_ptr<llvm::Module> M;
std::unique_ptr<CodeGen::CodeGenModule> Builder;
@@ -147,7 +147,7 @@ class PCHContainerGenerator : public ASTConsumer {
// The debug info output isn't affected by CodeModel and
// ThreadModel, but the backend expects them to be nonempty.
CodeGenOpts.CodeModel = "default";
- CodeGenOpts.ThreadModel = "single";
+ LangOpts.setThreadModel(LangOptions::ThreadModelKind::Single);
CodeGenOpts.DebugTypeExtRefs = true;
// When building a module MainFileName is the name of the modulemap file.
CodeGenOpts.MainFileName =
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index a68d3aab897e..f6753b5b91a0 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1044,12 +1044,6 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
Opts.StrictVTablePointers = Args.hasArg(OPT_fstrict_vtable_pointers);
Opts.ForceEmitVTables = Args.hasArg(OPT_fforce_emit_vtables);
Opts.UnwindTables = Args.hasArg(OPT_munwind_tables);
- Opts.ThreadModel =
- std::string(Args.getLastArgValue(OPT_mthread_model, "posix"));
- if (Opts.ThreadModel != "posix" && Opts.ThreadModel != "single")
- Diags.Report(diag::err_drv_invalid_value)
- << Args.getLastArg(OPT_mthread_model)->getAsString(Args)
- << Opts.ThreadModel;
Opts.TrapFuncName = std::string(Args.getLastArgValue(OPT_ftrap_function_EQ));
Opts.UseInitArray = !Args.hasArg(OPT_fno_use_init_array);
@@ -3529,6 +3523,16 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
Args.hasFlag(OPT_fexperimental_relative_cxx_abi_vtables,
OPT_fno_experimental_relative_cxx_abi_vtables,
/*default=*/false);
+
+ std::string ThreadModel =
+ std::string(Args.getLastArgValue(OPT_mthread_model, "posix"));
+ if (ThreadModel != "posix" && ThreadModel != "single")
+ Diags.Report(diag::err_drv_invalid_value)
+ << Args.getLastArg(OPT_mthread_model)->getAsString(Args) << ThreadModel;
+ Opts.setThreadModel(
+ llvm::StringSwitch<LangOptions::ThreadModelKind>(ThreadModel)
+ .Case("posix", LangOptions::ThreadModelKind::POSIX)
+ .Case("single", LangOptions::ThreadModelKind::Single));
}
static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) {
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 08907fe2469c..86d5e61b7112 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -403,6 +403,12 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
Builder.defineMacro("__STDCPP_DEFAULT_NEW_ALIGNMENT__",
Twine(TI.getNewAlign() / TI.getCharWidth()) +
TI.getTypeConstantSuffix(TI.getSizeType()));
+
+ // -- __STDCPP_THREADS__
+ // Defined, and has the value integer literal 1, if and only if a
+ // program can have more than one thread of execution.
+ if (LangOpts.getThreadModel() == LangOptions::ThreadModelKind::POSIX)
+ Builder.defineMacro("__STDCPP_THREADS__", "1");
}
// In C11 these are environment macros. In C++11 they are only defined
diff --git a/clang/test/CXX/cpp/cpp.predefined/p2.cpp b/clang/test/CXX/cpp/cpp.predefined/p2.cpp
new file mode 100644
index 000000000000..0d6352166e5c
--- /dev/null
+++ b/clang/test/CXX/cpp/cpp.predefined/p2.cpp
@@ -0,0 +1,17 @@
+// RUN: split-file %s %t.dir
+// RUN: %clang_cc1 -verify %t.dir/defined.cpp
+// RUN: %clang_cc1 -verify -mthread-model posix %t.dir/defined.cpp
+// RUN: %clang_cc1 -verify -mthread-model single %t.dir/not-defined.cpp
+// RUN: %clang_cc1 -verify -x c %t.dir/not-defined.cpp
+
+//--- defined.cpp
+// expected-no-diagnostics
+#ifndef __STDCPP_THREADS__
+#error __STDCPP_THREADS__ is not defined in posix thread model.
+#endif
+
+//--- not-defined.cpp
+// expected-no-diagnostics
+#ifdef __STDCPP_THREADS__
+#error __STDCPP_THREADS__ is defined in single thread model.
+#endif
diff --git a/clang/test/Preprocessor/init-aarch64.c b/clang/test/Preprocessor/init-aarch64.c
index df2a6128989b..7f9ae3a5cf5b 100644
--- a/clang/test/Preprocessor/init-aarch64.c
+++ b/clang/test/Preprocessor/init-aarch64.c
@@ -233,6 +233,7 @@
// AARCH64-NEXT: #define __SIZE_TYPE__ long unsigned int
// AARCH64-NEXT: #define __SIZE_WIDTH__ 64
// AARCH64_CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
+// AARCH64_CXX: #define __STDCPP_THREADS__ 1
// AARCH64-NEXT: #define __STDC_HOSTED__ 1
// AARCH64-NEXT: #define __STDC_UTF_16__ 1
// AARCH64-NEXT: #define __STDC_UTF_32__ 1
More information about the cfe-commits
mailing list