Make ERROR_IF_USED macro work with GCC <= 4.2, Apple GCCs
[oota-llvm.git] / include / llvm / Support / Compiler.h
1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines several macros, based on the current compiler.  This allows
11 // use of compiler-specific features in a way that remains portable.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
17
18 // The VISIBILITY_HIDDEN macro, used for marking classes with the GCC-specific
19 // visibility("hidden") attribute.
20 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
21 #define VISIBILITY_HIDDEN __attribute__ ((visibility("hidden")))
22 #else
23 #define VISIBILITY_HIDDEN
24 #endif
25
26 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
27 #define ATTRIBUTE_USED __attribute__((__used__))
28 #else
29 #define ATTRIBUTE_USED
30 #endif
31
32 #ifdef __GNUC__ // aka 'ATTRIBUTE_CONST' but following LLVM Conventions.
33 #define ATTRIBUTE_READNONE __attribute__((__const__))
34 #else
35 #define ATTRIBUTE_READNONE
36 #endif
37
38 #ifdef __GNUC__  // aka 'ATTRIBUTE_PURE' but following LLVM Conventions.
39 #define ATTRIBUTE_READONLY __attribute__((__pure__))
40 #else
41 #define ATTRIBUTE_READONLY
42 #endif
43
44 #if (__GNUC__ >= 4)
45 #define BUILTIN_EXPECT(EXPR, VALUE) __builtin_expect((EXPR), (VALUE))
46 #else
47 #define BUILTIN_EXPECT(EXPR, VALUE) (EXPR)
48 #endif
49
50 // C++ doesn't support 'extern template' of template specializations.  GCC does,
51 // but requires __extension__ before it.  In the header, use this:
52 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
53 // in the .cpp file, use this:
54 //   TEMPLATE_INSTANTIATION(class foo<bar>);
55 #ifdef __GNUC__
56 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
57 #define TEMPLATE_INSTANTIATION(X) template X
58 #else
59 #define EXTERN_TEMPLATE_INSTANTIATION(X)
60 #define TEMPLATE_INSTANTIATION(X)
61 #endif
62
63 // DISABLE_INLINE - On compilers where we have a directive to do so, mark a
64 // method "not for inlining".
65 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
66 #define DISABLE_INLINE __attribute__((noinline))
67 #elif defined(_MSC_VER)
68 #define DISABLE_INLINE __declspec(noinline)
69 #else
70 #define DISABLE_INLINE
71 #endif
72
73 #ifdef __GNUC__
74 #define NORETURN __attribute__((noreturn))
75 #elif defined(_MSC_VER)
76 #define NORETURN __declspec(noreturn)
77 #else
78 #define NORETURN
79 #endif
80
81 #if defined(__GNUC__) && ((__GNUC__ > 4)||(__GNUC__ == 4 && __GNUC_MINOR__ > 2))
82 #define ERROR_IF_USED __attribute__((error("wrong usage")))
83 #elif defined(__APPLE__)
84 #define ERROR_IF_USED __attribute__((unavailable))
85 #else
86 #define ERROR_IF_USED
87 #endif
88
89 #endif