Add basic support for -pthread.
- Patch by David Chisnall, with PCH and Darwin support mixed in.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80883 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticFrontendKinds.td b/include/clang/Basic/DiagnosticFrontendKinds.td
index ab6e2bc..a06042e 100644
--- a/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -87,8 +87,11 @@
"PCH file was compiled with builtins %select{enabled|disabled}0 but "
"builtins are currently %select{enabled|disabled}1">;
def warn_pch_thread_safe_statics : Error<
- "PCH file was compiled %select{without|with}0 thread-safe statics but"
+ "PCH file was compiled %select{without|with}0 thread-safe statics but "
"thread-safe statics are currently %select{disabled|enabled}1">;
+def warn_pch_posix_threads : Error<
+ "PCH file was compiled %select{without|with}0 POSIX thread support but "
+ "POSIX threads are currently %select{disabled|enabled}1">;
def warn_pch_blocks : Error<
"blocks were %select{disabled|enabled}0 in PCH file but "
"are currently %select{disabled|enabled}1">;
diff --git a/include/clang/Basic/LangOptions.h b/include/clang/Basic/LangOptions.h
index f162f2e..da702a8 100644
--- a/include/clang/Basic/LangOptions.h
+++ b/include/clang/Basic/LangOptions.h
@@ -54,6 +54,8 @@
unsigned ThreadsafeStatics : 1; // Whether static initializers are protected
// by locks.
+ unsigned POSIXThreads : 1; // Compiling with POSIX thread support
+ // (-pthread)
unsigned Blocks : 1; // block extension to C
unsigned EmitAllDecls : 1; // Emit all declarations, even if
// they are unused.
@@ -135,6 +137,7 @@
// FIXME: The default should be 1.
ThreadsafeStatics = 0;
+ POSIXThreads = 0;
Blocks = 0;
EmitAllDecls = 0;
MathErrno = 1;
diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp
index 305ca2d..d9cd42c 100644
--- a/lib/Basic/Targets.cpp
+++ b/lib/Basic/Targets.cpp
@@ -102,6 +102,9 @@
Define(Defs, "__STATIC__");
else
Define(Defs, "__DYNAMIC__");
+
+ if (Opts.POSIXThreads)
+ Define(Defs, "_REENTRANT", "1");
}
static void getDarwinOSXDefines(std::vector<char> &Defs,
@@ -272,6 +275,8 @@
DefineStd(Defs, "linux", Opts);
Define(Defs, "__gnu_linux__");
Define(Defs, "__ELF__", "1");
+ if (Opts.POSIXThreads)
+ Define(Defs, "_REENTRANT", "1");
}
public:
LinuxTargetInfo(const std::string& triple)
@@ -290,6 +295,8 @@
Define(Defs, "__NetBSD__", "1");
Define(Defs, "__unix__", "1");
Define(Defs, "__ELF__", "1");
+ if (Opts.POSIXThreads)
+ Define(Defs, "_POSIX_THREADS", "1");
}
public:
NetBSDTargetInfo(const std::string &triple)
@@ -309,6 +316,8 @@
Define(Defs, "__OpenBSD__", "1");
DefineStd(Defs, "unix", Opts);
Define(Defs, "__ELF__", "1");
+ if (Opts.POSIXThreads)
+ Define(Defs, "_POSIX_THREADS", "1");
}
public:
OpenBSDTargetInfo(const std::string &triple)
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index e93e642..b7357ec 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -517,6 +517,8 @@
Args.AddLastArg(CmdArgs, options::OPT_fvisibility_EQ);
Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
+ Args.AddLastArg(CmdArgs, options::OPT_pthread);
+
// Forward stack protector flags.
if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
options::OPT_fstack_protector_all,
diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp
index cb008c0..55dbd97 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -78,6 +78,7 @@
PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
diag::warn_pch_thread_safe_statics);
+ PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
PARSE_LANGOPT_BENIGN(EmitAllDecls);
PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp
index f664854..241cc11 100644
--- a/tools/clang-cc/clang-cc.cpp
+++ b/tools/clang-cc/clang-cc.cpp
@@ -397,6 +397,10 @@
llvm::cl::init(false));
static llvm::cl::opt<bool>
+PThread("pthread", llvm::cl::desc("Support POSIX threads in generated code"),
+ llvm::cl::init(false));
+
+static llvm::cl::opt<bool>
ObjCSenderDispatch("fobjc-sender-dependent-dispatch",
llvm::cl::desc("Enable sender-dependent dispatch for"
"Objective-C messages"), llvm::cl::init(false));
@@ -514,6 +518,9 @@
if (AltiVec)
Options.AltiVec = 1;
+
+ if (PThread)
+ Options.POSIXThreads = 1;
Options.setVisibilityMode(SymbolVisibility);
Options.OverflowChecking = OverflowChecking;