天天看點

使用Clang作為編譯器 —— Clang 中的屬性

Clang 中的屬性(待完成)

      • 1. 介紹
      • 2. 函數屬性
          • 2.1 #pragma omp declare simd
          • 2.2 #pragma omp declare target
          • 2.3 _Noreturn
          • 2.4 abi_tag
          • 2.5 acquire_capability, acquire_shared_capability
          • 2.6 alloc_align
          • 2.7 alloc_size
          • 2.8 allocator
          • 2.9 artificial
          • 2.10 assert_capability, assert_shared_capability
          • 2.11 assume_aligned
          • 2.12 availability
          • 2.13 callback
          • 2.14 carries_dependency
          • 2.15 cf_consumed
          • 2.16 cf_returns_not_retained
          • 2.17 cf_returns_retained
          • 2.18 code_seg
          • 2.19 convergent
          • 2.20 cpu_dispatch
          • 2.21 cpu_specific
          • 2.22 diagnose_if
          • 2.23 disable_tail_calls
          • 2.24 enable_if
          • 2.25 exclude_from_explicit_instantiation
          • 2.26 flatten
          • 2.27 force_align_arg_pointer
          • 2.28 format
          • 2.29 gnu_inline
          • 2.30 ifunc
          • 2.31 import_module
          • 2.32 import_name
          • 2.33 internal_linkage
          • 2.34 interrupt (ARM)
          • 2.35 interrupt (AVR)
          • 2.36 interrupt (MIPS)
          • 2.37 interrupt (RISCV)
          • 2.38 kernel
          • 2.39 lifetimebound
          • 2.40 long_call, far
          • 2.41 micromips
          • 2.42 mig_server_routine
          • 2.43 min_vector_width
          • 2.44 no_caller_saved_registers
          • 2.45 no_sanitize
          • 2.46 no_sanitize_address, no_address_safety_analysis
          • 2.47 no_sanitize_memory
          • 2.48 no_sanitize_thread
          • 2.49 no_speculative_load_hardening
          • 2.50 no_split_stack
          • 2.51 no_stack_protector
          • 2.52 noalias
          • 2.53 nocf_check
          • 2.54 nodiscard, warn_unused_result
          • 2.55 noduplicate
          • 2.56 nomicromips
          • 2.57 noreturn
          • 2.58 not_tail_called
          • 2.59 nothrow
          • 2.60 ns_consumed
          • 2.61 ns_consumes_self
          • 2.62 ns_returns_autoreleased
          • 2.63 ns_returns_not_retained
          • 2.64 ns_returns_retained
          • 2.65 objc_method_family
          • 2.66 objc_requires_super
          • 2.67 optnone
          • 2.68 os_consumed
          • 2.69 os_consumes_this
          • 2.70 os_returns_not_retained
          • 2.71 os_returns_retained
          • 2.72 os_returns_retained_on_non_zero
          • 2.73 os_returns_retained_on_zero
          • 2.74 overloadable
          • 2.75 reinitializes
          • 2.76 release_capability, release_shared_capability
          • 2.77 short_call, near
          • 2.78 signal
          • 2.79 speculative_load_hardening
          • 2.80 target
          • 2.81 try_acquire_capability, try_acquire_shared_capability
          • 2.82 xray_always_instrument, xray_never_instrument, xray_log_args
          • 2.83 xray_always_instrument, xray_never_instrument, xray_log_args
      • 3. 變量屬性
          • 3.1 always_destroy
          • 3.2 dllexport
          • 3.3 dllimport
          • 3.4 init_seg
          • 3.5 maybe_unused, unused
          • 3.6 no_destroy
          • 3.7 nodebug
          • 3.8 noescape
          • 3.9 nosvm
          • 3.10 objc_externally_retained
          • 3.11 pass_object_size, pass_dynamic_object_size
          • 3.12 require_constant_initialization
          • 3.13 section, __declspec(allocate)
          • 3.14 swift_context
          • 3.15 swift_error_result
          • 3.16 swift_indirect_result
          • 3.17 swiftcall
          • 3.18 thread
          • 3.19 tls_model
          • 3.20 uninitialized

本文為譯文,點選 此處檢視原文。

1. 介紹

這個頁面列出了 Clang 目前支援的屬性。

2. 函數屬性

pragma:編譯訓示

declare:聲明

SIMD:單指令多資料結構(Single Instruction Multiple Data)

declspec:關鍵字詳細用法

2.1 #pragma omp declare simd
  • 支援文法
GNU C++11 C2x __declspec Keyword #pragma #pragma clang attribute
omp declare simd

可以将

declare simd

構造應用于一個函數,以支援建立一個或多個版本,這些版本可以使用來着一個

SIMD

循環中單個調用的

SIMD

指令處理多個參數。

declare simd

訓示是一個聲明性訓示。一個函數可能有多個

declare simd

訓示。對函數使用

declare simd

構造可以建立關聯函數的

SIMD

版本,該版本可用于同時處理來自一個

SIMD

循環的單個調用的多個參數。

declare simd

構造的文法如下:

#pragma omp declare simd [clause[[,] clause] ...] new-line
[#pragma omp declare simd [clause[[,] clause] ...] new-line]
[...]
function definition or declaration
           

這裡的

clause

是下列

clause

之一:

simdlen(length)
linear(argument-list[:constant-linear-step])
aligned(argument-list[:alignment])
uniform(argument-list)
inbranch
notinbranch
           
2.2 #pragma omp declare target
  • 支援文法
GNU C++11 C2x __declspec Keyword #pragma #pragma clang attribute
omp declare target

declare target

訓示指定将變量和函數映射到一個用于 OpenMP offload 機制的裝置。

declare target

訓示的文法如下:

#pragma omp declare target new-line
declarations-definition-seq
#pragma omp end declare target new-line
           
2.3 _Noreturn
  • 支援文法
GNU C++11 C2x __declspec Keyword #pragma #pragma clang attribute
_Noreturn

聲明為

_Noreturn

的函數不會傳回給調用者。編譯器将為聲明為

_Noreturn

的函數生成診斷(diagnostic),該診斷似乎能夠傳回給調用者。

2.4 abi_tag
  • 支援文法
GNU C++11 C2x __declspec Keyword #pragma #pragma clang attribute

abi_tag

gnu::abi_tag

abi_tag

屬性可以應用于函數、變量、類或内聯命名空間聲明,以修改實體修飾過的名稱。它能夠區分同一實體的不同版本,但是支援不同的ABI版本。例如,一個類的較新版本可以有一組不同的資料成員,是以具有不同的大小。使用

abi_tag

屬性,可以為類類型的一個全局變量使用不同的修飾過的名稱。是以,舊代碼可以繼續使用舊的修飾過的名稱,而新代碼将使用帶有

tags

的新修飾過的名稱。

2.5 acquire_capability, acquire_shared_capability
  • 支援文法
GNU C++11 C2x __declspec Keyword #pragma #pragma clang attribute

acquire_capability

acquire_shared_capability

exclusive_lock_function

shared_lock_function

clang::acquire_capability

clang::acquire_shared_capability

将函數标記為擷取

capability

2.6 alloc_align
  • 支援文法
GNU C++11 C2x __declspec Keyword #pragma #pragma clang attribute

alloc_align

gnu::alloc_align

在一個函數聲明上使用

_attribute__((alloc_align(<alignment>))

來指定此函數的傳回值(必須是指針類型)至少與指定參數的值對齊。、該參數由其在形式參數清單中的索引給出;第一個參數有索引

1

,除非該函數是一個 C++ 非靜态成員函數,在這種情況下,第一個參數有索引

2

來解釋隐式的

this

參數。

// 傳回的指針具有由第一個參數指定的對齊方式。
void *a(size_t align) __attribute__((alloc_align(1)));

// 傳回的指針具有由第二個參數指定的對齊方式。
void *b(void *v, size_t align) __attribute__((alloc_align(2)));

// 傳回的指針具有由第二個可見參數指定的對齊方式,但是必須為隐式的“this”參數進行調整。
void *Foo::b(void *v, size_t align) __attribute__((alloc_align(3)));
           

注意,這個屬性隻是通知編譯器,一個函數總是傳回一個足夠對齊的指針。它不會導緻編譯器發出代碼來強制執行這種對齊。如果傳回的指針沒有充分對齊,則該行為是未定義的。

2.7 alloc_size
  • 支援文法
GNU C++11 C2x __declspec Keyword #pragma #pragma clang attribute

alloc_size

gnu::alloc_size

Yes

可以将

alloc_size

屬性放在傳回指針的函數上,以便向編譯器提示傳回指針上可用的記憶體位元組數。

alloc_size

接受一個或兩個參數。

  • alloc_size(N)

    表示參數數字

    N

    等于傳回指針上可用位元組數。
  • alloc_size(N, M)表示參數數字

    N

    和參數數字

    M

    的乘積等于傳回指針上可用位元組數。

參數數字是基于1的。

如何使用

alloc_size

的示例:

void *my_malloc(int a) __attribute__((alloc_size(1)));
void *my_calloc(int a, int b) __attribute__((alloc_size(1, 2)));

int main() {
  void *const p = my_malloc(100);
  assert(__builtin_object_size(p, 0) == 100);
  void *const a = my_calloc(20, 5);
  assert(__builtin_object_size(a, 0) == 100);
}
           

注意

這個屬性在 Clang 中的工作方式與在 GCC 中的工作方式不同。具體來說, clang 将隻跟蹤

const

指針(如上所述);我們放棄了沒有标記為

const

的指針。在大多數情況下,這并不重要,因為 LLVM 支援

alloc_size

屬性。然而,當與其他屬性(如

enable_if

)一起使用時,這可能會導緻輕微的不直覺行為。
2.8 allocator
  • 支援文法
GNU C++11 C2x __declspec Keyword #pragma #pragma clang attribute
allocator

__declspec(allocator)

屬性用于配置設定記憶體的函數,比如 C++ 中的

new

操作符。當發出 CodeView 調試資訊(由

clang -gcodeview

clang-cl /Z7

啟用)時,Clang 将嘗試在調試資訊中記錄堆配置設定調用站點的代碼偏移量。它還将記錄使用一些本地啟發式配置設定的類型。Visual Studio調試器使用這些資訊來分析記憶體使用情況。

與GCC的

__attribute__((malloc))

不同,這個屬性不會以任何方式影響優化。

2.9 artificial
2.10 assert_capability, assert_shared_capability
2.11 assume_aligned
2.12 availability
2.13 callback
2.14 carries_dependency
2.15 cf_consumed
2.16 cf_returns_not_retained
2.17 cf_returns_retained
2.18 code_seg
2.19 convergent
2.20 cpu_dispatch
2.21 cpu_specific
2.22 diagnose_if
2.23 disable_tail_calls
2.24 enable_if
2.25 exclude_from_explicit_instantiation
2.26 flatten
2.27 force_align_arg_pointer
2.28 format
2.29 gnu_inline
2.30 ifunc
2.31 import_module
2.32 import_name
2.33 internal_linkage
2.34 interrupt (ARM)
2.35 interrupt (AVR)
2.36 interrupt (MIPS)
2.37 interrupt (RISCV)
2.38 kernel
2.39 lifetimebound
2.40 long_call, far
2.41 micromips
2.42 mig_server_routine
2.43 min_vector_width
2.44 no_caller_saved_registers
2.45 no_sanitize
2.46 no_sanitize_address, no_address_safety_analysis
2.47 no_sanitize_memory
2.48 no_sanitize_thread
2.49 no_speculative_load_hardening
2.50 no_split_stack
2.51 no_stack_protector
2.52 noalias
2.53 nocf_check
2.54 nodiscard, warn_unused_result
2.55 noduplicate
2.56 nomicromips
2.57 noreturn
2.58 not_tail_called
2.59 nothrow
2.60 ns_consumed
2.61 ns_consumes_self
2.62 ns_returns_autoreleased
2.63 ns_returns_not_retained
2.64 ns_returns_retained
2.65 objc_method_family
2.66 objc_requires_super
2.67 optnone
2.68 os_consumed
2.69 os_consumes_this
2.70 os_returns_not_retained
2.71 os_returns_retained
2.72 os_returns_retained_on_non_zero
2.73 os_returns_retained_on_zero
2.74 overloadable
2.75 reinitializes
2.76 release_capability, release_shared_capability
2.77 short_call, near
2.78 signal
2.79 speculative_load_hardening
2.80 target
2.81 try_acquire_capability, try_acquire_shared_capability
2.82 xray_always_instrument, xray_never_instrument, xray_log_args
2.83 xray_always_instrument, xray_never_instrument, xray_log_args

3. 變量屬性

3.1 always_destroy
3.2 dllexport
3.3 dllimport
3.4 init_seg
3.5 maybe_unused, unused
3.6 no_destroy
3.7 nodebug
3.8 noescape
3.9 nosvm
3.10 objc_externally_retained
3.11 pass_object_size, pass_dynamic_object_size
3.12 require_constant_initialization
3.13 section, __declspec(allocate)
3.14 swift_context
3.15 swift_error_result
3.16 swift_indirect_result
3.17 swiftcall
3.18 thread
3.19 tls_model
3.20 uninitialized

繼續閱讀