From a53767ea228792321b1549620e747b8c0df0d0c9 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 17 Dec 2012 08:52:05 +0000 Subject: [PATCH] tsan: add __has_feature(thread_sanitizer) llvm-svn: 170314 --- clang/docs/ThreadSanitizer.rst | 14 ++++++++++++++ clang/lib/Lex/PPMacroExpansion.cpp | 1 + clang/test/Lexer/has_feature_thread_sanitizer.cpp | 11 +++++++++++ 3 files changed, 26 insertions(+) create mode 100644 clang/test/Lexer/has_feature_thread_sanitizer.cpp diff --git a/clang/docs/ThreadSanitizer.rst b/clang/docs/ThreadSanitizer.rst index e434062..4cf0e37 100644 --- a/clang/docs/ThreadSanitizer.rst +++ b/clang/docs/ThreadSanitizer.rst @@ -68,6 +68,20 @@ Currently, ThreadSanitizer symbolizes its output using an external #0 pthread_create tsan_interceptors.cc:705 (exe+0x00000000c790) #1 main tiny_race.c:9 (exe+0x00000000a3a4) +``__has_feature(thread_sanitizer)`` +------------------------------------ + +In some cases one may need to execute different code depending on whether +ThreadSanitizer is enabled. +:ref:`\_\_has\_feature ` can be used for +this purpose. + +.. code-block:: c + + #if defined(__has_feature) && __has_feature(thread_sanitizer) + // code that builds only under ThreadSanitizer + #endif + Limitations ----------- diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 5582100..941c2d4 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -780,6 +780,7 @@ static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) { .Case("cxx_exceptions", LangOpts.Exceptions) .Case("cxx_rtti", LangOpts.RTTI) .Case("enumerator_attributes", true) + .Case("thread_sanitizer", LangOpts.SanitizeThread) // Objective-C features .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? .Case("objc_arc", LangOpts.ObjCAutoRefCount) diff --git a/clang/test/Lexer/has_feature_thread_sanitizer.cpp b/clang/test/Lexer/has_feature_thread_sanitizer.cpp new file mode 100644 index 0000000..0a24810 --- /dev/null +++ b/clang/test/Lexer/has_feature_thread_sanitizer.cpp @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -E -fsanitize=thread %s -o - | FileCheck --check-prefix=CHECK-TSAN %s +// RUN: %clang_cc1 -E %s -o - | FileCheck --check-prefix=CHECK-NO-TSAN %s + +#if __has_feature(thread_sanitizer) +int ThreadSanitizerEnabled(); +#else +int ThreadSanitizerDisabled(); +#endif + +// CHECK-TSAN: ThreadSanitizerEnabled +// CHECK-NO-TSAN: ThreadSanitizerDisabled -- 2.7.4